Merge branch 'bug-css-selector'

This commit is contained in:
n1474335 2017-12-20 13:43:00 +00:00
commit fc8a0480fb
4 changed files with 1567 additions and 623 deletions

2136
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -89,6 +89,7 @@
"moment": "^2.20.1",
"moment-timezone": "^0.5.14",
"node-md6": "^0.1.0",
"nwmatcher": "^1.4.3",
"otp": "^0.1.3",
"sladex-blowfish": "^0.8.1",
"sortablejs": "^1.7.0",

View File

@ -2,9 +2,10 @@ import {camelCase, kebabCase, snakeCase} from "lodash";
import Utils from "../Utils.js";
import vkbeautify from "vkbeautify";
import {DOMParser as dom} from "xmldom";
import {DOMParser} from "xmldom";
import xpath from "xpath";
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";
@ -336,7 +337,7 @@ const Code = {
let doc;
try {
doc = new dom().parseFromString(input);
doc = new DOMParser().parseFromString(input, "application/xml");
} catch (err) {
return "Invalid input XML.";
}
@ -423,7 +424,7 @@ const Code = {
let query = args[0],
delimiter = args[1],
parser = new DOMParser(),
html,
dom,
result;
if (!query.length || !input.length) {
@ -431,32 +432,32 @@ const Code = {
}
try {
html = parser.parseFromString(input, "text/html");
dom = parser.parseFromString(input);
} catch (err) {
return "Invalid input HTML.";
}
try {
result = html.querySelectorAll(query);
const matcher = nwmatcher({document: dom});
result = matcher.select(query, dom);
} catch (err) {
return "Invalid CSS Selector. Details:\n" + err.message;
}
const nodeToString = function(node) {
return node.toString();
/* xmldom does not return the outerHTML value.
switch (node.nodeType) {
case Node.ELEMENT_NODE: return node.outerHTML;
case Node.ATTRIBUTE_NODE: return node.value;
case Node.COMMENT_NODE: return node.data;
case Node.TEXT_NODE: return node.wholeText;
case Node.DOCUMENT_NODE: return node.outerHTML;
case node.ELEMENT_NODE: return node.outerHTML;
case node.ATTRIBUTE_NODE: return node.value;
case node.TEXT_NODE: return node.wholeText;
case node.COMMENT_NODE: return node.data;
case node.DOCUMENT_NODE: return node.outerHTML;
default: throw new Error("Unknown Node Type: " + node.nodeType);
}
}*/
};
return Array.apply(null, Array(result.length))
.map(function(_, i) {
return result[i];
})
return result
.map(nodeToString)
.join(delimiter);
},

View File

@ -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"]
}
]
}
]);