From 029030deeec028538d1bbd9a8bc7094a07ffdba2 Mon Sep 17 00:00:00 2001 From: Graham Trott Date: Sun, 14 Jun 2020 11:58:05 +0100 Subject: [PATCH] Improved error handling --- dist/easycoder-min.js | 32 ++++++++++++++++---------------- dist/easycoder.js | 11 +++++++++-- js/easycoder/Browser.js | 8 +++++++- js/easycoder/Value.js | 3 ++- 4 files changed, 34 insertions(+), 20 deletions(-) diff --git a/dist/easycoder-min.js b/dist/easycoder-min.js index 032ee40..a83f34d 100644 --- a/dist/easycoder-min.js +++ b/dist/easycoder-min.js @@ -167,16 +167,16 @@ break;case "storage":if(a.nextTokenIs("keys"))return a.next(),{domain:"browser", ["left","top"].includes(b))return a.next(),{domain:"browser",type:"click",which:b}}return null},getCoord:function(a,b,c){if(a.nextTokenIs("of")){a.nextTokenIs("the")&&a.nextToken();var d=a.getToken();if(["window","viewport"].includes(d))return a.next(),{domain:"browser",type:b,symbol:d,offset:c};if(a.isSymbol()&&(d=a.getSymbolRecord(),"dom"===d.extra))return a.next(),{domain:"browser",type:b,symbol:d.name,offset:c}}return null},get:function(a,b){switch(b.type){case "file":case "input":case "select":case "textarea":var c= a.getSymbolRecord(b.value);var d=c.element[c.index];return{type:"constant",numeric:!1,content:d.value};case "exists":return c=a.getSymbolRecord(b.value),{domain:"browser",type:"boolean",content:"undefined"!==typeof c.element[c.index]};case "mobile":return{domain:"browser",type:"boolean",content:"undefined"!==typeof window.orientation||-1!==navigator.userAgent.indexOf("IEMobile")};case "portrait":return{domain:"browser",type:"boolean",content:document.documentElement.clientWidth=document.documentElement.clientHeight};case "br":return{type:"constant",numeric:!1,content:decodeURIComponent("%3Cbr%20%2F%3E")};case "attributeOf":return c=a.getSymbolRecord(b.symbol),b=a.getValue(b.attribute),d=c.element[c.index],0===b.indexOf("data-")?a.getSimpleValue(d.dataset[b.substr(5)]):a.getSimpleValue(d[b]);case "style":return c=a.getSymbolRecord(b.target),b=a.getValue(b.style),d=c.element[c.index], -a.getSimpleValue(d.style[b]);case "confirm":return{type:"boolean",content:window.confirm(a.getValue(b.text))};case "prompt":return d=a.getValue(b.text),b=a.getValue(b.pre),{type:"constant",numeric:!1,content:b?window.prompt(d,b):window.prompt(d)};case "contentOf":c=a.getSymbolRecord(b.symbol);d=c.element[c.index];switch(c.keyword){case "input":case "textarea":b=d.value;break;case "pre":b=d.innerHTML;break;default:b=d.innerHTML.split("\n").join("")}return{type:"constant",numeric:!1,content:b};case "selected":return c= -a.getSymbolRecord(b.symbol),d=c.element[c.index],a=d.selectedIndex,d=0<=a?d.options[a].text:"",b="index"===b.arg?a:d,{type:"constant",numeric:!1,content:b};case "top":if("window"==b.symbol)return{type:"constant",numeric:!0,content:window.screenY};c=a.getSymbolRecord(b.symbol);a=c.element[c.index];b=Math.round(b.offset?a.offsetTop:a.getBoundingClientRect().top);return{type:"constant",numeric:!0,content:b};case "bottom":if("window"==b.symbol)return{type:"constant",numeric:!0,content:window.screenY+ -window.innerHeight};c=a.getSymbolRecord(b.symbol);b=Math.round(c.element[c.index].getBoundingClientRect().bottom);return{type:"constant",numeric:!0,content:b};case "left":if("window"==b.symbol)return{type:"constant",numeric:!0,content:window.screenLeft};c=a.getSymbolRecord(b.symbol);a=c.element[c.index];b=Math.round(b.offset?a.offsetLeft:a.getBoundingClientRect().left);return{type:"constant",numeric:!0,content:b};case "right":if("window"==b.symbol)return{type:"constant",numeric:!0,content:window.screenX+ -window.innerWidth};c=a.getSymbolRecord(b.symbol);b=Math.round(c.element[c.index].getBoundingClientRect().right);return{type:"constant",numeric:!0,content:b};case "width":if("window"==b.symbol)return{type:"constant",numeric:!0,content:window.innerWidth};c=a.getSymbolRecord(b.symbol);b=Math.round(c.element[c.index].getBoundingClientRect().width);return{type:"constant",numeric:!0,content:b};case "height":if("window"==b.symbol)return{type:"constant",numeric:!0,content:window.innerHeight};c=a.getSymbolRecord(b.symbol); -b=Math.round(c.element[c.index].getBoundingClientRect().height);return{type:"constant",numeric:!0,content:b};case "color":return{type:"constant",numeric:!1,content:"#"+a.value.evaluate(a,b.value).content.toString(16).padStart(6,"0")};case "docPath":return{type:"constant",numeric:!1,content:a.docPath};case "storageKeys":return{type:"constant",numeric:!1,content:JSON.stringify(Object.keys(localStorage))};case "location":return{type:"constant",numeric:!1,content:window.location.href};case "historyState":return{type:"constant", -numeric:!1,content:window.history.state};case "scrollPosition":return{type:"constant",numeric:!0,content:scrollPosition};case "varName":return{type:"constant",numeric:!1,content:a.varName};case "varIndex":return{type:"constant",numeric:!0,content:a.varIndex};case "key":return{type:"constant",numeric:!1,content:a.key};case "hostname":return{type:"constant",numeric:!1,content:location.hostname};case "screen":return{type:"constant",numeric:!0,content:screen[b.attribute]};case "pickPosition":return{type:"constant", -numeric:!1,content:JSON.stringify({x:document.pickX,y:document.pickY})};case "dragPosition":return{type:"constant",numeric:!1,content:JSON.stringify({x:document.dragX,y:document.dragY})};case "click":a=EasyCoder_Browser.clickData;if("undefined"===typeof a)return 0;d=a.target.getBoundingClientRect();return{type:"constant",numeric:!0,content:"left"===b.which?a.clientX-Math.round(d.left):a.clientY-Math.round(d.top)}}}},condition:{compile:function(a){if(a.tokenIs("confirm"))return{domain:"browser",type:"confirm", -value:a.getNextValue()};if(a.tokenIs("element")&&a.nextIsSymbol()){var b=a.getSymbolRecord();if("dom"===b.extra){var c=a.nextToken();if("has"===c){if(a.nextTokenIs("the")&&a.next(),a.tokenIs("focus"))return a.next(),{domain:"browser",type:"focus",element:b.name}}else if("contains"===c)return a=a.getNextValue(),{domain:"browser",type:"contains",element:b.name,position:a}}}return null},test:function(a,b){switch(b.type){case "confirm":return confirm(a.getValue(b.value));case "focus":var c=a.getSymbolRecord(b.element); -return c.element[c.index]===document.activeElement;case "contains":c=a.getSymbolRecord(b.element);var d=c.element[c.index].getBoundingClientRect();c=Math.round(d.left);var e=Math.round(d.right),f=Math.round(d.top);d=Math.round(d.bottom);b=JSON.parse(a.getValue(b.position));a=b.x;b=b.y;return a>=c&&a<=e&&b>=f&&b<=d?!0:!1}}},setStyles:function(a,b){a=document.getElementById(a);b=b.split(";");b=$jscomp.makeIterator(b);for(var c=b.next();!c.done;c=b.next())c=c.value.split(":"),a.setAttribute(c[0],c[1])}}, -scrollPosition=0;window.addEventListener("scroll",function(){scrollPosition=this.scrollY});window.onpopstate=function(a){window.EasyCoder.timestamp=Date.now();(a=JSON.parse(a.state))&&a.script&&((a=window.EasyCoder.scripts[a.script])?a.onBrowserBack&&a.run(a.onBrowserBack):console.log("No script property in window state object"))}; +a.getSimpleValue(d.style[b]);case "confirm":return{type:"boolean",content:window.confirm(a.getValue(b.text))};case "prompt":return d=a.getValue(b.text),b=a.getValue(b.pre),{type:"constant",numeric:!1,content:b?window.prompt(d,b):window.prompt(d)};case "contentOf":c=a.getSymbolRecord(b.symbol);d=c.element[c.index];if(null===d||"undefined"===typeof d)return a.runtimeError(a[a.pc].lino,"Variable '"+c.name+"' is not attached to a DOM element."),null;switch(c.keyword){case "input":case "textarea":b=d.value; +break;case "pre":b=d.innerHTML;break;default:b=d.innerHTML.split("\n").join("")}return{type:"constant",numeric:!1,content:b};case "selected":return c=a.getSymbolRecord(b.symbol),d=c.element[c.index],a=d.selectedIndex,d=0<=a?d.options[a].text:"",b="index"===b.arg?a:d,{type:"constant",numeric:!1,content:b};case "top":if("window"==b.symbol)return{type:"constant",numeric:!0,content:window.screenY};c=a.getSymbolRecord(b.symbol);a=c.element[c.index];b=Math.round(b.offset?a.offsetTop:a.getBoundingClientRect().top); +return{type:"constant",numeric:!0,content:b};case "bottom":if("window"==b.symbol)return{type:"constant",numeric:!0,content:window.screenY+window.innerHeight};c=a.getSymbolRecord(b.symbol);b=Math.round(c.element[c.index].getBoundingClientRect().bottom);return{type:"constant",numeric:!0,content:b};case "left":if("window"==b.symbol)return{type:"constant",numeric:!0,content:window.screenLeft};c=a.getSymbolRecord(b.symbol);a=c.element[c.index];b=Math.round(b.offset?a.offsetLeft:a.getBoundingClientRect().left); +return{type:"constant",numeric:!0,content:b};case "right":if("window"==b.symbol)return{type:"constant",numeric:!0,content:window.screenX+window.innerWidth};c=a.getSymbolRecord(b.symbol);b=Math.round(c.element[c.index].getBoundingClientRect().right);return{type:"constant",numeric:!0,content:b};case "width":if("window"==b.symbol)return{type:"constant",numeric:!0,content:window.innerWidth};c=a.getSymbolRecord(b.symbol);b=Math.round(c.element[c.index].getBoundingClientRect().width);return{type:"constant", +numeric:!0,content:b};case "height":if("window"==b.symbol)return{type:"constant",numeric:!0,content:window.innerHeight};c=a.getSymbolRecord(b.symbol);b=Math.round(c.element[c.index].getBoundingClientRect().height);return{type:"constant",numeric:!0,content:b};case "color":return{type:"constant",numeric:!1,content:"#"+a.value.evaluate(a,b.value).content.toString(16).padStart(6,"0")};case "docPath":return{type:"constant",numeric:!1,content:a.docPath};case "storageKeys":return{type:"constant",numeric:!1, +content:JSON.stringify(Object.keys(localStorage))};case "location":return{type:"constant",numeric:!1,content:window.location.href};case "historyState":return{type:"constant",numeric:!1,content:window.history.state};case "scrollPosition":return{type:"constant",numeric:!0,content:scrollPosition};case "varName":return{type:"constant",numeric:!1,content:a.varName};case "varIndex":return{type:"constant",numeric:!0,content:a.varIndex};case "key":return{type:"constant",numeric:!1,content:a.key};case "hostname":return{type:"constant", +numeric:!1,content:location.hostname};case "screen":return{type:"constant",numeric:!0,content:screen[b.attribute]};case "pickPosition":return{type:"constant",numeric:!1,content:JSON.stringify({x:document.pickX,y:document.pickY})};case "dragPosition":return{type:"constant",numeric:!1,content:JSON.stringify({x:document.dragX,y:document.dragY})};case "click":a=EasyCoder_Browser.clickData;if("undefined"===typeof a)return 0;d=a.target.getBoundingClientRect();return{type:"constant",numeric:!0,content:"left"=== +b.which?a.clientX-Math.round(d.left):a.clientY-Math.round(d.top)}}}},condition:{compile:function(a){if(a.tokenIs("confirm"))return{domain:"browser",type:"confirm",value:a.getNextValue()};if(a.tokenIs("element")&&a.nextIsSymbol()){var b=a.getSymbolRecord();if("dom"===b.extra){var c=a.nextToken();if("has"===c){if(a.nextTokenIs("the")&&a.next(),a.tokenIs("focus"))return a.next(),{domain:"browser",type:"focus",element:b.name}}else if("contains"===c)return a=a.getNextValue(),{domain:"browser",type:"contains", +element:b.name,position:a}}}return null},test:function(a,b){switch(b.type){case "confirm":return confirm(a.getValue(b.value));case "focus":var c=a.getSymbolRecord(b.element);return c.element[c.index]===document.activeElement;case "contains":c=a.getSymbolRecord(b.element);var d=c.element[c.index].getBoundingClientRect();c=Math.round(d.left);var e=Math.round(d.right),f=Math.round(d.top);d=Math.round(d.bottom);b=JSON.parse(a.getValue(b.position));a=b.x;b=b.y;return a>=c&&a<=e&&b>=f&&b<=d?!0:!1}}},setStyles:function(a, +b){a=document.getElementById(a);b=b.split(";");b=$jscomp.makeIterator(b);for(var c=b.next();!c.done;c=b.next())c=c.value.split(":"),a.setAttribute(c[0],c[1])}},scrollPosition=0;window.addEventListener("scroll",function(){scrollPosition=this.scrollY});window.onpopstate=function(a){window.EasyCoder.timestamp=Date.now();(a=JSON.parse(a.state))&&a.script&&((a=window.EasyCoder.scripts[a.script])?a.onBrowserBack&&a.run(a.onBrowserBack):console.log("No script property in window state object"))}; var EasyCoder_Json={name:"EasyCoder_JSON",Json:{compile:function(a){var b=a.getLino(),c=a.nextToken();switch(c){case "set":a.next();if(a.isSymbol())if(c=a.getSymbolRecord(),"variable"===c.keyword){if(a.nextTokenIs("to")){var d=a.nextToken();if('["array","object"]'.includes(d))return a.next(),a.addCommand({domain:"json",keyword:"json",lino:b,request:"setVariable",target:c.name,type:d}),!0}}else if("select"===c.keyword&&a.nextTokenIs("from")&&(a.next(),a.isSymbol()&&(d=a.getSymbolRecord(),"variable"=== d.keyword))){var e=null;a.nextTokenIs("as")&&(e=a.getNextValue());a.addCommand({domain:"json",keyword:"json",lino:b,request:"setList",target:c.name,source:d.name,display:e});return!0}break;case "sort":case "shuffle":case "format":if(a.nextIsSymbol()&&(d=a.getSymbolRecord(),"variable"===d.keyword))return a.next(),a.addCommand({domain:"json",keyword:"json",lino:b,request:c,target:d.name}),!0;break;case "parse":if(a.nextTokenIs("url")&&(d=a.getNextValue(),a.tokenIs("as")&&a.nextIsSymbol()&&(e=a.getSymbolRecord(), "variable"===e.keyword)))return a.next(),a.addCommand({domain:"json",keyword:"json",lino:b,request:c,source:d,target:e.name}),!0;break;case "delete":d=a.nextToken();if(["property","element"].includes(d)&&(e=a.getNextValue(),["from","of"].includes(a.getToken())&&a.nextIsSymbol())){var f=a.getSymbolRecord();if("variable"===f.keyword)return a.next(),a.addCommand({domain:"json",keyword:"json",lino:b,request:c,what:d,value:e,target:f.name}),!0}break;case "rename":d=a.getNextValue();if(a.tokenIs("to")&& @@ -200,12 +200,12 @@ a.numeric&&(d=d.toString()),"undefined"===typeof c&&(c=""),"undefined"===typeof if("true"===b)return a.next(),{type:"boolean",content:!0};if("false"===b)return a.next(),{type:"boolean",content:!1};if("`"===b.charAt(0))return a.next(),{type:"constant",numeric:!1,content:b.substring(1,b.length-1)};if(b.charAt(0).match(/[0-9-]/)){var c=eval(b);if(Number.isInteger(c))return a.next(),{type:"constant",numeric:!0,content:c};throw Error("'"+b+"' is not an integer");}b=a.getIndex();c=$jscomp.makeIterator(Object.keys(a.domain));for(var d=c.next();!d.done;d=c.next())if(d=d.value,a.rewindTo(b), d=a.domain[d].value.compile(a))return d;return null},compile:function(a){var b=a.getToken(),c=EasyCoder_Value.getItem(a);if(!c)throw Error("Undefined value: '"+b+"'");if("cat"===a.getToken()){for(b={type:"cat",numeric:!1,parts:[c]};a.tokenIs("cat");)a.next(),b.parts.push(a.value.getItem(a));return b}return c},doValue:function(a,b){if("undefined"===typeof b.type)return a.runtimeError(a[a.pc].lino,"Undefined value (variable not initialized?)"),null;switch(b.type){case "cat":return{type:"constant",numeric:!1, content:b.parts.reduce(function(b,c){c=EasyCoder_Value.doValue(a,c);return b+(c?c.content:"")},"")};case "boolean":case "constant":return b;case "symbol":var c=a.getSymbolRecord(b.name);if(c.isVHolder){if(b=c.value[c.index]){c=b.content;if(null===c||"undefined"===typeof c)b.content=b.numeric?0:"";return b}return null}return a.domain[c.domain].value.get(a,b)}return a.domain[b.domain].value.get(a,b)},constant:function(a,b){return{type:"constant",numeric:b,content:a}},evaluate:function(a,b){if(!b)return{type:"constant", -numeric:!1,content:""};var c=EasyCoder_Value.doValue(a,b);if(c)return c;a.runtimeError(a[a.pc].lino,"Can't decode value: "+b)},getValue:function(a,b){return EasyCoder_Value.evaluate(a,b).content},encode:function(a,b){if(a)switch(b){case "ec":return a.replace(/\n/g,"%0a").replace(/\r/g,"%0d").replace(/"/g,"~dq~").replace(/'/g,"~sq~").replace(/\\/g,"~bs~");case "url":return encodeURIComponent(a.replace(/\s/g,"+"));case "sanitize":return a.normalize("NFD").replace(/[\u0300-\u036f]/g,"")}return a},decode:function(a, -b){if(a)switch(b){case "ec":return a.replace(/%0a/g,"\n").replace(/%0d/g,"\r").replace(/~dq~/g,'"').replace(/~sq~/g,"'").replace(/~bs~/g,"\\");case "url":return decodeURIComponent(a).replace(/\+/g," ")}return a}},EasyCoder_Run={name:"EasyCoder_Run",run:function(a,b){var c=[],d=function(a){var b=9999;a.forEach(function(a){a=a.line;for(var c=0;c')}l+="
"}g.innerHTML=k+" "+l;g.style.display="block";b.$jscomp$loop$prop$run$135=document.getElementById("easycoder-run-button");b.$jscomp$loop$prop$step$136=document.getElementById("easycoder-step-button"); -b.$jscomp$loop$prop$run$135.onclick=function(b){return function(){b.$jscomp$loop$prop$run$135.blur();a.tracing=!1;document.getElementById("easycoder-tracer-content").style.display="none";try{EasyCoder_Run.run(a,a.resume)}catch(q){var c="Error in run handler: "+q.message;console.log(c);alert(c)}}}(b);b.$jscomp$loop$prop$step$136.onclick=function(b){return function(){console.log("step");b.$jscomp$loop$prop$step$136.blur();a.tracing=!0;document.getElementById("easycoder-tracer-content").style.display= +numeric:!1,content:""};var c=EasyCoder_Value.doValue(a,b);if(c)return c;a.runtimeError(a[a.pc].lino,"Can't decode value: "+b)},getValue:function(a,b){return(a=EasyCoder_Value.evaluate(a,b))?a.content:null},encode:function(a,b){if(a)switch(b){case "ec":return a.replace(/\n/g,"%0a").replace(/\r/g,"%0d").replace(/"/g,"~dq~").replace(/'/g,"~sq~").replace(/\\/g,"~bs~");case "url":return encodeURIComponent(a.replace(/\s/g,"+"));case "sanitize":return a.normalize("NFD").replace(/[\u0300-\u036f]/g,"")}return a}, +decode:function(a,b){if(a)switch(b){case "ec":return a.replace(/%0a/g,"\n").replace(/%0d/g,"\r").replace(/~dq~/g,'"').replace(/~sq~/g,"'").replace(/~bs~/g,"\\");case "url":return decodeURIComponent(a).replace(/\+/g," ")}return a}},EasyCoder_Run={name:"EasyCoder_Run",run:function(a,b){var c=[],d=function(a){var b=9999;a.forEach(function(a){a=a.line;for(var c=0;c')}l+="
"}g.innerHTML=k+" "+l;g.style.display="block";b.$jscomp$loop$prop$run$135=document.getElementById("easycoder-run-button");b.$jscomp$loop$prop$step$136= +document.getElementById("easycoder-step-button");b.$jscomp$loop$prop$run$135.onclick=function(b){return function(){b.$jscomp$loop$prop$run$135.blur();a.tracing=!1;document.getElementById("easycoder-tracer-content").style.display="none";try{EasyCoder_Run.run(a,a.resume)}catch(q){var c="Error in run handler: "+q.message;console.log(c);alert(c)}}}(b);b.$jscomp$loop$prop$step$136.onclick=function(b){return function(){console.log("step");b.$jscomp$loop$prop$step$136.blur();a.tracing=!0;document.getElementById("easycoder-tracer-content").style.display= "block";try{a.run(a.resume)}catch(q){var c="Error in step handler: "+q.message;console.log(c);alert(c)}}}(b)}a.resume=a.pc;a.pc=0}break}b={$jscomp$loop$prop$run$135:b.$jscomp$loop$prop$run$135,$jscomp$loop$prop$step$136:b.$jscomp$loop$prop$step$136}}},exit:function(a){a.onExit&&a.run(a.onExit);var b=a.parent,c=a.afterExit;delete EasyCoder.scripts[a.script];a.module&&delete a.module.program;Object.keys(a).forEach(function(b){delete a[b]});b&&c&&EasyCoder.scripts[b].run(c)}},EasyCoder_Compiler={name:"EasyCoder_Compiler", getTokens:function(){return this.tokens},addWarning:function(a){this.warnings.push(a)},warning:function(a){this.addWarning(a)},unrecognisedSymbol:function(a){this.addWarning("Unrecognised symbol '"+a+"'")},getWarnings:function(){return this.warnings},getIndex:function(){return this.index},next:function(a){this.index+=void 0===a?1:a},peek:function(){return this.tokens[this.index+1].token},more:function(){return this.index=this.tokens.length? null:this.tokens[this.index]?this.tokens[this.index].token:null},nextToken:function(){this.next();return this.getToken()},tokenIs:function(a){return this.index>=this.tokens.length?!1:a===this.tokens[this.index].token},nextTokenIs:function(a){this.next();return this.tokenIs(a)},skip:function(a){if(this.index>=this.tokens.length)return null;this.next();this.tokenIs(a)&&this.next()},prev:function(){this.index--},getLino:function(){return this.index>=this.tokens.length?0:this.tokens[this.index].lino}, diff --git a/dist/easycoder.js b/dist/easycoder.js index 9c30bb3..86b3f67 100644 --- a/dist/easycoder.js +++ b/dist/easycoder.js @@ -5382,7 +5382,8 @@ const EasyCoder_Browser = { if (!target) { const symbolElement = symbol.value[symbol.index]; if (!symbolElement.type) { - program.runtimeError(command.lino, `Variable '${symbol.name}' is not attached to a DOM element.`); + program.runtimeError(command.lino, + `Variable '${symbol.name}' is not attached to a DOM element.`); return 0; } targetId = program.getValue(symbolElement); @@ -6209,6 +6210,11 @@ const EasyCoder_Browser = { case `contentOf`: symbolRecord = program.getSymbolRecord(value.symbol); target = symbolRecord.element[symbolRecord.index]; + if (target === null || typeof target === `undefined`) { + program.runtimeError(program[program.pc].lino, + `Variable '${symbolRecord.name}' is not attached to a DOM element.`); + return null; + } switch (symbolRecord.keyword) { case `input`: case `textarea`: @@ -7484,7 +7490,8 @@ const EasyCoder_Value = { }, getValue: (program, value) => { - return EasyCoder_Value.evaluate(program, value).content; + const v = EasyCoder_Value.evaluate(program, value); + return v ? v.content : null; }, // tools diff --git a/js/easycoder/Browser.js b/js/easycoder/Browser.js index 6d25c28..ee2e4c5 100644 --- a/js/easycoder/Browser.js +++ b/js/easycoder/Browser.js @@ -2308,7 +2308,8 @@ const EasyCoder_Browser = { if (!target) { const symbolElement = symbol.value[symbol.index]; if (!symbolElement.type) { - program.runtimeError(command.lino, `Variable '${symbol.name}' is not attached to a DOM element.`); + program.runtimeError(command.lino, + `Variable '${symbol.name}' is not attached to a DOM element.`); return 0; } targetId = program.getValue(symbolElement); @@ -3135,6 +3136,11 @@ const EasyCoder_Browser = { case `contentOf`: symbolRecord = program.getSymbolRecord(value.symbol); target = symbolRecord.element[symbolRecord.index]; + if (target === null || typeof target === `undefined`) { + program.runtimeError(program[program.pc].lino, + `Variable '${symbolRecord.name}' is not attached to a DOM element.`); + return null; + } switch (symbolRecord.keyword) { case `input`: case `textarea`: diff --git a/js/easycoder/Value.js b/js/easycoder/Value.js index 335d7f7..f465038 100644 --- a/js/easycoder/Value.js +++ b/js/easycoder/Value.js @@ -160,7 +160,8 @@ const EasyCoder_Value = { }, getValue: (program, value) => { - return EasyCoder_Value.evaluate(program, value).content; + const v = EasyCoder_Value.evaluate(program, value); + return v ? v.content : null; }, // tools