Merge branch 'bug-css-selector'
This commit is contained in:
commit
fc8a0480fb
2136
package-lock.json
generated
2136
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -89,6 +89,7 @@
|
|||||||
"moment": "^2.20.1",
|
"moment": "^2.20.1",
|
||||||
"moment-timezone": "^0.5.14",
|
"moment-timezone": "^0.5.14",
|
||||||
"node-md6": "^0.1.0",
|
"node-md6": "^0.1.0",
|
||||||
|
"nwmatcher": "^1.4.3",
|
||||||
"otp": "^0.1.3",
|
"otp": "^0.1.3",
|
||||||
"sladex-blowfish": "^0.8.1",
|
"sladex-blowfish": "^0.8.1",
|
||||||
"sortablejs": "^1.7.0",
|
"sortablejs": "^1.7.0",
|
||||||
|
@ -2,9 +2,10 @@ import {camelCase, kebabCase, snakeCase} from "lodash";
|
|||||||
|
|
||||||
import Utils from "../Utils.js";
|
import Utils from "../Utils.js";
|
||||||
import vkbeautify from "vkbeautify";
|
import vkbeautify from "vkbeautify";
|
||||||
import {DOMParser as dom} from "xmldom";
|
import {DOMParser} from "xmldom";
|
||||||
import xpath from "xpath";
|
import xpath from "xpath";
|
||||||
import jpath from "jsonpath";
|
import jpath from "jsonpath";
|
||||||
|
import nwmatcher from "nwmatcher";
|
||||||
import prettyPrintOne from "imports-loader?window=>global!exports-loader?prettyPrintOne!google-code-prettify/bin/prettify.min.js";
|
import prettyPrintOne from "imports-loader?window=>global!exports-loader?prettyPrintOne!google-code-prettify/bin/prettify.min.js";
|
||||||
|
|
||||||
|
|
||||||
@ -336,7 +337,7 @@ const Code = {
|
|||||||
|
|
||||||
let doc;
|
let doc;
|
||||||
try {
|
try {
|
||||||
doc = new dom().parseFromString(input);
|
doc = new DOMParser().parseFromString(input, "application/xml");
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return "Invalid input XML.";
|
return "Invalid input XML.";
|
||||||
}
|
}
|
||||||
@ -423,7 +424,7 @@ const Code = {
|
|||||||
let query = args[0],
|
let query = args[0],
|
||||||
delimiter = args[1],
|
delimiter = args[1],
|
||||||
parser = new DOMParser(),
|
parser = new DOMParser(),
|
||||||
html,
|
dom,
|
||||||
result;
|
result;
|
||||||
|
|
||||||
if (!query.length || !input.length) {
|
if (!query.length || !input.length) {
|
||||||
@ -431,32 +432,32 @@ const Code = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
html = parser.parseFromString(input, "text/html");
|
dom = parser.parseFromString(input);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return "Invalid input HTML.";
|
return "Invalid input HTML.";
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
result = html.querySelectorAll(query);
|
const matcher = nwmatcher({document: dom});
|
||||||
|
result = matcher.select(query, dom);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return "Invalid CSS Selector. Details:\n" + err.message;
|
return "Invalid CSS Selector. Details:\n" + err.message;
|
||||||
}
|
}
|
||||||
|
|
||||||
const nodeToString = function(node) {
|
const nodeToString = function(node) {
|
||||||
|
return node.toString();
|
||||||
|
/* xmldom does not return the outerHTML value.
|
||||||
switch (node.nodeType) {
|
switch (node.nodeType) {
|
||||||
case Node.ELEMENT_NODE: return node.outerHTML;
|
case node.ELEMENT_NODE: return node.outerHTML;
|
||||||
case Node.ATTRIBUTE_NODE: return node.value;
|
case node.ATTRIBUTE_NODE: return node.value;
|
||||||
case Node.COMMENT_NODE: return node.data;
|
case node.TEXT_NODE: return node.wholeText;
|
||||||
case Node.TEXT_NODE: return node.wholeText;
|
case node.COMMENT_NODE: return node.data;
|
||||||
case Node.DOCUMENT_NODE: return node.outerHTML;
|
case node.DOCUMENT_NODE: return node.outerHTML;
|
||||||
default: throw new Error("Unknown Node Type: " + node.nodeType);
|
default: throw new Error("Unknown Node Type: " + node.nodeType);
|
||||||
}
|
}*/
|
||||||
};
|
};
|
||||||
|
|
||||||
return Array.apply(null, Array(result.length))
|
return result
|
||||||
.map(function(_, i) {
|
|
||||||
return result[i];
|
|
||||||
})
|
|
||||||
.map(nodeToString)
|
.map(nodeToString)
|
||||||
.join(delimiter);
|
.join(delimiter);
|
||||||
},
|
},
|
||||||
|
@ -310,4 +310,26 @@ TestRegister.addTests([
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "CSS selector",
|
||||||
|
input: '<div id="test">\n<p class="a">hello</p>\n<p>world</p>\n<p class="a">again</p>\n</div>',
|
||||||
|
expectedOutput: '<p class="a">hello</p>\n<p class="a">again</p>',
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
"op": "CSS selector",
|
||||||
|
"args": ["#test p.a", "\\n"]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "XPath expression",
|
||||||
|
input: '<div id="test">\n<p class="a">hello</p>\n<p>world</p>\n<p class="a">again</p>\n</div>',
|
||||||
|
expectedOutput: '<p class="a">hello</p>\n<p class="a">again</p>',
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
"op": "XPath expression",
|
||||||
|
"args": ["/div/p[@class=\"a\"]", "\\n"]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
]);
|
]);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user