From 3190de873e6f6719154c4047c307af33524685c1 Mon Sep 17 00:00:00 2001 From: Dave Conway-Jones Date: Tue, 1 May 2018 12:42:27 +0100 Subject: [PATCH] add output property select to HTML parse node (#1701) --- nodes/core/locales/en-US/messages.json | 3 ++- nodes/core/parsers/70-HTML.html | 8 +++++++- nodes/core/parsers/70-HTML.js | 5 +++-- test/nodes/core/parsers/70-HTML_spec.js | 22 ++++++++++++++++++++-- 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/nodes/core/locales/en-US/messages.json b/nodes/core/locales/en-US/messages.json index 3232069db..9fc8f729b 100644 --- a/nodes/core/locales/en-US/messages.json +++ b/nodes/core/locales/en-US/messages.json @@ -671,7 +671,8 @@ "html": { "label": { "select": "Selector", - "output": "Output" + "output": "Output", + "in": "in" }, "output": { "html": "the html content of the elements", diff --git a/nodes/core/parsers/70-HTML.html b/nodes/core/parsers/70-HTML.html index 79003f935..4fa9227d1 100644 --- a/nodes/core/parsers/70-HTML.html +++ b/nodes/core/parsers/70-HTML.html @@ -2,7 +2,7 @@ diff --git a/nodes/core/parsers/70-HTML.js b/nodes/core/parsers/70-HTML.js index 7e4f48fd0..0a24be368 100644 --- a/nodes/core/parsers/70-HTML.js +++ b/nodes/core/parsers/70-HTML.js @@ -21,6 +21,7 @@ module.exports = function(RED) { function CheerioNode(n) { RED.nodes.createNode(this,n); this.property = n.property||"payload"; + this.outproperty = n.outproperty||this.property||"payload"; this.tag = n.tag; this.ret = n.ret || "html"; this.as = n.as || "single"; @@ -48,7 +49,7 @@ module.exports = function(RED) { /* istanbul ignore else */ if (pay2) { var new_msg = RED.util.cloneMessage(msg); - RED.util.setMessageProperty(new_msg,node.property,pay2); + RED.util.setMessageProperty(new_msg,node.outproperty,pay2); new_msg.parts = { id: msg._msgid, index: index, @@ -68,7 +69,7 @@ module.exports = function(RED) { index++; }); if (node.as === "single") { // Always return an array - even if blank - RED.util.setMessageProperty(msg,node.property,pay); + RED.util.setMessageProperty(msg,node.outproperty,pay); node.send(msg); } } diff --git a/test/nodes/core/parsers/70-HTML_spec.js b/test/nodes/core/parsers/70-HTML_spec.js index 5874ed3ae..a1e363761 100644 --- a/test/nodes/core/parsers/70-HTML_spec.js +++ b/test/nodes/core/parsers/70-HTML_spec.js @@ -69,7 +69,7 @@ describe('html node', function() { }); }); - it('should retrieve header contents if asked to by msg.select - alternative property', function(done) { + it('should retrieve header contents if asked to by msg.select - alternative in property', function(done) { fs.readFile(file, 'utf8', function(err, data) { var flow = [{id:"n1",type:"html",property:"foo",wires:[["n2"]],func:"return msg;"}, {id:"n2", type:"helper"}]; @@ -79,7 +79,7 @@ describe('html node', function() { var n2 = helper.getNode("n2"); n2.on("input", function(msg) { msg.should.have.property('topic', 'bar'); - should.equal(msg.foo, 'This is a test page for node 70-HTML'); + msg.foo[0].should.equal('This is a test page for node 70-HTML'); done(); }); n1.receive({foo:data,topic:"bar",select:"h1"}); @@ -87,6 +87,24 @@ describe('html node', function() { }); }); + it('should retrieve header contents if asked to by msg.select - alternative in and out properties', function(done) { + fs.readFile(file, 'utf8', function(err, data) { + var flow = [{id:"n1",type:"html",property:"foo",outproperty:"bar",tag:"h1",wires:[["n2"]],func:"return msg;"}, + {id:"n2", type:"helper"}]; + + helper.load(htmlNode, flow, function() { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n2.on("input", function(msg) { + msg.should.have.property('topic', 'bar'); + msg.bar[0].should.equal('This is a test page for node 70-HTML'); + done(); + }); + n1.receive({foo:data,topic:"bar"}); + }); + }); + }); + it('should emit an empty array if no matching elements', function(done) { fs.readFile(file, 'utf8', function(err, data) { var flow = [{id:"n1",type:"html",wires:[["n2"]],func:"return msg;"},