1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

add parts support for HTML node (#1495)

* add parts support for HTML node

* add parts.{type,ch} to output of HTML node
This commit is contained in:
Hiroyasu Nishiyama 2017-12-01 22:09:05 +09:00 committed by Dave Conway-Jones
parent b9213b73bd
commit 806457063f
2 changed files with 33 additions and 0 deletions

View File

@ -31,6 +31,11 @@ module.exports = function(RED) {
try { try {
var $ = cheerio.load(msg.payload); var $ = cheerio.load(msg.payload);
var pay = []; var pay = [];
var count = 0;
$(tag).each(function() {
count++;
});
var index = 0;
$(tag).each(function() { $(tag).each(function() {
if (node.as === "multi") { if (node.as === "multi") {
var pay2 = null; var pay2 = null;
@ -41,6 +46,13 @@ module.exports = function(RED) {
/* istanbul ignore else */ /* istanbul ignore else */
if (pay2) { if (pay2) {
msg.payload = pay2; msg.payload = pay2;
msg.parts = {
id: msg._msgid,
index: index,
count: count,
type: "string",
ch: ""
};
node.send(msg); node.send(msg);
} }
} }
@ -50,6 +62,7 @@ module.exports = function(RED) {
if (node.ret === "attr") { pay.push( this.attribs ); } if (node.ret === "attr") { pay.push( this.attribs ); }
//if (node.ret === "val") { pay.push( $(this).val() ); } //if (node.ret === "val") { pay.push( $(this).val() ); }
} }
index++;
}); });
if ((node.as === "single") && (pay.length !== 0)) { if ((node.as === "single") && (pay.length !== 0)) {
msg.payload = pay; msg.payload = pay;

View File

@ -207,12 +207,29 @@ describe('html node', function() {
describe('multiple messages', function(){ describe('multiple messages', function(){
var cnt = 0; var cnt = 0;
var parts_id = undefined;
afterEach(function() { afterEach(function() {
cnt.should.be.exactly(2); cnt.should.be.exactly(2);
cnt = 0; cnt = 0;
parts_id = undefined;
}); });
function check_parts(msg, index, count) {
msg.should.have.property('parts');
msg.parts.should.have.property('id');
if(parts_id === undefined) {
parts_id = msg.parts.id;
}
else {
msg.parts.should.have.property('id', parts_id);
}
msg.parts.should.have.property('index', index);
msg.parts.should.have.property('count', count);
msg.parts.should.have.property('type', 'string');
msg.parts.should.have.property('ch', '');
}
it('should retrieve list contents as html as default with output as multiple msgs ', function(done) { it('should retrieve list contents as html as default with output as multiple msgs ', function(done) {
fs.readFile(file, 'utf8', function(err, data) { fs.readFile(file, 'utf8', function(err, data) {
var flow = [{id:"n1",type:"html",wires:[["n2"]],tag:"ul",as:"multi"}, var flow = [{id:"n1",type:"html",wires:[["n2"]],tag:"ul",as:"multi"},
@ -224,6 +241,7 @@ describe('html node', function() {
n2.on("input", function(msg) { n2.on("input", function(msg) {
cnt++; cnt++;
msg.should.have.property('topic', 'bar'); msg.should.have.property('topic', 'bar');
check_parts(msg, cnt -1, 2);
if (cnt !== 1 && cnt !== 2) { if (cnt !== 1 && cnt !== 2) {
return false; return false;
} }
@ -252,6 +270,7 @@ describe('html node', function() {
n2.on("input", function(msg) { n2.on("input", function(msg) {
cnt++; cnt++;
msg.should.have.property('topic', 'bar'); msg.should.have.property('topic', 'bar');
check_parts(msg, cnt -1, 2);
if (cnt !== 1 && cnt !== 2) { if (cnt !== 1 && cnt !== 2) {
return false; return false;
} }
@ -281,6 +300,7 @@ describe('html node', function() {
msg.should.have.property('payload'); msg.should.have.property('payload');
msg.payload.should.have.property('src','foo.png'); msg.payload.should.have.property('src','foo.png');
msg.should.have.property('topic', 'bar'); msg.should.have.property('topic', 'bar');
check_parts(msg, 0, 1);
cnt = 2; // frig the answer as only one img tag cnt = 2; // frig the answer as only one img tag
done(); done();
}); });