2017-05-02 11:21:04 -04:00
|
|
|
import {camelCase, kebabCase, snakeCase} from "lodash";
|
|
|
|
|
2017-03-23 17:52:20 +00:00
|
|
|
import Utils from "../Utils.js";
|
|
|
|
import vkbeautify from "vkbeautify";
|
|
|
|
import {DOMParser as dom} from "xmldom";
|
|
|
|
import xpath from "xpath";
|
2017-08-04 13:59:32 +00:00
|
|
|
import jpath from "jsonpath";
|
2017-03-25 13:32:35 +00:00
|
|
|
import prettyPrintOne from "imports-loader?window=>global!exports-loader?prettyPrintOne!google-code-prettify/bin/prettify.min.js";
|
2017-03-06 12:45:51 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Code operations.
|
|
|
|
*
|
|
|
|
* @author n1474335 [n1474335@gmail.com]
|
|
|
|
* @copyright Crown Copyright 2016
|
|
|
|
* @license Apache-2.0
|
|
|
|
*
|
|
|
|
* @namespace
|
|
|
|
*/
|
2017-03-23 17:52:20 +00:00
|
|
|
const Code = {
|
2016-12-14 16:39:17 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
LANGUAGES: ["default-code", "default-markup", "bash", "bsh", "c", "cc", "coffee", "cpp", "cs", "csh", "cv", "cxx", "cyc", "htm", "html", "in.tag", "java", "javascript", "js", "json", "m", "mxml", "perl", "pl", "pm", "py", "python", "rb", "rc", "rs", "ruby", "rust", "sh", "uq.val", "xhtml", "xml", "xsl"],
|
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
LINE_NUMS: false,
|
2016-12-14 16:39:17 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* Syntax highlighter operation.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {html}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runSyntaxHighlight: function(input, args) {
|
2017-04-13 18:08:50 +01:00
|
|
|
let language = args[0],
|
2017-01-31 18:24:56 +00:00
|
|
|
lineNums = args[1];
|
2017-03-21 22:41:44 +00:00
|
|
|
return "<code class='prettyprint'>" + prettyPrintOne(Utils.escapeHtml(input), language, lineNums) + "</code>";
|
2016-11-28 10:42:58 +00:00
|
|
|
},
|
2016-12-14 16:39:17 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
BEAUTIFY_INDENT: "\\t",
|
2016-12-14 16:39:17 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* XML Beautify operation.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runXmlBeautify: function(input, args) {
|
2017-04-13 18:08:50 +01:00
|
|
|
const indentStr = args[0];
|
2017-03-23 17:52:20 +00:00
|
|
|
return vkbeautify.xml(input, indentStr);
|
2016-11-28 10:42:58 +00:00
|
|
|
},
|
2016-12-14 16:39:17 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* JSON Beautify operation.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runJsonBeautify: function(input, args) {
|
2017-04-13 18:08:50 +01:00
|
|
|
const indentStr = args[0];
|
2016-12-03 00:51:17 +00:00
|
|
|
if (!input) return "";
|
2017-03-23 17:52:20 +00:00
|
|
|
return vkbeautify.json(input, indentStr);
|
2016-11-28 10:42:58 +00:00
|
|
|
},
|
2016-12-14 16:39:17 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* CSS Beautify operation.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runCssBeautify: function(input, args) {
|
2017-04-13 18:08:50 +01:00
|
|
|
const indentStr = args[0];
|
2017-03-23 17:52:20 +00:00
|
|
|
return vkbeautify.css(input, indentStr);
|
2016-11-28 10:42:58 +00:00
|
|
|
},
|
2016-12-14 16:39:17 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* SQL Beautify operation.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runSqlBeautify: function(input, args) {
|
2017-04-13 18:08:50 +01:00
|
|
|
const indentStr = args[0];
|
2017-03-23 17:52:20 +00:00
|
|
|
return vkbeautify.sql(input, indentStr);
|
2016-11-28 10:42:58 +00:00
|
|
|
},
|
2016-12-14 16:39:17 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
PRESERVE_COMMENTS: false,
|
2016-12-14 16:39:17 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* XML Minify operation.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runXmlMinify: function(input, args) {
|
2017-04-13 18:08:50 +01:00
|
|
|
const preserveComments = args[0];
|
2017-03-23 17:52:20 +00:00
|
|
|
return vkbeautify.xmlmin(input, preserveComments);
|
2016-11-28 10:42:58 +00:00
|
|
|
},
|
2016-12-14 16:39:17 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* JSON Minify operation.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runJsonMinify: function(input, args) {
|
2016-12-03 00:51:17 +00:00
|
|
|
if (!input) return "";
|
2017-03-23 17:52:20 +00:00
|
|
|
return vkbeautify.jsonmin(input);
|
2016-11-28 10:42:58 +00:00
|
|
|
},
|
2016-12-14 16:39:17 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* CSS Minify operation.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runCssMinify: function(input, args) {
|
2017-04-13 18:08:50 +01:00
|
|
|
const preserveComments = args[0];
|
2017-03-23 17:52:20 +00:00
|
|
|
return vkbeautify.cssmin(input, preserveComments);
|
2016-11-28 10:42:58 +00:00
|
|
|
},
|
2016-12-14 16:39:17 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* SQL Minify operation.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runSqlMinify: function(input, args) {
|
2017-03-23 17:52:20 +00:00
|
|
|
return vkbeautify.sqlmin(input);
|
2016-11-28 10:42:58 +00:00
|
|
|
},
|
2016-12-14 16:39:17 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* Generic Code Beautify operation.
|
|
|
|
*
|
|
|
|
* Yeeeaaah...
|
|
|
|
*
|
|
|
|
* I'm not proud of this code, but seriously, try writing a generic lexer and parser that
|
|
|
|
* correctly generates an AST for multiple different languages. I have tried, and I can tell
|
|
|
|
* you it's pretty much impossible.
|
2016-12-14 16:39:17 +00:00
|
|
|
*
|
2016-11-28 10:42:58 +00:00
|
|
|
* This basically works. That'll have to be good enough. It's not meant to produce working code,
|
|
|
|
* just slightly more readable code.
|
2016-12-14 16:39:17 +00:00
|
|
|
*
|
2016-11-28 10:42:58 +00:00
|
|
|
* Things that don't work:
|
|
|
|
* - For loop formatting
|
|
|
|
* - Do-While loop formatting
|
|
|
|
* - Switch/Case indentation
|
|
|
|
* - Bit shift operators
|
|
|
|
*
|
|
|
|
* @author n1474335 [n1474335@gmail.com]
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runGenericBeautify: function(input, args) {
|
2017-04-13 18:08:50 +01:00
|
|
|
let code = input,
|
2016-11-28 10:42:58 +00:00
|
|
|
t = 0,
|
2017-01-31 18:24:56 +00:00
|
|
|
preservedTokens = [],
|
2016-11-28 10:42:58 +00:00
|
|
|
m;
|
2016-12-14 16:39:17 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
// Remove strings
|
2017-04-13 18:08:50 +01:00
|
|
|
const sstrings = /'([^'\\]|\\.)*'/g;
|
2016-12-14 16:39:17 +00:00
|
|
|
while ((m = sstrings.exec(code))) {
|
2017-01-31 18:24:56 +00:00
|
|
|
code = preserveToken(code, m, t++);
|
2016-11-28 10:42:58 +00:00
|
|
|
sstrings.lastIndex = m.index;
|
|
|
|
}
|
2016-12-14 16:39:17 +00:00
|
|
|
|
2017-04-13 18:08:50 +01:00
|
|
|
const dstrings = /"([^"\\]|\\.)*"/g;
|
2016-12-14 16:39:17 +00:00
|
|
|
while ((m = dstrings.exec(code))) {
|
2017-01-31 18:24:56 +00:00
|
|
|
code = preserveToken(code, m, t++);
|
2016-11-28 10:42:58 +00:00
|
|
|
dstrings.lastIndex = m.index;
|
|
|
|
}
|
2016-12-14 16:39:17 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
// Remove comments
|
2017-04-13 18:08:50 +01:00
|
|
|
const scomments = /\/\/[^\n\r]*/g;
|
2016-12-14 16:39:17 +00:00
|
|
|
while ((m = scomments.exec(code))) {
|
2017-01-31 18:24:56 +00:00
|
|
|
code = preserveToken(code, m, t++);
|
2016-11-28 10:42:58 +00:00
|
|
|
scomments.lastIndex = m.index;
|
|
|
|
}
|
2016-12-14 16:39:17 +00:00
|
|
|
|
2017-04-13 18:08:50 +01:00
|
|
|
const mcomments = /\/\*[\s\S]*?\*\//gm;
|
2016-12-14 16:39:17 +00:00
|
|
|
while ((m = mcomments.exec(code))) {
|
2017-01-31 18:24:56 +00:00
|
|
|
code = preserveToken(code, m, t++);
|
2016-11-28 10:42:58 +00:00
|
|
|
mcomments.lastIndex = m.index;
|
|
|
|
}
|
2016-12-14 16:39:17 +00:00
|
|
|
|
2017-04-13 18:08:50 +01:00
|
|
|
const hcomments = /(^|\n)#[^\n\r#]+/g;
|
2016-12-14 16:39:17 +00:00
|
|
|
while ((m = hcomments.exec(code))) {
|
2017-01-31 18:24:56 +00:00
|
|
|
code = preserveToken(code, m, t++);
|
2016-11-28 10:42:58 +00:00
|
|
|
hcomments.lastIndex = m.index;
|
|
|
|
}
|
2016-12-14 16:39:17 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
// Remove regexes
|
2017-04-13 18:08:50 +01:00
|
|
|
const regexes = /\/.*?[^\\]\/[gim]{0,3}/gi;
|
2016-12-14 16:39:17 +00:00
|
|
|
while ((m = regexes.exec(code))) {
|
2017-01-31 18:24:56 +00:00
|
|
|
code = preserveToken(code, m, t++);
|
2016-11-28 10:42:58 +00:00
|
|
|
regexes.lastIndex = m.index;
|
|
|
|
}
|
2016-12-14 16:39:17 +00:00
|
|
|
|
2017-04-07 13:59:00 +01:00
|
|
|
code = code
|
2017-07-24 13:49:16 +00:00
|
|
|
// Create newlines after ;
|
|
|
|
.replace(/;/g, ";\n")
|
|
|
|
// Create newlines after { and around }
|
|
|
|
.replace(/{/g, "{\n")
|
|
|
|
.replace(/}/g, "\n}\n")
|
|
|
|
// Remove carriage returns
|
|
|
|
.replace(/\r/g, "")
|
|
|
|
// Remove all indentation
|
|
|
|
.replace(/^\s+/g, "")
|
|
|
|
.replace(/\n\s+/g, "\n")
|
|
|
|
// Remove trailing spaces
|
|
|
|
.replace(/\s*$/g, "")
|
|
|
|
.replace(/\n{/g, "{");
|
2016-12-14 16:39:17 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
// Indent
|
2017-04-13 18:08:50 +01:00
|
|
|
let i = 0,
|
2017-04-13 18:31:26 +01:00
|
|
|
level = 0,
|
|
|
|
indent;
|
2016-11-28 10:42:58 +00:00
|
|
|
while (i < code.length) {
|
2017-02-09 15:09:33 +00:00
|
|
|
switch (code[i]) {
|
2016-11-28 10:42:58 +00:00
|
|
|
case "{":
|
|
|
|
level++;
|
|
|
|
break;
|
|
|
|
case "\n":
|
|
|
|
if (i+1 >= code.length) break;
|
2016-12-14 16:39:17 +00:00
|
|
|
|
|
|
|
if (code[i+1] === "}") level--;
|
2017-04-13 18:31:26 +01:00
|
|
|
indent = (level >= 0) ? Array(level*4+1).join(" ") : "";
|
2016-12-14 16:39:17 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
code = code.substring(0, i+1) + indent + code.substring(i+1);
|
|
|
|
if (level > 0) i += level*4;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2017-04-07 13:59:00 +01:00
|
|
|
code = code
|
2017-07-24 13:49:16 +00:00
|
|
|
// Add strategic spaces
|
|
|
|
.replace(/\s*([!<>=+-/*]?)=\s*/g, " $1= ")
|
|
|
|
.replace(/\s*<([=]?)\s*/g, " <$1 ")
|
|
|
|
.replace(/\s*>([=]?)\s*/g, " >$1 ")
|
|
|
|
.replace(/([^+])\+([^+=])/g, "$1 + $2")
|
|
|
|
.replace(/([^-])-([^-=])/g, "$1 - $2")
|
|
|
|
.replace(/([^*])\*([^*=])/g, "$1 * $2")
|
|
|
|
.replace(/([^/])\/([^/=])/g, "$1 / $2")
|
|
|
|
.replace(/\s*,\s*/g, ", ")
|
|
|
|
.replace(/\s*{/g, " {")
|
|
|
|
.replace(/}\n/g, "}\n\n")
|
|
|
|
// Hacky horribleness
|
|
|
|
.replace(/(if|for|while|with|elif|elseif)\s*\(([^\n]*)\)\s*\n([^{])/gim, "$1 ($2)\n $3")
|
|
|
|
.replace(/(if|for|while|with|elif|elseif)\s*\(([^\n]*)\)([^{])/gim, "$1 ($2) $3")
|
|
|
|
.replace(/else\s*\n([^{])/gim, "else\n $1")
|
|
|
|
.replace(/else\s+([^{])/gim, "else $1")
|
|
|
|
// Remove strategic spaces
|
|
|
|
.replace(/\s+;/g, ";")
|
|
|
|
.replace(/\{\s+\}/g, "{}")
|
|
|
|
.replace(/\[\s+\]/g, "[]")
|
|
|
|
.replace(/}\s*(else|catch|except|finally|elif|elseif|else if)/gi, "} $1");
|
2016-12-14 16:39:17 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
// Replace preserved tokens
|
2017-04-13 18:08:50 +01:00
|
|
|
const ptokens = /###preservedToken(\d+)###/g;
|
2016-12-14 16:39:17 +00:00
|
|
|
while ((m = ptokens.exec(code))) {
|
2017-04-13 18:08:50 +01:00
|
|
|
const ti = parseInt(m[1], 10);
|
2017-01-31 18:24:56 +00:00
|
|
|
code = code.substring(0, m.index) + preservedTokens[ti] + code.substring(m.index + m[0].length);
|
2016-11-28 10:42:58 +00:00
|
|
|
ptokens.lastIndex = m.index;
|
|
|
|
}
|
|
|
|
|
|
|
|
return code;
|
2016-12-14 16:39:17 +00:00
|
|
|
|
2017-02-07 20:31:15 +00:00
|
|
|
/**
|
|
|
|
* Replaces a matched token with a placeholder value.
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
function preserveToken(str, match, t) {
|
|
|
|
preservedTokens[t] = match[0];
|
2016-11-28 10:42:58 +00:00
|
|
|
return str.substring(0, match.index) +
|
2017-01-31 18:24:56 +00:00
|
|
|
"###preservedToken" + t + "###" +
|
2016-11-28 10:42:58 +00:00
|
|
|
str.substring(match.index + match[0].length);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-12-20 18:49:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
XPATH_INITIAL: "",
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
XPATH_DELIMITER: "\\n",
|
|
|
|
|
|
|
|
/**
|
|
|
|
* XPath expression operation.
|
|
|
|
*
|
|
|
|
* @author Mikescher (https://github.com/Mikescher | https://mikescher.com)
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-07-24 14:55:48 +00:00
|
|
|
runXpath: function(input, args) {
|
2017-04-13 18:08:50 +01:00
|
|
|
let query = args[0],
|
2016-12-20 18:49:25 +00:00
|
|
|
delimiter = args[1];
|
|
|
|
|
2017-04-13 18:08:50 +01:00
|
|
|
let doc;
|
2016-12-20 18:49:25 +00:00
|
|
|
try {
|
2017-03-21 22:41:44 +00:00
|
|
|
doc = new dom().parseFromString(input);
|
2016-12-20 18:49:25 +00:00
|
|
|
} catch (err) {
|
|
|
|
return "Invalid input XML.";
|
|
|
|
}
|
|
|
|
|
2017-04-13 18:08:50 +01:00
|
|
|
let nodes;
|
2016-12-20 18:49:25 +00:00
|
|
|
try {
|
2017-03-21 22:41:44 +00:00
|
|
|
nodes = xpath.select(query, doc);
|
2016-12-20 18:49:25 +00:00
|
|
|
} catch (err) {
|
|
|
|
return "Invalid XPath. Details:\n" + err.message;
|
|
|
|
}
|
|
|
|
|
2017-04-13 18:08:50 +01:00
|
|
|
const nodeToString = function(node) {
|
2017-03-21 22:41:44 +00:00
|
|
|
return node.toString();
|
2016-12-20 18:49:25 +00:00
|
|
|
};
|
|
|
|
|
2017-03-21 22:41:44 +00:00
|
|
|
return nodes.map(nodeToString).join(delimiter);
|
2016-12-20 18:49:25 +00:00
|
|
|
},
|
|
|
|
|
2017-08-04 13:59:32 +00:00
|
|
|
|
2017-07-25 16:27:59 +01:00
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
JPATH_INITIAL: "",
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
JPATH_DELIMITER: "\\n",
|
|
|
|
|
|
|
|
/**
|
2017-08-04 13:59:32 +00:00
|
|
|
* JPath expression operation.
|
2017-07-25 16:27:59 +01:00
|
|
|
*
|
|
|
|
* @author Matt C (matt@artemisbot.uk)
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
runJpath: function(input, args) {
|
|
|
|
let query = args[0],
|
2017-08-03 14:50:16 +01:00
|
|
|
delimiter = args[1],
|
|
|
|
results,
|
|
|
|
obj;
|
2017-08-04 13:59:32 +00:00
|
|
|
|
2017-07-25 16:27:59 +01:00
|
|
|
try {
|
|
|
|
obj = JSON.parse(input);
|
|
|
|
} catch (err) {
|
2017-08-04 13:59:32 +00:00
|
|
|
return "Invalid input JSON: " + err.message;
|
2017-07-25 16:27:59 +01:00
|
|
|
}
|
2017-08-04 13:59:32 +00:00
|
|
|
|
2017-08-03 14:50:16 +01:00
|
|
|
try {
|
|
|
|
results = jpath.query(obj, query);
|
2017-08-04 13:59:32 +00:00
|
|
|
} catch (err) {
|
|
|
|
return "Invalid JPath expression: " + err.message;
|
2017-08-03 14:50:16 +01:00
|
|
|
}
|
2017-08-04 13:59:32 +00:00
|
|
|
|
2017-07-25 16:27:59 +01:00
|
|
|
return results.map(result => JSON.stringify(result)).join(delimiter);
|
|
|
|
},
|
|
|
|
|
2016-12-20 18:49:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
CSS_SELECTOR_INITIAL: "",
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
CSS_QUERY_DELIMITER: "\\n",
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CSS selector operation.
|
|
|
|
*
|
|
|
|
* @author Mikescher (https://github.com/Mikescher | https://mikescher.com)
|
2017-02-07 15:04:10 +00:00
|
|
|
* @author n1474335 [n1474335@gmail.com]
|
2016-12-20 18:49:25 +00:00
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-02-07 15:04:10 +00:00
|
|
|
runCSSQuery: function(input, args) {
|
2017-04-13 18:08:50 +01:00
|
|
|
let query = args[0],
|
2017-02-07 15:04:10 +00:00
|
|
|
delimiter = args[1],
|
|
|
|
parser = new DOMParser(),
|
|
|
|
html,
|
|
|
|
result;
|
|
|
|
|
|
|
|
if (!query.length || !input.length) {
|
|
|
|
return "";
|
|
|
|
}
|
2016-12-20 18:49:25 +00:00
|
|
|
|
|
|
|
try {
|
2017-02-07 15:04:10 +00:00
|
|
|
html = parser.parseFromString(input, "text/html");
|
2016-12-20 18:49:25 +00:00
|
|
|
} catch (err) {
|
|
|
|
return "Invalid input HTML.";
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2017-02-07 15:04:10 +00:00
|
|
|
result = html.querySelectorAll(query);
|
2016-12-20 18:49:25 +00:00
|
|
|
} catch (err) {
|
|
|
|
return "Invalid CSS Selector. Details:\n" + err.message;
|
|
|
|
}
|
|
|
|
|
2017-04-13 18:08:50 +01:00
|
|
|
const nodeToString = function(node) {
|
2016-12-20 18:49:25 +00:00
|
|
|
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;
|
|
|
|
default: throw new Error("Unknown Node Type: " + node.nodeType);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return Array.apply(null, Array(result.length))
|
|
|
|
.map(function(_, i) {
|
|
|
|
return result[i];
|
|
|
|
})
|
2017-01-31 18:24:56 +00:00
|
|
|
.map(nodeToString)
|
2016-12-20 18:49:25 +00:00
|
|
|
.join(delimiter);
|
|
|
|
},
|
|
|
|
|
2017-05-02 11:21:04 -04:00
|
|
|
/**
|
|
|
|
* This tries to rename variable names in a code snippet according to a function.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {function} replacer - this function will be fed the token which should be renamed.
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
_replaceVariableNames(input, replacer) {
|
2017-05-02 23:06:28 +01:00
|
|
|
const tokenRegex = /\\"|"(?:\\"|[^"])*"|(\b[a-z0-9\-_]+\b)/ig;
|
2017-05-02 11:21:04 -04:00
|
|
|
|
|
|
|
return input.replace(tokenRegex, (...args) => {
|
|
|
|
let match = args[0],
|
|
|
|
quotes = args[1];
|
|
|
|
|
|
|
|
if (!quotes) {
|
|
|
|
return match;
|
|
|
|
} else {
|
|
|
|
return replacer(match);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts to snake_case.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
runToSnakeCase(input, args) {
|
2017-05-02 23:06:28 +01:00
|
|
|
const smart = args[0];
|
2017-05-02 11:21:04 -04:00
|
|
|
|
|
|
|
if (smart) {
|
|
|
|
return Code._replaceVariableNames(input, snakeCase);
|
|
|
|
} else {
|
|
|
|
return snakeCase(input);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts to camelCase.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
runToCamelCase(input, args) {
|
2017-05-02 23:06:28 +01:00
|
|
|
const smart = args[0];
|
2017-05-02 11:21:04 -04:00
|
|
|
|
|
|
|
if (smart) {
|
|
|
|
return Code._replaceVariableNames(input, camelCase);
|
|
|
|
} else {
|
|
|
|
return camelCase(input);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts to kebab-case.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
runToKebabCase(input, args) {
|
2017-05-02 23:06:28 +01:00
|
|
|
const smart = args[0];
|
2017-05-02 11:21:04 -04:00
|
|
|
|
|
|
|
if (smart) {
|
|
|
|
return Code._replaceVariableNames(input, kebabCase);
|
|
|
|
} else {
|
|
|
|
return kebabCase(input);
|
|
|
|
}
|
|
|
|
},
|
2016-11-28 10:42:58 +00:00
|
|
|
};
|
2017-03-23 17:52:20 +00:00
|
|
|
|
|
|
|
export default Code;
|