From 655e777a3eaefb3252aad6e4c0a374205a601242 Mon Sep 17 00:00:00 2001 From: Frank van de Pol Date: Sun, 22 Dec 2013 14:00:25 +0100 Subject: [PATCH 1/6] Add exception handing to Change node MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adding exception handling to the change node to prevent node-RED from crashing on invalid regular expressions eg. “*kW” (missing escape before the asterix) --- nodes/core/logic/15-change.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/nodes/core/logic/15-change.js b/nodes/core/logic/15-change.js index 4866239c0..5f518987f 100644 --- a/nodes/core/logic/15-change.js +++ b/nodes/core/logic/15-change.js @@ -35,9 +35,13 @@ function ChangeNode(n) { this.on('input', function (msg) { if (node.action == "change") { - node.re = new RegExp(this.from, "g"); - if (typeof msg[node.property] === "string") { - msg[node.property] = (msg[node.property]).replace(node.re, node.to); + try { + node.re = new RegExp(this.from, "g"); + if (typeof msg[node.property] === "string") { + msg[node.property] = (msg[node.property]).replace(node.re, node.to); + } + } catch(err) { + this.error(err.message); } } //else if (node.action == "replace") { From a03b4e4dd43f57bc5209ccbf3e65f545f44e76c7 Mon Sep 17 00:00:00 2001 From: Frank van de Pol Date: Sun, 22 Dec 2013 17:46:25 +0100 Subject: [PATCH 2/6] Added validation logic to Change editor for validity of regular expressions This change adds input validation to the gui of Change Nodes to prevent the user from unintentionally entering an invalid regular expression. The user will be notified on the specific error code to help resolve the issue. --- nodes/core/logic/15-change.html | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/nodes/core/logic/15-change.html b/nodes/core/logic/15-change.html index ea360908f..b1ad880d8 100644 --- a/nodes/core/logic/15-change.html +++ b/nodes/core/logic/15-change.html @@ -56,7 +56,18 @@ defaults: { action: {value:"change",required:true}, property: {value:"payload"}, - from: {value:""}, + from: {value:"",validate: function(v) { + if (this.action == "change") { + try { + var re = new RegExp(this.from, "g"); + return true; + } catch(err) { + RED.notify("Error: "+err.message,"error"); + return false; + } + } + return true; + }}, to: {value:""}, name: {value:""} }, From 231f8b6a4d47ff5024daa571b238398fe4a2dbda Mon Sep 17 00:00:00 2001 From: Frank van de Pol Date: Tue, 24 Dec 2013 20:02:30 +0100 Subject: [PATCH 3/6] undo my changes to the Change node; revert to original undo the local changes to the Change node to get back aligned with the master tree --- nodes/core/logic/15-change.html | 25 ++++++++++++------------- nodes/core/logic/15-change.js | 19 ++++++++++++------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/nodes/core/logic/15-change.html b/nodes/core/logic/15-change.html index b1ad880d8..9d8ad6652 100644 --- a/nodes/core/logic/15-change.html +++ b/nodes/core/logic/15-change.html @@ -34,6 +34,11 @@ +
+ + + +

@@ -56,19 +61,9 @@ defaults: { action: {value:"change",required:true}, property: {value:"payload"}, - from: {value:"",validate: function(v) { - if (this.action == "change") { - try { - var re = new RegExp(this.from, "g"); - return true; - } catch(err) { - RED.notify("Error: "+err.message,"error"); - return false; - } - } - return true; - }}, + from: {value:""}, to: {value:""}, + reg: {value:false}, name: {value:""} }, inputs: 1, @@ -81,6 +76,7 @@ return this.name ? "node_label_italic" : ""; }, oneditprepare: function() { + if (this.reg === null) { $("#node-input-reg").prop('checked', true); } $("#node-input-action").change( function() { var a = $("#node-input-action").val(); if (a === "replace") { @@ -89,6 +85,7 @@ $("#node-input-t").html("with value"); $("#node-from-row").hide(); $("#node-to-row").show(); + $("#node-reg-row").hide(); $("#node-tip").html("Tip: expects a new property name and either a fixed value OR the full name of another msg.property eg: msg.sentiment.score"); } if (a === "delete") { @@ -97,6 +94,7 @@ //$("#node-input-t").html("to"); $("#node-from-row").hide(); $("#node-to-row").hide(); + $("#node-reg-row").hide(); $("#node-tip").html("Tip: deletes the named property and all sub-properties"); } if (a === "change") { @@ -105,7 +103,8 @@ $("#node-input-t").html("replace with"); $("#node-from-row").show(); $("#node-to-row").show(); - $("#node-tip").html("Tip: if it contains can be a regex, likewise replace with can accept regex results. Only works on strings."); + $("#node-reg-row").show(); + $("#node-tip").html("Tip: If contains is a regex, you must escape any special characters. Likewise replace with can accept regex results. Only works on strings."); } //if (a === "replace") { // $("#node-input-todo").html("called"); diff --git a/nodes/core/logic/15-change.js b/nodes/core/logic/15-change.js index 5f518987f..8922bde20 100644 --- a/nodes/core/logic/15-change.js +++ b/nodes/core/logic/15-change.js @@ -22,6 +22,7 @@ function ChangeNode(n) { this.property = n.property || ""; this.from = n.from || " "; this.to = n.to || " "; + this.reg = (n.reg === null || n.reg); var node = this; var makeNew = function( stem, path, value ) { @@ -35,13 +36,17 @@ function ChangeNode(n) { this.on('input', function (msg) { if (node.action == "change") { - try { - node.re = new RegExp(this.from, "g"); - if (typeof msg[node.property] === "string") { - msg[node.property] = (msg[node.property]).replace(node.re, node.to); - } - } catch(err) { - this.error(err.message); + var from = node.from; + if (node.reg === false) { + from = from.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"); + } + try { + node.re = new RegExp(from, "g"); + } catch (e) { + node.error("Invalid regex: "+from); + } + if (typeof msg[node.property] === "string") { + msg[node.property] = (msg[node.property]).replace(node.re, node.to); } } //else if (node.action == "replace") { From b411d59d4367ec01ad0d5d4064eca75d2f6e7c7e Mon Sep 17 00:00:00 2001 From: Frank van de Pol Date: Tue, 24 Dec 2013 20:17:42 +0100 Subject: [PATCH 4/6] Make log message in invalid regular expressions more verbose Make logging of erratic regular expressions more verbose to help identification and resolving of the configuration issue: eg.: old: 24 Dec 18:40:09 - [error] [change:Strip kW] Invalid regex: *kW new: 24 Dec 18:40:09 - [error] [change:Strip kW] Invalid regular expression: /*kW/: Nothing to repeat old: 24 Dec 20:15:57 - [error] [change:Strip kW] Invalid regex: *kW new: 24 Dec 20:15:57 - [error] [change:Strip kW] Invalid regular expression: /[kW/: Unterminated character class --- nodes/core/logic/15-change.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nodes/core/logic/15-change.js b/nodes/core/logic/15-change.js index 8922bde20..7c80a7863 100644 --- a/nodes/core/logic/15-change.js +++ b/nodes/core/logic/15-change.js @@ -43,7 +43,7 @@ function ChangeNode(n) { try { node.re = new RegExp(from, "g"); } catch (e) { - node.error("Invalid regex: "+from); + node.error(e.message); } if (typeof msg[node.property] === "string") { msg[node.property] = (msg[node.property]).replace(node.re, node.to); From 1ebc5979aa31159740be2ddbaa2b2194e7f6bbc6 Mon Sep 17 00:00:00 2001 From: Frank van de Pol Date: Tue, 24 Dec 2013 20:28:08 +0100 Subject: [PATCH 5/6] Add validation to Change node editor for invalid regex MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This change adds input validation to the gui of Change Nodes to prevent the user from unintentionally entering an invalid regular expression (in case the ‘use regular expressions’ option is enabled). The user will be notified (using the RED notification mechanism) on the specific error code to help resolve the issue. --- nodes/core/logic/15-change.html | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/nodes/core/logic/15-change.html b/nodes/core/logic/15-change.html index 9d8ad6652..cfcc11817 100644 --- a/nodes/core/logic/15-change.html +++ b/nodes/core/logic/15-change.html @@ -61,7 +61,18 @@ defaults: { action: {value:"change",required:true}, property: {value:"payload"}, - from: {value:""}, + from: {value:"",validate: function(v) { + if (this.action == "change" && this.reg) { + try { + var re = new RegExp(this.from, "g"); + return true; + } catch(err) { + RED.notify("Error: "+err.message,"error"); + return false; + } + } + return true; + }}, to: {value:""}, reg: {value:false}, name: {value:""} From 44e920fde22b462df2db2fbbb4396690892c3b6b Mon Sep 17 00:00:00 2001 From: Frank van de Pol Date: Tue, 24 Dec 2013 23:29:40 +0100 Subject: [PATCH 6/6] Disable the notification for the Change node editor Disable the notification on the Change node. Once the infrastructure for validation error messages is implemented this can be re-enabled and retrofitted to the new structure. --- nodes/core/logic/15-change.html | 1 - 1 file changed, 1 deletion(-) diff --git a/nodes/core/logic/15-change.html b/nodes/core/logic/15-change.html index cfcc11817..4e56a1f2f 100644 --- a/nodes/core/logic/15-change.html +++ b/nodes/core/logic/15-change.html @@ -67,7 +67,6 @@ var re = new RegExp(this.from, "g"); return true; } catch(err) { - RED.notify("Error: "+err.message,"error"); return false; } }