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
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);
}
}