Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add parts support for HTML node #1495

Merged
merged 2 commits into from Dec 1, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions nodes/core/parsers/70-HTML.js
Expand Up @@ -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;
Expand All @@ -41,6 +46,11 @@ module.exports = function(RED) {
/* istanbul ignore else */
if (pay2) {
msg.payload = pay2;
msg.parts = {
id: msg._msgid,
index: index,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we also add the msg.parts.type ? in case someone wants to rebuild an array automatically ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for reviewing.
It seems to be useful.
And msg.parts.ch is also needed for automatic join.

count: count
};
node.send(msg);
}
}
Expand All @@ -50,6 +60,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;
Expand Down
9 changes: 9 additions & 0 deletions test/nodes/core/parsers/70-HTML_spec.js
Expand Up @@ -213,6 +213,12 @@ describe('html node', function() {
cnt = 0;
});

function check_parts(msg, index, count) {
msg.should.have.property('parts');
msg.parts.should.have.property('index', index);
msg.parts.should.have.property('count', count);
}

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"},
Expand All @@ -224,6 +230,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;
}
Expand Down Expand Up @@ -252,6 +259,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;
}
Expand Down Expand Up @@ -281,6 +289,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();
});
Expand Down