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

View File

@ -207,12 +207,29 @@ describe('html node', function() {
describe('multiple messages', function(){
var cnt = 0;
var parts_id = undefined;
afterEach(function() {
cnt.should.be.exactly(2);
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) {
fs.readFile(file, 'utf8', function(err, data) {
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) {
cnt++;
msg.should.have.property('topic', 'bar');
check_parts(msg, cnt -1, 2);
if (cnt !== 1 && cnt !== 2) {
return false;
}
@ -252,6 +270,7 @@ describe('html node', function() {
n2.on("input", function(msg) {
cnt++;
msg.should.have.property('topic', 'bar');
check_parts(msg, cnt -1, 2);
if (cnt !== 1 && cnt !== 2) {
return false;
}
@ -281,6 +300,7 @@ describe('html node', function() {
msg.should.have.property('payload');
msg.payload.should.have.property('src','foo.png');
msg.should.have.property('topic', 'bar');
check_parts(msg, 0, 1);
cnt = 2; // frig the answer as only one img tag
done();
});