Skip to content

Commit

Permalink
Close #347 by implementing unescapeString().
Browse files Browse the repository at this point in the history
  • Loading branch information
donkirkby committed Dec 21, 2021
1 parent 2b95d8b commit ffa1025
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 6 deletions.
54 changes: 52 additions & 2 deletions html/src/SampleAnalyst.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
35 changes: 31 additions & 4 deletions html/src/SampleAnalyst.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);

Expand All @@ -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
Expand All @@ -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);

Expand Down

0 comments on commit ffa1025

Please sign in to comment.