Skip to content

Commit 3a9c789

Browse files
committed
Test for JSON; fix 'select'
1 parent 14f1f1a commit 3a9c789

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

js/easycoder/Core.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,14 +1711,17 @@ const EasyCoder_Core = {
17111711
targetValue = `{}`;
17121712
}
17131713
// This is object whose property is being set
1714-
let targetJSON = JSON.parse(targetValue);
1714+
let targetJSON = targetValue;
1715+
if (program.isJsonString(targetValue)) {
1716+
targetJSON = JSON.parse(targetValue);
1717+
}
17151718
// This is the name of the property
17161719
const itemName = program.getValue(command.name);
17171720
// This is the value of the property
17181721
const itemValue = program.evaluate(command.value);
17191722
let content = itemValue.content;
17201723
if (itemValue) {
1721-
if (content.length >= 2 && [`[`, `{`].includes(content[0])) {
1724+
if (program.isJsonString(itemValue.content)) {
17221725
targetJSON[itemName] = JSON.parse(itemValue.content);
17231726
content = JSON.stringify(targetJSON);
17241727
}

js/easycoder/Main.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,15 @@ const EasyCoder = {
167167
return typeof item === `undefined`;
168168
},
169169

170+
isJsonString: function (str) {
171+
try {
172+
JSON.parse(str);
173+
} catch (e) {
174+
return false;
175+
}
176+
return true;
177+
},
178+
170179
runScript: function (program) {
171180
const command = program[program.pc];
172181
const script = program.getValue(command.script);
@@ -227,6 +236,7 @@ const EasyCoder = {
227236
program.domain = this.domain;
228237
program.require = this.require;
229238
program.isUndefined = this.isUndefined;
239+
program.isJsonString = this.isJsonString;
230240
program.checkPlugin = this.checkPlugin;
231241
program.getPlugin = this.getPlugin;
232242
program.addLocalPlugin = this.addLocalPlugin;

js/plugins/browser.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2575,10 +2575,10 @@ const EasyCoder_Browser = {
25752575
case `selected`:
25762576
let arg = compiler.nextToken();
25772577
if ([`index`, `item`].includes(arg)) {
2578-
if (compiler.nextTokenIs(`in`)) {
2578+
if ([`in`, `of`].includes(compiler.nextToken())) {
25792579
if (compiler.nextIsSymbol()) {
25802580
const symbol = compiler.getSymbolRecord();
2581-
if ([`ul`, `ol`].includes(symbol.keyword)) {
2581+
if ([`ul`, `ol`, `select`].includes(symbol.keyword)) {
25822582
compiler.next();
25832583
return {
25842584
domain: `browser`,
@@ -2841,9 +2841,10 @@ const EasyCoder_Browser = {
28412841
};
28422842
case `selected`:
28432843
symbolRecord = program.getSymbolRecord(value.symbol);
2844-
element = symbolRecord.value[symbolRecord.index].content;
2845-
target = document.getElementById(element);
2846-
content = (value.arg === `index`) ? target.selectedIndex : target.options[target.selectedIndex].text;
2844+
target = symbolRecord.element[symbolRecord.index];
2845+
let selectedIndex = target.selectedIndex;
2846+
let selectedText = selectedIndex >= 0 ? target.options[selectedIndex].text : ``;
2847+
content = (value.arg === `index`) ? selectedIndex : selectedText;
28472848
return {
28482849
type: `constant`,
28492850
numeric: false,

0 commit comments

Comments
 (0)