From ffa102565ffa6c89ffed79a6b38e4073c1d8da85 Mon Sep 17 00:00:00 2001 From: donkirkby Date: Mon, 20 Dec 2021 19:04:16 -0800 Subject: [PATCH] Close #347 by implementing unescapeString(). --- html/src/SampleAnalyst.js | 54 ++++++++++++++++++++++++++++++++-- html/src/SampleAnalyst.test.js | 35 +++++++++++++++++++--- 2 files changed, 83 insertions(+), 6 deletions(-) diff --git a/html/src/SampleAnalyst.js b/html/src/SampleAnalyst.js index 574a9fcf..4e6d79a8 100644 --- a/html/src/SampleAnalyst.js +++ b/html/src/SampleAnalyst.js @@ -1,7 +1,57 @@ import { diffChars } from 'diff'; -function unescapeString(s) { - return Function('"use strict";return (' + s + ')')(); +function unescapeString(value) { + if ((value.startsWith("'") && value.endsWith("'")) || + (value.startsWith('"') && value.endsWith('"'))) { + value = value.substring(1, value.length - 1); + let newValue = ''; + for (let i = 0; i < value.length; i++) { + const c = value[i]; + if (c !== '\\' || i === value.length - 1) { + newValue += c; + } else { + const c2 = value[++i]; + switch (c2) { + case '\\': + newValue += '\\'; + break; + case '\'': + newValue += '\''; + break; + case 'n': + newValue += '\n'; + break; + case 'r': + newValue += '\r'; + break; + case 't': + newValue += '\t'; + break; + case 'x': + if (i+2 < value.length) + { + const charCode = parseInt( + value.substring(i+1, i+3), + 16); + newValue += String.fromCharCode(charCode); + i += 2; + break; + } + newValue += c; + newValue += c2; + break; + default: + newValue += c; + newValue += c2; + break; + } + } + } + value = newValue; + } else if (value.match(/^[0-9.]+$/)) { + value = parseFloat(value); + } + return value; } export default class SampleAnalyst { diff --git a/html/src/SampleAnalyst.test.js b/html/src/SampleAnalyst.test.js index 4bf73715..9da2fd06 100644 --- a/html/src/SampleAnalyst.test.js +++ b/html/src/SampleAnalyst.test.js @@ -344,6 +344,13 @@ create_line 50 fill='black' pensize=1 +create_line + 50 + 150 + 150 + 150 + fill='black' + pensize=1.5 end_canvas . x = 1 @@ -366,6 +373,12 @@ x = 1 fill: 'black', pensize: 1, coords: [50, 50, 150, 50] + }, + { + name: 'create_line', + fill: 'black', + pensize: 1.5, + coords: [50, 150, 150, 150] }]; let analyst = new SampleAnalyst(source, run); @@ -378,15 +391,21 @@ x = 1 it('parses font options', () => { let run = () => { - let display = `\ -start_canvas + let display = String.raw`start_canvas create_text 100 0 anchor='sw' fill='black' font=('Courier', 14, 'bold') - text='Bob' + text='Bob with quotes "\' and\r\n\t\x03whitespace.' +create_text + 100 + 20 + anchor='sw' + fill='black' + font=('Courier', 14, 'bold') + text="Bob's secret message with a \\backslash." end_canvas . x = 1 @@ -403,8 +422,16 @@ line 1 fill: 'black', anchor: 'sw', font: 'bold 14px Courier', - text: 'Bob', + text: 'Bob with quotes "\' and\r\n\t\x03whitespace.', coords: [100, 0] + }, + { + name: 'create_text', + fill: 'black', + anchor: 'sw', + font: 'bold 14px Courier', + text: "Bob's secret message with a \\backslash.", + coords: [100, 20] }]; let analyst = new SampleAnalyst(source, run);