mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-20 14:56:19 -04:00
Ported rest of Code ops & enabled/fixed some tests
This commit is contained in:
parent
ade056e881
commit
eb3a2502f5
14 changed files with 597 additions and 4 deletions
88
src/core/operations/CSSSelector.mjs
Normal file
88
src/core/operations/CSSSelector.mjs
Normal file
|
@ -0,0 +1,88 @@
|
|||
/**
|
||||
* @author n1474335 [n1474335@gmail.com]
|
||||
* @copyright Crown Copyright 2016
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import nwmatcher from "nwmatcher";
|
||||
import Operation from "../Operation";
|
||||
|
||||
/**
|
||||
* CSS selector operation
|
||||
*/
|
||||
class CSSSelector extends Operation {
|
||||
|
||||
/**
|
||||
* CSSSelector constructor
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.name = "CSS selector";
|
||||
this.module = "Code";
|
||||
this.description = "Extract information from an HTML document with a CSS selector";
|
||||
this.inputType = "string";
|
||||
this.outputType = "string";
|
||||
this.args = [
|
||||
{
|
||||
"name": "CSS selector",
|
||||
"type": "string",
|
||||
"value": ""
|
||||
},
|
||||
{
|
||||
"name": "Delimiter",
|
||||
"type": "binaryShortString",
|
||||
"value": "\\n"
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} input
|
||||
* @param {Object[]} args
|
||||
* @returns {string}
|
||||
*/
|
||||
run(input, args) {
|
||||
const [query, delimiter] = args,
|
||||
parser = new DOMParser();
|
||||
let dom,
|
||||
result;
|
||||
|
||||
if (!query.length || !input.length) {
|
||||
return "";
|
||||
}
|
||||
|
||||
try {
|
||||
dom = parser.parseFromString(input);
|
||||
} catch (err) {
|
||||
return "Invalid input HTML.";
|
||||
}
|
||||
|
||||
try {
|
||||
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.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 result
|
||||
.map(nodeToString)
|
||||
.join(delimiter);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default CSSSelector;
|
Loading…
Add table
Add a link
Reference in a new issue