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

let HTML node use alternative msg property

This commit is contained in:
Dave Conway-Jones 2018-01-30 16:09:01 +00:00
parent 6725f870d2
commit b0c876019a
No known key found for this signature in database
GPG Key ID: 9E7F9C73F5168CD4
3 changed files with 60 additions and 6 deletions

View File

@ -1,5 +1,9 @@
<script type="text/x-red" data-template-name="html">
<div class="form-row">
<label for="node-input-property"><i class="fa fa-ellipsis-h"></i> <span data-i18n="node-red:common.label.property"></span></label>
<input type="text" id="node-input-property" style="width:70%;"/>
</div>
<div class="form-row">
<label for="node-input-tag"><i class="fa fa-filter"></i> <span data-i18n="html.label.select"></span></label>
<input type="text" id="node-input-tag" placeholder="h1">
@ -40,7 +44,7 @@
<dl class="message-properties">
<dt>payload <span class="property-type">array | string</span></dt>
<dd>the result can be either a single message with a payload containing an array of the matched elements, or multiple
messages that each contain a matched element.</dd>
messages that each contain a matched element. If multiple messages are sent they will also have <code>parts</code> set.</dd>
</dl>
<h3>Details</h3>
<p>This node supports a combination of CSS and jQuery selectors. See the

View File

@ -20,16 +20,18 @@ module.exports = function(RED) {
function CheerioNode(n) {
RED.nodes.createNode(this,n);
this.property = n.property||"payload";
this.tag = n.tag;
this.ret = n.ret || "html";
this.as = n.as || "single";
var node = this;
this.on("input", function(msg) {
if (msg.hasOwnProperty("payload")) {
var value = RED.util.getMessageProperty(msg,node.property);
if (value !== undefined) {
var tag = node.tag;
if (msg.hasOwnProperty("select")) { tag = node.tag || msg.select; }
try {
var $ = cheerio.load(msg.payload);
var $ = cheerio.load(value);
var pay = [];
var count = 0;
$(tag).each(function() {
@ -46,7 +48,7 @@ module.exports = function(RED) {
/* istanbul ignore else */
if (pay2) {
var new_msg = RED.util.cloneMessage(msg);
new_msg.payload = pay2;
RED.util.setMessageProperty(new_msg,node.property,pay2);
new_msg.parts = {
id: msg._msgid,
index: index,
@ -66,7 +68,7 @@ module.exports = function(RED) {
index++;
});
if (node.as === "single") { // Always return an array - even if blank
msg.payload = pay;
RED.util.setMessageProperty(msg,node.property,pay);
node.send(msg);
}
}

View File

@ -66,6 +66,24 @@ describe('html node', function() {
});
});
it('should retrieve header contents if asked to by msg.select - alternative property', function(done) {
fs.readFile(file, 'utf8', function(err, data) {
var flow = [{id:"n1",type:"html",property:"foo",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');
should.equal(msg.foo, 'This is a test page for node 70-HTML');
done();
});
n1.receive({foo:data,topic:"bar",select:"h1"});
});
});
});
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;"},
@ -249,7 +267,7 @@ describe('html node', function() {
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) {
var flow = [{id:"n1",type:"html",wires:[["n2"]],tag:"ul",as:"multi"},
{id:"n2", type:"helper"}];
@ -278,6 +296,36 @@ describe('html node', function() {
});
});
it('should retrieve list contents as html as default with output as multiple msgs - alternative property', function(done) {
fs.readFile(file, 'utf8', function(err, data) {
var flow = [{id:"n1",type:"html",property:"foo",wires:[["n2"]],tag:"ul",as:"multi"},
{id:"n2", type:"helper"}];
helper.load(htmlNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
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;
}
if (cnt === 1) {
msg.foo.indexOf("<li>Apple</li>").should.be.above(-1);
msg.foo.indexOf("<li>Pear</li>").should.be.above(-1);
} else if (cnt === 2) {
msg.foo.indexOf("<li>Potato</li>").should.be.above(-1);
msg.foo.indexOf("<li>Parsnip</li>").should.be.above(-1);
done();
}
});
n1.receive({foo:data, topic:"bar"});
});
});
});
it('should retrieve list contents as text with output as multiple msgs ', function(done) {
fs.readFile(file, 'utf8', function(err, data) {
var flow = [{id:"n1",type:"html",wires:[["n2"]],tag:"ul",ret:"text",as:"multi"},