Publish Vulpes Preview 15 sources for desktop, Android and Tundra
Share Gaia assets and adapters, keep private profiles and development documents local, and update both public READMEs.
This commit is contained in:
parent
93e3a48004
commit
b38dea0597
25901 changed files with 9982925 additions and 369 deletions
|
|
@ -0,0 +1,4 @@
|
|||
#!/usr/bin/env node
|
||||
var cli = require('../lib/cli'); cli.interpret();
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
#!/usr/bin/env node
|
||||
var cli = require('../lib/cli'); cli.interpret();
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
var cli = require('../lib/cli');
|
||||
cli.interpret();
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
{
|
||||
"indent_size": 4,
|
||||
"indent_char": " ",
|
||||
"indent_level": 0,
|
||||
"indent_with_tabs": false,
|
||||
"preserve_newlines": true,
|
||||
"max_preserve_newlines": 10,
|
||||
"jslint_happy": false,
|
||||
"space_after_anon_function": false,
|
||||
"brace_style": "collapse",
|
||||
"keep_array_indentation": false,
|
||||
"keep_function_indentation": false,
|
||||
"space_before_conditional": true,
|
||||
"break_chained_methods": false,
|
||||
"eval_code": false,
|
||||
"unescape_strings": false,
|
||||
"wrap_line_length": 0
|
||||
}
|
||||
55
gaia/distros/spark/apps/customizer/components/js-beautify/js/index.js
Executable file
55
gaia/distros/spark/apps/customizer/components/js-beautify/js/index.js
Executable file
|
|
@ -0,0 +1,55 @@
|
|||
/**
|
||||
The following batches are equivalent:
|
||||
|
||||
var beautify_js = require('js-beautify');
|
||||
var beautify_js = require('js-beautify').js;
|
||||
var beautify_js = require('js-beautify').js_beautify;
|
||||
|
||||
var beautify_css = require('js-beautify').css;
|
||||
var beautify_css = require('js-beautify').css_beautify;
|
||||
|
||||
var beautify_html = require('js-beautify').html;
|
||||
var beautify_html = require('js-beautify').html_beautify;
|
||||
|
||||
All methods returned accept two arguments, the source string and an options object.
|
||||
**/
|
||||
|
||||
function get_beautify(js_beautify, css_beautify, html_beautify) {
|
||||
// the default is js
|
||||
var beautify = function (src, config) {
|
||||
return js_beautify.js_beautify(src, config);
|
||||
};
|
||||
|
||||
// short aliases
|
||||
beautify.js = js_beautify.js_beautify;
|
||||
beautify.css = css_beautify.css_beautify;
|
||||
beautify.html = html_beautify.html_beautify;
|
||||
|
||||
// legacy aliases
|
||||
beautify.js_beautify = js_beautify.js_beautify;
|
||||
beautify.css_beautify = css_beautify.css_beautify;
|
||||
beautify.html_beautify = html_beautify.html_beautify;
|
||||
|
||||
return beautify;
|
||||
}
|
||||
|
||||
if (typeof define === "function" && define.amd) {
|
||||
// Add support for AMD ( https://github.com/amdjs/amdjs-api/wiki/AMD#defineamd-property- )
|
||||
define([
|
||||
"./lib/beautify",
|
||||
"./lib/beautify-css",
|
||||
"./lib/beautify-html"
|
||||
], function(js_beautify, css_beautify, html_beautify) {
|
||||
return get_beautify(js_beautify, css_beautify, html_beautify);
|
||||
});
|
||||
} else {
|
||||
(function(mod) {
|
||||
var js_beautify = require('./lib/beautify');
|
||||
var css_beautify = require('./lib/beautify-css');
|
||||
var html_beautify = require('./lib/beautify-html');
|
||||
|
||||
mod.exports = get_beautify(js_beautify, css_beautify, html_beautify);
|
||||
|
||||
})(module);
|
||||
}
|
||||
|
||||
491
gaia/distros/spark/apps/customizer/components/js-beautify/js/lib/beautify-css.js
Executable file
491
gaia/distros/spark/apps/customizer/components/js-beautify/js/lib/beautify-css.js
Executable file
|
|
@ -0,0 +1,491 @@
|
|||
/*jshint curly:true, eqeqeq:true, laxbreak:true, noempty:false */
|
||||
/*
|
||||
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2007-2013 Einar Lielmanis and contributors.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person
|
||||
obtaining a copy of this software and associated documentation files
|
||||
(the "Software"), to deal in the Software without restriction,
|
||||
including without limitation the rights to use, copy, modify, merge,
|
||||
publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
|
||||
CSS Beautifier
|
||||
---------------
|
||||
|
||||
Written by Harutyun Amirjanyan, (amirjanyan@gmail.com)
|
||||
|
||||
Based on code initially developed by: Einar Lielmanis, <einar@jsbeautifier.org>
|
||||
http://jsbeautifier.org/
|
||||
|
||||
Usage:
|
||||
css_beautify(source_text);
|
||||
css_beautify(source_text, options);
|
||||
|
||||
The options are (default in brackets):
|
||||
indent_size (4) — indentation size,
|
||||
indent_char (space) — character to indent with,
|
||||
selector_separator_newline (true) - separate selectors with newline or
|
||||
not (e.g. "a,\nbr" or "a, br")
|
||||
end_with_newline (false) - end with a newline
|
||||
newline_between_rules (true) - add a new line after every css rule
|
||||
|
||||
e.g
|
||||
|
||||
css_beautify(css_source_text, {
|
||||
'indent_size': 1,
|
||||
'indent_char': '\t',
|
||||
'selector_separator': ' ',
|
||||
'end_with_newline': false,
|
||||
'newline_between_rules': true
|
||||
});
|
||||
*/
|
||||
|
||||
// http://www.w3.org/TR/CSS21/syndata.html#tokenization
|
||||
// http://www.w3.org/TR/css3-syntax/
|
||||
|
||||
(function() {
|
||||
function css_beautify(source_text, options) {
|
||||
options = options || {};
|
||||
source_text = source_text || '';
|
||||
// HACK: newline parsing inconsistent. This brute force normalizes the input.
|
||||
source_text = source_text.replace(/\r\n|[\r\u2028\u2029]/g, '\n')
|
||||
|
||||
var indentSize = options.indent_size || 4;
|
||||
var indentCharacter = options.indent_char || ' ';
|
||||
var selectorSeparatorNewline = (options.selector_separator_newline === undefined) ? true : options.selector_separator_newline;
|
||||
var end_with_newline = (options.end_with_newline === undefined) ? false : options.end_with_newline;
|
||||
var newline_between_rules = (options.newline_between_rules === undefined) ? true : options.newline_between_rules;
|
||||
var eol = options.eol ? options.eol : '\n';
|
||||
|
||||
// compatibility
|
||||
if (typeof indentSize === "string") {
|
||||
indentSize = parseInt(indentSize, 10);
|
||||
}
|
||||
|
||||
if(options.indent_with_tabs){
|
||||
indentCharacter = '\t';
|
||||
indentSize = 1;
|
||||
}
|
||||
|
||||
eol = eol.replace(/\\r/, '\r').replace(/\\n/, '\n')
|
||||
|
||||
|
||||
// tokenizer
|
||||
var whiteRe = /^\s+$/;
|
||||
var wordRe = /[\w$\-_]/;
|
||||
|
||||
var pos = -1,
|
||||
ch;
|
||||
var parenLevel = 0;
|
||||
|
||||
function next() {
|
||||
ch = source_text.charAt(++pos);
|
||||
return ch || '';
|
||||
}
|
||||
|
||||
function peek(skipWhitespace) {
|
||||
var result = '';
|
||||
var prev_pos = pos;
|
||||
if (skipWhitespace) {
|
||||
eatWhitespace();
|
||||
}
|
||||
result = source_text.charAt(pos + 1) || '';
|
||||
pos = prev_pos - 1;
|
||||
next();
|
||||
return result;
|
||||
}
|
||||
|
||||
function eatString(endChars) {
|
||||
var start = pos;
|
||||
while (next()) {
|
||||
if (ch === "\\") {
|
||||
next();
|
||||
} else if (endChars.indexOf(ch) !== -1) {
|
||||
break;
|
||||
} else if (ch === "\n") {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return source_text.substring(start, pos + 1);
|
||||
}
|
||||
|
||||
function peekString(endChar) {
|
||||
var prev_pos = pos;
|
||||
var str = eatString(endChar);
|
||||
pos = prev_pos - 1;
|
||||
next();
|
||||
return str;
|
||||
}
|
||||
|
||||
function eatWhitespace() {
|
||||
var result = '';
|
||||
while (whiteRe.test(peek())) {
|
||||
next();
|
||||
result += ch;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function skipWhitespace() {
|
||||
var result = '';
|
||||
if (ch && whiteRe.test(ch)) {
|
||||
result = ch;
|
||||
}
|
||||
while (whiteRe.test(next())) {
|
||||
result += ch;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function eatComment(singleLine) {
|
||||
var start = pos;
|
||||
singleLine = peek() === "/";
|
||||
next();
|
||||
while (next()) {
|
||||
if (!singleLine && ch === "*" && peek() === "/") {
|
||||
next();
|
||||
break;
|
||||
} else if (singleLine && ch === "\n") {
|
||||
return source_text.substring(start, pos);
|
||||
}
|
||||
}
|
||||
|
||||
return source_text.substring(start, pos) + ch;
|
||||
}
|
||||
|
||||
|
||||
function lookBack(str) {
|
||||
return source_text.substring(pos - str.length, pos).toLowerCase() ===
|
||||
str;
|
||||
}
|
||||
|
||||
// Nested pseudo-class if we are insideRule
|
||||
// and the next special character found opens
|
||||
// a new block
|
||||
function foundNestedPseudoClass() {
|
||||
var openParen = 0;
|
||||
for (var i = pos + 1; i < source_text.length; i++) {
|
||||
var ch = source_text.charAt(i);
|
||||
if (ch === "{") {
|
||||
return true;
|
||||
} else if (ch === '(') {
|
||||
// pseudoclasses can contain ()
|
||||
openParen += 1;
|
||||
} else if (ch === ')') {
|
||||
if (openParen == 0) {
|
||||
return false;
|
||||
}
|
||||
openParen -= 1;
|
||||
} else if (ch === ";" || ch === "}") {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// printer
|
||||
var basebaseIndentString = source_text.match(/^[\t ]*/)[0];
|
||||
var singleIndent = new Array(indentSize + 1).join(indentCharacter);
|
||||
var indentLevel = 0;
|
||||
var nestedLevel = 0;
|
||||
|
||||
function indent() {
|
||||
indentLevel++;
|
||||
basebaseIndentString += singleIndent;
|
||||
}
|
||||
|
||||
function outdent() {
|
||||
indentLevel--;
|
||||
basebaseIndentString = basebaseIndentString.slice(0, -indentSize);
|
||||
}
|
||||
|
||||
var print = {};
|
||||
print["{"] = function(ch) {
|
||||
print.singleSpace();
|
||||
output.push(ch);
|
||||
print.newLine();
|
||||
};
|
||||
print["}"] = function(ch) {
|
||||
print.newLine();
|
||||
output.push(ch);
|
||||
print.newLine();
|
||||
};
|
||||
|
||||
print._lastCharWhitespace = function() {
|
||||
return whiteRe.test(output[output.length - 1]);
|
||||
};
|
||||
|
||||
print.newLine = function(keepWhitespace) {
|
||||
if (output.length) {
|
||||
if (!keepWhitespace && output[output.length - 1] !== '\n') {
|
||||
print.trim();
|
||||
}
|
||||
|
||||
output.push('\n');
|
||||
|
||||
if (basebaseIndentString) {
|
||||
output.push(basebaseIndentString);
|
||||
}
|
||||
}
|
||||
};
|
||||
print.singleSpace = function() {
|
||||
if (output.length && !print._lastCharWhitespace()) {
|
||||
output.push(' ');
|
||||
}
|
||||
};
|
||||
|
||||
print.preserveSingleSpace = function() {
|
||||
if (isAfterSpace) {
|
||||
print.singleSpace();
|
||||
}
|
||||
};
|
||||
|
||||
print.trim = function() {
|
||||
while (print._lastCharWhitespace()) {
|
||||
output.pop();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
var output = [];
|
||||
/*_____________________--------------------_____________________*/
|
||||
|
||||
var insideRule = false;
|
||||
var insidePropertyValue = false;
|
||||
var enteringConditionalGroup = false;
|
||||
var top_ch = '';
|
||||
var last_top_ch = '';
|
||||
|
||||
while (true) {
|
||||
var whitespace = skipWhitespace();
|
||||
var isAfterSpace = whitespace !== '';
|
||||
var isAfterNewline = whitespace.indexOf('\n') !== -1;
|
||||
last_top_ch = top_ch;
|
||||
top_ch = ch;
|
||||
|
||||
if (!ch) {
|
||||
break;
|
||||
} else if (ch === '/' && peek() === '*') { /* css comment */
|
||||
var header = indentLevel === 0;
|
||||
|
||||
if (isAfterNewline || header) {
|
||||
print.newLine();
|
||||
}
|
||||
|
||||
output.push(eatComment());
|
||||
print.newLine();
|
||||
if (header) {
|
||||
print.newLine(true);
|
||||
}
|
||||
} else if (ch === '/' && peek() === '/') { // single line comment
|
||||
if (!isAfterNewline && last_top_ch !== '{' ) {
|
||||
print.trim();
|
||||
}
|
||||
print.singleSpace();
|
||||
output.push(eatComment());
|
||||
print.newLine();
|
||||
} else if (ch === '@') {
|
||||
print.preserveSingleSpace();
|
||||
output.push(ch);
|
||||
|
||||
// strip trailing space, if present, for hash property checks
|
||||
var variableOrRule = peekString(": ,;{}()[]/='\"");
|
||||
|
||||
if (variableOrRule.match(/[ :]$/)) {
|
||||
// we have a variable or pseudo-class, add it and insert one space before continuing
|
||||
next();
|
||||
variableOrRule = eatString(": ").replace(/\s$/, '');
|
||||
output.push(variableOrRule);
|
||||
print.singleSpace();
|
||||
}
|
||||
|
||||
variableOrRule = variableOrRule.replace(/\s$/, '')
|
||||
|
||||
// might be a nesting at-rule
|
||||
if (variableOrRule in css_beautify.NESTED_AT_RULE) {
|
||||
nestedLevel += 1;
|
||||
if (variableOrRule in css_beautify.CONDITIONAL_GROUP_RULE) {
|
||||
enteringConditionalGroup = true;
|
||||
}
|
||||
}
|
||||
} else if (ch === '#' && peek() === '{') {
|
||||
print.preserveSingleSpace();
|
||||
output.push(eatString('}'));
|
||||
} else if (ch === '{') {
|
||||
if (peek(true) === '}') {
|
||||
eatWhitespace();
|
||||
next();
|
||||
print.singleSpace();
|
||||
output.push("{}");
|
||||
print.newLine();
|
||||
if (newline_between_rules && indentLevel === 0) {
|
||||
print.newLine(true);
|
||||
}
|
||||
} else {
|
||||
indent();
|
||||
print["{"](ch);
|
||||
// when entering conditional groups, only rulesets are allowed
|
||||
if (enteringConditionalGroup) {
|
||||
enteringConditionalGroup = false;
|
||||
insideRule = (indentLevel > nestedLevel);
|
||||
} else {
|
||||
// otherwise, declarations are also allowed
|
||||
insideRule = (indentLevel >= nestedLevel);
|
||||
}
|
||||
}
|
||||
} else if (ch === '}') {
|
||||
outdent();
|
||||
print["}"](ch);
|
||||
insideRule = false;
|
||||
insidePropertyValue = false;
|
||||
if (nestedLevel) {
|
||||
nestedLevel--;
|
||||
}
|
||||
if (newline_between_rules && indentLevel === 0) {
|
||||
print.newLine(true);
|
||||
}
|
||||
} else if (ch === ":") {
|
||||
eatWhitespace();
|
||||
if ((insideRule || enteringConditionalGroup) &&
|
||||
!(lookBack("&") || foundNestedPseudoClass())) {
|
||||
// 'property: value' delimiter
|
||||
// which could be in a conditional group query
|
||||
insidePropertyValue = true;
|
||||
output.push(':');
|
||||
print.singleSpace();
|
||||
} else {
|
||||
// sass/less parent reference don't use a space
|
||||
// sass nested pseudo-class don't use a space
|
||||
if (peek() === ":") {
|
||||
// pseudo-element
|
||||
next();
|
||||
output.push("::");
|
||||
} else {
|
||||
// pseudo-class
|
||||
output.push(':');
|
||||
}
|
||||
}
|
||||
} else if (ch === '"' || ch === '\'') {
|
||||
print.preserveSingleSpace();
|
||||
output.push(eatString(ch));
|
||||
} else if (ch === ';') {
|
||||
insidePropertyValue = false;
|
||||
output.push(ch);
|
||||
print.newLine();
|
||||
} else if (ch === '(') { // may be a url
|
||||
if (lookBack("url")) {
|
||||
output.push(ch);
|
||||
eatWhitespace();
|
||||
if (next()) {
|
||||
if (ch !== ')' && ch !== '"' && ch !== '\'') {
|
||||
output.push(eatString(')'));
|
||||
} else {
|
||||
pos--;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
parenLevel++;
|
||||
print.preserveSingleSpace();
|
||||
output.push(ch);
|
||||
eatWhitespace();
|
||||
}
|
||||
} else if (ch === ')') {
|
||||
output.push(ch);
|
||||
parenLevel--;
|
||||
} else if (ch === ',') {
|
||||
output.push(ch);
|
||||
eatWhitespace();
|
||||
if (selectorSeparatorNewline && !insidePropertyValue && parenLevel < 1) {
|
||||
print.newLine();
|
||||
} else {
|
||||
print.singleSpace();
|
||||
}
|
||||
} else if (ch === ']') {
|
||||
output.push(ch);
|
||||
} else if (ch === '[') {
|
||||
print.preserveSingleSpace();
|
||||
output.push(ch);
|
||||
} else if (ch === '=') { // no whitespace before or after
|
||||
eatWhitespace()
|
||||
ch = '=';
|
||||
output.push(ch);
|
||||
} else {
|
||||
print.preserveSingleSpace();
|
||||
output.push(ch);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
var sweetCode = '';
|
||||
if (basebaseIndentString) {
|
||||
sweetCode += basebaseIndentString;
|
||||
}
|
||||
|
||||
sweetCode += output.join('').replace(/[\r\n\t ]+$/, '');
|
||||
|
||||
// establish end_with_newline
|
||||
if (end_with_newline) {
|
||||
sweetCode += '\n';
|
||||
}
|
||||
|
||||
if (eol != '\n') {
|
||||
sweetCode = sweetCode.replace(/[\n]/g, eol);
|
||||
}
|
||||
|
||||
return sweetCode;
|
||||
}
|
||||
|
||||
// https://developer.mozilla.org/en-US/docs/Web/CSS/At-rule
|
||||
css_beautify.NESTED_AT_RULE = {
|
||||
"@page": true,
|
||||
"@font-face": true,
|
||||
"@keyframes": true,
|
||||
// also in CONDITIONAL_GROUP_RULE below
|
||||
"@media": true,
|
||||
"@supports": true,
|
||||
"@document": true
|
||||
};
|
||||
css_beautify.CONDITIONAL_GROUP_RULE = {
|
||||
"@media": true,
|
||||
"@supports": true,
|
||||
"@document": true
|
||||
};
|
||||
|
||||
/*global define */
|
||||
if (typeof define === "function" && define.amd) {
|
||||
// Add support for AMD ( https://github.com/amdjs/amdjs-api/wiki/AMD#defineamd-property- )
|
||||
define([], function() {
|
||||
return {
|
||||
css_beautify: css_beautify
|
||||
};
|
||||
});
|
||||
} else if (typeof exports !== "undefined") {
|
||||
// Add support for CommonJS. Just put this file somewhere on your require.paths
|
||||
// and you will be able to `var html_beautify = require("beautify").html_beautify`.
|
||||
exports.css_beautify = css_beautify;
|
||||
} else if (typeof window !== "undefined") {
|
||||
// If we're running a web page and don't have either of the above, add our one global
|
||||
window.css_beautify = css_beautify;
|
||||
} else if (typeof global !== "undefined") {
|
||||
// If we don't even have window, try global.
|
||||
global.css_beautify = css_beautify;
|
||||
}
|
||||
|
||||
}());
|
||||
|
|
@ -0,0 +1,952 @@
|
|||
/*jshint curly:true, eqeqeq:true, laxbreak:true, noempty:false */
|
||||
/*
|
||||
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2007-2013 Einar Lielmanis and contributors.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person
|
||||
obtaining a copy of this software and associated documentation files
|
||||
(the "Software"), to deal in the Software without restriction,
|
||||
including without limitation the rights to use, copy, modify, merge,
|
||||
publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
|
||||
Style HTML
|
||||
---------------
|
||||
|
||||
Written by Nochum Sossonko, (nsossonko@hotmail.com)
|
||||
|
||||
Based on code initially developed by: Einar Lielmanis, <einar@jsbeautifier.org>
|
||||
http://jsbeautifier.org/
|
||||
|
||||
Usage:
|
||||
style_html(html_source);
|
||||
|
||||
style_html(html_source, options);
|
||||
|
||||
The options are:
|
||||
indent_inner_html (default false) — indent <head> and <body> sections,
|
||||
indent_size (default 4) — indentation size,
|
||||
indent_char (default space) — character to indent with,
|
||||
wrap_line_length (default 250) - maximum amount of characters per line (0 = disable)
|
||||
brace_style (default "collapse") - "collapse" | "expand" | "end-expand" | "none"
|
||||
put braces on the same line as control statements (default), or put braces on own line (Allman / ANSI style), or just put end braces on own line, or attempt to keep them where they are.
|
||||
unformatted (defaults to inline tags) - list of tags, that shouldn't be reformatted
|
||||
indent_scripts (default normal) - "keep"|"separate"|"normal"
|
||||
preserve_newlines (default true) - whether existing line breaks before elements should be preserved
|
||||
Only works before elements, not inside tags or for text.
|
||||
max_preserve_newlines (default unlimited) - maximum number of line breaks to be preserved in one chunk
|
||||
indent_handlebars (default false) - format and indent {{#foo}} and {{/foo}}
|
||||
end_with_newline (false) - end with a newline
|
||||
extra_liners (default [head,body,/html]) -List of tags that should have an extra newline before them.
|
||||
|
||||
e.g.
|
||||
|
||||
style_html(html_source, {
|
||||
'indent_inner_html': false,
|
||||
'indent_size': 2,
|
||||
'indent_char': ' ',
|
||||
'wrap_line_length': 78,
|
||||
'brace_style': 'expand',
|
||||
'unformatted': ['a', 'sub', 'sup', 'b', 'i', 'u'],
|
||||
'preserve_newlines': true,
|
||||
'max_preserve_newlines': 5,
|
||||
'indent_handlebars': false,
|
||||
'extra_liners': ['/html']
|
||||
});
|
||||
*/
|
||||
|
||||
(function() {
|
||||
|
||||
function trim(s) {
|
||||
return s.replace(/^\s+|\s+$/g, '');
|
||||
}
|
||||
|
||||
function ltrim(s) {
|
||||
return s.replace(/^\s+/g, '');
|
||||
}
|
||||
|
||||
function rtrim(s) {
|
||||
return s.replace(/\s+$/g,'');
|
||||
}
|
||||
|
||||
function style_html(html_source, options, js_beautify, css_beautify) {
|
||||
//Wrapper function to invoke all the necessary constructors and deal with the output.
|
||||
|
||||
var multi_parser,
|
||||
indent_inner_html,
|
||||
indent_size,
|
||||
indent_character,
|
||||
wrap_line_length,
|
||||
brace_style,
|
||||
unformatted,
|
||||
preserve_newlines,
|
||||
max_preserve_newlines,
|
||||
indent_handlebars,
|
||||
wrap_attributes,
|
||||
wrap_attributes_indent_size,
|
||||
end_with_newline,
|
||||
extra_liners,
|
||||
eol;
|
||||
|
||||
options = options || {};
|
||||
|
||||
// backwards compatibility to 1.3.4
|
||||
if ((options.wrap_line_length === undefined || parseInt(options.wrap_line_length, 10) === 0) &&
|
||||
(options.max_char !== undefined && parseInt(options.max_char, 10) !== 0)) {
|
||||
options.wrap_line_length = options.max_char;
|
||||
}
|
||||
|
||||
indent_inner_html = (options.indent_inner_html === undefined) ? false : options.indent_inner_html;
|
||||
indent_size = (options.indent_size === undefined) ? 4 : parseInt(options.indent_size, 10);
|
||||
indent_character = (options.indent_char === undefined) ? ' ' : options.indent_char;
|
||||
brace_style = (options.brace_style === undefined) ? 'collapse' : options.brace_style;
|
||||
wrap_line_length = parseInt(options.wrap_line_length, 10) === 0 ? 32786 : parseInt(options.wrap_line_length || 250, 10);
|
||||
unformatted = options.unformatted || ['a', 'span', 'img', 'bdo', 'em', 'strong', 'dfn', 'code', 'samp', 'kbd',
|
||||
'var', 'cite', 'abbr', 'acronym', 'q', 'sub', 'sup', 'tt', 'i', 'b', 'big', 'small', 'u', 's', 'strike',
|
||||
'font', 'ins', 'del', 'pre', 'address', 'dt', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'];
|
||||
preserve_newlines = (options.preserve_newlines === undefined) ? true : options.preserve_newlines;
|
||||
max_preserve_newlines = preserve_newlines ?
|
||||
(isNaN(parseInt(options.max_preserve_newlines, 10)) ? 32786 : parseInt(options.max_preserve_newlines, 10))
|
||||
: 0;
|
||||
indent_handlebars = (options.indent_handlebars === undefined) ? false : options.indent_handlebars;
|
||||
wrap_attributes = (options.wrap_attributes === undefined) ? 'auto' : options.wrap_attributes;
|
||||
wrap_attributes_indent_size = (options.wrap_attributes_indent_size === undefined) ? indent_size : parseInt(options.wrap_attributes_indent_size, 10) || indent_size;
|
||||
end_with_newline = (options.end_with_newline === undefined) ? false : options.end_with_newline;
|
||||
extra_liners = (typeof options.extra_liners == 'object') && options.extra_liners ?
|
||||
options.extra_liners.concat() : (typeof options.extra_liners === 'string') ?
|
||||
options.extra_liners.split(',') : 'head,body,/html'.split(',');
|
||||
eol = options.eol ? options.eol : '\n';
|
||||
|
||||
if(options.indent_with_tabs){
|
||||
indent_character = '\t';
|
||||
indent_size = 1;
|
||||
}
|
||||
|
||||
eol = eol.replace(/\\r/, '\r').replace(/\\n/, '\n')
|
||||
|
||||
function Parser() {
|
||||
|
||||
this.pos = 0; //Parser position
|
||||
this.token = '';
|
||||
this.current_mode = 'CONTENT'; //reflects the current Parser mode: TAG/CONTENT
|
||||
this.tags = { //An object to hold tags, their position, and their parent-tags, initiated with default values
|
||||
parent: 'parent1',
|
||||
parentcount: 1,
|
||||
parent1: ''
|
||||
};
|
||||
this.tag_type = '';
|
||||
this.token_text = this.last_token = this.last_text = this.token_type = '';
|
||||
this.newlines = 0;
|
||||
this.indent_content = indent_inner_html;
|
||||
|
||||
this.Utils = { //Uilities made available to the various functions
|
||||
whitespace: "\n\r\t ".split(''),
|
||||
single_token: 'br,input,link,meta,source,!doctype,basefont,base,area,hr,wbr,param,img,isindex,embed'.split(','), //all the single tags for HTML
|
||||
extra_liners: extra_liners, //for tags that need a line of whitespace before them
|
||||
in_array: function(what, arr) {
|
||||
for (var i = 0; i < arr.length; i++) {
|
||||
if (what === arr[i]) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
// Return true if the given text is composed entirely of whitespace.
|
||||
this.is_whitespace = function(text) {
|
||||
for (var n = 0; n < text.length; text++) {
|
||||
if (!this.Utils.in_array(text.charAt(n), this.Utils.whitespace)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
this.traverse_whitespace = function() {
|
||||
var input_char = '';
|
||||
|
||||
input_char = this.input.charAt(this.pos);
|
||||
if (this.Utils.in_array(input_char, this.Utils.whitespace)) {
|
||||
this.newlines = 0;
|
||||
while (this.Utils.in_array(input_char, this.Utils.whitespace)) {
|
||||
if (preserve_newlines && input_char === '\n' && this.newlines <= max_preserve_newlines) {
|
||||
this.newlines += 1;
|
||||
}
|
||||
|
||||
this.pos++;
|
||||
input_char = this.input.charAt(this.pos);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
// Append a space to the given content (string array) or, if we are
|
||||
// at the wrap_line_length, append a newline/indentation.
|
||||
this.space_or_wrap = function(content) {
|
||||
if (this.line_char_count >= this.wrap_line_length) { //insert a line when the wrap_line_length is reached
|
||||
this.print_newline(false, content);
|
||||
this.print_indentation(content);
|
||||
} else {
|
||||
this.line_char_count++;
|
||||
content.push(' ');
|
||||
}
|
||||
};
|
||||
|
||||
this.get_content = function() { //function to capture regular content between tags
|
||||
var input_char = '',
|
||||
content = [],
|
||||
space = false; //if a space is needed
|
||||
|
||||
while (this.input.charAt(this.pos) !== '<') {
|
||||
if (this.pos >= this.input.length) {
|
||||
return content.length ? content.join('') : ['', 'TK_EOF'];
|
||||
}
|
||||
|
||||
if (this.traverse_whitespace()) {
|
||||
this.space_or_wrap(content);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (indent_handlebars) {
|
||||
// Handlebars parsing is complicated.
|
||||
// {{#foo}} and {{/foo}} are formatted tags.
|
||||
// {{something}} should get treated as content, except:
|
||||
// {{else}} specifically behaves like {{#if}} and {{/if}}
|
||||
var peek3 = this.input.substr(this.pos, 3);
|
||||
if (peek3 === '{{#' || peek3 === '{{/') {
|
||||
// These are tags and not content.
|
||||
break;
|
||||
} else if (peek3 === '{{!') {
|
||||
return [this.get_tag(), 'TK_TAG_HANDLEBARS_COMMENT'];
|
||||
} else if (this.input.substr(this.pos, 2) === '{{') {
|
||||
if (this.get_tag(true) === '{{else}}') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
input_char = this.input.charAt(this.pos);
|
||||
this.pos++;
|
||||
this.line_char_count++;
|
||||
content.push(input_char); //letter at-a-time (or string) inserted to an array
|
||||
}
|
||||
return content.length ? content.join('') : '';
|
||||
};
|
||||
|
||||
this.get_contents_to = function(name) { //get the full content of a script or style to pass to js_beautify
|
||||
if (this.pos === this.input.length) {
|
||||
return ['', 'TK_EOF'];
|
||||
}
|
||||
var input_char = '';
|
||||
var content = '';
|
||||
var reg_match = new RegExp('</' + name + '\\s*>', 'igm');
|
||||
reg_match.lastIndex = this.pos;
|
||||
var reg_array = reg_match.exec(this.input);
|
||||
var end_script = reg_array ? reg_array.index : this.input.length; //absolute end of script
|
||||
if (this.pos < end_script) { //get everything in between the script tags
|
||||
content = this.input.substring(this.pos, end_script);
|
||||
this.pos = end_script;
|
||||
}
|
||||
return content;
|
||||
};
|
||||
|
||||
this.record_tag = function(tag) { //function to record a tag and its parent in this.tags Object
|
||||
if (this.tags[tag + 'count']) { //check for the existence of this tag type
|
||||
this.tags[tag + 'count']++;
|
||||
this.tags[tag + this.tags[tag + 'count']] = this.indent_level; //and record the present indent level
|
||||
} else { //otherwise initialize this tag type
|
||||
this.tags[tag + 'count'] = 1;
|
||||
this.tags[tag + this.tags[tag + 'count']] = this.indent_level; //and record the present indent level
|
||||
}
|
||||
this.tags[tag + this.tags[tag + 'count'] + 'parent'] = this.tags.parent; //set the parent (i.e. in the case of a div this.tags.div1parent)
|
||||
this.tags.parent = tag + this.tags[tag + 'count']; //and make this the current parent (i.e. in the case of a div 'div1')
|
||||
};
|
||||
|
||||
this.retrieve_tag = function(tag) { //function to retrieve the opening tag to the corresponding closer
|
||||
if (this.tags[tag + 'count']) { //if the openener is not in the Object we ignore it
|
||||
var temp_parent = this.tags.parent; //check to see if it's a closable tag.
|
||||
while (temp_parent) { //till we reach '' (the initial value);
|
||||
if (tag + this.tags[tag + 'count'] === temp_parent) { //if this is it use it
|
||||
break;
|
||||
}
|
||||
temp_parent = this.tags[temp_parent + 'parent']; //otherwise keep on climbing up the DOM Tree
|
||||
}
|
||||
if (temp_parent) { //if we caught something
|
||||
this.indent_level = this.tags[tag + this.tags[tag + 'count']]; //set the indent_level accordingly
|
||||
this.tags.parent = this.tags[temp_parent + 'parent']; //and set the current parent
|
||||
}
|
||||
delete this.tags[tag + this.tags[tag + 'count'] + 'parent']; //delete the closed tags parent reference...
|
||||
delete this.tags[tag + this.tags[tag + 'count']]; //...and the tag itself
|
||||
if (this.tags[tag + 'count'] === 1) {
|
||||
delete this.tags[tag + 'count'];
|
||||
} else {
|
||||
this.tags[tag + 'count']--;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.indent_to_tag = function(tag) {
|
||||
// Match the indentation level to the last use of this tag, but don't remove it.
|
||||
if (!this.tags[tag + 'count']) {
|
||||
return;
|
||||
}
|
||||
var temp_parent = this.tags.parent;
|
||||
while (temp_parent) {
|
||||
if (tag + this.tags[tag + 'count'] === temp_parent) {
|
||||
break;
|
||||
}
|
||||
temp_parent = this.tags[temp_parent + 'parent'];
|
||||
}
|
||||
if (temp_parent) {
|
||||
this.indent_level = this.tags[tag + this.tags[tag + 'count']];
|
||||
}
|
||||
};
|
||||
|
||||
this.get_tag = function(peek) { //function to get a full tag and parse its type
|
||||
var input_char = '',
|
||||
content = [],
|
||||
comment = '',
|
||||
space = false,
|
||||
first_attr = true,
|
||||
tag_start, tag_end,
|
||||
tag_start_char,
|
||||
orig_pos = this.pos,
|
||||
orig_line_char_count = this.line_char_count;
|
||||
|
||||
peek = peek !== undefined ? peek : false;
|
||||
|
||||
do {
|
||||
if (this.pos >= this.input.length) {
|
||||
if (peek) {
|
||||
this.pos = orig_pos;
|
||||
this.line_char_count = orig_line_char_count;
|
||||
}
|
||||
return content.length ? content.join('') : ['', 'TK_EOF'];
|
||||
}
|
||||
|
||||
input_char = this.input.charAt(this.pos);
|
||||
this.pos++;
|
||||
|
||||
if (this.Utils.in_array(input_char, this.Utils.whitespace)) { //don't want to insert unnecessary space
|
||||
space = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (input_char === "'" || input_char === '"') {
|
||||
input_char += this.get_unformatted(input_char);
|
||||
space = true;
|
||||
|
||||
}
|
||||
|
||||
if (input_char === '=') { //no space before =
|
||||
space = false;
|
||||
}
|
||||
|
||||
if (content.length && content[content.length - 1] !== '=' && input_char !== '>' && space) {
|
||||
//no space after = or before >
|
||||
this.space_or_wrap(content);
|
||||
space = false;
|
||||
if (!first_attr && wrap_attributes === 'force' && input_char !== '/') {
|
||||
this.print_newline(true, content);
|
||||
this.print_indentation(content);
|
||||
for (var count = 0; count < wrap_attributes_indent_size; count++) {
|
||||
content.push(indent_character);
|
||||
}
|
||||
}
|
||||
for (var i = 0; i < content.length; i++) {
|
||||
if (content[i] === ' ') {
|
||||
first_attr = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (indent_handlebars && tag_start_char === '<') {
|
||||
// When inside an angle-bracket tag, put spaces around
|
||||
// handlebars not inside of strings.
|
||||
if ((input_char + this.input.charAt(this.pos)) === '{{') {
|
||||
input_char += this.get_unformatted('}}');
|
||||
if (content.length && content[content.length - 1] !== ' ' && content[content.length - 1] !== '<') {
|
||||
input_char = ' ' + input_char;
|
||||
}
|
||||
space = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (input_char === '<' && !tag_start_char) {
|
||||
tag_start = this.pos - 1;
|
||||
tag_start_char = '<';
|
||||
}
|
||||
|
||||
if (indent_handlebars && !tag_start_char) {
|
||||
if (content.length >= 2 && content[content.length - 1] === '{' && content[content.length - 2] === '{') {
|
||||
if (input_char === '#' || input_char === '/' || input_char === '!') {
|
||||
tag_start = this.pos - 3;
|
||||
} else {
|
||||
tag_start = this.pos - 2;
|
||||
}
|
||||
tag_start_char = '{';
|
||||
}
|
||||
}
|
||||
|
||||
this.line_char_count++;
|
||||
content.push(input_char); //inserts character at-a-time (or string)
|
||||
|
||||
if (content[1] && (content[1] === '!' || content[1] === '?' || content[1] === '%')) { //if we're in a comment, do something special
|
||||
// We treat all comments as literals, even more than preformatted tags
|
||||
// we just look for the appropriate close tag
|
||||
content = [this.get_comment(tag_start)];
|
||||
break;
|
||||
}
|
||||
|
||||
if (indent_handlebars && content[1] && content[1] === '{' && content[2] && content[2] === '!') { //if we're in a comment, do something special
|
||||
// We treat all comments as literals, even more than preformatted tags
|
||||
// we just look for the appropriate close tag
|
||||
content = [this.get_comment(tag_start)];
|
||||
break;
|
||||
}
|
||||
|
||||
if (indent_handlebars && tag_start_char === '{' && content.length > 2 && content[content.length - 2] === '}' && content[content.length - 1] === '}') {
|
||||
break;
|
||||
}
|
||||
} while (input_char !== '>');
|
||||
|
||||
var tag_complete = content.join('');
|
||||
var tag_index;
|
||||
var tag_offset;
|
||||
|
||||
if (tag_complete.indexOf(' ') !== -1) { //if there's whitespace, thats where the tag name ends
|
||||
tag_index = tag_complete.indexOf(' ');
|
||||
} else if (tag_complete.charAt(0) === '{') {
|
||||
tag_index = tag_complete.indexOf('}');
|
||||
} else { //otherwise go with the tag ending
|
||||
tag_index = tag_complete.indexOf('>');
|
||||
}
|
||||
if (tag_complete.charAt(0) === '<' || !indent_handlebars) {
|
||||
tag_offset = 1;
|
||||
} else {
|
||||
tag_offset = tag_complete.charAt(2) === '#' ? 3 : 2;
|
||||
}
|
||||
var tag_check = tag_complete.substring(tag_offset, tag_index).toLowerCase();
|
||||
if (tag_complete.charAt(tag_complete.length - 2) === '/' ||
|
||||
this.Utils.in_array(tag_check, this.Utils.single_token)) { //if this tag name is a single tag type (either in the list or has a closing /)
|
||||
if (!peek) {
|
||||
this.tag_type = 'SINGLE';
|
||||
}
|
||||
} else if (indent_handlebars && tag_complete.charAt(0) === '{' && tag_check === 'else') {
|
||||
if (!peek) {
|
||||
this.indent_to_tag('if');
|
||||
this.tag_type = 'HANDLEBARS_ELSE';
|
||||
this.indent_content = true;
|
||||
this.traverse_whitespace();
|
||||
}
|
||||
} else if (this.is_unformatted(tag_check, unformatted)) { // do not reformat the "unformatted" tags
|
||||
comment = this.get_unformatted('</' + tag_check + '>', tag_complete); //...delegate to get_unformatted function
|
||||
content.push(comment);
|
||||
tag_end = this.pos - 1;
|
||||
this.tag_type = 'SINGLE';
|
||||
} else if (tag_check === 'script' &&
|
||||
(tag_complete.search('type') === -1 ||
|
||||
(tag_complete.search('type') > -1 &&
|
||||
tag_complete.search(/\b(text|application)\/(x-)?(javascript|ecmascript|jscript|livescript)/) > -1))) {
|
||||
if (!peek) {
|
||||
this.record_tag(tag_check);
|
||||
this.tag_type = 'SCRIPT';
|
||||
}
|
||||
} else if (tag_check === 'style' &&
|
||||
(tag_complete.search('type') === -1 ||
|
||||
(tag_complete.search('type') > -1 && tag_complete.search('text/css') > -1))) {
|
||||
if (!peek) {
|
||||
this.record_tag(tag_check);
|
||||
this.tag_type = 'STYLE';
|
||||
}
|
||||
} else if (tag_check.charAt(0) === '!') { //peek for <! comment
|
||||
// for comments content is already correct.
|
||||
if (!peek) {
|
||||
this.tag_type = 'SINGLE';
|
||||
this.traverse_whitespace();
|
||||
}
|
||||
} else if (!peek) {
|
||||
if (tag_check.charAt(0) === '/') { //this tag is a double tag so check for tag-ending
|
||||
this.retrieve_tag(tag_check.substring(1)); //remove it and all ancestors
|
||||
this.tag_type = 'END';
|
||||
} else { //otherwise it's a start-tag
|
||||
this.record_tag(tag_check); //push it on the tag stack
|
||||
if (tag_check.toLowerCase() !== 'html') {
|
||||
this.indent_content = true;
|
||||
}
|
||||
this.tag_type = 'START';
|
||||
}
|
||||
|
||||
// Allow preserving of newlines after a start or end tag
|
||||
if (this.traverse_whitespace()) {
|
||||
this.space_or_wrap(content);
|
||||
}
|
||||
|
||||
if (this.Utils.in_array(tag_check, this.Utils.extra_liners)) { //check if this double needs an extra line
|
||||
this.print_newline(false, this.output);
|
||||
if (this.output.length && this.output[this.output.length - 2] !== '\n') {
|
||||
this.print_newline(true, this.output);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (peek) {
|
||||
this.pos = orig_pos;
|
||||
this.line_char_count = orig_line_char_count;
|
||||
}
|
||||
|
||||
return content.join(''); //returns fully formatted tag
|
||||
};
|
||||
|
||||
this.get_comment = function(start_pos) { //function to return comment content in its entirety
|
||||
// this is will have very poor perf, but will work for now.
|
||||
var comment = '',
|
||||
delimiter = '>',
|
||||
matched = false;
|
||||
|
||||
this.pos = start_pos;
|
||||
input_char = this.input.charAt(this.pos);
|
||||
this.pos++;
|
||||
|
||||
while (this.pos <= this.input.length) {
|
||||
comment += input_char;
|
||||
|
||||
// only need to check for the delimiter if the last chars match
|
||||
if (comment.charAt(comment.length - 1) === delimiter.charAt(delimiter.length - 1) &&
|
||||
comment.indexOf(delimiter) !== -1) {
|
||||
break;
|
||||
}
|
||||
|
||||
// only need to search for custom delimiter for the first few characters
|
||||
if (!matched && comment.length < 10) {
|
||||
if (comment.indexOf('<![if') === 0) { //peek for <![if conditional comment
|
||||
delimiter = '<![endif]>';
|
||||
matched = true;
|
||||
} else if (comment.indexOf('<![cdata[') === 0) { //if it's a <[cdata[ comment...
|
||||
delimiter = ']]>';
|
||||
matched = true;
|
||||
} else if (comment.indexOf('<![') === 0) { // some other ![ comment? ...
|
||||
delimiter = ']>';
|
||||
matched = true;
|
||||
} else if (comment.indexOf('<!--') === 0) { // <!-- comment ...
|
||||
delimiter = '-->';
|
||||
matched = true;
|
||||
} else if (comment.indexOf('{{!') === 0) { // {{! handlebars comment
|
||||
delimiter = '}}';
|
||||
matched = true;
|
||||
} else if (comment.indexOf('<?') === 0) { // {{! handlebars comment
|
||||
delimiter = '?>';
|
||||
matched = true;
|
||||
} else if (comment.indexOf('<%') === 0) { // {{! handlebars comment
|
||||
delimiter = '%>';
|
||||
matched = true;
|
||||
}
|
||||
}
|
||||
|
||||
input_char = this.input.charAt(this.pos);
|
||||
this.pos++;
|
||||
}
|
||||
|
||||
return comment;
|
||||
};
|
||||
|
||||
this.get_unformatted = function(delimiter, orig_tag) { //function to return unformatted content in its entirety
|
||||
|
||||
if (orig_tag && orig_tag.toLowerCase().indexOf(delimiter) !== -1) {
|
||||
return '';
|
||||
}
|
||||
var input_char = '';
|
||||
var content = '';
|
||||
var min_index = 0;
|
||||
var space = true;
|
||||
do {
|
||||
|
||||
if (this.pos >= this.input.length) {
|
||||
return content;
|
||||
}
|
||||
|
||||
input_char = this.input.charAt(this.pos);
|
||||
this.pos++;
|
||||
|
||||
if (this.Utils.in_array(input_char, this.Utils.whitespace)) {
|
||||
if (!space) {
|
||||
this.line_char_count--;
|
||||
continue;
|
||||
}
|
||||
if (input_char === '\n' || input_char === '\r') {
|
||||
content += '\n';
|
||||
/* Don't change tab indention for unformatted blocks. If using code for html editing, this will greatly affect <pre> tags if they are specified in the 'unformatted array'
|
||||
for (var i=0; i<this.indent_level; i++) {
|
||||
content += this.indent_string;
|
||||
}
|
||||
space = false; //...and make sure other indentation is erased
|
||||
*/
|
||||
this.line_char_count = 0;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
content += input_char;
|
||||
this.line_char_count++;
|
||||
space = true;
|
||||
|
||||
if (indent_handlebars && input_char === '{' && content.length && content.charAt(content.length - 2) === '{') {
|
||||
// Handlebars expressions in strings should also be unformatted.
|
||||
content += this.get_unformatted('}}');
|
||||
// These expressions are opaque. Ignore delimiters found in them.
|
||||
min_index = content.length;
|
||||
}
|
||||
} while (content.toLowerCase().indexOf(delimiter, min_index) === -1);
|
||||
return content;
|
||||
};
|
||||
|
||||
this.get_token = function() { //initial handler for token-retrieval
|
||||
var token;
|
||||
|
||||
if (this.last_token === 'TK_TAG_SCRIPT' || this.last_token === 'TK_TAG_STYLE') { //check if we need to format javascript
|
||||
var type = this.last_token.substr(7);
|
||||
token = this.get_contents_to(type);
|
||||
if (typeof token !== 'string') {
|
||||
return token;
|
||||
}
|
||||
return [token, 'TK_' + type];
|
||||
}
|
||||
if (this.current_mode === 'CONTENT') {
|
||||
token = this.get_content();
|
||||
if (typeof token !== 'string') {
|
||||
return token;
|
||||
} else {
|
||||
return [token, 'TK_CONTENT'];
|
||||
}
|
||||
}
|
||||
|
||||
if (this.current_mode === 'TAG') {
|
||||
token = this.get_tag();
|
||||
if (typeof token !== 'string') {
|
||||
return token;
|
||||
} else {
|
||||
var tag_name_type = 'TK_TAG_' + this.tag_type;
|
||||
return [token, tag_name_type];
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.get_full_indent = function(level) {
|
||||
level = this.indent_level + level || 0;
|
||||
if (level < 1) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return Array(level + 1).join(this.indent_string);
|
||||
};
|
||||
|
||||
this.is_unformatted = function(tag_check, unformatted) {
|
||||
//is this an HTML5 block-level link?
|
||||
if (!this.Utils.in_array(tag_check, unformatted)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (tag_check.toLowerCase() !== 'a' || !this.Utils.in_array('a', unformatted)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
//at this point we have an tag; is its first child something we want to remain
|
||||
//unformatted?
|
||||
var next_tag = this.get_tag(true /* peek. */ );
|
||||
|
||||
// test next_tag to see if it is just html tag (no external content)
|
||||
var tag = (next_tag || "").match(/^\s*<\s*\/?([a-z]*)\s*[^>]*>\s*$/);
|
||||
|
||||
// if next_tag comes back but is not an isolated tag, then
|
||||
// let's treat the 'a' tag as having content
|
||||
// and respect the unformatted option
|
||||
if (!tag || this.Utils.in_array(tag, unformatted)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
this.printer = function(js_source, indent_character, indent_size, wrap_line_length, brace_style) { //handles input/output and some other printing functions
|
||||
|
||||
this.input = js_source || ''; //gets the input for the Parser
|
||||
|
||||
// HACK: newline parsing inconsistent. This brute force normalizes the input.
|
||||
this.input = this.input.replace(/\r\n|[\r\u2028\u2029]/g, '\n')
|
||||
|
||||
this.output = [];
|
||||
this.indent_character = indent_character;
|
||||
this.indent_string = '';
|
||||
this.indent_size = indent_size;
|
||||
this.brace_style = brace_style;
|
||||
this.indent_level = 0;
|
||||
this.wrap_line_length = wrap_line_length;
|
||||
this.line_char_count = 0; //count to see if wrap_line_length was exceeded
|
||||
|
||||
for (var i = 0; i < this.indent_size; i++) {
|
||||
this.indent_string += this.indent_character;
|
||||
}
|
||||
|
||||
this.print_newline = function(force, arr) {
|
||||
this.line_char_count = 0;
|
||||
if (!arr || !arr.length) {
|
||||
return;
|
||||
}
|
||||
if (force || (arr[arr.length - 1] !== '\n')) { //we might want the extra line
|
||||
if ((arr[arr.length - 1] !== '\n')) {
|
||||
arr[arr.length - 1] = rtrim(arr[arr.length - 1]);
|
||||
}
|
||||
arr.push('\n');
|
||||
}
|
||||
};
|
||||
|
||||
this.print_indentation = function(arr) {
|
||||
for (var i = 0; i < this.indent_level; i++) {
|
||||
arr.push(this.indent_string);
|
||||
this.line_char_count += this.indent_string.length;
|
||||
}
|
||||
};
|
||||
|
||||
this.print_token = function(text) {
|
||||
// Avoid printing initial whitespace.
|
||||
if (this.is_whitespace(text) && !this.output.length) {
|
||||
return;
|
||||
}
|
||||
if (text || text !== '') {
|
||||
if (this.output.length && this.output[this.output.length - 1] === '\n') {
|
||||
this.print_indentation(this.output);
|
||||
text = ltrim(text);
|
||||
}
|
||||
}
|
||||
this.print_token_raw(text);
|
||||
};
|
||||
|
||||
this.print_token_raw = function(text) {
|
||||
// If we are going to print newlines, truncate trailing
|
||||
// whitespace, as the newlines will represent the space.
|
||||
if (this.newlines > 0) {
|
||||
text = rtrim(text);
|
||||
}
|
||||
|
||||
if (text && text !== '') {
|
||||
if (text.length > 1 && text.charAt(text.length - 1) === '\n') {
|
||||
// unformatted tags can grab newlines as their last character
|
||||
this.output.push(text.slice(0, -1));
|
||||
this.print_newline(false, this.output);
|
||||
} else {
|
||||
this.output.push(text);
|
||||
}
|
||||
}
|
||||
|
||||
for (var n = 0; n < this.newlines; n++) {
|
||||
this.print_newline(n > 0, this.output);
|
||||
}
|
||||
this.newlines = 0;
|
||||
};
|
||||
|
||||
this.indent = function() {
|
||||
this.indent_level++;
|
||||
};
|
||||
|
||||
this.unindent = function() {
|
||||
if (this.indent_level > 0) {
|
||||
this.indent_level--;
|
||||
}
|
||||
};
|
||||
};
|
||||
return this;
|
||||
}
|
||||
|
||||
/*_____________________--------------------_____________________*/
|
||||
|
||||
multi_parser = new Parser(); //wrapping functions Parser
|
||||
multi_parser.printer(html_source, indent_character, indent_size, wrap_line_length, brace_style); //initialize starting values
|
||||
|
||||
while (true) {
|
||||
var t = multi_parser.get_token();
|
||||
multi_parser.token_text = t[0];
|
||||
multi_parser.token_type = t[1];
|
||||
|
||||
if (multi_parser.token_type === 'TK_EOF') {
|
||||
break;
|
||||
}
|
||||
|
||||
switch (multi_parser.token_type) {
|
||||
case 'TK_TAG_START':
|
||||
multi_parser.print_newline(false, multi_parser.output);
|
||||
multi_parser.print_token(multi_parser.token_text);
|
||||
if (multi_parser.indent_content) {
|
||||
multi_parser.indent();
|
||||
multi_parser.indent_content = false;
|
||||
}
|
||||
multi_parser.current_mode = 'CONTENT';
|
||||
break;
|
||||
case 'TK_TAG_STYLE':
|
||||
case 'TK_TAG_SCRIPT':
|
||||
multi_parser.print_newline(false, multi_parser.output);
|
||||
multi_parser.print_token(multi_parser.token_text);
|
||||
multi_parser.current_mode = 'CONTENT';
|
||||
break;
|
||||
case 'TK_TAG_END':
|
||||
//Print new line only if the tag has no content and has child
|
||||
if (multi_parser.last_token === 'TK_CONTENT' && multi_parser.last_text === '') {
|
||||
var tag_name = multi_parser.token_text.match(/\w+/)[0];
|
||||
var tag_extracted_from_last_output = null;
|
||||
if (multi_parser.output.length) {
|
||||
tag_extracted_from_last_output = multi_parser.output[multi_parser.output.length - 1].match(/(?:<|{{#)\s*(\w+)/);
|
||||
}
|
||||
if (tag_extracted_from_last_output === null ||
|
||||
(tag_extracted_from_last_output[1] !== tag_name && !multi_parser.Utils.in_array(tag_extracted_from_last_output[1], unformatted))) {
|
||||
multi_parser.print_newline(false, multi_parser.output);
|
||||
}
|
||||
}
|
||||
multi_parser.print_token(multi_parser.token_text);
|
||||
multi_parser.current_mode = 'CONTENT';
|
||||
break;
|
||||
case 'TK_TAG_SINGLE':
|
||||
// Don't add a newline before elements that should remain unformatted.
|
||||
var tag_check = multi_parser.token_text.match(/^\s*<([a-z-]+)/i);
|
||||
if (!tag_check || !multi_parser.Utils.in_array(tag_check[1], unformatted)) {
|
||||
multi_parser.print_newline(false, multi_parser.output);
|
||||
}
|
||||
multi_parser.print_token(multi_parser.token_text);
|
||||
multi_parser.current_mode = 'CONTENT';
|
||||
break;
|
||||
case 'TK_TAG_HANDLEBARS_ELSE':
|
||||
multi_parser.print_token(multi_parser.token_text);
|
||||
if (multi_parser.indent_content) {
|
||||
multi_parser.indent();
|
||||
multi_parser.indent_content = false;
|
||||
}
|
||||
multi_parser.current_mode = 'CONTENT';
|
||||
break;
|
||||
case 'TK_TAG_HANDLEBARS_COMMENT':
|
||||
multi_parser.print_token(multi_parser.token_text);
|
||||
multi_parser.current_mode = 'TAG';
|
||||
break;
|
||||
case 'TK_CONTENT':
|
||||
multi_parser.print_token(multi_parser.token_text);
|
||||
multi_parser.current_mode = 'TAG';
|
||||
break;
|
||||
case 'TK_STYLE':
|
||||
case 'TK_SCRIPT':
|
||||
if (multi_parser.token_text !== '') {
|
||||
multi_parser.print_newline(false, multi_parser.output);
|
||||
var text = multi_parser.token_text,
|
||||
_beautifier,
|
||||
script_indent_level = 1;
|
||||
if (multi_parser.token_type === 'TK_SCRIPT') {
|
||||
_beautifier = typeof js_beautify === 'function' && js_beautify;
|
||||
} else if (multi_parser.token_type === 'TK_STYLE') {
|
||||
_beautifier = typeof css_beautify === 'function' && css_beautify;
|
||||
}
|
||||
|
||||
if (options.indent_scripts === "keep") {
|
||||
script_indent_level = 0;
|
||||
} else if (options.indent_scripts === "separate") {
|
||||
script_indent_level = -multi_parser.indent_level;
|
||||
}
|
||||
|
||||
var indentation = multi_parser.get_full_indent(script_indent_level);
|
||||
if (_beautifier) {
|
||||
|
||||
// call the Beautifier if avaliable
|
||||
var Child_options = function() {
|
||||
this.eol = '\n';
|
||||
};
|
||||
Child_options.prototype = options;
|
||||
var child_options = new Child_options();
|
||||
text = _beautifier(text.replace(/^\s*/, indentation), child_options);
|
||||
} else {
|
||||
// simply indent the string otherwise
|
||||
var white = text.match(/^\s*/)[0];
|
||||
var _level = white.match(/[^\n\r]*$/)[0].split(multi_parser.indent_string).length - 1;
|
||||
var reindent = multi_parser.get_full_indent(script_indent_level - _level);
|
||||
text = text.replace(/^\s*/, indentation)
|
||||
.replace(/\r\n|\r|\n/g, '\n' + reindent)
|
||||
.replace(/\s+$/, '');
|
||||
}
|
||||
if (text) {
|
||||
multi_parser.print_token_raw(text);
|
||||
multi_parser.print_newline(true, multi_parser.output);
|
||||
}
|
||||
}
|
||||
multi_parser.current_mode = 'TAG';
|
||||
break;
|
||||
default:
|
||||
// We should not be getting here but we don't want to drop input on the floor
|
||||
// Just output the text and move on
|
||||
if (multi_parser.token_text !== '') {
|
||||
multi_parser.print_token(multi_parser.token_text);
|
||||
}
|
||||
break;
|
||||
}
|
||||
multi_parser.last_token = multi_parser.token_type;
|
||||
multi_parser.last_text = multi_parser.token_text;
|
||||
}
|
||||
var sweet_code = multi_parser.output.join('').replace(/[\r\n\t ]+$/, '');
|
||||
|
||||
// establish end_with_newline
|
||||
if (end_with_newline) {
|
||||
sweet_code += '\n';
|
||||
}
|
||||
|
||||
if (eol != '\n') {
|
||||
sweet_code = sweet_code.replace(/[\n]/g, eol);
|
||||
}
|
||||
|
||||
return sweet_code;
|
||||
}
|
||||
|
||||
if (typeof define === "function" && define.amd) {
|
||||
// Add support for AMD ( https://github.com/amdjs/amdjs-api/wiki/AMD#defineamd-property- )
|
||||
define(["require", "./beautify", "./beautify-css"], function(requireamd) {
|
||||
var js_beautify = requireamd("./beautify");
|
||||
var css_beautify = requireamd("./beautify-css");
|
||||
|
||||
return {
|
||||
html_beautify: function(html_source, options) {
|
||||
return style_html(html_source, options, js_beautify.js_beautify, css_beautify.css_beautify);
|
||||
}
|
||||
};
|
||||
});
|
||||
} else if (typeof exports !== "undefined") {
|
||||
// Add support for CommonJS. Just put this file somewhere on your require.paths
|
||||
// and you will be able to `var html_beautify = require("beautify").html_beautify`.
|
||||
var js_beautify = require('./beautify.js');
|
||||
var css_beautify = require('./beautify-css.js');
|
||||
|
||||
exports.html_beautify = function(html_source, options) {
|
||||
return style_html(html_source, options, js_beautify.js_beautify, css_beautify.css_beautify);
|
||||
};
|
||||
} else if (typeof window !== "undefined") {
|
||||
// If we're running a web page and don't have either of the above, add our one global
|
||||
window.html_beautify = function(html_source, options) {
|
||||
return style_html(html_source, options, window.js_beautify, window.css_beautify);
|
||||
};
|
||||
} else if (typeof global !== "undefined") {
|
||||
// If we don't even have window, try global.
|
||||
global.html_beautify = function(html_source, options) {
|
||||
return style_html(html_source, options, global.js_beautify, global.css_beautify);
|
||||
};
|
||||
}
|
||||
|
||||
}());
|
||||
2087
gaia/distros/spark/apps/customizer/components/js-beautify/js/lib/beautify.js
Executable file
2087
gaia/distros/spark/apps/customizer/components/js-beautify/js/lib/beautify.js
Executable file
File diff suppressed because it is too large
Load diff
477
gaia/distros/spark/apps/customizer/components/js-beautify/js/lib/cli.js
Executable file
477
gaia/distros/spark/apps/customizer/components/js-beautify/js/lib/cli.js
Executable file
|
|
@ -0,0 +1,477 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2007-2013 Einar Lielmanis and contributors.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person
|
||||
obtaining a copy of this software and associated documentation files
|
||||
(the "Software"), to deal in the Software without restriction,
|
||||
including without limitation the rights to use, copy, modify, merge,
|
||||
publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
Js-Beautify Command-line for node.js
|
||||
-------------------------------------
|
||||
|
||||
Written by Daniel Stockman (daniel.stockman@gmail.com)
|
||||
|
||||
*/
|
||||
|
||||
var debug = process.env.DEBUG_JSBEAUTIFY || process.env.JSBEAUTIFY_DEBUG ? function() {
|
||||
console.error.apply(console, arguments);
|
||||
} : function() {};
|
||||
|
||||
var fs = require('fs'),
|
||||
cc = require('config-chain'),
|
||||
beautify = require('../index'),
|
||||
mkdirp = require('mkdirp'),
|
||||
nopt = require('nopt'),
|
||||
path = require('path'),
|
||||
knownOpts = {
|
||||
// Beautifier
|
||||
"indent_size": Number,
|
||||
"indent_char": String,
|
||||
"eol": String,
|
||||
"indent_level": Number,
|
||||
"indent_with_tabs": Boolean,
|
||||
"preserve_newlines": Boolean,
|
||||
"max_preserve_newlines": Number,
|
||||
"space_in_paren": Boolean,
|
||||
"space_in_empty_paren": Boolean,
|
||||
"jslint_happy": Boolean,
|
||||
"space_after_anon_function": Boolean,
|
||||
// TODO: expand-strict is obsolete, now identical to expand. Remove in future version
|
||||
"brace_style": ["collapse", "expand", "end-expand", "expand-strict", "none"],
|
||||
"break_chained_methods": Boolean,
|
||||
"keep_array_indentation": Boolean,
|
||||
"unescape_strings": Boolean,
|
||||
"wrap_line_length": Number,
|
||||
"wrap_attributes": ["auto", "force"],
|
||||
"wrap_attributes_indent_size": Number,
|
||||
"e4x": Boolean,
|
||||
"end_with_newline": Boolean,
|
||||
"comma_first": Boolean,
|
||||
// CSS-only
|
||||
"selector_separator_newline": Boolean,
|
||||
"newline_between_rules": Boolean,
|
||||
// HTML-only
|
||||
"max_char": Number, // obsolete since 1.3.5
|
||||
"unformatted": [String, Array],
|
||||
"indent_inner_html": [Boolean],
|
||||
"indent_scripts": ["keep", "separate", "normal"],
|
||||
"extra_liners": [String, Array],
|
||||
// CLI
|
||||
"version": Boolean,
|
||||
"help": Boolean,
|
||||
"files": [path, Array],
|
||||
"outfile": path,
|
||||
"replace": Boolean,
|
||||
"quiet": Boolean,
|
||||
"type": ["js", "css", "html"],
|
||||
"config": path
|
||||
},
|
||||
// dasherizeShorthands provides { "indent-size": ["--indent_size"] }
|
||||
// translation, allowing more convenient dashes in CLI arguments
|
||||
shortHands = dasherizeShorthands({
|
||||
// Beautifier
|
||||
"s": ["--indent_size"],
|
||||
"c": ["--indent_char"],
|
||||
"e": ["--eol"],
|
||||
"l": ["--indent_level"],
|
||||
"t": ["--indent_with_tabs"],
|
||||
"p": ["--preserve_newlines"],
|
||||
"m": ["--max_preserve_newlines"],
|
||||
"P": ["--space_in_paren"],
|
||||
"E": ["--space_in_empty_paren"],
|
||||
"j": ["--jslint_happy"],
|
||||
"a": ["--space_after_anon_function"],
|
||||
"b": ["--brace_style"],
|
||||
"B": ["--break_chained_methods"],
|
||||
"k": ["--keep_array_indentation"],
|
||||
"x": ["--unescape_strings"],
|
||||
"w": ["--wrap_line_length"],
|
||||
"X": ["--e4x"],
|
||||
"n": ["--end_with_newline"],
|
||||
"C": ["--comma_first"],
|
||||
// CSS-only
|
||||
"L": ["--selector_separator_newline"],
|
||||
"N": ["--newline_between_rules"],
|
||||
// HTML-only
|
||||
"A": ["--wrap_attributes"],
|
||||
"i": ["--wrap_attributes_indent_size"],
|
||||
"W": ["--max_char"], // obsolete since 1.3.5
|
||||
"U": ["--unformatted"],
|
||||
"I": ["--indent_inner_html"],
|
||||
"S": ["--indent_scripts"],
|
||||
"E": ["--extra_liners"],
|
||||
// non-dasherized hybrid shortcuts
|
||||
"good-stuff": [
|
||||
"--keep_array_indentation",
|
||||
"--keep_function_indentation",
|
||||
"--jslint_happy"
|
||||
],
|
||||
"js": ["--type", "js"],
|
||||
"css": ["--type", "css"],
|
||||
"html": ["--type", "html"],
|
||||
// CLI
|
||||
"v": ["--version"],
|
||||
"h": ["--help"],
|
||||
"f": ["--files"],
|
||||
"o": ["--outfile"],
|
||||
"r": ["--replace"],
|
||||
"q": ["--quiet"]
|
||||
// no shorthand for "config"
|
||||
});
|
||||
|
||||
function verifyExists(fullPath) {
|
||||
return fs.existsSync(fullPath) ? fullPath : null;
|
||||
}
|
||||
|
||||
function findRecursive(dir, fileName) {
|
||||
var fullPath = path.join(dir, fileName);
|
||||
var nextDir = path.dirname(dir);
|
||||
var result = verifyExists(fullPath);
|
||||
|
||||
if (!result && (nextDir !== dir)) {
|
||||
result = findRecursive(nextDir, fileName);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
function getUserHome() {
|
||||
return process.env.HOME || process.env.USERPROFILE;
|
||||
}
|
||||
|
||||
// var cli = require('js-beautify/cli'); cli.interpret();
|
||||
var interpret = exports.interpret = function(argv, slice) {
|
||||
var parsed = nopt(knownOpts, shortHands, argv, slice);
|
||||
|
||||
if (parsed.version) {
|
||||
console.log(require('../../package.json').version);
|
||||
process.exit(0);
|
||||
} else if (parsed.help) {
|
||||
usage();
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
var cfg = cc(
|
||||
parsed,
|
||||
cleanOptions(cc.env('jsbeautify_'), knownOpts),
|
||||
parsed.config,
|
||||
findRecursive(process.cwd(), '.jsbeautifyrc'),
|
||||
verifyExists(path.join(getUserHome() || "", ".jsbeautifyrc")),
|
||||
__dirname + '/../config/defaults.json'
|
||||
).snapshot;
|
||||
|
||||
try {
|
||||
// Verify arguments
|
||||
checkType(cfg);
|
||||
checkFiles(cfg);
|
||||
debug(cfg);
|
||||
|
||||
// Process files synchronously to avoid EMFILE error
|
||||
cfg.files.forEach(processInputSync, {
|
||||
cfg: cfg
|
||||
});
|
||||
} catch (ex) {
|
||||
debug(cfg);
|
||||
// usage(ex);
|
||||
console.error(ex);
|
||||
console.error('Run `' + getScriptName() + ' -h` for help.');
|
||||
process.exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
// interpret args immediately when called as executable
|
||||
if (require.main === module) {
|
||||
interpret();
|
||||
}
|
||||
|
||||
function usage(err) {
|
||||
var scriptName = getScriptName();
|
||||
var msg = [
|
||||
scriptName + '@' + require('../../package.json').version,
|
||||
'',
|
||||
'CLI Options:',
|
||||
' -f, --file Input file(s) (Pass \'-\' for stdin)',
|
||||
' -r, --replace Write output in-place, replacing input',
|
||||
' -o, --outfile Write output to file (default stdout)',
|
||||
' --config Path to config file',
|
||||
' --type [js|css|html] ["js"]',
|
||||
' -q, --quiet Suppress logging to stdout',
|
||||
' -h, --help Show this help',
|
||||
' -v, --version Show the version',
|
||||
'',
|
||||
'Beautifier Options:',
|
||||
' -s, --indent-size Indentation size [4]',
|
||||
' -c, --indent-char Indentation character [" "]',
|
||||
' -t, --indent-with-tabs Indent with tabs, overrides -s and -c',
|
||||
' -e, --eol Character(s) to use as line terminators. (default newline - "\\n")',
|
||||
' -n, --end-with-newline End output with newline'
|
||||
];
|
||||
|
||||
switch (scriptName.split('-').shift()) {
|
||||
case "js":
|
||||
msg.push(' -l, --indent-level Initial indentation level [0]');
|
||||
msg.push(' -p, --preserve-newlines Preserve line-breaks (--no-preserve-newlines disables)');
|
||||
msg.push(' -m, --max-preserve-newlines Number of line-breaks to be preserved in one chunk [10]');
|
||||
msg.push(' -P, --space-in-paren Add padding spaces within paren, ie. f( a, b )');
|
||||
msg.push(' -E, --space-in-empty-paren Add a single space inside empty paren, ie. f( )');
|
||||
msg.push(' -j, --jslint-happy Enable jslint-stricter mode');
|
||||
msg.push(' -a, --space-after-anon-function Add a space before an anonymous function\'s parens, ie. function ()');
|
||||
msg.push(' -b, --brace-style [collapse|expand|end-expand|none] ["collapse"]');
|
||||
msg.push(' -B, --break-chained-methods Break chained method calls across subsequent lines');
|
||||
msg.push(' -k, --keep-array-indentation Preserve array indentation');
|
||||
msg.push(' -x, --unescape-strings Decode printable characters encoded in xNN notation');
|
||||
msg.push(' -w, --wrap-line-length Wrap lines at next opportunity after N characters [0]');
|
||||
msg.push(' -X, --e4x Pass E4X xml literals through untouched');
|
||||
msg.push(' --good-stuff Warm the cockles of Crockford\'s heart');
|
||||
msg.push(' -C, --comma-first Put commas at the beginning of new line instead of end');
|
||||
break;
|
||||
case "html":
|
||||
msg.push(' -b, --brace-style [collapse|expand|end-expand] ["collapse"]');
|
||||
msg.push(' -I, --indent-inner-html Indent body and head sections. Default is false.');
|
||||
msg.push(' -S, --indent-scripts [keep|separate|normal] ["normal"]');
|
||||
msg.push(' -w, --wrap-line-length Wrap lines at next opportunity after N characters [0]');
|
||||
msg.push(' -A, --wrap-attributes Wrap html tag attributes to new lines [auto|force] ["auto"]');
|
||||
msg.push(' -i, --wrap-attributes-indent-size Indent wrapped tags to after N characters [indent-level]');
|
||||
msg.push(' -p, --preserve-newlines Preserve line-breaks (--no-preserve-newlines disables)');
|
||||
msg.push(' -m, --max-preserve-newlines Number of line-breaks to be preserved in one chunk [10]');
|
||||
msg.push(' -U, --unformatted List of tags (defaults to inline) that should not be reformatted');
|
||||
msg.push(' -E, --extra_liners List of tags (defaults to [head,body,/html] that should have an extra newline');
|
||||
break;
|
||||
case "css":
|
||||
msg.push(' -L, --selector-separator-newline Add a newline between multiple selectors.')
|
||||
msg.push(' -N, --newline-between-rules Add a newline between CSS rules.')
|
||||
}
|
||||
|
||||
if (err) {
|
||||
msg.push(err);
|
||||
msg.push('');
|
||||
console.error(msg.join('\n'));
|
||||
} else {
|
||||
console.log(msg.join('\n'));
|
||||
}
|
||||
}
|
||||
|
||||
// main iterator, {cfg} passed as thisArg of forEach call
|
||||
|
||||
function processInputSync(filepath) {
|
||||
var data = '',
|
||||
config = this.cfg,
|
||||
outfile = config.outfile,
|
||||
input;
|
||||
|
||||
// -o passed with no value overwrites
|
||||
if (outfile === true || config.replace) {
|
||||
outfile = filepath;
|
||||
}
|
||||
|
||||
if (filepath === '-') {
|
||||
input = process.stdin;
|
||||
input.resume();
|
||||
input.setEncoding('utf8');
|
||||
|
||||
input.on('data', function(chunk) {
|
||||
data += chunk;
|
||||
});
|
||||
|
||||
input.on('end', function() {
|
||||
makePretty(data, config, outfile, writePretty);
|
||||
});
|
||||
} else {
|
||||
var dir = path.dirname(outfile);
|
||||
mkdirp.sync(dir);
|
||||
data = fs.readFileSync(filepath, 'utf8');
|
||||
makePretty(data, config, outfile, writePretty);
|
||||
}
|
||||
}
|
||||
|
||||
function makePretty(code, config, outfile, callback) {
|
||||
try {
|
||||
var fileType = getOutputType(outfile, config.type);
|
||||
var pretty = beautify[fileType](code, config);
|
||||
|
||||
callback(null, pretty, outfile, config);
|
||||
} catch (ex) {
|
||||
callback(ex);
|
||||
}
|
||||
}
|
||||
|
||||
function writePretty(err, pretty, outfile, config) {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
if (outfile) {
|
||||
if (isFileDifferent(outfile, pretty)) {
|
||||
try {
|
||||
fs.writeFileSync(outfile, pretty, 'utf8');
|
||||
logToStdout('beautified ' + path.relative(process.cwd(), outfile), config);
|
||||
} catch (ex) {
|
||||
onOutputError(ex);
|
||||
}
|
||||
} else {
|
||||
logToStdout('beautified ' + path.relative(process.cwd(), outfile) + ' - unchanged', config);
|
||||
}
|
||||
} else {
|
||||
process.stdout.write(pretty);
|
||||
}
|
||||
}
|
||||
|
||||
function isFileDifferent(filePath, expected) {
|
||||
try {
|
||||
return fs.readFileSync(filePath, 'utf8') !== expected;
|
||||
} catch (ex) {
|
||||
// failing to read is the same as different
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// workaround the fact that nopt.clean doesn't return the object passed in :P
|
||||
|
||||
function cleanOptions(data, types) {
|
||||
nopt.clean(data, types);
|
||||
return data;
|
||||
}
|
||||
|
||||
// error handler for output stream that swallows errors silently,
|
||||
// allowing the loop to continue over unwritable files.
|
||||
|
||||
function onOutputError(err) {
|
||||
if (err.code === 'EACCES') {
|
||||
console.error(err.path + " is not writable. Skipping!");
|
||||
} else {
|
||||
console.error(err);
|
||||
process.exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
// turn "--foo_bar" into "foo-bar"
|
||||
|
||||
function dasherizeFlag(str) {
|
||||
return str.replace(/^\-+/, '').replace(/_/g, '-');
|
||||
}
|
||||
|
||||
// translate weird python underscored keys into dashed argv,
|
||||
// avoiding single character aliases.
|
||||
|
||||
function dasherizeShorthands(hash) {
|
||||
// operate in-place
|
||||
Object.keys(hash).forEach(function(key) {
|
||||
// each key value is an array
|
||||
var val = hash[key][0];
|
||||
// only dasherize one-character shorthands
|
||||
if (key.length === 1 && val.indexOf('_') > -1) {
|
||||
hash[dasherizeFlag(val)] = val;
|
||||
}
|
||||
});
|
||||
|
||||
return hash;
|
||||
}
|
||||
|
||||
function getOutputType(outfile, configType) {
|
||||
if (outfile && /\.(js|css|html)$/.test(outfile)) {
|
||||
return outfile.split('.').pop();
|
||||
}
|
||||
return configType;
|
||||
}
|
||||
|
||||
function getScriptName() {
|
||||
return path.basename(process.argv[1]);
|
||||
}
|
||||
|
||||
function checkType(parsed) {
|
||||
var scriptType = getScriptName().split('-').shift();
|
||||
debug("executable type:", scriptType);
|
||||
|
||||
var parsedType = parsed.type;
|
||||
debug("parsed type:", parsedType);
|
||||
|
||||
if (!parsedType) {
|
||||
debug("type defaulted:", scriptType);
|
||||
parsed.type = scriptType;
|
||||
}
|
||||
}
|
||||
|
||||
function checkFiles(parsed) {
|
||||
var argv = parsed.argv;
|
||||
|
||||
if (!parsed.files) {
|
||||
parsed.files = [];
|
||||
} else {
|
||||
if (argv.cooked.indexOf('-') > -1) {
|
||||
// strip stdin path eagerly added by nopt in '-f -' case
|
||||
parsed.files.some(removeDashedPath);
|
||||
}
|
||||
}
|
||||
|
||||
if (argv.remain.length) {
|
||||
// assume any remaining args are files
|
||||
argv.remain.forEach(function(f) {
|
||||
parsed.files.push(path.resolve(f));
|
||||
});
|
||||
}
|
||||
|
||||
if ('string' === typeof parsed.outfile && !parsed.files.length) {
|
||||
// use outfile as input when no other files passed in args
|
||||
parsed.files.push(parsed.outfile);
|
||||
// operation is now an implicit overwrite
|
||||
parsed.replace = true;
|
||||
}
|
||||
|
||||
if (argv.original.indexOf('-') > -1) {
|
||||
// ensure '-' without '-f' still consumes stdin
|
||||
parsed.files.push('-');
|
||||
}
|
||||
|
||||
if (!parsed.files.length) {
|
||||
throw 'Must define at least one file.';
|
||||
}
|
||||
debug('files.length ' + parsed.files.length);
|
||||
|
||||
parsed.files.forEach(testFilePath);
|
||||
|
||||
return parsed;
|
||||
}
|
||||
|
||||
function removeDashedPath(filepath, i, arr) {
|
||||
var found = filepath.lastIndexOf('-') === (filepath.length - 1);
|
||||
if (found) {
|
||||
arr.splice(i, 1);
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
function testFilePath(filepath) {
|
||||
try {
|
||||
if (filepath !== "-") {
|
||||
fs.statSync(filepath);
|
||||
}
|
||||
} catch (err) {
|
||||
throw 'Unable to open path "' + filepath + '"';
|
||||
}
|
||||
}
|
||||
|
||||
function logToStdout(str, config) {
|
||||
if (typeof config.quiet === "undefined" || !config.quiet) {
|
||||
console.log(str);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,103 @@
|
|||
//
|
||||
// simple unpacker/deobfuscator for scripts messed up with javascriptobfuscator.com
|
||||
// written by Einar Lielmanis <einar@jsbeautifier.org>
|
||||
//
|
||||
// usage:
|
||||
//
|
||||
// if (JavascriptObfuscator.detect(some_string)) {
|
||||
// var unpacked = JavascriptObfuscator.unpack(some_string);
|
||||
// }
|
||||
//
|
||||
//
|
||||
|
||||
var JavascriptObfuscator = {
|
||||
detect: function (str) {
|
||||
return /^var _0x[a-f0-9]+ ?\= ?\[/.test(str);
|
||||
},
|
||||
|
||||
unpack: function (str) {
|
||||
if (JavascriptObfuscator.detect(str)) {
|
||||
var matches = /var (_0x[a-f\d]+) ?\= ?\[(.*?)\];/.exec(str);
|
||||
if (matches) {
|
||||
var var_name = matches[1];
|
||||
var strings = JavascriptObfuscator._smart_split(matches[2]);
|
||||
str = str.substring(matches[0].length);
|
||||
for (var k in strings) {
|
||||
str = str.replace(new RegExp(var_name + '\\[' + k + '\\]', 'g'),
|
||||
JavascriptObfuscator._fix_quotes(JavascriptObfuscator._unescape(strings[k])));
|
||||
}
|
||||
}
|
||||
}
|
||||
return str;
|
||||
},
|
||||
|
||||
_fix_quotes: function(str) {
|
||||
var matches = /^"(.*)"$/.exec(str);
|
||||
if (matches) {
|
||||
str = matches[1];
|
||||
str = "'" + str.replace(/'/g, "\\'") + "'";
|
||||
}
|
||||
return str;
|
||||
},
|
||||
|
||||
_smart_split: function(str) {
|
||||
var strings = [];
|
||||
var pos = 0;
|
||||
while (pos < str.length) {
|
||||
if (str.charAt(pos) == '"') {
|
||||
// new word
|
||||
var word = '';
|
||||
pos += 1;
|
||||
while (pos < str.length) {
|
||||
if (str.charAt(pos) == '"') {
|
||||
break;
|
||||
}
|
||||
if (str.charAt(pos) == '\\') {
|
||||
word += '\\';
|
||||
pos++;
|
||||
}
|
||||
word += str.charAt(pos);
|
||||
pos++;
|
||||
}
|
||||
strings.push('"' + word + '"');
|
||||
}
|
||||
pos += 1;
|
||||
}
|
||||
return strings;
|
||||
},
|
||||
|
||||
|
||||
_unescape: function (str) {
|
||||
// inefficient if used repeatedly or on small strings, but wonderful on single large chunk of text
|
||||
for (var i = 32; i < 128; i++) {
|
||||
str = str.replace(new RegExp('\\\\x' + i.toString(16), 'ig'), String.fromCharCode(i));
|
||||
}
|
||||
str = str.replace(/\\x09/g, "\t");
|
||||
return str;
|
||||
},
|
||||
|
||||
run_tests: function (sanity_test) {
|
||||
var t = sanity_test || new SanityTest();
|
||||
|
||||
t.test_function(JavascriptObfuscator._smart_split, "JavascriptObfuscator._smart_split");
|
||||
t.expect('', []);
|
||||
t.expect('"a", "b"', ['"a"', '"b"']);
|
||||
t.expect('"aaa","bbbb"', ['"aaa"', '"bbbb"']);
|
||||
t.expect('"a", "b\\\""', ['"a"', '"b\\\""']);
|
||||
t.test_function(JavascriptObfuscator._unescape, 'JavascriptObfuscator._unescape');
|
||||
t.expect('\\x40', '@');
|
||||
t.expect('\\x10', '\\x10');
|
||||
t.expect('\\x1', '\\x1');
|
||||
t.expect("\\x61\\x62\\x22\\x63\\x64", 'ab"cd');
|
||||
t.test_function(JavascriptObfuscator.detect, 'JavascriptObfuscator.detect');
|
||||
t.expect('', false);
|
||||
t.expect('abcd', false);
|
||||
t.expect('var _0xaaaa', false);
|
||||
t.expect('var _0xaaaa = ["a", "b"]', true);
|
||||
t.expect('var _0xaaaa=["a", "b"]', true);
|
||||
t.expect('var _0x1234=["a","b"]', true);
|
||||
return t;
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
|
@ -0,0 +1,90 @@
|
|||
//
|
||||
// simple unpacker/deobfuscator for scripts messed up with myobfuscate.com
|
||||
// You really don't want to obfuscate your scripts there: they're tracking
|
||||
// your unpackings, your script gets turned into something like this,
|
||||
// as of 2011-04-25:
|
||||
/*
|
||||
|
||||
var _escape = 'your_script_escaped';
|
||||
var _111 = document.createElement('script');
|
||||
_111.src = 'http://api.www.myobfuscate.com/?getsrc=ok' +
|
||||
'&ref=' + encodeURIComponent(document.referrer) +
|
||||
'&url=' + encodeURIComponent(document.URL);
|
||||
var 000 = document.getElementsByTagName('head')[0];
|
||||
000.appendChild(_111);
|
||||
document.write(unescape(_escape));
|
||||
|
||||
*/
|
||||
//
|
||||
// written by Einar Lielmanis <einar@jsbeautifier.org>
|
||||
//
|
||||
// usage:
|
||||
//
|
||||
// if (MyObfuscate.detect(some_string)) {
|
||||
// var unpacked = MyObfuscate.unpack(some_string);
|
||||
// }
|
||||
//
|
||||
//
|
||||
|
||||
var MyObfuscate = {
|
||||
detect: function (str) {
|
||||
if (/^var _?[0O1lI]{3}\=('|\[).*\)\)\);/.test(str)) {
|
||||
return true;
|
||||
}
|
||||
if (/^function _?[0O1lI]{3}\(_/.test(str) && /eval\(/.test(str)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
},
|
||||
|
||||
unpack: function (str) {
|
||||
if (MyObfuscate.detect(str)) {
|
||||
var __eval = eval;
|
||||
try {
|
||||
eval = function (unpacked) {
|
||||
if (MyObfuscate.starts_with(unpacked, 'var _escape')) {
|
||||
// fetch the urlencoded stuff from the script,
|
||||
var matches = /'([^']*)'/.exec(unpacked);
|
||||
var unescaped = unescape(matches[1]);
|
||||
if (MyObfuscate.starts_with(unescaped, '<script>')) {
|
||||
unescaped = unescaped.substr(8, unescaped.length - 8);
|
||||
}
|
||||
if (MyObfuscate.ends_with(unescaped, '</script>')) {
|
||||
unescaped = unescaped.substr(0, unescaped.length - 9);
|
||||
}
|
||||
unpacked = unescaped;
|
||||
}
|
||||
// throw to terminate the script
|
||||
unpacked = "// Unpacker warning: be careful when using myobfuscate.com for your projects:\n" +
|
||||
"// scripts obfuscated by the free online version may call back home.\n" +
|
||||
"\n//\n" + unpacked;
|
||||
throw unpacked;
|
||||
};
|
||||
__eval(str); // should throw
|
||||
} catch (e) {
|
||||
// well, it failed. we'll just return the original, instead of crashing on user.
|
||||
if (typeof e === "string") {
|
||||
str = e;
|
||||
}
|
||||
}
|
||||
eval = __eval;
|
||||
}
|
||||
return str;
|
||||
},
|
||||
|
||||
starts_with: function (str, what) {
|
||||
return str.substr(0, what.length) === what;
|
||||
},
|
||||
|
||||
ends_with: function (str, what) {
|
||||
return str.substr(str.length - what.length, what.length) === what;
|
||||
},
|
||||
|
||||
run_tests: function (sanity_test) {
|
||||
var t = sanity_test || new SanityTest();
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
|
@ -0,0 +1,80 @@
|
|||
//
|
||||
// Unpacker for Dean Edward's p.a.c.k.e.r, a part of javascript beautifier
|
||||
// written by Einar Lielmanis <einar@jsbeautifier.org>
|
||||
//
|
||||
// Coincidentally, it can defeat a couple of other eval-based compressors.
|
||||
//
|
||||
// usage:
|
||||
//
|
||||
// if (P_A_C_K_E_R.detect(some_string)) {
|
||||
// var unpacked = P_A_C_K_E_R.unpack(some_string);
|
||||
// }
|
||||
//
|
||||
//
|
||||
|
||||
var P_A_C_K_E_R = {
|
||||
detect: function (str) {
|
||||
return (P_A_C_K_E_R.get_chunks(str).length > 0);
|
||||
},
|
||||
|
||||
get_chunks: function(str) {
|
||||
var chunks = str.match(/eval\(\(?function\(.*?(,0,\{\}\)\)|split\('\|'\)\)\))($|\n)/g);
|
||||
return chunks ? chunks : [];
|
||||
},
|
||||
|
||||
unpack: function (str) {
|
||||
var chunks = P_A_C_K_E_R.get_chunks(str),
|
||||
chunk;
|
||||
for(var i = 0; i < chunks.length; i++) {
|
||||
chunk = chunks[i].replace(/\n$/, '');
|
||||
str = str.split(chunk).join( P_A_C_K_E_R.unpack_chunk(chunk) );
|
||||
}
|
||||
return str;
|
||||
},
|
||||
|
||||
unpack_chunk: function (str) {
|
||||
var unpacked_source = '';
|
||||
var __eval = eval;
|
||||
if (P_A_C_K_E_R.detect(str)) {
|
||||
try {
|
||||
eval = function (s) { unpacked_source += s; return unpacked_source; };
|
||||
__eval(str);
|
||||
if (typeof unpacked_source == 'string' && unpacked_source) {
|
||||
str = unpacked_source;
|
||||
}
|
||||
} catch (e) {
|
||||
// well, it failed. we'll just return the original, instead of crashing on user.
|
||||
}
|
||||
}
|
||||
eval = __eval;
|
||||
return str;
|
||||
},
|
||||
|
||||
run_tests: function (sanity_test) {
|
||||
var t = sanity_test || new SanityTest(),
|
||||
|
||||
pk1 = "eval(function(p,a,c,k,e,r){e=String;if(!''.replace(/^/,String)){while(c--)r[c]=k[c]||c;k=[function(e){return r[e]}];e=function(){return'\\\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\\\b'+e(c)+'\\\\b','g'),k[c]);return p}('0 2=1',3,3,'var||a'.split('|'),0,{}))",
|
||||
unpk1 = 'var a=1',
|
||||
pk2 = "eval(function(p,a,c,k,e,r){e=String;if(!''.replace(/^/,String)){while(c--)r[c]=k[c]||c;k=[function(e){return r[e]}];e=function(){return'\\\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\\\b'+e(c)+'\\\\b','g'),k[c]);return p}('0 2=1',3,3,'foo||b'.split('|'),0,{}))",
|
||||
unpk2 = 'foo b=1',
|
||||
pk_broken = "eval(function(p,a,c,k,e,r){BORKBORK;if(!''.replace(/^/,String)){while(c--)r[c]=k[c]||c;k=[function(e){return r[e]}];e=function(){return'\\\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\\\b'+e(c)+'\\\\b','g'),k[c]);return p}('0 2=1',3,3,'var||a'.split('|'),0,{}))";
|
||||
pk3 = "eval(function(p,a,c,k,e,r){e=String;if(!''.replace(/^/,String)){while(c--)r[c]=k[c]||c;k=[function(e){return r[e]}];e=function(){return'\\\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\\\b'+e(c)+'\\\\b','g'),k[c]);return p}('0 2=1{}))',3,3,'var||a'.split('|'),0,{}))",
|
||||
unpk3 = 'var a=1{}))',
|
||||
|
||||
t.test_function(P_A_C_K_E_R.detect, "P_A_C_K_E_R.detect");
|
||||
t.expect('', false);
|
||||
t.expect('var a = b', false);
|
||||
t.test_function(P_A_C_K_E_R.unpack, "P_A_C_K_E_R.unpack");
|
||||
t.expect(pk_broken, pk_broken);
|
||||
t.expect(pk1, unpk1);
|
||||
t.expect(pk2, unpk2);
|
||||
t.expect(pk3, unpk3);
|
||||
|
||||
var filler = '\nfiller\n';
|
||||
t.expect(filler + pk1 + "\n" + pk_broken + filler + pk2 + filler, filler + unpk1 + "\n" + pk_broken + filler + unpk2 + filler);
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
|
@ -0,0 +1,73 @@
|
|||
/*global unescape */
|
||||
/*jshint curly: false, scripturl: true */
|
||||
//
|
||||
// trivial bookmarklet/escaped script detector for the javascript beautifier
|
||||
// written by Einar Lielmanis <einar@jsbeautifier.org>
|
||||
//
|
||||
// usage:
|
||||
//
|
||||
// if (Urlencoded.detect(some_string)) {
|
||||
// var unpacked = Urlencoded.unpack(some_string);
|
||||
// }
|
||||
//
|
||||
//
|
||||
|
||||
var isNode = (typeof module !== 'undefined' && module.exports);
|
||||
if (isNode) {
|
||||
var SanityTest = require(__dirname + '/../../test/sanitytest');
|
||||
}
|
||||
|
||||
var Urlencoded = {
|
||||
detect: function (str) {
|
||||
// the fact that script doesn't contain any space, but has %20 instead
|
||||
// should be sufficient check for now.
|
||||
if (str.indexOf(' ') == -1) {
|
||||
if (str.indexOf('%2') != -1) return true;
|
||||
if (str.replace(/[^%]+/g, '').length > 3) return true;
|
||||
}
|
||||
return false;
|
||||
},
|
||||
|
||||
unpack: function (str) {
|
||||
if (Urlencoded.detect(str)) {
|
||||
if (str.indexOf('%2B') != -1 || str.indexOf('%2b') != -1) {
|
||||
// "+" escaped as "%2B"
|
||||
return unescape(str.replace(/\+/g, '%20'));
|
||||
} else {
|
||||
return unescape(str);
|
||||
}
|
||||
}
|
||||
return str;
|
||||
},
|
||||
|
||||
|
||||
|
||||
run_tests: function (sanity_test) {
|
||||
var t = sanity_test || new SanityTest();
|
||||
t.test_function(Urlencoded.detect, "Urlencoded.detect");
|
||||
t.expect('', false);
|
||||
t.expect('var a = b', false);
|
||||
t.expect('var%20a+=+b', true);
|
||||
t.expect('var%20a=b', true);
|
||||
t.expect('var%20%21%22', true);
|
||||
t.expect('javascript:(function(){var%20whatever={init:function(){alert(%22a%22+%22b%22)}};whatever.init()})();', true);
|
||||
t.test_function(Urlencoded.unpack, 'Urlencoded.unpack');
|
||||
|
||||
t.expect('javascript:(function(){var%20whatever={init:function(){alert(%22a%22+%22b%22)}};whatever.init()})();',
|
||||
'javascript:(function(){var whatever={init:function(){alert("a"+"b")}};whatever.init()})();'
|
||||
);
|
||||
t.expect('', '');
|
||||
t.expect('abcd', 'abcd');
|
||||
t.expect('var a = b', 'var a = b');
|
||||
t.expect('var%20a=b', 'var a=b');
|
||||
t.expect('var%20a=b+1', 'var a=b+1');
|
||||
t.expect('var%20a=b%2b1', 'var a=b+1');
|
||||
return t;
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
if (isNode) {
|
||||
module.exports = Urlencoded;
|
||||
}
|
||||
|
|
@ -0,0 +1,63 @@
|
|||
/*global js_beautify: true */
|
||||
/*jshint node:true */
|
||||
|
||||
var requirejs = require('requirejs'),
|
||||
SanityTest = require('./sanitytest'),
|
||||
Urlencoded = require('../lib/unpackers/urlencode_unpacker'),
|
||||
run_javascript_tests = require('./beautify-javascript-tests').run_javascript_tests,
|
||||
run_css_tests = require('./beautify-css-tests').run_css_tests,
|
||||
run_html_tests = require('./beautify-html-tests').run_html_tests;
|
||||
|
||||
requirejs.config({
|
||||
paths: {
|
||||
'beautify': "..",
|
||||
'beautify-lib': "../lib"
|
||||
}
|
||||
});
|
||||
|
||||
function amd_beautifier_index_tests(name, test_runner) {
|
||||
console.log('Testing ' + name + ' with node.js Require.js (index file)...');
|
||||
var results = new SanityTest();
|
||||
var beautify = requirejs('beautify/index');
|
||||
|
||||
test_runner(
|
||||
results,
|
||||
Urlencoded,
|
||||
beautify.js,
|
||||
beautify.html,
|
||||
beautify.css);
|
||||
|
||||
console.log(results.results_raw());
|
||||
return results;
|
||||
}
|
||||
|
||||
function amd_beautifier_tests(name, test_runner) {
|
||||
console.log('Testing ' + name + ' with node.js Require.js (separate file)...');
|
||||
var results = new SanityTest();
|
||||
var js_beautify = requirejs('beautify-lib/beautify'),
|
||||
css_beautify = requirejs('beautify-lib/beautify-css'),
|
||||
html_beautify = requirejs('beautify-lib/beautify-html');
|
||||
|
||||
test_runner(
|
||||
results,
|
||||
Urlencoded,
|
||||
js_beautify.js_beautify,
|
||||
html_beautify.html_beautify,
|
||||
css_beautify.css_beautify);
|
||||
|
||||
console.log(results.results_raw());
|
||||
return results;
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (require.main === module) {
|
||||
process.exit(
|
||||
amd_beautifier_tests('js-beautifier', run_javascript_tests).get_exitcode() +
|
||||
amd_beautifier_index_tests('js-beautifier', run_javascript_tests).get_exitcode() +
|
||||
amd_beautifier_tests('cs-beautifier', run_css_tests).get_exitcode() +
|
||||
amd_beautifier_index_tests('css-beautifier', run_css_tests).get_exitcode() +
|
||||
amd_beautifier_tests('html-beautifier', run_html_tests).get_exitcode() +
|
||||
amd_beautifier_index_tests('html-beautifier', run_html_tests).get_exitcode()
|
||||
);
|
||||
}
|
||||
|
|
@ -0,0 +1,335 @@
|
|||
/*global js_beautify: true */
|
||||
|
||||
function run_css_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_beautify)
|
||||
{
|
||||
|
||||
var opts = {
|
||||
indent_size: 4,
|
||||
indent_char: ' ',
|
||||
preserve_newlines: true,
|
||||
jslint_happy: false,
|
||||
keep_array_indentation: false,
|
||||
brace_style: 'collapse',
|
||||
space_before_conditional: true,
|
||||
break_chained_methods: false,
|
||||
selector_separator: '\n',
|
||||
end_with_newline: false,
|
||||
newline_between_rules: true
|
||||
};
|
||||
|
||||
function test_css_beautifier(input)
|
||||
{
|
||||
return css_beautify(input, opts);
|
||||
}
|
||||
|
||||
var sanitytest;
|
||||
|
||||
// test the input on beautifier with the current flag settings
|
||||
// does not check the indentation / surroundings as bt() does
|
||||
function test_fragment(input, expected)
|
||||
{
|
||||
expected = expected || expected === '' ? expected : input;
|
||||
sanitytest.expect(input, expected);
|
||||
// if the expected is different from input, run it again
|
||||
// expected output should be unchanged when run twice.
|
||||
if (expected !== input) {
|
||||
sanitytest.expect(expected, expected);
|
||||
}
|
||||
|
||||
// Everywhere we do newlines, they should be replaced with opts.eol
|
||||
opts.eol = '\r\\n';
|
||||
expected = expected.replace(/[\n]/g, '\r\n');
|
||||
sanitytest.expect(input, expected);
|
||||
input = input.replace(/[\n]/g, '\r\n');
|
||||
sanitytest.expect(input, expected);
|
||||
opts.eol = '\n';
|
||||
}
|
||||
|
||||
// test css
|
||||
function t(input, expectation)
|
||||
{
|
||||
var wrapped_input, wrapped_expectation;
|
||||
|
||||
expectation = expectation || expectation === '' ? expectation : input;
|
||||
sanitytest.test_function(test_css_beautifier, 'css_beautify');
|
||||
test_fragment(input, expectation);
|
||||
}
|
||||
|
||||
function unicode_char(value) {
|
||||
return String.fromCharCode(value)
|
||||
}
|
||||
|
||||
function beautifier_tests()
|
||||
{
|
||||
sanitytest = test_obj;
|
||||
|
||||
t(".tabs {}");
|
||||
|
||||
opts.indent_size = 1;
|
||||
opts.indent_char = '\t';
|
||||
opts.selector_separator_newline = true;
|
||||
opts.end_with_newline = false;
|
||||
opts.newline_between_rules = false;
|
||||
|
||||
// End With Newline - (eof = "\n")
|
||||
opts.end_with_newline = true;
|
||||
test_fragment('', '\n');
|
||||
test_fragment(' .tabs{}', ' .tabs {}\n');
|
||||
test_fragment(' \n\n.tabs{}\n\n\n\n', ' .tabs {}\n');
|
||||
test_fragment('\n');
|
||||
|
||||
// End With Newline - (eof = "")
|
||||
opts.end_with_newline = false;
|
||||
test_fragment('');
|
||||
test_fragment(' .tabs{}', ' .tabs {}');
|
||||
test_fragment(' \n\n.tabs{}\n\n\n\n', ' .tabs {}');
|
||||
test_fragment('\n', '');
|
||||
|
||||
// Empty braces
|
||||
t('.tabs{}', '.tabs {}');
|
||||
t('.tabs { }', '.tabs {}');
|
||||
t('.tabs { }', '.tabs {}');
|
||||
t('.tabs \n{\n \n }', '.tabs {}');
|
||||
|
||||
//
|
||||
t('#cboxOverlay {\n\tbackground: url(images/overlay.png) repeat 0 0;\n\topacity: 0.9;\n\tfilter: alpha(opacity = 90);\n}', '#cboxOverlay {\n\tbackground: url(images/overlay.png) repeat 0 0;\n\topacity: 0.9;\n\tfilter: alpha(opacity=90);\n}');
|
||||
|
||||
// Selector Separator - (separator = " ", separator1 = " ")
|
||||
opts.selector_separator_newline = false;
|
||||
opts.selector_separator = " ";
|
||||
t('#bla, #foo{color:green}', '#bla, #foo {\n\tcolor: green\n}');
|
||||
t('@media print {.tab{}}', '@media print {\n\t.tab {}\n}');
|
||||
t('@media print {.tab,.bat{}}', '@media print {\n\t.tab, .bat {}\n}');
|
||||
t('#bla, #foo{color:black}', '#bla, #foo {\n\tcolor: black\n}');
|
||||
t('a:first-child,a:first-child{color:red;div:first-child,div:hover{color:black;}}', 'a:first-child, a:first-child {\n\tcolor: red;\n\tdiv:first-child, div:hover {\n\t\tcolor: black;\n\t}\n}');
|
||||
|
||||
// Selector Separator - (separator = " ", separator1 = " ")
|
||||
opts.selector_separator_newline = false;
|
||||
opts.selector_separator = " ";
|
||||
t('#bla, #foo{color:green}', '#bla, #foo {\n\tcolor: green\n}');
|
||||
t('@media print {.tab{}}', '@media print {\n\t.tab {}\n}');
|
||||
t('@media print {.tab,.bat{}}', '@media print {\n\t.tab, .bat {}\n}');
|
||||
t('#bla, #foo{color:black}', '#bla, #foo {\n\tcolor: black\n}');
|
||||
t('a:first-child,a:first-child{color:red;div:first-child,div:hover{color:black;}}', 'a:first-child, a:first-child {\n\tcolor: red;\n\tdiv:first-child, div:hover {\n\t\tcolor: black;\n\t}\n}');
|
||||
|
||||
// Selector Separator - (separator = "\n", separator1 = "\n\t")
|
||||
opts.selector_separator_newline = true;
|
||||
opts.selector_separator = " ";
|
||||
t('#bla, #foo{color:green}', '#bla,\n#foo {\n\tcolor: green\n}');
|
||||
t('@media print {.tab{}}', '@media print {\n\t.tab {}\n}');
|
||||
t('@media print {.tab,.bat{}}', '@media print {\n\t.tab,\n\t.bat {}\n}');
|
||||
t('#bla, #foo{color:black}', '#bla,\n#foo {\n\tcolor: black\n}');
|
||||
t('a:first-child,a:first-child{color:red;div:first-child,div:hover{color:black;}}', 'a:first-child,\na:first-child {\n\tcolor: red;\n\tdiv:first-child,\n\tdiv:hover {\n\t\tcolor: black;\n\t}\n}');
|
||||
|
||||
// Selector Separator - (separator = "\n", separator1 = "\n\t")
|
||||
opts.selector_separator_newline = true;
|
||||
opts.selector_separator = " ";
|
||||
t('#bla, #foo{color:green}', '#bla,\n#foo {\n\tcolor: green\n}');
|
||||
t('@media print {.tab{}}', '@media print {\n\t.tab {}\n}');
|
||||
t('@media print {.tab,.bat{}}', '@media print {\n\t.tab,\n\t.bat {}\n}');
|
||||
t('#bla, #foo{color:black}', '#bla,\n#foo {\n\tcolor: black\n}');
|
||||
t('a:first-child,a:first-child{color:red;div:first-child,div:hover{color:black;}}', 'a:first-child,\na:first-child {\n\tcolor: red;\n\tdiv:first-child,\n\tdiv:hover {\n\t\tcolor: black;\n\t}\n}');
|
||||
|
||||
// Newline Between Rules - (separator = "\n")
|
||||
opts.newline_between_rules = true;
|
||||
t('.div {}\n.span {}', '.div {}\n\n.span {}');
|
||||
t('.div{}\n \n.span{}', '.div {}\n\n.span {}');
|
||||
t('.div {} \n \n.span { } \n', '.div {}\n\n.span {}');
|
||||
t('.div {\n \n} \n .span {\n } ', '.div {}\n\n.span {}');
|
||||
t('.selector1 {\n\tmargin: 0; /* This is a comment including an url http://domain.com/path/to/file.ext */\n}\n.div{height:15px;}', '.selector1 {\n\tmargin: 0;\n\t/* This is a comment including an url http://domain.com/path/to/file.ext */\n}\n\n.div {\n\theight: 15px;\n}');
|
||||
t('.tabs{width:10px;//end of line comment\nheight:10px;//another\n}\n.div{height:15px;}', '.tabs {\n\twidth: 10px; //end of line comment\n\theight: 10px; //another\n}\n\n.div {\n\theight: 15px;\n}');
|
||||
t('#foo {\n\tbackground-image: url(foo@2x.png);\n\t@font-face {\n\t\tfont-family: "Bitstream Vera Serif Bold";\n\t\tsrc: url("http://developer.mozilla.org/@api/deki/files/2934/=VeraSeBd.ttf");\n\t}\n}\n.div{height:15px;}', '#foo {\n\tbackground-image: url(foo@2x.png);\n\t@font-face {\n\t\tfont-family: "Bitstream Vera Serif Bold";\n\t\tsrc: url("http://developer.mozilla.org/@api/deki/files/2934/=VeraSeBd.ttf");\n\t}\n}\n\n.div {\n\theight: 15px;\n}');
|
||||
t('@media screen {\n\t#foo:hover {\n\t\tbackground-image: url(foo@2x.png);\n\t}\n\t@font-face {\n\t\tfont-family: "Bitstream Vera Serif Bold";\n\t\tsrc: url("http://developer.mozilla.org/@api/deki/files/2934/=VeraSeBd.ttf");\n\t}\n}\n.div{height:15px;}', '@media screen {\n\t#foo:hover {\n\t\tbackground-image: url(foo@2x.png);\n\t}\n\t@font-face {\n\t\tfont-family: "Bitstream Vera Serif Bold";\n\t\tsrc: url("http://developer.mozilla.org/@api/deki/files/2934/=VeraSeBd.ttf");\n\t}\n}\n\n.div {\n\theight: 15px;\n}');
|
||||
t('@font-face {\n\tfont-family: "Bitstream Vera Serif Bold";\n\tsrc: url("http://developer.mozilla.org/@api/deki/files/2934/=VeraSeBd.ttf");\n}\n@media screen {\n\t#foo:hover {\n\t\tbackground-image: url(foo.png);\n\t}\n\t@media screen and (min-device-pixel-ratio: 2) {\n\t\t@font-face {\n\t\t\tfont-family: "Helvetica Neue"\n\t\t}\n\t\t#foo:hover {\n\t\t\tbackground-image: url(foo@2x.png);\n\t\t}\n\t}\n}', '@font-face {\n\tfont-family: "Bitstream Vera Serif Bold";\n\tsrc: url("http://developer.mozilla.org/@api/deki/files/2934/=VeraSeBd.ttf");\n}\n\n@media screen {\n\t#foo:hover {\n\t\tbackground-image: url(foo.png);\n\t}\n\t@media screen and (min-device-pixel-ratio: 2) {\n\t\t@font-face {\n\t\t\tfont-family: "Helvetica Neue"\n\t\t}\n\t\t#foo:hover {\n\t\t\tbackground-image: url(foo@2x.png);\n\t\t}\n\t}\n}');
|
||||
t('a:first-child{color:red;div:first-child{color:black;}}\n.div{height:15px;}', 'a:first-child {\n\tcolor: red;\n\tdiv:first-child {\n\t\tcolor: black;\n\t}\n}\n\n.div {\n\theight: 15px;\n}');
|
||||
t('a:first-child{color:red;div:not(.peq){color:black;}}\n.div{height:15px;}', 'a:first-child {\n\tcolor: red;\n\tdiv:not(.peq) {\n\t\tcolor: black;\n\t}\n}\n\n.div {\n\theight: 15px;\n}');
|
||||
|
||||
// Newline Between Rules - (separator = "")
|
||||
opts.newline_between_rules = false;
|
||||
t('.div {}\n.span {}');
|
||||
t('.div{}\n \n.span{}', '.div {}\n.span {}');
|
||||
t('.div {} \n \n.span { } \n', '.div {}\n.span {}');
|
||||
t('.div {\n \n} \n .span {\n } ', '.div {}\n.span {}');
|
||||
t('.selector1 {\n\tmargin: 0; /* This is a comment including an url http://domain.com/path/to/file.ext */\n}\n.div{height:15px;}', '.selector1 {\n\tmargin: 0;\n\t/* This is a comment including an url http://domain.com/path/to/file.ext */\n}\n.div {\n\theight: 15px;\n}');
|
||||
t('.tabs{width:10px;//end of line comment\nheight:10px;//another\n}\n.div{height:15px;}', '.tabs {\n\twidth: 10px; //end of line comment\n\theight: 10px; //another\n}\n.div {\n\theight: 15px;\n}');
|
||||
t('#foo {\n\tbackground-image: url(foo@2x.png);\n\t@font-face {\n\t\tfont-family: "Bitstream Vera Serif Bold";\n\t\tsrc: url("http://developer.mozilla.org/@api/deki/files/2934/=VeraSeBd.ttf");\n\t}\n}\n.div{height:15px;}', '#foo {\n\tbackground-image: url(foo@2x.png);\n\t@font-face {\n\t\tfont-family: "Bitstream Vera Serif Bold";\n\t\tsrc: url("http://developer.mozilla.org/@api/deki/files/2934/=VeraSeBd.ttf");\n\t}\n}\n.div {\n\theight: 15px;\n}');
|
||||
t('@media screen {\n\t#foo:hover {\n\t\tbackground-image: url(foo@2x.png);\n\t}\n\t@font-face {\n\t\tfont-family: "Bitstream Vera Serif Bold";\n\t\tsrc: url("http://developer.mozilla.org/@api/deki/files/2934/=VeraSeBd.ttf");\n\t}\n}\n.div{height:15px;}', '@media screen {\n\t#foo:hover {\n\t\tbackground-image: url(foo@2x.png);\n\t}\n\t@font-face {\n\t\tfont-family: "Bitstream Vera Serif Bold";\n\t\tsrc: url("http://developer.mozilla.org/@api/deki/files/2934/=VeraSeBd.ttf");\n\t}\n}\n.div {\n\theight: 15px;\n}');
|
||||
t('@font-face {\n\tfont-family: "Bitstream Vera Serif Bold";\n\tsrc: url("http://developer.mozilla.org/@api/deki/files/2934/=VeraSeBd.ttf");\n}\n@media screen {\n\t#foo:hover {\n\t\tbackground-image: url(foo.png);\n\t}\n\t@media screen and (min-device-pixel-ratio: 2) {\n\t\t@font-face {\n\t\t\tfont-family: "Helvetica Neue"\n\t\t}\n\t\t#foo:hover {\n\t\t\tbackground-image: url(foo@2x.png);\n\t\t}\n\t}\n}');
|
||||
t('a:first-child{color:red;div:first-child{color:black;}}\n.div{height:15px;}', 'a:first-child {\n\tcolor: red;\n\tdiv:first-child {\n\t\tcolor: black;\n\t}\n}\n.div {\n\theight: 15px;\n}');
|
||||
t('a:first-child{color:red;div:not(.peq){color:black;}}\n.div{height:15px;}', 'a:first-child {\n\tcolor: red;\n\tdiv:not(.peq) {\n\t\tcolor: black;\n\t}\n}\n.div {\n\theight: 15px;\n}');
|
||||
|
||||
// Functions braces
|
||||
t('.tabs(){}', '.tabs() {}');
|
||||
t('.tabs (){}', '.tabs () {}');
|
||||
t('.tabs (pa, pa(1,2)), .cols { }', '.tabs (pa, pa(1, 2)),\n.cols {}');
|
||||
t('.tabs(pa, pa(1,2)), .cols { }', '.tabs(pa, pa(1, 2)),\n.cols {}');
|
||||
t('.tabs ( ) { }', '.tabs () {}');
|
||||
t('.tabs( ) { }', '.tabs() {}');
|
||||
t('.tabs (t, t2) \n{\n key: val(p1 ,p2); \n }', '.tabs (t, t2) {\n\tkey: val(p1, p2);\n}');
|
||||
t('.box-shadow(@shadow: 0 1px 3px rgba(0, 0, 0, .25)) {\n\t-webkit-box-shadow: @shadow;\n\t-moz-box-shadow: @shadow;\n\tbox-shadow: @shadow;\n}');
|
||||
|
||||
// Comments
|
||||
t('/* test */');
|
||||
t('.tabs{/* test */}', '.tabs {\n\t/* test */\n}');
|
||||
t('.tabs{/* test */}', '.tabs {\n\t/* test */\n}');
|
||||
t('/* header */.tabs {}', '/* header */\n\n.tabs {}');
|
||||
t('.tabs {\n/* non-header */\nwidth:10px;}', '.tabs {\n\t/* non-header */\n\twidth: 10px;\n}');
|
||||
t('/* header');
|
||||
t('// comment');
|
||||
t('.selector1 {\n\tmargin: 0; /* This is a comment including an url http://domain.com/path/to/file.ext */\n}', '.selector1 {\n\tmargin: 0;\n\t/* This is a comment including an url http://domain.com/path/to/file.ext */\n}');
|
||||
|
||||
// single line comment support (less/sass)
|
||||
t('.tabs{\n// comment\nwidth:10px;\n}', '.tabs {\n\t// comment\n\twidth: 10px;\n}');
|
||||
t('.tabs{// comment\nwidth:10px;\n}', '.tabs {\n\t// comment\n\twidth: 10px;\n}');
|
||||
t('//comment\n.tabs{width:10px;}', '//comment\n.tabs {\n\twidth: 10px;\n}');
|
||||
t('.tabs{//comment\n//2nd single line comment\nwidth:10px;}', '.tabs {\n\t//comment\n\t//2nd single line comment\n\twidth: 10px;\n}');
|
||||
t('.tabs{width:10px;//end of line comment\n}', '.tabs {\n\twidth: 10px; //end of line comment\n}');
|
||||
t('.tabs{width:10px;//end of line comment\nheight:10px;}', '.tabs {\n\twidth: 10px; //end of line comment\n\theight: 10px;\n}');
|
||||
t('.tabs{width:10px;//end of line comment\nheight:10px;//another\n}', '.tabs {\n\twidth: 10px; //end of line comment\n\theight: 10px; //another\n}');
|
||||
|
||||
// Psuedo-classes vs Variables
|
||||
t('@page :first {}');
|
||||
|
||||
// Assume the colon goes with the @name. If we're in LESS, this is required regardless of the at-string.
|
||||
t('@page:first {}', '@page: first {}');
|
||||
t('@page: first {}');
|
||||
|
||||
// SASS/SCSS
|
||||
|
||||
// Basic Interpolation
|
||||
t('p {\n\t$font-size: 12px;\n\t$line-height: 30px;\n\tfont: #{$font-size}/#{$line-height};\n}');
|
||||
t('p.#{$name} {}');
|
||||
t(
|
||||
'@mixin itemPropertiesCoverItem($items, $margin) {\n' +
|
||||
'\twidth: calc((100% - ((#{$items} - 1) * #{$margin}rem)) / #{$items});\n' +
|
||||
'\tmargin: 1.6rem #{$margin}rem 1.6rem 0;\n' +
|
||||
'}');
|
||||
|
||||
//
|
||||
|
||||
// test basic css beautifier
|
||||
t(".tabs {}");
|
||||
t(".tabs{color:red;}", ".tabs {\n\tcolor: red;\n}");
|
||||
t(".tabs{color:rgb(255, 255, 0)}", ".tabs {\n\tcolor: rgb(255, 255, 0)\n}");
|
||||
t(".tabs{background:url('back.jpg')}", ".tabs {\n\tbackground: url('back.jpg')\n}");
|
||||
t("#bla, #foo{color:red}", "#bla,\n#foo {\n\tcolor: red\n}");
|
||||
t("@media print {.tab{}}", "@media print {\n\t.tab {}\n}");
|
||||
t("@media print {.tab{background-image:url(foo@2x.png)}}", "@media print {\n\t.tab {\n\t\tbackground-image: url(foo@2x.png)\n\t}\n}");
|
||||
|
||||
t("a:before {\n" +
|
||||
"\tcontent: 'a{color:black;}\"\"\\'\\'\"\\n\\n\\na{color:black}\';\n" +
|
||||
"}");
|
||||
|
||||
//lead-in whitespace determines base-indent.
|
||||
// lead-in newlines are stripped.
|
||||
t("\n\na, img {padding: 0.2px}", "a,\nimg {\n\tpadding: 0.2px\n}");
|
||||
t(" a, img {padding: 0.2px}", " a,\n img {\n \tpadding: 0.2px\n }");
|
||||
t(" \t \na, img {padding: 0.2px}", " \t a,\n \t img {\n \t \tpadding: 0.2px\n \t }");
|
||||
t("\n\n a, img {padding: 0.2px}", "a,\nimg {\n\tpadding: 0.2px\n}");
|
||||
|
||||
// separate selectors
|
||||
t("#bla, #foo{color:red}", "#bla,\n#foo {\n\tcolor: red\n}");
|
||||
t("a, img {padding: 0.2px}", "a,\nimg {\n\tpadding: 0.2px\n}");
|
||||
|
||||
// block nesting
|
||||
t("#foo {\n\tbackground-image: url(foo@2x.png);\n\t@font-face {\n\t\tfont-family: 'Bitstream Vera Serif Bold';\n\t\tsrc: url('http://developer.mozilla.org/@api/deki/files/2934/=VeraSeBd.ttf');\n\t}\n}");
|
||||
t("@media screen {\n\t#foo:hover {\n\t\tbackground-image: url(foo@2x.png);\n\t}\n\t@font-face {\n\t\tfont-family: 'Bitstream Vera Serif Bold';\n\t\tsrc: url('http://developer.mozilla.org/@api/deki/files/2934/=VeraSeBd.ttf');\n\t}\n}");
|
||||
/*
|
||||
@font-face {
|
||||
font-family: 'Bitstream Vera Serif Bold';
|
||||
src: url('http://developer.mozilla.org/@api/deki/files/2934/=VeraSeBd.ttf');
|
||||
}
|
||||
@media screen {
|
||||
#foo:hover {
|
||||
background-image: url(foo.png);
|
||||
}
|
||||
@media screen and (min-device-pixel-ratio: 2) {
|
||||
@font-face {
|
||||
font-family: 'Helvetica Neue'
|
||||
}
|
||||
#foo:hover {
|
||||
background-image: url(foo@2x.png);
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
t("@font-face {\n\tfont-family: 'Bitstream Vera Serif Bold';\n\tsrc: url('http://developer.mozilla.org/@api/deki/files/2934/=VeraSeBd.ttf');\n}\n@media screen {\n\t#foo:hover {\n\t\tbackground-image: url(foo.png);\n\t}\n\t@media screen and (min-device-pixel-ratio: 2) {\n\t\t@font-face {\n\t\t\tfont-family: 'Helvetica Neue'\n\t\t}\n\t\t#foo:hover {\n\t\t\tbackground-image: url(foo@2x.png);\n\t\t}\n\t}\n}");
|
||||
|
||||
// less-css cases
|
||||
t('.well{@well-bg:@bg-color;@well-fg:@fg-color;}','.well {\n\t@well-bg: @bg-color;\n\t@well-fg: @fg-color;\n}');
|
||||
t('.well {&.active {\nbox-shadow: 0 1px 1px @border-color, 1px 0 1px @border-color;}}',
|
||||
'.well {\n' +
|
||||
'\t&.active {\n' +
|
||||
'\t\tbox-shadow: 0 1px 1px @border-color, 1px 0 1px @border-color;\n' +
|
||||
'\t}\n' +
|
||||
'}');
|
||||
t('a {\n' +
|
||||
'\tcolor: blue;\n' +
|
||||
'\t&:hover {\n' +
|
||||
'\t\tcolor: green;\n' +
|
||||
'\t}\n' +
|
||||
'\t& & &&&.active {\n' +
|
||||
'\t\tcolor: green;\n' +
|
||||
'\t}\n' +
|
||||
'}');
|
||||
|
||||
// Not sure if this is sensible
|
||||
// but I believe it is correct to not remove the space in "&: hover".
|
||||
t('a {\n' +
|
||||
'\t&: hover {\n' +
|
||||
'\t\tcolor: green;\n' +
|
||||
'\t}\n' +
|
||||
'}');
|
||||
|
||||
// import
|
||||
t('@import "test";');
|
||||
|
||||
// don't break nested pseudo-classes
|
||||
t("a:first-child{color:red;div:first-child{color:black;}}",
|
||||
"a:first-child {\n\tcolor: red;\n\tdiv:first-child {\n\t\tcolor: black;\n\t}\n}");
|
||||
|
||||
// handle SASS/LESS parent reference
|
||||
t("div{&:first-letter {text-transform: uppercase;}}",
|
||||
"div {\n\t&:first-letter {\n\t\ttext-transform: uppercase;\n\t}\n}");
|
||||
|
||||
//nested modifiers (&:hover etc)
|
||||
t(".tabs{&:hover{width:10px;}}", ".tabs {\n\t&:hover {\n\t\twidth: 10px;\n\t}\n}");
|
||||
t(".tabs{&.big{width:10px;}}", ".tabs {\n\t&.big {\n\t\twidth: 10px;\n\t}\n}");
|
||||
t(".tabs{&>big{width:10px;}}", ".tabs {\n\t&>big {\n\t\twidth: 10px;\n\t}\n}");
|
||||
t(".tabs{&+.big{width:10px;}}", ".tabs {\n\t&+.big {\n\t\twidth: 10px;\n\t}\n}");
|
||||
|
||||
//nested rules
|
||||
t(".tabs{.child{width:10px;}}", ".tabs {\n\t.child {\n\t\twidth: 10px;\n\t}\n}");
|
||||
|
||||
//variables
|
||||
t("@myvar:10px;.tabs{width:10px;}", "@myvar: 10px;\n.tabs {\n\twidth: 10px;\n}");
|
||||
t("@myvar:10px; .tabs{width:10px;}", "@myvar: 10px;\n.tabs {\n\twidth: 10px;\n}");
|
||||
|
||||
// test options
|
||||
opts.indent_size = 2;
|
||||
opts.indent_char = ' ';
|
||||
opts.selector_separator_newline = false;
|
||||
|
||||
// pseudo-classes and pseudo-elements
|
||||
t("#foo:hover {\n background-image: url(foo@2x.png)\n}");
|
||||
t("#foo *:hover {\n color: purple\n}");
|
||||
t("::selection {\n color: #ff0000;\n}");
|
||||
|
||||
// TODO: don't break nested pseduo-classes
|
||||
t("@media screen {.tab,.bat:hover {color:red}}", "@media screen {\n .tab, .bat:hover {\n color: red\n }\n}");
|
||||
|
||||
// particular edge case with braces and semicolons inside tags that allows custom text
|
||||
t("a:not(\"foobar\\\";{}omg\"){\ncontent: 'example\\';{} text';\ncontent: \"example\\\";{} text\";}",
|
||||
"a:not(\"foobar\\\";{}omg\") {\n content: 'example\\';{} text';\n content: \"example\\\";{} text\";\n}");
|
||||
|
||||
// may not eat the space before "["
|
||||
t('html.js [data-custom="123"] {\n opacity: 1.00;\n}');
|
||||
t('html.js *[data-custom="123"] {\n opacity: 1.00;\n}');
|
||||
}
|
||||
|
||||
beautifier_tests();
|
||||
}
|
||||
|
||||
if (typeof exports !== "undefined") {
|
||||
exports.run_css_tests = run_css_tests;
|
||||
}
|
||||
|
|
@ -0,0 +1,736 @@
|
|||
/*global js_beautify: true */
|
||||
|
||||
function run_html_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_beautify)
|
||||
{
|
||||
|
||||
var opts = {
|
||||
indent_size: 4,
|
||||
indent_char: ' ',
|
||||
preserve_newlines: true,
|
||||
jslint_happy: false,
|
||||
keep_array_indentation: false,
|
||||
brace_style: 'collapse',
|
||||
space_before_conditional: true,
|
||||
break_chained_methods: false,
|
||||
selector_separator: '\n',
|
||||
end_with_newline: false
|
||||
};
|
||||
|
||||
function test_html_beautifier(input)
|
||||
{
|
||||
return html_beautify(input, opts);
|
||||
}
|
||||
|
||||
var sanitytest;
|
||||
|
||||
// test the input on beautifier with the current flag settings
|
||||
// does not check the indentation / surroundings as bt() does
|
||||
function test_fragment(input, expected)
|
||||
{
|
||||
expected = expected || expected === '' ? expected : input;
|
||||
sanitytest.expect(input, expected);
|
||||
// if the expected is different from input, run it again
|
||||
// expected output should be unchanged when run twice.
|
||||
if (expected !== input) {
|
||||
sanitytest.expect(expected, expected);
|
||||
}
|
||||
|
||||
// Everywhere we do newlines, they should be replaced with opts.eol
|
||||
opts.eol = '\r\n';
|
||||
expected = expected.replace(/[\n]/g, '\r\n');
|
||||
sanitytest.expect(input, expected);
|
||||
input = input.replace(/[\n]/g, '\r\n');
|
||||
sanitytest.expect(input, expected);
|
||||
opts.eol = '\n';
|
||||
}
|
||||
|
||||
// test html
|
||||
function bth(input, expectation)
|
||||
{
|
||||
var wrapped_input, wrapped_expectation, field_input, field_expectation;
|
||||
|
||||
expectation = expectation || expectation === '' ? expectation : input;
|
||||
sanitytest.test_function(test_html_beautifier, 'html_beautify');
|
||||
test_fragment(input, expectation);
|
||||
|
||||
if (opts.indent_size === 4 && input) {
|
||||
wrapped_input = '<div>\n' + input.replace(/^(.+)$/mg, ' $1') + '\n <span>inline</span>\n</div>';
|
||||
wrapped_expectation = '<div>\n' + expectation.replace(/^(.+)$/mg, ' $1') + '\n <span>inline</span>\n</div>';
|
||||
test_fragment(wrapped_input, wrapped_expectation);
|
||||
}
|
||||
}
|
||||
|
||||
function unicode_char(value) {
|
||||
return String.fromCharCode(value)
|
||||
}
|
||||
|
||||
function beautifier_tests()
|
||||
{
|
||||
sanitytest = test_obj;
|
||||
|
||||
bth('');
|
||||
|
||||
opts.indent_size = 4;
|
||||
opts.indent_char = ' ';
|
||||
opts.indent_with_tabs = false;
|
||||
opts.preserve_newlines = true;
|
||||
opts.jslint_happy = false;
|
||||
opts.keep_array_indentation = false;
|
||||
opts.brace_style = 'collapse';
|
||||
opts.extra_liners = ['html', 'head', '/html'];
|
||||
|
||||
// End With Newline - (eof = "\n")
|
||||
opts.end_with_newline = true;
|
||||
test_fragment('', '\n');
|
||||
test_fragment('<div></div>', '<div></div>\n');
|
||||
test_fragment('\n');
|
||||
|
||||
// End With Newline - (eof = "")
|
||||
opts.end_with_newline = false;
|
||||
test_fragment('');
|
||||
test_fragment('<div></div>');
|
||||
test_fragment('\n', '');
|
||||
|
||||
|
||||
// Custom Extra Liners (empty) - ()
|
||||
opts.extra_liners = [];
|
||||
test_fragment('<html><head><meta></head><body><div><p>x</p></div></body></html>', '<html>\n<head>\n <meta>\n</head>\n<body>\n <div>\n <p>x</p>\n </div>\n</body>\n</html>');
|
||||
|
||||
|
||||
// Custom Extra Liners (default) - ()
|
||||
opts.extra_liners = null;
|
||||
test_fragment('<html><head></head><body></body></html>', '<html>\n\n<head></head>\n\n<body></body>\n\n</html>');
|
||||
|
||||
|
||||
// Custom Extra Liners (p, string) - ()
|
||||
opts.extra_liners = 'p,/p';
|
||||
test_fragment('<html><head><meta></head><body><div><p>x</p></div></body></html>', '<html>\n<head>\n <meta>\n</head>\n<body>\n <div>\n\n <p>x\n\n </p>\n </div>\n</body>\n</html>');
|
||||
|
||||
|
||||
// Custom Extra Liners (p) - ()
|
||||
opts.extra_liners = ['p', '/p'];
|
||||
test_fragment('<html><head><meta></head><body><div><p>x</p></div></body></html>', '<html>\n<head>\n <meta>\n</head>\n<body>\n <div>\n\n <p>x\n\n </p>\n </div>\n</body>\n</html>');
|
||||
|
||||
|
||||
// Attribute Wrap - (eof = "\n", indent_attr = " ", over80 = "\n")
|
||||
opts.wrap_attributes = 'force';
|
||||
test_fragment('<div attr0 attr1="123" data-attr2="hello t here">This is some text</div>', '<div attr0\n attr1="123"\n data-attr2="hello t here">This is some text</div>');
|
||||
test_fragment('<div lookatthissuperduperlongattributenamewhoahcrazy0="true" attr0 attr1="123" data-attr2="hello t here" heymanimreallylongtoowhocomesupwiththesenames="false">This is some text</div>', '<div lookatthissuperduperlongattributenamewhoahcrazy0="true"\n attr0\n attr1="123"\n data-attr2="hello t here"\n heymanimreallylongtoowhocomesupwiththesenames="false">This is some text</div>');
|
||||
test_fragment('<img attr0 attr1="123" data-attr2="hello t here"/>', '<img attr0\n attr1="123"\n data-attr2="hello t here" />');
|
||||
|
||||
// Attribute Wrap - (eof = "\n", indent_attr = " ", over80 = "\n")
|
||||
opts.wrap_attributes = 'force';
|
||||
opts.wrap_line_length = 80;
|
||||
test_fragment('<div attr0 attr1="123" data-attr2="hello t here">This is some text</div>', '<div attr0\n attr1="123"\n data-attr2="hello t here">This is some text</div>');
|
||||
test_fragment('<div lookatthissuperduperlongattributenamewhoahcrazy0="true" attr0 attr1="123" data-attr2="hello t here" heymanimreallylongtoowhocomesupwiththesenames="false">This is some text</div>', '<div lookatthissuperduperlongattributenamewhoahcrazy0="true"\n attr0\n attr1="123"\n data-attr2="hello t here"\n heymanimreallylongtoowhocomesupwiththesenames="false">This is some text</div>');
|
||||
test_fragment('<img attr0 attr1="123" data-attr2="hello t here"/>', '<img attr0\n attr1="123"\n data-attr2="hello t here" />');
|
||||
|
||||
// Attribute Wrap - (eof = "\n", indent_attr = " ", over80 = "\n")
|
||||
opts.wrap_attributes = 'force';
|
||||
opts.wrap_attributes_indent_size = 8;
|
||||
test_fragment('<div attr0 attr1="123" data-attr2="hello t here">This is some text</div>', '<div attr0\n attr1="123"\n data-attr2="hello t here">This is some text</div>');
|
||||
test_fragment('<div lookatthissuperduperlongattributenamewhoahcrazy0="true" attr0 attr1="123" data-attr2="hello t here" heymanimreallylongtoowhocomesupwiththesenames="false">This is some text</div>', '<div lookatthissuperduperlongattributenamewhoahcrazy0="true"\n attr0\n attr1="123"\n data-attr2="hello t here"\n heymanimreallylongtoowhocomesupwiththesenames="false">This is some text</div>');
|
||||
test_fragment('<img attr0 attr1="123" data-attr2="hello t here"/>', '<img attr0\n attr1="123"\n data-attr2="hello t here" />');
|
||||
|
||||
// Attribute Wrap - (eof = " ", indent_attr = "", over80 = "\n")
|
||||
opts.wrap_attributes = 'auto';
|
||||
opts.wrap_line_length = 80;
|
||||
test_fragment('<div attr0 attr1="123" data-attr2="hello t here">This is some text</div>');
|
||||
test_fragment('<div lookatthissuperduperlongattributenamewhoahcrazy0="true" attr0 attr1="123" data-attr2="hello t here" heymanimreallylongtoowhocomesupwiththesenames="false">This is some text</div>', '<div lookatthissuperduperlongattributenamewhoahcrazy0="true" attr0 attr1="123" data-attr2="hello t here"\nheymanimreallylongtoowhocomesupwiththesenames="false">This is some text</div>');
|
||||
test_fragment('<img attr0 attr1="123" data-attr2="hello t here"/>', '<img attr0 attr1="123" data-attr2="hello t here" />');
|
||||
|
||||
// Attribute Wrap - (eof = " ", indent_attr = "", over80 = " ")
|
||||
opts.wrap_attributes = 'auto';
|
||||
opts.wrap_line_length = 0;
|
||||
test_fragment('<div attr0 attr1="123" data-attr2="hello t here">This is some text</div>');
|
||||
test_fragment('<div lookatthissuperduperlongattributenamewhoahcrazy0="true" attr0 attr1="123" data-attr2="hello t here" heymanimreallylongtoowhocomesupwiththesenames="false">This is some text</div>');
|
||||
test_fragment('<img attr0 attr1="123" data-attr2="hello t here"/>', '<img attr0 attr1="123" data-attr2="hello t here" />');
|
||||
|
||||
|
||||
|
||||
// Handlebars Indenting Off
|
||||
opts.indent_handlebars = false;
|
||||
test_fragment(
|
||||
'{{#if 0}}\n <div>\n </div>\n{{/if}}',
|
||||
'{{#if 0}}\n<div>\n</div>\n{{/if}}');
|
||||
test_fragment(
|
||||
'<div>\n{{#each thing}}\n {{name}}\n{{/each}}\n</div>',
|
||||
'<div>\n {{#each thing}} {{name}} {{/each}}\n</div>');
|
||||
|
||||
|
||||
// Handlebars Indenting On - (content = "{{field}}")
|
||||
opts.indent_handlebars = true;
|
||||
test_fragment('{{page-title}}');
|
||||
test_fragment('{{#if 0}}{{/if}}');
|
||||
test_fragment('{{#if 0}}{{field}}{{/if}}');
|
||||
test_fragment('{{#if 0}}\n{{/if}}');
|
||||
test_fragment(
|
||||
'{{#if words}}{{/if}}',
|
||||
'{{#if words}}{{/if}}');
|
||||
test_fragment(
|
||||
'{{#if words}}{{field}}{{/if}}',
|
||||
'{{#if words}}{{field}}{{/if}}');
|
||||
test_fragment(
|
||||
'{{#if words}}{{field}}{{/if}}',
|
||||
'{{#if words}}{{field}}{{/if}}');
|
||||
test_fragment('{{#if 1}}\n <div>\n </div>\n{{/if}}');
|
||||
test_fragment(
|
||||
'{{#if 1}}\n<div>\n</div>\n{{/if}}',
|
||||
'{{#if 1}}\n <div>\n </div>\n{{/if}}');
|
||||
test_fragment('<div>\n {{#if 1}}\n {{/if}}\n</div>');
|
||||
test_fragment(
|
||||
'<div>\n{{#if 1}}\n{{/if}}\n</div>',
|
||||
'<div>\n {{#if 1}}\n {{/if}}\n</div>');
|
||||
test_fragment(
|
||||
'{{#if}}\n{{#each}}\n{{#if}}\n{{field}}\n{{/if}}\n{{#if}}\n{{field}}\n{{/if}}\n{{/each}}\n{{/if}}',
|
||||
'{{#if}}\n {{#each}}\n {{#if}}\n {{field}}\n {{/if}}\n {{#if}}\n {{field}}\n {{/if}}\n {{/each}}\n{{/if}}');
|
||||
test_fragment('{{#if 1}}\n <div>\n </div>\n{{/if}}');
|
||||
test_fragment(
|
||||
'{{#if 1}}\n {{field}}\n {{else}}\n {{field}}\n{{/if}}',
|
||||
'{{#if 1}}\n {{field}}\n{{else}}\n {{field}}\n{{/if}}');
|
||||
test_fragment(
|
||||
'{{#if 1}}\n {{else}}\n {{/if}}',
|
||||
'{{#if 1}}\n{{else}}\n{{/if}}');
|
||||
test_fragment(
|
||||
'{{#if thing}}\n{{#if otherthing}}\n {{field}}\n {{else}}\n{{field}}\n {{/if}}\n {{else}}\n{{field}}\n{{/if}}',
|
||||
'{{#if thing}}\n {{#if otherthing}}\n {{field}}\n {{else}}\n {{field}}\n {{/if}}\n{{else}}\n {{field}}\n{{/if}}');
|
||||
test_fragment(
|
||||
'<div{{somestyle}}></div>',
|
||||
'<div {{somestyle}}></div>');
|
||||
test_fragment(
|
||||
'<div{{#if test}}class="foo"{{/if}}>{{field}}</div>',
|
||||
'<div {{#if test}} class="foo" {{/if}}>{{field}}</div>');
|
||||
test_fragment(
|
||||
'<div{{#if thing}}{{somestyle}}class="{{class}}"{{else}}class="{{class2}}"{{/if}}>{{field}}</div>',
|
||||
'<div {{#if thing}} {{somestyle}} class="{{class}}" {{else}} class="{{class2}}" {{/if}}>{{field}}</div>');
|
||||
test_fragment(
|
||||
'<span{{#if condition}}class="foo"{{/if}}>{{field}}</span>',
|
||||
'<span {{#if condition}} class="foo" {{/if}}>{{field}}</span>');
|
||||
test_fragment('<div unformatted="{{#if}}{{field}}{{/if}}">{{field}}</div>');
|
||||
test_fragment('<div unformatted="{{#if }} {{field}}{{/if}}">{{field}}</div>');
|
||||
test_fragment('<div class="{{#if thingIs "value"}}{{field}}{{/if}}"></div>');
|
||||
test_fragment('<div class="{{#if thingIs \'value\'}}{{field}}{{/if}}"></div>');
|
||||
test_fragment('<div class=\'{{#if thingIs "value"}}{{field}}{{/if}}\'></div>');
|
||||
test_fragment('<div class=\'{{#if thingIs \'value\'}}{{field}}{{/if}}\'></div>');
|
||||
|
||||
// Handlebars Indenting On - (content = "{{! comment}}")
|
||||
opts.indent_handlebars = true;
|
||||
test_fragment('{{page-title}}');
|
||||
test_fragment('{{#if 0}}{{/if}}');
|
||||
test_fragment('{{#if 0}}{{! comment}}{{/if}}');
|
||||
test_fragment('{{#if 0}}\n{{/if}}');
|
||||
test_fragment(
|
||||
'{{#if words}}{{/if}}',
|
||||
'{{#if words}}{{/if}}');
|
||||
test_fragment(
|
||||
'{{#if words}}{{! comment}}{{/if}}',
|
||||
'{{#if words}}{{! comment}}{{/if}}');
|
||||
test_fragment(
|
||||
'{{#if words}}{{! comment}}{{/if}}',
|
||||
'{{#if words}}{{! comment}}{{/if}}');
|
||||
test_fragment('{{#if 1}}\n <div>\n </div>\n{{/if}}');
|
||||
test_fragment(
|
||||
'{{#if 1}}\n<div>\n</div>\n{{/if}}',
|
||||
'{{#if 1}}\n <div>\n </div>\n{{/if}}');
|
||||
test_fragment('<div>\n {{#if 1}}\n {{/if}}\n</div>');
|
||||
test_fragment(
|
||||
'<div>\n{{#if 1}}\n{{/if}}\n</div>',
|
||||
'<div>\n {{#if 1}}\n {{/if}}\n</div>');
|
||||
test_fragment(
|
||||
'{{#if}}\n{{#each}}\n{{#if}}\n{{! comment}}\n{{/if}}\n{{#if}}\n{{! comment}}\n{{/if}}\n{{/each}}\n{{/if}}',
|
||||
'{{#if}}\n {{#each}}\n {{#if}}\n {{! comment}}\n {{/if}}\n {{#if}}\n {{! comment}}\n {{/if}}\n {{/each}}\n{{/if}}');
|
||||
test_fragment('{{#if 1}}\n <div>\n </div>\n{{/if}}');
|
||||
test_fragment(
|
||||
'{{#if 1}}\n {{! comment}}\n {{else}}\n {{! comment}}\n{{/if}}',
|
||||
'{{#if 1}}\n {{! comment}}\n{{else}}\n {{! comment}}\n{{/if}}');
|
||||
test_fragment(
|
||||
'{{#if 1}}\n {{else}}\n {{/if}}',
|
||||
'{{#if 1}}\n{{else}}\n{{/if}}');
|
||||
test_fragment(
|
||||
'{{#if thing}}\n{{#if otherthing}}\n {{! comment}}\n {{else}}\n{{! comment}}\n {{/if}}\n {{else}}\n{{! comment}}\n{{/if}}',
|
||||
'{{#if thing}}\n {{#if otherthing}}\n {{! comment}}\n {{else}}\n {{! comment}}\n {{/if}}\n{{else}}\n {{! comment}}\n{{/if}}');
|
||||
test_fragment(
|
||||
'<div{{somestyle}}></div>',
|
||||
'<div {{somestyle}}></div>');
|
||||
test_fragment(
|
||||
'<div{{#if test}}class="foo"{{/if}}>{{! comment}}</div>',
|
||||
'<div {{#if test}} class="foo" {{/if}}>{{! comment}}</div>');
|
||||
test_fragment(
|
||||
'<div{{#if thing}}{{somestyle}}class="{{class}}"{{else}}class="{{class2}}"{{/if}}>{{! comment}}</div>',
|
||||
'<div {{#if thing}} {{somestyle}} class="{{class}}" {{else}} class="{{class2}}" {{/if}}>{{! comment}}</div>');
|
||||
test_fragment(
|
||||
'<span{{#if condition}}class="foo"{{/if}}>{{! comment}}</span>',
|
||||
'<span {{#if condition}} class="foo" {{/if}}>{{! comment}}</span>');
|
||||
test_fragment('<div unformatted="{{#if}}{{! comment}}{{/if}}">{{! comment}}</div>');
|
||||
test_fragment('<div unformatted="{{#if }} {{! comment}}{{/if}}">{{! comment}}</div>');
|
||||
test_fragment('<div class="{{#if thingIs "value"}}{{! comment}}{{/if}}"></div>');
|
||||
test_fragment('<div class="{{#if thingIs \'value\'}}{{! comment}}{{/if}}"></div>');
|
||||
test_fragment('<div class=\'{{#if thingIs "value"}}{{! comment}}{{/if}}\'></div>');
|
||||
test_fragment('<div class=\'{{#if thingIs \'value\'}}{{! comment}}{{/if}}\'></div>');
|
||||
|
||||
// Handlebars Indenting On - (content = "{pre{{field1}} {{field2}} {{field3}}post")
|
||||
opts.indent_handlebars = true;
|
||||
test_fragment('{{page-title}}');
|
||||
test_fragment('{{#if 0}}{{/if}}');
|
||||
test_fragment('{{#if 0}}{pre{{field1}} {{field2}} {{field3}}post{{/if}}');
|
||||
test_fragment('{{#if 0}}\n{{/if}}');
|
||||
test_fragment(
|
||||
'{{#if words}}{{/if}}',
|
||||
'{{#if words}}{{/if}}');
|
||||
test_fragment(
|
||||
'{{#if words}}{pre{{field1}} {{field2}} {{field3}}post{{/if}}',
|
||||
'{{#if words}}{pre{{field1}} {{field2}} {{field3}}post{{/if}}');
|
||||
test_fragment(
|
||||
'{{#if words}}{pre{{field1}} {{field2}} {{field3}}post{{/if}}',
|
||||
'{{#if words}}{pre{{field1}} {{field2}} {{field3}}post{{/if}}');
|
||||
test_fragment('{{#if 1}}\n <div>\n </div>\n{{/if}}');
|
||||
test_fragment(
|
||||
'{{#if 1}}\n<div>\n</div>\n{{/if}}',
|
||||
'{{#if 1}}\n <div>\n </div>\n{{/if}}');
|
||||
test_fragment('<div>\n {{#if 1}}\n {{/if}}\n</div>');
|
||||
test_fragment(
|
||||
'<div>\n{{#if 1}}\n{{/if}}\n</div>',
|
||||
'<div>\n {{#if 1}}\n {{/if}}\n</div>');
|
||||
test_fragment(
|
||||
'{{#if}}\n{{#each}}\n{{#if}}\n{pre{{field1}} {{field2}} {{field3}}post\n{{/if}}\n{{#if}}\n{pre{{field1}} {{field2}} {{field3}}post\n{{/if}}\n{{/each}}\n{{/if}}',
|
||||
'{{#if}}\n {{#each}}\n {{#if}}\n {pre{{field1}} {{field2}} {{field3}}post\n {{/if}}\n {{#if}}\n {pre{{field1}} {{field2}} {{field3}}post\n {{/if}}\n {{/each}}\n{{/if}}');
|
||||
test_fragment('{{#if 1}}\n <div>\n </div>\n{{/if}}');
|
||||
test_fragment(
|
||||
'{{#if 1}}\n {pre{{field1}} {{field2}} {{field3}}post\n {{else}}\n {pre{{field1}} {{field2}} {{field3}}post\n{{/if}}',
|
||||
'{{#if 1}}\n {pre{{field1}} {{field2}} {{field3}}post\n{{else}}\n {pre{{field1}} {{field2}} {{field3}}post\n{{/if}}');
|
||||
test_fragment(
|
||||
'{{#if 1}}\n {{else}}\n {{/if}}',
|
||||
'{{#if 1}}\n{{else}}\n{{/if}}');
|
||||
test_fragment(
|
||||
'{{#if thing}}\n{{#if otherthing}}\n {pre{{field1}} {{field2}} {{field3}}post\n {{else}}\n{pre{{field1}} {{field2}} {{field3}}post\n {{/if}}\n {{else}}\n{pre{{field1}} {{field2}} {{field3}}post\n{{/if}}',
|
||||
'{{#if thing}}\n {{#if otherthing}}\n {pre{{field1}} {{field2}} {{field3}}post\n {{else}}\n {pre{{field1}} {{field2}} {{field3}}post\n {{/if}}\n{{else}}\n {pre{{field1}} {{field2}} {{field3}}post\n{{/if}}');
|
||||
test_fragment(
|
||||
'<div{{somestyle}}></div>',
|
||||
'<div {{somestyle}}></div>');
|
||||
test_fragment(
|
||||
'<div{{#if test}}class="foo"{{/if}}>{pre{{field1}} {{field2}} {{field3}}post</div>',
|
||||
'<div {{#if test}} class="foo" {{/if}}>{pre{{field1}} {{field2}} {{field3}}post</div>');
|
||||
test_fragment(
|
||||
'<div{{#if thing}}{{somestyle}}class="{{class}}"{{else}}class="{{class2}}"{{/if}}>{pre{{field1}} {{field2}} {{field3}}post</div>',
|
||||
'<div {{#if thing}} {{somestyle}} class="{{class}}" {{else}} class="{{class2}}" {{/if}}>{pre{{field1}} {{field2}} {{field3}}post</div>');
|
||||
test_fragment(
|
||||
'<span{{#if condition}}class="foo"{{/if}}>{pre{{field1}} {{field2}} {{field3}}post</span>',
|
||||
'<span {{#if condition}} class="foo" {{/if}}>{pre{{field1}} {{field2}} {{field3}}post</span>');
|
||||
test_fragment('<div unformatted="{{#if}}{pre{{field1}} {{field2}} {{field3}}post{{/if}}">{pre{{field1}} {{field2}} {{field3}}post</div>');
|
||||
test_fragment('<div unformatted="{{#if }} {pre{{field1}} {{field2}} {{field3}}post{{/if}}">{pre{{field1}} {{field2}} {{field3}}post</div>');
|
||||
test_fragment('<div class="{{#if thingIs "value"}}{pre{{field1}} {{field2}} {{field3}}post{{/if}}"></div>');
|
||||
test_fragment('<div class="{{#if thingIs \'value\'}}{pre{{field1}} {{field2}} {{field3}}post{{/if}}"></div>');
|
||||
test_fragment('<div class=\'{{#if thingIs "value"}}{pre{{field1}} {{field2}} {{field3}}post{{/if}}\'></div>');
|
||||
test_fragment('<div class=\'{{#if thingIs \'value\'}}{pre{{field1}} {{field2}} {{field3}}post{{/if}}\'></div>');
|
||||
|
||||
// Handlebars Indenting On - (content = "{{! \n mult-line\ncomment \n with spacing\n}}")
|
||||
opts.indent_handlebars = true;
|
||||
test_fragment('{{page-title}}');
|
||||
test_fragment('{{#if 0}}{{/if}}');
|
||||
test_fragment('{{#if 0}}{{! \n mult-line\ncomment \n with spacing\n}}{{/if}}');
|
||||
test_fragment('{{#if 0}}\n{{/if}}');
|
||||
test_fragment(
|
||||
'{{#if words}}{{/if}}',
|
||||
'{{#if words}}{{/if}}');
|
||||
test_fragment(
|
||||
'{{#if words}}{{! \n mult-line\ncomment \n with spacing\n}}{{/if}}',
|
||||
'{{#if words}}{{! \n mult-line\ncomment \n with spacing\n}}{{/if}}');
|
||||
test_fragment(
|
||||
'{{#if words}}{{! \n mult-line\ncomment \n with spacing\n}}{{/if}}',
|
||||
'{{#if words}}{{! \n mult-line\ncomment \n with spacing\n}}{{/if}}');
|
||||
test_fragment('{{#if 1}}\n <div>\n </div>\n{{/if}}');
|
||||
test_fragment(
|
||||
'{{#if 1}}\n<div>\n</div>\n{{/if}}',
|
||||
'{{#if 1}}\n <div>\n </div>\n{{/if}}');
|
||||
test_fragment('<div>\n {{#if 1}}\n {{/if}}\n</div>');
|
||||
test_fragment(
|
||||
'<div>\n{{#if 1}}\n{{/if}}\n</div>',
|
||||
'<div>\n {{#if 1}}\n {{/if}}\n</div>');
|
||||
test_fragment(
|
||||
'{{#if}}\n{{#each}}\n{{#if}}\n{{! \n mult-line\ncomment \n with spacing\n}}\n{{/if}}\n{{#if}}\n{{! \n mult-line\ncomment \n with spacing\n}}\n{{/if}}\n{{/each}}\n{{/if}}',
|
||||
'{{#if}}\n {{#each}}\n {{#if}}\n {{! \n mult-line\ncomment \n with spacing\n}}\n {{/if}}\n {{#if}}\n {{! \n mult-line\ncomment \n with spacing\n}}\n {{/if}}\n {{/each}}\n{{/if}}');
|
||||
test_fragment('{{#if 1}}\n <div>\n </div>\n{{/if}}');
|
||||
test_fragment(
|
||||
'{{#if 1}}\n {{! \n mult-line\ncomment \n with spacing\n}}\n {{else}}\n {{! \n mult-line\ncomment \n with spacing\n}}\n{{/if}}',
|
||||
'{{#if 1}}\n {{! \n mult-line\ncomment \n with spacing\n}}\n{{else}}\n {{! \n mult-line\ncomment \n with spacing\n}}\n{{/if}}');
|
||||
test_fragment(
|
||||
'{{#if 1}}\n {{else}}\n {{/if}}',
|
||||
'{{#if 1}}\n{{else}}\n{{/if}}');
|
||||
test_fragment(
|
||||
'{{#if thing}}\n{{#if otherthing}}\n {{! \n mult-line\ncomment \n with spacing\n}}\n {{else}}\n{{! \n mult-line\ncomment \n with spacing\n}}\n {{/if}}\n {{else}}\n{{! \n mult-line\ncomment \n with spacing\n}}\n{{/if}}',
|
||||
'{{#if thing}}\n {{#if otherthing}}\n {{! \n mult-line\ncomment \n with spacing\n}}\n {{else}}\n {{! \n mult-line\ncomment \n with spacing\n}}\n {{/if}}\n{{else}}\n {{! \n mult-line\ncomment \n with spacing\n}}\n{{/if}}');
|
||||
test_fragment(
|
||||
'<div{{somestyle}}></div>',
|
||||
'<div {{somestyle}}></div>');
|
||||
test_fragment(
|
||||
'<div{{#if test}}class="foo"{{/if}}>{{! \n mult-line\ncomment \n with spacing\n}}</div>',
|
||||
'<div {{#if test}} class="foo" {{/if}}>{{! \n mult-line\ncomment \n with spacing\n}}</div>');
|
||||
test_fragment(
|
||||
'<div{{#if thing}}{{somestyle}}class="{{class}}"{{else}}class="{{class2}}"{{/if}}>{{! \n mult-line\ncomment \n with spacing\n}}</div>',
|
||||
'<div {{#if thing}} {{somestyle}} class="{{class}}" {{else}} class="{{class2}}" {{/if}}>{{! \n mult-line\ncomment \n with spacing\n}}</div>');
|
||||
test_fragment(
|
||||
'<span{{#if condition}}class="foo"{{/if}}>{{! \n mult-line\ncomment \n with spacing\n}}</span>',
|
||||
'<span {{#if condition}} class="foo" {{/if}}>{{! \n mult-line\ncomment \n with spacing\n}}</span>');
|
||||
test_fragment('<div unformatted="{{#if}}{{! \n mult-line\ncomment \n with spacing\n}}{{/if}}">{{! \n mult-line\ncomment \n with spacing\n}}</div>');
|
||||
test_fragment('<div unformatted="{{#if }} {{! \n mult-line\ncomment \n with spacing\n}}{{/if}}">{{! \n mult-line\ncomment \n with spacing\n}}</div>');
|
||||
test_fragment('<div class="{{#if thingIs "value"}}{{! \n mult-line\ncomment \n with spacing\n}}{{/if}}"></div>');
|
||||
test_fragment('<div class="{{#if thingIs \'value\'}}{{! \n mult-line\ncomment \n with spacing\n}}{{/if}}"></div>');
|
||||
test_fragment('<div class=\'{{#if thingIs "value"}}{{! \n mult-line\ncomment \n with spacing\n}}{{/if}}\'></div>');
|
||||
test_fragment('<div class=\'{{#if thingIs \'value\'}}{{! \n mult-line\ncomment \n with spacing\n}}{{/if}}\'></div>');
|
||||
|
||||
|
||||
|
||||
// Unclosed html elements
|
||||
test_fragment('<source>\n<source>');
|
||||
test_fragment('<br>\n<br>');
|
||||
test_fragment('<input>\n<input>');
|
||||
test_fragment('<meta>\n<meta>');
|
||||
test_fragment('<link>\n<link>');
|
||||
|
||||
|
||||
|
||||
// Unformatted tags
|
||||
test_fragment('<ol>\n <li>b<pre>c</pre></li>\n</ol>');
|
||||
test_fragment('<ol>\n <li>b<code>c</code></li>\n</ol>');
|
||||
|
||||
|
||||
|
||||
// Php formatting
|
||||
test_fragment('<h1 class="content-page-header"><?=$view["name"]; ?></h1>');
|
||||
test_fragment(
|
||||
'<?php\n' +
|
||||
'for($i = 1; $i <= 100; $i++;) {\n' +
|
||||
' #count to 100!\n' +
|
||||
' echo($i . "</br>");\n' +
|
||||
'}\n' +
|
||||
'?>');
|
||||
|
||||
|
||||
|
||||
// underscore.js formatting
|
||||
test_fragment(
|
||||
'<div class="col-sm-9">\n' +
|
||||
' <textarea id="notes" class="form-control" rows="3">\n' +
|
||||
' <%= notes %>\n' +
|
||||
' </textarea>\n' +
|
||||
'</div>');
|
||||
|
||||
|
||||
|
||||
// Indent with tabs
|
||||
opts.indent_with_tabs = true;
|
||||
test_fragment(
|
||||
'<div>\n<div>\n</div>\n</div>',
|
||||
'<div>\n\t<div>\n\t</div>\n</div>');
|
||||
|
||||
|
||||
|
||||
// Indent without tabs
|
||||
opts.indent_with_tabs = false;
|
||||
test_fragment(
|
||||
'<div>\n<div>\n</div>\n</div>',
|
||||
'<div>\n <div>\n </div>\n</div>');
|
||||
|
||||
|
||||
|
||||
// New Test Suite
|
||||
|
||||
|
||||
opts.end_with_newline = true;
|
||||
test_fragment('', '\n');
|
||||
test_fragment('<div></div>\n');
|
||||
test_fragment('<div></div>\n\n\n', '<div></div>\n');
|
||||
test_fragment('<head>\n' +
|
||||
' <script>\n' +
|
||||
' mocha.setup("bdd");\n' +
|
||||
'\n' +
|
||||
' </script>\n' +
|
||||
'</head>\n');
|
||||
|
||||
|
||||
opts.end_with_newline = false;
|
||||
// error cases need love too
|
||||
bth('<img title="Bad food!" src="foo.jpg" alt="Evil" ">');
|
||||
bth("<!-- don't blow up if a comment is not complete"); // -->
|
||||
|
||||
test_fragment(
|
||||
'<head>\n' +
|
||||
' <script>\n' +
|
||||
' mocha.setup("bdd");\n' +
|
||||
' </script>\n' +
|
||||
'</head>');
|
||||
|
||||
test_fragment('<div></div>\n', '<div></div>');
|
||||
bth('<div></div>');
|
||||
bth('<div>content</div>');
|
||||
bth('<div><div></div></div>',
|
||||
'<div>\n' +
|
||||
' <div></div>\n' +
|
||||
'</div>');
|
||||
bth('<div><div>content</div></div>',
|
||||
'<div>\n' +
|
||||
' <div>content</div>\n' +
|
||||
'</div>');
|
||||
bth('<div>\n' +
|
||||
' <span>content</span>\n' +
|
||||
'</div>');
|
||||
bth('<div>\n' +
|
||||
'</div>');
|
||||
bth('<div>\n' +
|
||||
' content\n' +
|
||||
'</div>');
|
||||
bth('<div>\n' +
|
||||
' </div>',
|
||||
'<div>\n' +
|
||||
'</div>');
|
||||
bth(' <div>\n' +
|
||||
' </div>',
|
||||
'<div>\n' +
|
||||
'</div>');
|
||||
bth('<div>\n' +
|
||||
'</div>\n' +
|
||||
' <div>\n' +
|
||||
' </div>',
|
||||
'<div>\n' +
|
||||
'</div>\n' +
|
||||
'<div>\n' +
|
||||
'</div>');
|
||||
bth(' <div>\n' +
|
||||
'</div>',
|
||||
'<div>\n' +
|
||||
'</div>');
|
||||
bth('<div >content</div>',
|
||||
'<div>content</div>');
|
||||
bth('<div thinger="preserve space here" ></div >',
|
||||
'<div thinger="preserve space here"></div>');
|
||||
bth('content\n' +
|
||||
' <div>\n' +
|
||||
' </div>\n' +
|
||||
'content',
|
||||
'content\n' +
|
||||
'<div>\n' +
|
||||
'</div>\n' +
|
||||
'content');
|
||||
bth('<li>\n' +
|
||||
' <div>\n' +
|
||||
' </div>\n' +
|
||||
'</li>');
|
||||
bth('<li>\n' +
|
||||
'<div>\n' +
|
||||
'</div>\n' +
|
||||
'</li>',
|
||||
'<li>\n' +
|
||||
' <div>\n' +
|
||||
' </div>\n' +
|
||||
'</li>');
|
||||
bth('<li>\n' +
|
||||
' content\n' +
|
||||
'</li>\n' +
|
||||
'<li>\n' +
|
||||
' content\n' +
|
||||
'</li>');
|
||||
|
||||
bth('<img>content');
|
||||
bth('<img> content');
|
||||
bth('<img> content', '<img> content');
|
||||
|
||||
bth('<img><img>content');
|
||||
bth('<img> <img>content');
|
||||
bth('<img> <img>content', '<img> <img>content');
|
||||
|
||||
bth('<img><b>content</b>');
|
||||
bth('<img> <b>content</b>');
|
||||
bth('<img> <b>content</b>', '<img> <b>content</b>');
|
||||
|
||||
bth('<div>content<img>content</div>');
|
||||
bth('<div> content <img> content</div>');
|
||||
bth('<div> content <img> content </div>',
|
||||
'<div> content <img> content </div>');
|
||||
bth('Text <a href="#">Link</a> Text');
|
||||
|
||||
|
||||
// START tests for issue 453
|
||||
bth('<script type="text/unknown"><div></div></script>',
|
||||
'<script type="text/unknown">\n' +
|
||||
' <div></div>\n' +
|
||||
'</script>');
|
||||
bth('<script type="text/javascript"><div></div></script>',
|
||||
'<script type="text/javascript">\n' +
|
||||
' < div > < /div>\n' +
|
||||
'</script>');
|
||||
bth('<script><div></div></script>',
|
||||
'<script>\n' +
|
||||
' < div > < /div>\n' +
|
||||
'</script>');
|
||||
bth('<script type="text/javascript">var foo = "bar";</script>',
|
||||
'<script type="text/javascript">\n' +
|
||||
' var foo = "bar";\n' +
|
||||
'</script>');
|
||||
bth('<script type="application/javascript">var foo = "bar";</script>',
|
||||
'<script type="application/javascript">\n' +
|
||||
' var foo = "bar";\n' +
|
||||
'</script>');
|
||||
bth('<script type="application/javascript;version=1.8">var foo = "bar";</script>',
|
||||
'<script type="application/javascript;version=1.8">\n' +
|
||||
' var foo = "bar";\n' +
|
||||
'</script>');
|
||||
bth('<script type="application/x-javascript">var foo = "bar";</script>',
|
||||
'<script type="application/x-javascript">\n' +
|
||||
' var foo = "bar";\n' +
|
||||
'</script>');
|
||||
bth('<script type="application/ecmascript">var foo = "bar";</script>',
|
||||
'<script type="application/ecmascript">\n' +
|
||||
' var foo = "bar";\n' +
|
||||
'</script>');
|
||||
bth('<script type="text/javascript1.5">var foo = "bar";</script>',
|
||||
'<script type="text/javascript1.5">\n' +
|
||||
' var foo = "bar";\n' +
|
||||
'</script>');
|
||||
bth('<script>var foo = "bar";</script>',
|
||||
'<script>\n' +
|
||||
' var foo = "bar";\n' +
|
||||
'</script>');
|
||||
|
||||
bth('<style type="text/unknown"><tag></tag></style>',
|
||||
'<style type="text/unknown">\n' +
|
||||
' <tag></tag>\n' +
|
||||
'</style>');
|
||||
bth('<style type="text/css"><tag></tag></style>',
|
||||
'<style type="text/css">\n' +
|
||||
' <tag></tag>\n' +
|
||||
'</style>');
|
||||
bth('<style><tag></tag></style>',
|
||||
'<style>\n' +
|
||||
' <tag></tag>\n' +
|
||||
'</style>');
|
||||
bth('<style type="text/css">.selector {font-size:12px;}</style>',
|
||||
'<style type="text/css">\n' +
|
||||
' .selector {\n' +
|
||||
' font-size: 12px;\n' +
|
||||
' }\n'+
|
||||
'</style>');
|
||||
bth('<style>.selector {font-size:12px;}</style>',
|
||||
'<style>\n' +
|
||||
' .selector {\n' +
|
||||
' font-size: 12px;\n' +
|
||||
' }\n'+
|
||||
'</style>');
|
||||
// END tests for issue 453
|
||||
|
||||
var unformatted = opts.unformatted;
|
||||
opts.unformatted = ['script', 'style'];
|
||||
bth('<script id="javascriptTemplate" type="text/x-kendo-template">\n' +
|
||||
' <ul>\n' +
|
||||
' # for (var i = 0; i < data.length; i++) { #\n' +
|
||||
' <li>#= data[i] #</li>\n' +
|
||||
' # } #\n' +
|
||||
' </ul>\n' +
|
||||
'</script>');
|
||||
bth('<style>\n' +
|
||||
' body {background-color:lightgrey}\n' +
|
||||
' h1 {color:blue}\n' +
|
||||
'</style>');
|
||||
opts.unformatted = unformatted;
|
||||
|
||||
unformatted = opts.unformatted;
|
||||
opts.unformatted = ['custom-element'];
|
||||
test_fragment('<div>should <custom-element>not</custom-element>' +
|
||||
' insert newlines</div>',
|
||||
'<div>should <custom-element>not</custom-element>' +
|
||||
' insert newlines</div>');
|
||||
opts.unformatted = unformatted;
|
||||
|
||||
// Tests that don't pass, but probably should.
|
||||
// bth('<div><span>content</span></div>');
|
||||
|
||||
// Handlebars tests
|
||||
// Without the indent option on, handlebars are treated as content.
|
||||
|
||||
opts.wrap_line_length = 0;
|
||||
//...---------1---------2---------3---------4---------5---------6---------7
|
||||
//...1234567890123456789012345678901234567890123456789012345678901234567890
|
||||
bth('<div>Some text that should not wrap at all.</div>',
|
||||
/* expected */
|
||||
'<div>Some text that should not wrap at all.</div>');
|
||||
|
||||
// A value of 0 means no max line length, and should not wrap.
|
||||
//...---------1---------2---------3---------4---------5---------6---------7---------8---------9--------10--------11--------12--------13--------14--------15--------16--------17--------18--------19--------20--------21--------22--------23--------24--------25--------26--------27--------28--------29
|
||||
//...12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
|
||||
bth('<div>Some text that should not wrap at all. Some text that should not wrap at all. Some text that should not wrap at all. Some text that should not wrap at all. Some text that should not wrap at all. Some text that should not wrap at all. Some text that should not wrap at all.</div>',
|
||||
/* expected */
|
||||
'<div>Some text that should not wrap at all. Some text that should not wrap at all. Some text that should not wrap at all. Some text that should not wrap at all. Some text that should not wrap at all. Some text that should not wrap at all. Some text that should not wrap at all.</div>');
|
||||
|
||||
opts.wrap_line_length = "0";
|
||||
//...---------1---------2---------3---------4---------5---------6---------7
|
||||
//...1234567890123456789012345678901234567890123456789012345678901234567890
|
||||
bth('<div>Some text that should not wrap at all.</div>',
|
||||
/* expected */
|
||||
'<div>Some text that should not wrap at all.</div>');
|
||||
|
||||
// A value of "0" means no max line length, and should not wrap
|
||||
//...---------1---------2---------3---------4---------5---------6---------7---------8---------9--------10--------11--------12--------13--------14--------15--------16--------17--------18--------19--------20--------21--------22--------23--------24--------25--------26--------27--------28--------29
|
||||
//...12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
|
||||
bth('<div>Some text that should not wrap at all. Some text that should not wrap at all. Some text that should not wrap at all. Some text that should not wrap at all. Some text that should not wrap at all. Some text that should not wrap at all. Some text that should not wrap at all.</div>',
|
||||
/* expected */
|
||||
'<div>Some text that should not wrap at all. Some text that should not wrap at all. Some text that should not wrap at all. Some text that should not wrap at all. Some text that should not wrap at all. Some text that should not wrap at all. Some text that should not wrap at all.</div>');
|
||||
|
||||
//BUGBUG: This should wrap before 40 not after.
|
||||
opts.wrap_line_length = 40;
|
||||
//...---------1---------2---------3---------4---------5---------6---------7
|
||||
//...1234567890123456789012345678901234567890123456789012345678901234567890
|
||||
bth('<div>Some test text that should wrap_inside_this section here.</div>',
|
||||
/* expected */
|
||||
'<div>Some test text that should wrap_inside_this\n' +
|
||||
' section here.</div>');
|
||||
|
||||
opts.wrap_line_length = "40";
|
||||
//...---------1---------2---------3---------4---------5---------6---------7
|
||||
//...1234567890123456789012345678901234567890123456789012345678901234567890
|
||||
bth('<div>Some test text that should wrap_inside_this section here.</div>',
|
||||
/* expected */
|
||||
'<div>Some test text that should wrap_inside_this\n' +
|
||||
' section here.</div>');
|
||||
|
||||
opts.indent_size = 1;
|
||||
opts.indent_char = '\t';
|
||||
opts.preserve_newlines = false;
|
||||
bth('<div>\n\tfoo\n</div>', '<div> foo </div>');
|
||||
|
||||
opts.preserve_newlines = true;
|
||||
bth('<div>\n\tfoo\n</div>');
|
||||
|
||||
|
||||
|
||||
// test preserve_newlines and max_preserve_newlines
|
||||
opts.preserve_newlines = false;
|
||||
bth('<div>Should not</div>\n\n\n' +
|
||||
'<div>preserve newlines</div>',
|
||||
'<div>Should not</div>\n' +
|
||||
'<div>preserve newlines</div>');
|
||||
|
||||
opts.preserve_newlines = true;
|
||||
opts.max_preserve_newlines = 0;
|
||||
bth('<div>Should</div>\n\n\n' +
|
||||
'<div>preserve zero newlines</div>',
|
||||
'<div>Should</div>\n' +
|
||||
'<div>preserve zero newlines</div>');
|
||||
|
||||
opts.max_preserve_newlines = 1;
|
||||
bth('<div>Should</div>\n\n\n' +
|
||||
'<div>preserve one newline</div>',
|
||||
'<div>Should</div>\n\n' +
|
||||
'<div>preserve one newline</div>');
|
||||
|
||||
opts.max_preserve_newlines = null;
|
||||
bth('<div>Should</div>\n\n\n' +
|
||||
'<div>preserve one newline</div>',
|
||||
'<div>Should</div>\n\n\n' +
|
||||
'<div>preserve one newline</div>');
|
||||
}
|
||||
|
||||
beautifier_tests();
|
||||
}
|
||||
|
||||
if (typeof exports !== "undefined") {
|
||||
exports.run_html_tests = run_html_tests;
|
||||
}
|
||||
File diff suppressed because it is too large
Load diff
|
|
@ -0,0 +1,50 @@
|
|||
/*global js_beautify: true */
|
||||
/*jshint node:true */
|
||||
|
||||
var fs = require('fs'),
|
||||
SanityTest = require('./sanitytest'),
|
||||
Benchmark = require('benchmark'),
|
||||
Urlencoded = require('../lib/unpackers/urlencode_unpacker'),
|
||||
js_beautify = require('../index').js_beautify,
|
||||
css_beautify = require('../index').css_beautify,
|
||||
html_beautify = require('../index').html_beautify;
|
||||
|
||||
function node_beautifier_tests() {
|
||||
console.log('Testing performance...');
|
||||
var data = fs.readFileSync(__dirname + '/../../test/underscore.js', 'utf8');
|
||||
var data_min = fs.readFileSync(__dirname + '/../../test/underscore-min.js', 'utf8');
|
||||
var options = {
|
||||
wrap_line_length: 80
|
||||
};
|
||||
|
||||
//warm-up
|
||||
js_beautify(data, options);
|
||||
js_beautify(data_min, options);
|
||||
|
||||
var suite = new Benchmark.Suite;
|
||||
|
||||
suite.add("js-beautify (underscore)", function() {
|
||||
js_beautify(data, options);
|
||||
})
|
||||
.add("js-beautify (underscore-min)", function() {
|
||||
js_beautify(data_min, options);
|
||||
})
|
||||
// add listeners
|
||||
.on('cycle', function(event) {
|
||||
console.log(String(event.target));
|
||||
})
|
||||
.on('error', function(event) {
|
||||
return 1;
|
||||
})
|
||||
.on('complete', function(event) {
|
||||
})
|
||||
.run()
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
if (require.main === module) {
|
||||
process.exit(node_beautifier_tests());
|
||||
}
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
/*global js_beautify: true */
|
||||
/*jshint node:true */
|
||||
|
||||
var SanityTest = require('./sanitytest'),
|
||||
Urlencoded = require('../lib/unpackers/urlencode_unpacker'),
|
||||
run_javascript_tests = require('./beautify-javascript-tests').run_javascript_tests,
|
||||
run_css_tests = require('./beautify-css-tests').run_css_tests,
|
||||
run_html_tests = require('./beautify-html-tests').run_html_tests;
|
||||
|
||||
function node_beautifier_legacy_tests(name, test_runner) {
|
||||
console.log('Testing ' + name + ' with node.js CommonJS (legacy names)...');
|
||||
var results = new SanityTest();
|
||||
var beautify = require('../index');
|
||||
|
||||
test_runner(
|
||||
results,
|
||||
Urlencoded,
|
||||
beautify.js_beautify,
|
||||
beautify.html_beautify,
|
||||
beautify.css_beautify);
|
||||
|
||||
console.log(results.results_raw());
|
||||
return results;
|
||||
}
|
||||
|
||||
function node_beautifier_tests(name, test_runner) {
|
||||
console.log('Testing ' + name + ' with node.js CommonJS (new names)...');
|
||||
var results = new SanityTest();
|
||||
var beautify = require('../index');
|
||||
|
||||
|
||||
test_runner(
|
||||
results,
|
||||
Urlencoded,
|
||||
beautify.js,
|
||||
beautify.html,
|
||||
beautify.css);
|
||||
|
||||
console.log(results.results_raw());
|
||||
return results;
|
||||
}
|
||||
|
||||
|
||||
if (require.main === module) {
|
||||
process.exit(
|
||||
node_beautifier_tests('js-beautifier', run_javascript_tests).get_exitcode() +
|
||||
node_beautifier_legacy_tests('js-beautifier', run_javascript_tests).get_exitcode() +
|
||||
node_beautifier_tests('cs-beautifier', run_css_tests).get_exitcode() +
|
||||
node_beautifier_legacy_tests('css-beautifier', run_css_tests).get_exitcode() +
|
||||
node_beautifier_tests('html-beautifier', run_html_tests).get_exitcode() +
|
||||
node_beautifier_legacy_tests('html-beautifier', run_html_tests).get_exitcode()
|
||||
);
|
||||
}
|
||||
|
|
@ -0,0 +1,58 @@
|
|||
<!doctype html>
|
||||
<html lang="en-US">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title></title>
|
||||
|
||||
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/require.js/2.1.10/require.min.js"></script>
|
||||
<script type="text/javascript">
|
||||
|
||||
require(["../lib/beautify-html"],function(html_beautify){
|
||||
var input = document.getElementById("input").value;
|
||||
var output = html_beautify.html_beautify(input);
|
||||
document.getElementById("output").innerHTML = output;
|
||||
});
|
||||
|
||||
</script>
|
||||
|
||||
<style type="text/css">
|
||||
#output{
|
||||
border: 1px solid black;
|
||||
height: 300px;
|
||||
width: 100%;
|
||||
}
|
||||
#input{
|
||||
width: 100%;
|
||||
height: 300px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<h1>RequireJS test</h1>
|
||||
|
||||
<p>
|
||||
This example loads the html-beautifier by using a relative path in the require call to the beautify-html.js file.
|
||||
(also works works with absolute paths)
|
||||
</p>
|
||||
|
||||
<pre>
|
||||
require(["../lib/beautify-html"],function(html_beautify){
|
||||
var input = document.getElementById("input").value;
|
||||
var output = html_beautify.html_beautify(input);
|
||||
document.getElementById("output").innerHTML = output;
|
||||
});
|
||||
</pre>
|
||||
|
||||
<h2>Input</h2>
|
||||
<textarea name="" id="input">
|
||||
<ul><li><a href="test"></a></li></ul>
|
||||
</textarea>
|
||||
|
||||
<h2>Output</h2>
|
||||
<textarea name="" id="output">
|
||||
|
||||
</textarea>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
function indentMe() {
|
||||
"no, me!";
|
||||
}
|
||||
17
gaia/distros/spark/apps/customizer/components/js-beautify/js/test/run-tests
Executable file
17
gaia/distros/spark/apps/customizer/components/js-beautify/js/test/run-tests
Executable file
|
|
@ -0,0 +1,17 @@
|
|||
#!/usr/bin/spidermonkey-1.7 -s
|
||||
|
||||
//#!/usr/bin/js
|
||||
|
||||
// a little helper for testing from command line
|
||||
// just run it, it will output the test results
|
||||
|
||||
load('js/lib/beautify.js');
|
||||
load('js/test/sanitytest.js')
|
||||
load('js/test/beautify-tests.js')
|
||||
load('js/lib/unpackers/urlencode_unpacker.js')
|
||||
|
||||
print(run_beautifier_tests(new SanityTest(), Urlencoded, js_beautify).results_raw())
|
||||
|
||||
|
||||
// for nodejs use this from the command line from the main directory:
|
||||
// node test/beautify-tests.js
|
||||
135
gaia/distros/spark/apps/customizer/components/js-beautify/js/test/sanitytest.js
Executable file
135
gaia/distros/spark/apps/customizer/components/js-beautify/js/test/sanitytest.js
Executable file
|
|
@ -0,0 +1,135 @@
|
|||
//
|
||||
// simple testing interface
|
||||
// written by Einar Lielmanis, einar@jsbeautifier.org
|
||||
//
|
||||
// usage:
|
||||
//
|
||||
// var t = new SanityTest(function (x) { return x; }, 'my function');
|
||||
// t.expect('input', 'output');
|
||||
// t.expect('a', 'a');
|
||||
// output_somewhere(t.results()); // good for <pre>, html safe-ish
|
||||
// alert(t.results_raw()); // html unescaped
|
||||
|
||||
|
||||
function SanityTest (func, name_of_test) {
|
||||
|
||||
var test_func = func || function (x) {
|
||||
return x;
|
||||
};
|
||||
|
||||
var test_name = name_of_test || '';
|
||||
|
||||
var n_failed = 0;
|
||||
var n_succeeded = 0;
|
||||
|
||||
var failures = [];
|
||||
|
||||
this.test_function = function(func, name) {
|
||||
test_func = func;
|
||||
test_name = name || '';
|
||||
};
|
||||
|
||||
this.get_exitcode = function() {
|
||||
return n_succeeded === 0 || n_failed !== 0 ? 1 : 0;
|
||||
};
|
||||
|
||||
this.expect = function(parameters, expected_value) {
|
||||
// multi-parameter calls not supported (I don't need them now).
|
||||
var result = test_func(parameters);
|
||||
// proper array checking is a pain. i'll maybe do it later, compare strings representations instead
|
||||
if ((result === expected_value) || (expected_value instanceof Array && result.join(', ') === expected_value.join(', '))) {
|
||||
n_succeeded += 1;
|
||||
} else {
|
||||
n_failed += 1;
|
||||
failures.push([test_name, parameters, expected_value, result]);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
this.results_raw = function() {
|
||||
var results = '';
|
||||
if (n_failed === 0) {
|
||||
if (n_succeeded === 0) {
|
||||
results = 'No tests run.';
|
||||
} else {
|
||||
results = 'All ' + n_succeeded + ' tests passed.';
|
||||
}
|
||||
} else {
|
||||
for (var i = 0 ; i < failures.length; i++) {
|
||||
var f = failures[i];
|
||||
if (f[0]) {
|
||||
f[0] = f[0] + ' ';
|
||||
}
|
||||
results += '---- ' + f[0] + 'input -------\n' + this.prettyprint(f[1]) + '\n';
|
||||
results += '---- ' + f[0] + 'expected ----\n' + this.prettyprint(f[2]) + '\n';
|
||||
results += '---- ' + f[0] + 'output ------\n' + this.prettyprint(f[3]) + '\n\n';
|
||||
|
||||
}
|
||||
results += n_failed + ' tests failed.\n';
|
||||
}
|
||||
return results;
|
||||
};
|
||||
|
||||
|
||||
this.results = function() {
|
||||
return this.lazy_escape(this.results_raw());
|
||||
};
|
||||
|
||||
|
||||
this.prettyprint = function(something, quote_strings) {
|
||||
var type = typeof something;
|
||||
switch(type.toLowerCase()) {
|
||||
case 'string':
|
||||
if (quote_strings) {
|
||||
return "'" + something.replace("'", "\\'") + "'";
|
||||
} else {
|
||||
return something;
|
||||
}
|
||||
case 'number':
|
||||
return '' + something;
|
||||
case 'boolean':
|
||||
return something ? 'true' : 'false';
|
||||
case 'undefined':
|
||||
return 'undefined';
|
||||
case 'object':
|
||||
if (something instanceof Array) {
|
||||
var x = [];
|
||||
var expected_index = 0;
|
||||
for (var k in something) {
|
||||
if (k === expected_index) {
|
||||
x.push(this.prettyprint(something[k], true));
|
||||
expected_index += 1;
|
||||
} else {
|
||||
x.push('\n' + k + ': ' + this.prettyprint(something[k], true));
|
||||
}
|
||||
}
|
||||
return '[' + x.join(', ') + ']';
|
||||
} else {
|
||||
return 'object: ' + something;
|
||||
}
|
||||
default:
|
||||
return type + ': ' + something;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
this.lazy_escape = function (str) {
|
||||
return str.replace(/</g, '<').replace(/\>/g, '>').replace(/\n/g, '<br />');
|
||||
};
|
||||
|
||||
|
||||
this.log = function () {
|
||||
if (window.console) {
|
||||
if (console.firebug) {
|
||||
console.log.apply(console, Array.prototype.slice.call(arguments));
|
||||
} else {
|
||||
console.log.call(console, Array.prototype.slice.call(arguments));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
if (typeof module !== 'undefined' && module.exports) {
|
||||
module.exports = SanityTest;
|
||||
}
|
||||
|
|
@ -0,0 +1,171 @@
|
|||
#!/bin/bash
|
||||
|
||||
REL_SCRIPT_DIR="`dirname \"$0\"`"
|
||||
SCRIPT_DIR="`( cd \"$REL_SCRIPT_DIR\" && pwd )`"
|
||||
|
||||
test_cli_common()
|
||||
{
|
||||
echo ----------------------------------------
|
||||
echo Testing common cli behavior...
|
||||
CLI_SCRIPT_NAME=${1:?missing_param}.js
|
||||
CLI_SCRIPT=$SCRIPT_DIR/../bin/$CLI_SCRIPT_NAME
|
||||
echo Script: $CLI_SCRIPT
|
||||
|
||||
# should find the minimal help output
|
||||
$CLI_SCRIPT 2>&1 | grep -q "Must define at least one file\." || {
|
||||
echo "[$CLI_SCRIPT_NAME] Output should be help message."
|
||||
exit 1
|
||||
}
|
||||
|
||||
$CLI_SCRIPT 2> /dev/null && {
|
||||
echo "[$CLI_SCRIPT_NAME (with no parameters)] Return code should be error."
|
||||
exit 1
|
||||
}
|
||||
|
||||
$CLI_SCRIPT -Z 2> /dev/null && {
|
||||
echo "[$CLI_SCRIPT_NAME -Z] Return code for invalid parameter should be error."
|
||||
exit 1
|
||||
}
|
||||
|
||||
$CLI_SCRIPT -h > /dev/null || {
|
||||
echo "[$CLI_SCRIPT_NAME -h] Return code should be success."
|
||||
exit 1
|
||||
}
|
||||
|
||||
$CLI_SCRIPT -v > /dev/null || {
|
||||
echo "[$CLI_SCRIPT_NAME -v] Return code should be success."
|
||||
exit 1
|
||||
}
|
||||
|
||||
MISSING_FILE="$SCRIPT_DIR/../../../js/bin/missing_file"
|
||||
MISSING_FILE_MESSAGE="Unable to open path"
|
||||
$CLI_SCRIPT $MISSING_FILE 2> /dev/null && {
|
||||
echo "[$CLI_SCRIPT_NAME $MISSING_FILE] Return code should be error."
|
||||
exit 1
|
||||
}
|
||||
|
||||
$CLI_SCRIPT $MISSING_FILE 2>&1 | grep -q "$MISSING_FILE_MESSAGE" || {
|
||||
echo "[$CLI_SCRIPT_NAME $MISSING_FILE] Stderr should have useful message."
|
||||
exit 1
|
||||
}
|
||||
|
||||
if [ "`$CLI_SCRIPT $MISSING_FILE 2> /dev/null`" != "" ]; then
|
||||
echo "[$CLI_SCRIPT_NAME $MISSING_FILE] Stdout should have no text."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
}
|
||||
|
||||
test_cli_js_beautify()
|
||||
{
|
||||
echo ----------------------------------------
|
||||
echo Testing js-beautify cli behavior...
|
||||
CLI_SCRIPT=$SCRIPT_DIR/../bin/js-beautify.js
|
||||
|
||||
$CLI_SCRIPT $SCRIPT_DIR/../bin/js-beautify.js > /dev/null || {
|
||||
echo "js-beautify output for $SCRIPT_DIR/../bin/js-beautify.js was expected succeed."
|
||||
exit 1
|
||||
}
|
||||
|
||||
$CLI_SCRIPT $SCRIPT_DIR/../bin/css-beautify.js > /dev/null || {
|
||||
echo "js-beautify output for $SCRIPT_DIR/../bin/css-beautify.js was expected succeed."
|
||||
exit 1
|
||||
}
|
||||
|
||||
$CLI_SCRIPT $SCRIPT_DIR/../bin/js-beautify.js | diff $SCRIPT_DIR/../bin/js-beautify.js - || {
|
||||
echo "js-beautify output for $SCRIPT_DIR/../bin/js-beautify.js was expected to be unchanged."
|
||||
exit 1
|
||||
}
|
||||
|
||||
rm -rf /tmp/js-beautify-mkdir
|
||||
$CLI_SCRIPT -o /tmp/js-beautify-mkdir/js-beautify.js $SCRIPT_DIR/../bin/js-beautify.js && diff $SCRIPT_DIR/../bin/js-beautify.js /tmp/js-beautify-mkdir/js-beautify.js || {
|
||||
echo "js-beautify output for $SCRIPT_DIR/../bin/js-beautify.js should have been created in /tmp/js-beautify-mkdir/js-beautify.js."
|
||||
exit 1
|
||||
}
|
||||
|
||||
# ensure unchanged files are not overwritten
|
||||
cp -p /tmp/js-beautify-mkdir/js-beautify.js /tmp/js-beautify-mkdir/js-beautify-old.js
|
||||
touch -A -05 /tmp/js-beautify-mkdir/js-beautify.js
|
||||
touch -A -01 /tmp/js-beautify-mkdir/js-beautify-old.js
|
||||
$CLI_SCRIPT -r /tmp/js-beautify-mkdir/js-beautify.js && test /tmp/js-beautify-mkdir/js-beautify.js -nt /tmp/js-beautify-mkdir/js-beautify-old.js && {
|
||||
echo "js-beautify should not replace unchanged file /tmp/js-beautify-mkdir/js-beautify.js when using -r"
|
||||
exit 1
|
||||
}
|
||||
|
||||
$CLI_SCRIPT -o /tmp/js-beautify-mkdir/js-beautify.js /tmp/js-beautify-mkdir/js-beautify.js && test /tmp/js-beautify-mkdir/js-beautify.js -nt /tmp/js-beautify-mkdir/js-beautify-old.js && {
|
||||
echo "js-beautify should not replace unchanged file /tmp/js-beautify-mkdir/js-beautify.js when using -o and same file name"
|
||||
exit 1
|
||||
}
|
||||
|
||||
$CLI_SCRIPT -o /tmp/js-beautify-mkdir/js-beautify.js /tmp/js-beautify-mkdir/js-beautify-old.js && test /tmp/js-beautify-mkdir/js-beautify.js -nt /tmp/js-beautify-mkdir/js-beautify-old.js && {
|
||||
echo "js-beautify should not replace unchanged file /tmp/js-beautify-mkdir/js-beautify.js when using -o and different file name"
|
||||
exit 1
|
||||
}
|
||||
|
||||
$CLI_SCRIPT $SCRIPT_DIR/../bin/css-beautify.js | diff -q $SCRIPT_DIR/../bin/css-beautify.js - && {
|
||||
echo "js-beautify output for $SCRIPT_DIR/../bin/css-beautify.js was expected to be different."
|
||||
exit 1
|
||||
}
|
||||
|
||||
export HOME=
|
||||
export USERPROFILE=
|
||||
$CLI_SCRIPT -o /tmp/js-beautify-mkdir/example1-default.js $SCRIPT_DIR/resources/example1.js
|
||||
|
||||
cd $SCRIPT_DIR/resources/indent11chars
|
||||
$CLI_SCRIPT /tmp/js-beautify-mkdir/example1-default.js | diff -q /tmp/js-beautify-mkdir/example1-default.js - && {
|
||||
echo "js-beautify output for /tmp/js-beautify-mkdir/example1-default.js was expected to be different based on CWD settings."
|
||||
exit 1
|
||||
}
|
||||
cd $SCRIPT_DIR/resources/indent11chars/subDir1/subDir2
|
||||
$CLI_SCRIPT /tmp/js-beautify-mkdir/example1-default.js | diff -q /tmp/js-beautify-mkdir/example1-default.js - && {
|
||||
echo "js-beautify output for /tmp/js-beautify-mkdir/example1-default.js was expected to be different based on CWD parent folder settings."
|
||||
exit 1
|
||||
}
|
||||
cd $SCRIPT_DIR
|
||||
|
||||
|
||||
export HOME=$SCRIPT_DIR/resources/indent11chars
|
||||
$CLI_SCRIPT /tmp/js-beautify-mkdir/example1-default.js | diff -q /tmp/js-beautify-mkdir/example1-default.js - && {
|
||||
echo "js-beautify output for /tmp/js-beautify-mkdir/example1-default.js was expected to be different based on HOME settings."
|
||||
exit 1
|
||||
}
|
||||
|
||||
export HOME=
|
||||
export USERPROFILE=$SCRIPT_DIR/resources/indent11chars
|
||||
$CLI_SCRIPT /tmp/js-beautify-mkdir/example1-default.js | diff -q /tmp/js-beautify-mkdir/example1-default.js - && {
|
||||
echo "js-beautify output for /tmp/js-beautify-mkdir/example1-default.js was expected to be different based on USERPROFILE settings."
|
||||
exit 1
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
test_smoke_js_beautify()
|
||||
{
|
||||
echo ----------------------------------------
|
||||
echo Testing js-beautify functionality...
|
||||
node $SCRIPT_DIR/node-beautify-tests.js || exit 1
|
||||
node $SCRIPT_DIR/amd-beautify-tests.js || exit 1
|
||||
}
|
||||
|
||||
|
||||
test_performance_js_beautify()
|
||||
{
|
||||
echo ----------------------------------------
|
||||
echo Testing js-beautify performance...
|
||||
node $SCRIPT_DIR/node-beautify-perf-tests.js || exit 1
|
||||
echo ----------------------------------------
|
||||
|
||||
}
|
||||
|
||||
test_cli_common css-beautify
|
||||
test_cli_common html-beautify
|
||||
test_cli_common js-beautify
|
||||
|
||||
test_cli_js_beautify
|
||||
test_smoke_js_beautify
|
||||
test_performance_js_beautify
|
||||
|
||||
echo ----------------------------------------
|
||||
echo $0 - PASSED.
|
||||
echo ----------------------------------------
|
||||
Loading…
Reference in a new issue