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

Update to latest cheerio module

This commit is contained in:
Nick O'Leary 2021-06-08 09:31:27 +01:00
parent e9e64f6a44
commit dbbdd3f799
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
4 changed files with 161 additions and 96 deletions

View File

@ -33,7 +33,7 @@
"basic-auth": "2.0.1", "basic-auth": "2.0.1",
"bcryptjs": "2.4.3", "bcryptjs": "2.4.3",
"body-parser": "1.19.0", "body-parser": "1.19.0",
"cheerio": "0.22.0", "cheerio": "^1.0.0-rc.9",
"clone": "2.1.2", "clone": "2.1.2",
"content-type": "1.0.4", "content-type": "1.0.4",
"cookie": "0.4.1", "cookie": "0.4.1",

View File

@ -32,7 +32,7 @@ module.exports = function(RED) {
var tag = node.tag; var tag = node.tag;
if (msg.hasOwnProperty("select")) { tag = node.tag || msg.select; } if (msg.hasOwnProperty("select")) { tag = node.tag || msg.select; }
try { try {
var $ = cheerio.load(value); var $ = cheerio.load(value,null,false);
var pay = []; var pay = [];
var count = 0; var count = 0;
$(tag).each(function() { $(tag).each(function() {
@ -42,9 +42,11 @@ module.exports = function(RED) {
$(tag).each(function() { $(tag).each(function() {
if (node.as === "multi") { if (node.as === "multi") {
var pay2 = null; var pay2 = null;
if (node.ret === "html") { pay2 = cheerio.load($(this).html().trim()).xml(); } if (node.ret === "html") { pay2 = cheerio.load($(this).html().trim(),null,false).xml(); }
if (node.ret === "text") { pay2 = $(this).text(); } if (node.ret === "text") { pay2 = $(this).text(); }
if (node.ret === "attr") { pay2 = this.attribs; } if (node.ret === "attr") {
pay2 = Object.assign({},this.attribs);
}
//if (node.ret === "val") { pay2 = $(this).val(); } //if (node.ret === "val") { pay2 = $(this).val(); }
/* istanbul ignore else */ /* istanbul ignore else */
if (pay2) { if (pay2) {
@ -61,9 +63,12 @@ module.exports = function(RED) {
} }
} }
if (node.as === "single") { if (node.as === "single") {
if (node.ret === "html") { pay.push( cheerio.load($(this).html().trim()).xml() ); } if (node.ret === "html") { pay.push( cheerio.load($(this).html().trim(),null,false).xml() ); }
if (node.ret === "text") { pay.push( $(this).text() ); } if (node.ret === "text") { pay.push( $(this).text() ); }
if (node.ret === "attr") { pay.push( this.attribs ); } if (node.ret === "attr") {
var attribs = Object.assign({},this.attribs);
pay.push( attribs );
}
//if (node.ret === "val") { pay.push( $(this).val() ); } //if (node.ret === "val") { pay.push( $(this).val() ); }
} }
index++; index++;

View File

@ -19,7 +19,7 @@
"acorn-walk": "8.1.0", "acorn-walk": "8.1.0",
"ajv": "8.5.0", "ajv": "8.5.0",
"body-parser": "1.19.0", "body-parser": "1.19.0",
"cheerio": "0.22.0", "cheerio": "^1.0.0-rc.9",
"content-type": "1.0.4", "content-type": "1.0.4",
"cookie-parser": "1.4.5", "cookie-parser": "1.4.5",
"cookie": "0.4.1", "cookie": "0.4.1",

View File

@ -60,9 +60,13 @@ describe('HTML node', function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
n2.on("input", function(msg) { n2.on("input", function(msg) {
try {
msg.should.have.property('topic', 'bar'); msg.should.have.property('topic', 'bar');
should.equal(msg.payload, 'This is a test page for node 70-HTML'); should.equal(msg.payload, 'This is a test page for node 70-HTML');
done(); done();
} catch(err) {
done(err)
}
}); });
n1.receive({payload:data,topic:"bar",select:"h1"}); n1.receive({payload:data,topic:"bar",select:"h1"});
}); });
@ -78,9 +82,13 @@ describe('HTML node', function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
n2.on("input", function(msg) { n2.on("input", function(msg) {
try {
msg.should.have.property('topic', 'bar'); msg.should.have.property('topic', 'bar');
msg.foo[0].should.equal('This is a test page for node 70-HTML'); msg.foo[0].should.equal('This is a test page for node 70-HTML');
done(); done();
} catch(err) {
done(err)
}
}); });
n1.receive({foo:data,topic:"bar",select:"h1"}); n1.receive({foo:data,topic:"bar",select:"h1"});
}); });
@ -96,9 +104,13 @@ describe('HTML node', function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
n2.on("input", function(msg) { n2.on("input", function(msg) {
try {
msg.should.have.property('topic', 'bar'); msg.should.have.property('topic', 'bar');
msg.bar[0].should.equal('This is a test page for node 70-HTML'); msg.bar[0].should.equal('This is a test page for node 70-HTML');
done(); done();
} catch(err) {
done(err)
}
}); });
n1.receive({foo:data,topic:"bar"}); n1.receive({foo:data,topic:"bar"});
}); });
@ -114,10 +126,14 @@ describe('HTML node', function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
n2.on("input", function(msg) { n2.on("input", function(msg) {
try {
msg.should.have.property('topic', 'bar'); msg.should.have.property('topic', 'bar');
msg.should.have.property('payload'); msg.should.have.property('payload');
msg.payload.should.be.empty; msg.payload.should.be.empty;
done(); done();
} catch(err) {
done(err)
}
}); });
n1.receive({payload:data,topic:"bar",select:"h4"}); n1.receive({payload:data,topic:"bar",select:"h4"});
}); });
@ -133,9 +149,13 @@ describe('HTML node', function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
n2.on("input", function(msg) { n2.on("input", function(msg) {
try {
msg.should.have.property('topic', 'bar'); msg.should.have.property('topic', 'bar');
should.equal(msg.payload, 'There\'s nothing to read here.'); should.equal(msg.payload, 'There\'s nothing to read here.');
done(); done();
} catch(err) {
done(err)
}
}); });
n1.receive({payload:data,topic: "bar"}); n1.receive({payload:data,topic: "bar"});
}); });
@ -151,10 +171,14 @@ describe('HTML node', function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
n2.on("input", function(msg) { n2.on("input", function(msg) {
try {
msg.should.have.property('topic', 'bar'); msg.should.have.property('topic', 'bar');
msg.payload[0].indexOf("<li>Blue</li>").should.be.above(-1); msg.payload[0].indexOf("<li>Blue</li>").should.be.above(-1);
msg.payload[0].indexOf("<li>Red</li>").should.be.above(-1); msg.payload[0].indexOf("<li>Red</li>").should.be.above(-1);
done(); done();
} catch(err) {
done(err)
}
}); });
n1.receive({payload:data,topic: "bar"}); n1.receive({payload:data,topic: "bar"});
}); });
@ -170,11 +194,14 @@ describe('HTML node', function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
n2.on("input", function(msg) { n2.on("input", function(msg) {
try {
msg.should.have.property('topic', 'bar'); msg.should.have.property('topic', 'bar');
msg.payload[0].indexOf("Blue").should.be.above(-1); msg.payload[0].indexOf("Blue").should.be.above(-1);
msg.payload[0].indexOf("Red").should.be.above(-1); msg.payload[0].indexOf("Red").should.be.above(-1);
done(); done();
} catch(err) {
done(err)
}
}); });
n1.receive({payload:data,topic: "bar"}); n1.receive({payload:data,topic: "bar"});
}); });
@ -190,9 +217,13 @@ describe('HTML node', function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
n2.on("input", function(msg) { n2.on("input", function(msg) {
try {
msg.should.have.property('topic', 'bar'); msg.should.have.property('topic', 'bar');
should.equal(msg.payload, '<img src="foo.png"/>'); should.equal(msg.payload, '<img src="foo.png"/>');
done(); done();
} catch(err) {
done(err)
}
}); });
n1.receive({payload:data,topic: "bar"}); n1.receive({payload:data,topic: "bar"});
}); });
@ -208,10 +239,14 @@ describe('HTML node', function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
n2.on("input", function(msg) { n2.on("input", function(msg) {
try {
msg.should.have.property('payload'); msg.should.have.property('payload');
msg.payload[0].should.have.property('src','foo.png'); msg.payload[0].should.have.property('src','foo.png');
msg.should.have.property('topic', 'bar'); msg.should.have.property('topic', 'bar');
done(); done();
} catch(err) {
done(err)
}
}); });
n1.receive({payload:data,topic: "bar"}); n1.receive({payload:data,topic: "bar"});
}); });
@ -258,9 +293,13 @@ describe('HTML node', function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
n2.on("input", function(msg) { n2.on("input", function(msg) {
try {
msg.should.have.property('topic', 'bar'); msg.should.have.property('topic', 'bar');
msg.should.not.have.property('payload'); msg.should.not.have.property('payload');
done(); done();
} catch(err) {
done(err)
}
}); });
n1.receive({topic: "bar"}); n1.receive({topic: "bar"});
}); });
@ -301,6 +340,7 @@ describe('HTML node', function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
n2.on("input", function(msg) { n2.on("input", function(msg) {
try {
cnt++; cnt++;
msg.should.have.property('topic', 'bar'); msg.should.have.property('topic', 'bar');
check_parts(msg, cnt -1, 2); check_parts(msg, cnt -1, 2);
@ -315,6 +355,10 @@ describe('HTML node', function() {
msg.payload.indexOf("<li>Parsnip</li>").should.be.above(-1); msg.payload.indexOf("<li>Parsnip</li>").should.be.above(-1);
done(); done();
} }
} catch(err) {
done(err)
}
}); });
n1.receive({payload:data,topic: "bar"}); n1.receive({payload:data,topic: "bar"});
}); });
@ -331,6 +375,7 @@ describe('HTML node', function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
n2.on("input", function(msg) { n2.on("input", function(msg) {
try {
cnt++; cnt++;
msg.should.have.property('topic', 'bar'); msg.should.have.property('topic', 'bar');
check_parts(msg, cnt -1, 2); check_parts(msg, cnt -1, 2);
@ -345,6 +390,9 @@ describe('HTML node', function() {
msg.foo.indexOf("<li>Parsnip</li>").should.be.above(-1); msg.foo.indexOf("<li>Parsnip</li>").should.be.above(-1);
done(); done();
} }
} catch(err) {
done(err)
}
}); });
n1.receive({foo:data, topic:"bar"}); n1.receive({foo:data, topic:"bar"});
}); });
@ -360,6 +408,7 @@ describe('HTML node', function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
n2.on("input", function(msg) { n2.on("input", function(msg) {
try {
cnt++; cnt++;
msg.should.have.property('topic', 'bar'); msg.should.have.property('topic', 'bar');
check_parts(msg, cnt -1, 2); check_parts(msg, cnt -1, 2);
@ -374,6 +423,9 @@ describe('HTML node', function() {
msg.payload.indexOf("Parsnip").should.be.above(-1); msg.payload.indexOf("Parsnip").should.be.above(-1);
done(); done();
} }
} catch(err) {
done(err)
}
}); });
n1.receive({payload:data,topic: "bar"}); n1.receive({payload:data,topic: "bar"});
}); });
@ -389,12 +441,16 @@ describe('HTML node', function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
n2.on("input", function(msg) { n2.on("input", function(msg) {
try {
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); 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();
} catch(err) {
done(err)
}
}); });
n1.receive({payload:data,topic: "bar"}); n1.receive({payload:data,topic: "bar"});
}); });
@ -411,6 +467,7 @@ describe('HTML node', function() {
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
var prev_msg = undefined; var prev_msg = undefined;
n2.on("input", function(msg) { n2.on("input", function(msg) {
try {
cnt++; cnt++;
if (prev_msg == undefined) { if (prev_msg == undefined) {
prev_msg = msg; prev_msg = msg;
@ -421,6 +478,9 @@ describe('HTML node', function() {
if (cnt == 2) { if (cnt == 2) {
done(); done();
} }
} catch(err) {
done(err)
}
}); });
n1.receive({payload:data,topic: "bar"}); n1.receive({payload:data,topic: "bar"});
}); });