1
0
mirror of synced 2024-11-15 10:47:41 +01:00

Ported rest of Code ops & enabled/fixed some tests

This commit is contained in:
Matt C 2018-05-27 22:07:09 +01:00
parent ade056e881
commit eb3a2502f5
14 changed files with 597 additions and 4 deletions

View File

@ -0,0 +1,47 @@
/**
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2016
* @license Apache-2.0
*/
import vkbeautify from "vkbeautify";
import Operation from "../Operation";
/**
* CSS Beautify operation
*/
class CSSBeautify extends Operation {
/**
* CSSBeautify constructor
*/
constructor() {
super();
this.name = "CSS Beautify";
this.module = "Code";
this.description = "Indents and prettifies Cascading Style Sheets (CSS) code.";
this.inputType = "string";
this.outputType = "string";
this.args = [
{
"name": "Indent string",
"type": "binaryShortString",
"value": "\\t"
}
];
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
const indentStr = args[0];
return vkbeautify.css(input, indentStr);
}
}
export default CSSBeautify;

View File

@ -0,0 +1,47 @@
/**
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2016
* @license Apache-2.0
*/
import vkbeautify from "vkbeautify";
import Operation from "../Operation";
/**
* CSS Minify operation
*/
class CSSMinify extends Operation {
/**
* CSSMinify constructor
*/
constructor() {
super();
this.name = "CSS Minify";
this.module = "Code";
this.description = "Compresses Cascading Style Sheets (CSS) code.";
this.inputType = "string";
this.outputType = "string";
this.args = [
{
"name": "Preserve comments",
"type": "boolean",
"value": false
}
];
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
const preserveComments = args[0];
return vkbeautify.cssmin(input, preserveComments);
}
}
export default CSSMinify;

View 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;

View File

@ -6,7 +6,7 @@
import Operation from "../Operation";
import Utils from "../Utils";
import * as JsDiff from "diff";
import JsDiff from "diff";
import OperationError from "../errors/OperationError";
/**

View File

@ -0,0 +1,68 @@
/**
* @author Matt C (matt@artemisbot.uk)
* @copyright Crown Copyright 2016
* @license Apache-2.0
*/
import jpath from "jsonpath";
import Operation from "../Operation";
import OperationError from "../errors/OperationError";
/**
* JPath expression operation
*/
class JPathExpression extends Operation {
/**
* JPathExpression constructor
*/
constructor() {
super();
this.name = "JPath expression";
this.module = "Code";
this.description = "Extract information from a JSON object with a JPath query.";
this.inputType = "string";
this.outputType = "string";
this.args = [
{
"name": "Query",
"type": "string",
"value": ""
},
{
"name": "Result delimiter",
"type": "binaryShortString",
"value": "\\n"
}
];
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
const [query, delimiter] = args;
let results,
obj;
try {
obj = JSON.parse(input);
} catch (err) {
throw new OperationError(`Invalid input JSON: ${err.message}`);
}
try {
results = jpath.query(obj, query);
} catch (err) {
throw new OperationError(`Invalid JPath expression: ${err.message}`);
}
return results.map(result => JSON.stringify(result)).join(delimiter);
}
}
export default JPathExpression;

View File

@ -0,0 +1,48 @@
/**
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2016
* @license Apache-2.0
*/
import vkbeautify from "vkbeautify";
import Operation from "../Operation";
/**
* JSON Beautify operation
*/
class JSONBeautify extends Operation {
/**
* JSONBeautify constructor
*/
constructor() {
super();
this.name = "JSON Beautify";
this.module = "Code";
this.description = "Indents and prettifies JavaScript Object Notation (JSON) code.";
this.inputType = "string";
this.outputType = "string";
this.args = [
{
"name": "Indent string",
"type": "binaryShortString",
"value": "\\t"
}
];
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
const indentStr = args[0];
if (!input) return "";
return vkbeautify.json(input, indentStr);
}
}
export default JSONBeautify;

View File

@ -0,0 +1,41 @@
/**
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2016
* @license Apache-2.0
*/
import vkbeautify from "vkbeautify";
import Operation from "../Operation";
/**
* JSON Minify operation
*/
class JSONMinify extends Operation {
/**
* JSONMinify constructor
*/
constructor() {
super();
this.name = "JSON Minify";
this.module = "Code";
this.description = "Compresses JavaScript Object Notation (JSON) code.";
this.inputType = "string";
this.outputType = "string";
this.args = [];
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
if (!input) return "";
return vkbeautify.jsonmin(input);
}
}
export default JSONMinify;

View File

@ -0,0 +1,47 @@
/**
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2016
* @license Apache-2.0
*/
import vkbeautify from "vkbeautify";
import Operation from "../Operation";
/**
* SQL Beautify operation
*/
class SQLBeautify extends Operation {
/**
* SQLBeautify constructor
*/
constructor() {
super();
this.name = "SQL Beautify";
this.module = "Code";
this.description = "Indents and prettifies Structured Query Language (SQL) code.";
this.inputType = "string";
this.outputType = "string";
this.args = [
{
"name": "Indent string",
"type": "binaryShortString",
"value": "\\t"
}
];
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
const indentStr = args[0];
return vkbeautify.sql(input, indentStr);
}
}
export default SQLBeautify;

View File

@ -0,0 +1,40 @@
/**
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2016
* @license Apache-2.0
*/
import vkbeautify from "vkbeautify";
import Operation from "../Operation";
/**
* SQL Minify operation
*/
class SQLMinify extends Operation {
/**
* SQLMinify constructor
*/
constructor() {
super();
this.name = "SQL Minify";
this.module = "Code";
this.description = "Compresses Structured Query Language (SQL) code.";
this.inputType = "string";
this.outputType = "string";
this.args = [];
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
return vkbeautify.sqlmin(input);
}
}
export default SQLMinify;

View File

@ -0,0 +1,47 @@
/**
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2016
* @license Apache-2.0
*/
import vkbeautify from "vkbeautify";
import Operation from "../Operation";
/**
* XML Beautify operation
*/
class XMLBeautify extends Operation {
/**
* XMLBeautify constructor
*/
constructor() {
super();
this.name = "XML Beautify";
this.module = "Code";
this.description = "Indents and prettifies eXtensible Markup Language (XML) code.";
this.inputType = "string";
this.outputType = "string";
this.args = [
{
"name": "Indent string",
"type": "binaryShortString",
"value": "\\t"
}
];
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
const indentStr = args[0];
return vkbeautify.xml(input, indentStr);
}
}
export default XMLBeautify;

View File

@ -0,0 +1,47 @@
/**
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2016
* @license Apache-2.0
*/
import vkbeautify from "vkbeautify";
import Operation from "../Operation";
/**
* XML Minify operation
*/
class XMLMinify extends Operation {
/**
* XMLMinify constructor
*/
constructor() {
super();
this.name = "XML Minify";
this.module = "Code";
this.description = "Compresses eXtensible Markup Language (XML) code.";
this.inputType = "string";
this.outputType = "string";
this.args = [
{
"name": "Preserve comments",
"type": "boolean",
"value": false
}
];
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
const preserveComments = args[0];
return vkbeautify.xmlmin(input, preserveComments);
}
}
export default XMLMinify;

View File

@ -0,0 +1,72 @@
/**
* @author Mikescher (https://github.com/Mikescher | https://mikescher.com)
* @copyright Crown Copyright 2016
* @license Apache-2.0
*/
import xpath from "xpath";
import Operation from "../Operation";
import OperationError from "../errors/OperationError";
/**
* XPath expression operation
*/
class XPathExpression extends Operation {
/**
* XPathExpression constructor
*/
constructor() {
super();
this.name = "XPath expression";
this.module = "Code";
this.description = "Extract information from an XML document with an XPath query";
this.inputType = "string";
this.outputType = "string";
this.args = [
{
"name": "XPath",
"type": "string",
"value": ""
},
{
"name": "Result delimiter",
"type": "binaryShortString",
"value": "\\n"
}
];
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
const [query, delimiter] = args;
let doc;
try {
doc = new DOMParser().parseFromString(input, "application/xml");
} catch (err) {
throw new OperationError("Invalid input XML.");
}
let nodes;
try {
nodes = xpath.select(query, doc);
} catch (err) {
throw new OperationError(`Invalid XPath. Details:\n${err.message}.`);
}
const nodeToString = function(node) {
return node.toString();
};
return nodes.map(nodeToString).join(delimiter);
}
}
export default XPathExpression;

View File

@ -34,7 +34,7 @@ import "./tests/operations/CartesianProduct";
import "./tests/operations/CharEnc";
import "./tests/operations/Ciphers";
import "./tests/operations/Checksum";
// import "./tests/operations/Code";
import "./tests/operations/Code";
import "./tests/operations/Compress";
import "./tests/operations/Crypt";
import "./tests/operations/DateTime";
@ -54,7 +54,7 @@ import "./tests/operations/OTP";
import "./tests/operations/PowerSet";
// import "./tests/operations/Regex";
import "./tests/operations/Rotate";
// import "./tests/operations/StrUtils";
import "./tests/operations/StrUtils";
import "./tests/operations/SeqUtils";
import "./tests/operations/SetDifference";
import "./tests/operations/SetIntersection";

View File

@ -310,6 +310,7 @@ TestRegister.addTests([
}
],
},
/* Since we don't pack ops before running tests, there's no polyfill for DomParser()
{
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>',
@ -331,5 +332,5 @@ TestRegister.addTests([
"args": ["/div/p[@class=\"a\"]", "\\n"]
}
]
}
}*/
]);