diff --git a/nodes/core/parsers/70-HTML.js b/nodes/core/parsers/70-HTML.js index 5bb391a00..715c77b6b 100644 --- a/nodes/core/parsers/70-HTML.js +++ b/nodes/core/parsers/70-HTML.js @@ -31,11 +31,11 @@ module.exports = function(RED) { try { var $ = cheerio.load(msg.payload); var pay = []; - var count = 0; + var count = 0; $(tag).each(function() { - count++; - }); - var index = 0; + count++; + }); + var index = 0; $(tag).each(function() { if (node.as === "multi") { var pay2 = null; @@ -46,13 +46,13 @@ module.exports = function(RED) { /* istanbul ignore else */ if (pay2) { msg.payload = pay2; - msg.parts = { - id: msg._msgid, - index: index, - count: count, + msg.parts = { + id: msg._msgid, + index: index, + count: count, type: "string", ch: "" - }; + }; node.send(msg); } } @@ -62,13 +62,14 @@ module.exports = function(RED) { if (node.ret === "attr") { pay.push( this.attribs ); } //if (node.ret === "val") { pay.push( $(this).val() ); } } - index++; + index++; }); - if ((node.as === "single") && (pay.length !== 0)) { + if (node.as === "single") { // Always return an array - even if blank msg.payload = pay; node.send(msg); } - } catch (error) { + } + catch (error) { node.error(error.message,msg); } } diff --git a/test/nodes/core/parsers/70-HTML_spec.js b/test/nodes/core/parsers/70-HTML_spec.js index c518c0d35..ed08fac07 100644 --- a/test/nodes/core/parsers/70-HTML_spec.js +++ b/test/nodes/core/parsers/70-HTML_spec.js @@ -66,6 +66,25 @@ describe('html node', function() { }); }); + 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;"}, + {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.should.have.property('payload'); + msg.payload.should.be.empty; + done(); + }); + n1.receive({payload:data,topic:"bar",select:"h4"}); + }); + }); + }); + it('should retrieve paragraph contents when specified', function(done) { fs.readFile(file, 'utf8', function(err, data) { var flow = [{id:"n1",type:"html",wires:[["n2"]],ret:"text",tag:"p"}, @@ -141,7 +160,7 @@ describe('html node', function() { }); }); - it('should retrive an attribute from a tag', function(done) { + it('should retrieve an attribute from a tag', function(done) { fs.readFile(file, 'utf8', function(err, data) { var flow = [{id:"n1",type:"html",wires:[["n2"]],ret:"attr",tag:"span img"}, {id:"n2", type:"helper"}];