MDL-75483 atto_html: Upgrade JS Beautify to 1.14.6

This commit is contained in:
David Woloszyn 2022-10-12 16:29:44 +11:00
parent e4c5a12a1c
commit 8f5799109e
8 changed files with 496 additions and 268 deletions

View File

@ -12,7 +12,7 @@
<location>yui/src/beautify</location>
<name>jsbeautify</name>
<description>Beautify HTML code in Atto.</description>
<version>1.14.0</version>
<version>1.14.6</version>
<license>MIT</license>
<repository>https://github.com/beautify-web/js-beautify</repository>
<copyrights>

View File

@ -143,8 +143,8 @@ var legacy_beautify_js;
var Beautifier = __webpack_require__(1).Beautifier,
Options = __webpack_require__(5).Options;
var Beautifier = (__webpack_require__(1).Beautifier),
Options = (__webpack_require__(5).Options);
function js_beautify(js_source_text, options) {
var beautifier = new Beautifier(js_source_text, options);
@ -191,14 +191,14 @@ module.exports.defaultOptions = function() {
var Output = __webpack_require__(2).Output;
var Token = __webpack_require__(3).Token;
var Output = (__webpack_require__(2).Output);
var Token = (__webpack_require__(3).Token);
var acorn = __webpack_require__(4);
var Options = __webpack_require__(5).Options;
var Tokenizer = __webpack_require__(7).Tokenizer;
var line_starters = __webpack_require__(7).line_starters;
var positionable_operators = __webpack_require__(7).positionable_operators;
var TOKEN = __webpack_require__(7).TOKEN;
var Options = (__webpack_require__(5).Options);
var Tokenizer = (__webpack_require__(7).Tokenizer);
var line_starters = (__webpack_require__(7).line_starters);
var positionable_operators = (__webpack_require__(7).positionable_operators);
var TOKEN = (__webpack_require__(7).TOKEN);
function in_array(what, arr) {
@ -348,12 +348,14 @@ Beautifier.prototype.create_flags = function(flags_base, mode) {
inline_frame: false,
if_block: false,
else_block: false,
class_start_block: false, // class A { INSIDE HERE } or class B extends C { INSIDE HERE }
do_block: false,
do_while: false,
import_block: false,
in_case_statement: false, // switch(..){ INSIDE HERE }
in_case: false, // we're on the exact line with "case 0:"
case_body: false, // the indented case-action block
case_block: false, // the indented case-action block is wrapped with {}
indentation_level: next_indent_level,
alignment: 0,
line_indent_level: flags_base ? flags_base.line_indent_level : next_indent_level,
@ -759,6 +761,8 @@ Beautifier.prototype.handle_start_expr = function(current_token) {
(peek_back_two.text === '*' && (peek_back_three.text === '{' || peek_back_three.text === ','))) {
this._output.space_before_token = true;
}
} else if (this._flags.parent && this._flags.parent.class_start_block) {
this._output.space_before_token = true;
}
}
} else {
@ -853,10 +857,10 @@ Beautifier.prototype.handle_start_block = function(current_token) {
)) {
// We don't support TypeScript,but we didn't break it for a very long time.
// We'll try to keep not breaking it.
if (!in_array(this._last_last_text, ['class', 'interface'])) {
this.set_mode(MODE.ObjectLiteral);
} else {
if (in_array(this._last_last_text, ['class', 'interface']) && !in_array(second_token.text, [':', ','])) {
this.set_mode(MODE.BlockStatement);
} else {
this.set_mode(MODE.ObjectLiteral);
}
} else if (this._flags.last_token.type === TOKEN.OPERATOR && this._flags.last_token.text === '=>') {
// arrow function: (param1, paramN) => { statements }
@ -873,6 +877,12 @@ Beautifier.prototype.handle_start_block = function(current_token) {
this.set_mode(MODE.BlockStatement);
}
if (this._flags.last_token) {
if (reserved_array(this._flags.last_token.previous, ['class', 'extends'])) {
this._flags.class_start_block = true;
}
}
var empty_braces = !next_token.comments_before && next_token.text === '}';
var empty_anonymous_function = empty_braces && this._flags.last_word === 'function' &&
this._flags.last_token.type === TOKEN.END_EXPR;
@ -972,7 +982,7 @@ Beautifier.prototype.handle_word = function(current_token) {
if (current_token.type === TOKEN.RESERVED) {
if (in_array(current_token.text, ['set', 'get']) && this._flags.mode !== MODE.ObjectLiteral) {
current_token.type = TOKEN.WORD;
} else if (current_token.text === 'import' && this._tokens.peek().text === '(') {
} else if (current_token.text === 'import' && in_array(this._tokens.peek().text, ['(', '.'])) {
current_token.type = TOKEN.WORD;
} else if (in_array(current_token.text, ['as', 'from']) && !this._flags.import_block) {
current_token.type = TOKEN.WORD;
@ -1032,7 +1042,7 @@ Beautifier.prototype.handle_word = function(current_token) {
if (this._flags.in_case_statement && reserved_array(current_token, ['case', 'default'])) {
this.print_newline();
if (this._flags.last_token.type !== TOKEN.END_BLOCK && (this._flags.case_body || this._options.jslint_happy)) {
if (!this._flags.case_block && (this._flags.case_body || this._options.jslint_happy)) {
// switch cases following one another
this.deindent();
}
@ -1313,13 +1323,6 @@ Beautifier.prototype.handle_operator = function(current_token) {
this.handle_whitespace_and_comments(current_token, preserve_statement_flags);
}
if (reserved_array(this._flags.last_token, special_words)) {
// "return" had a special handling in TK_WORD. Now we need to return the favor
this._output.space_before_token = true;
this.print_token(current_token);
return;
}
// hack for actionscript's import .*;
if (current_token.text === '*' && this._flags.last_token.type === TOKEN.DOT) {
this.print_token(current_token);
@ -1346,7 +1349,9 @@ Beautifier.prototype.handle_operator = function(current_token) {
if (this._tokens.peek().type !== TOKEN.START_BLOCK) {
this.indent();
this.print_newline();
this._flags.case_block = false;
} else {
this._flags.case_block = true;
this._output.space_before_token = true;
}
return;
@ -1444,8 +1449,12 @@ Beautifier.prototype.handle_operator = function(current_token) {
// http://www.ecma-international.org/ecma-262/5.1/#sec-7.9.1
// if there is a newline between -- or ++ and anything else we should preserve it.
if (current_token.newlines && (current_token.text === '--' || current_token.text === '++')) {
this.print_newline(false, true);
if (current_token.newlines && (current_token.text === '--' || current_token.text === '++' || current_token.text === '~')) {
var new_line_needed = reserved_array(this._flags.last_token, special_words) && current_token.newlines;
if (new_line_needed && (this._previous_flags.if_block || this._previous_flags.else_block)) {
this.restore_mode();
}
this.print_newline(new_line_needed, true);
}
if (this._flags.last_token.text === ';' && is_expression(this._flags.mode)) {
@ -1585,6 +1594,10 @@ Beautifier.prototype.handle_dot = function(current_token) {
this.handle_whitespace_and_comments(current_token, true);
}
if (this._flags.last_token.text.match('^[0-9]+$')) {
this._output.space_before_token = true;
}
if (reserved_array(this._flags.last_token, special_words)) {
this._output.space_before_token = false;
} else {
@ -2204,7 +2217,7 @@ exports.allLineBreaks = new RegExp(exports.lineBreak.source, 'g');
var BaseOptions = __webpack_require__(6).Options;
var BaseOptions = (__webpack_require__(6).Options);
var validPositionValues = ['before-newline', 'after-newline', 'preserve-newline'];
@ -2502,13 +2515,13 @@ module.exports.mergeOpts = _mergeOpts;
var InputScanner = __webpack_require__(8).InputScanner;
var BaseTokenizer = __webpack_require__(9).Tokenizer;
var BASETOKEN = __webpack_require__(9).TOKEN;
var Directives = __webpack_require__(13).Directives;
var InputScanner = (__webpack_require__(8).InputScanner);
var BaseTokenizer = (__webpack_require__(9).Tokenizer);
var BASETOKEN = (__webpack_require__(9).TOKEN);
var Directives = (__webpack_require__(13).Directives);
var acorn = __webpack_require__(4);
var Pattern = __webpack_require__(12).Pattern;
var TemplatablePattern = __webpack_require__(14).TemplatablePattern;
var Pattern = (__webpack_require__(12).Pattern);
var TemplatablePattern = (__webpack_require__(14).TemplatablePattern);
function in_array(what, arr) {
@ -2548,7 +2561,7 @@ var digit = /[0-9]/;
var dot_pattern = /[^\d\.]/;
var positionable_operators = (
">>> === !== " +
">>> === !== &&= ??= ||= " +
"<< && >= ** != == <= >> || ?? |> " +
"< / - + > : & % ? ^ | *").split(' ');
@ -2556,7 +2569,7 @@ var positionable_operators = (
// Also, you must update possitionable operators separately from punct
var punct =
">>>= " +
"... >>= <<= === >>> !== **= " +
"... >>= <<= === >>> !== **= &&= ??= ||= " +
"=> ^= :: /= << <= == && -= >= >> != -- += ** || ?? ++ %= &= *= |= |> " +
"= ! ? > < : / ^ - + * & % ~ |";
@ -2569,7 +2582,7 @@ var punct_pattern = new RegExp(punct);
// words which should always start on new line.
var line_starters = 'continue,try,throw,return,var,let,const,if,switch,case,default,for,while,break,function,import,export'.split(',');
var reserved_words = line_starters.concat(['do', 'in', 'of', 'else', 'get', 'set', 'new', 'catch', 'finally', 'typeof', 'yield', 'async', 'await', 'from', 'as']);
var reserved_words = line_starters.concat(['do', 'in', 'of', 'else', 'get', 'set', 'new', 'catch', 'finally', 'typeof', 'yield', 'async', 'await', 'from', 'as', 'class', 'extends']);
var reserved_word_pattern = new RegExp('^(?:' + reserved_words.join('|') + ')$');
// var template_pattern = /(?:(?:<\?php|<\?=)[\s\S]*?\?>)|(?:<%[\s\S]*?%>)/g;
@ -2600,7 +2613,7 @@ var Tokenizer = function(input_string, options) {
html_comment_end: pattern_reader.matching(/-->/),
include: pattern_reader.starting_with(/#include/).until_after(acorn.lineBreak),
shebang: pattern_reader.starting_with(/#!/).until_after(acorn.lineBreak),
xml: pattern_reader.matching(/[\s\S]*?<(\/?)([-a-zA-Z:0-9_.]+|{[\s\S]+?}|!\[CDATA\[[\s\S]*?\]\]|)(\s+{[\s\S]+?}|\s+[-a-zA-Z:0-9_.]+|\s+[-a-zA-Z:0-9_.]+\s*=\s*('[^']*'|"[^"]*"|{[\s\S]+?}))*\s*(\/?)\s*>/),
xml: pattern_reader.matching(/[\s\S]*?<(\/?)([-a-zA-Z:0-9_.]+|{[^}]+?}|!\[CDATA\[[^\]]*?\]\]|)(\s*{[^}]+?}|\s+[-a-zA-Z:0-9_.]+|\s+[-a-zA-Z:0-9_.]+\s*=\s*('[^']*'|"[^"]*"|{([^{}]|{[^}]+?})+?}))*\s*(\/?)\s*>/),
single_quote: templatable.until(/['\\\n\r\u2028\u2029]/),
double_quote: templatable.until(/["\\\n\r\u2028\u2029]/),
template_text: templatable.until(/[`\\$]/),
@ -2660,7 +2673,8 @@ Tokenizer.prototype._read_word = function(previous_token) {
if (!(previous_token.type === TOKEN.DOT ||
(previous_token.type === TOKEN.RESERVED && (previous_token.text === 'set' || previous_token.text === 'get'))) &&
reserved_word_pattern.test(resulting_string)) {
if (resulting_string === 'in' || resulting_string === 'of') { // hack for 'in' and 'of' operators
if ((resulting_string === 'in' || resulting_string === 'of') &&
(previous_token.type === TOKEN.WORD || previous_token.type === TOKEN.STRING)) { // hack for 'in' and 'of' operators
return this._create_token(TOKEN.OPERATOR, resulting_string);
}
return this._create_token(TOKEN.RESERVED, resulting_string);
@ -3272,10 +3286,10 @@ module.exports.InputScanner = InputScanner;
var InputScanner = __webpack_require__(8).InputScanner;
var Token = __webpack_require__(3).Token;
var TokenStream = __webpack_require__(10).TokenStream;
var WhitespacePattern = __webpack_require__(11).WhitespacePattern;
var InputScanner = (__webpack_require__(8).InputScanner);
var Token = (__webpack_require__(3).Token);
var TokenStream = (__webpack_require__(10).TokenStream);
var WhitespacePattern = (__webpack_require__(11).WhitespacePattern);
var TOKEN = {
START: 'TK_START',
@ -3502,7 +3516,7 @@ module.exports.TokenStream = TokenStream;
var Pattern = __webpack_require__(12).Pattern;
var Pattern = (__webpack_require__(12).Pattern);
function WhitespacePattern(input_scanner, parent) {
Pattern.call(this, input_scanner, parent);
@ -3781,7 +3795,7 @@ module.exports.Directives = Directives;
var Pattern = __webpack_require__(12).Pattern;
var Pattern = (__webpack_require__(12).Pattern);
var template_names = {
@ -5027,8 +5041,8 @@ module.exports.Directives = Directives;
var Beautifier = __webpack_require__(16).Beautifier,
Options = __webpack_require__(17).Options;
var Beautifier = (__webpack_require__(16).Beautifier),
Options = (__webpack_require__(17).Options);
function css_beautify(source_text, options) {
var beautifier = new Beautifier(source_text, options);
@ -5075,10 +5089,10 @@ module.exports.defaultOptions = function() {
var Options = __webpack_require__(17).Options;
var Output = __webpack_require__(2).Output;
var InputScanner = __webpack_require__(8).InputScanner;
var Directives = __webpack_require__(13).Directives;
var Options = (__webpack_require__(17).Options);
var Output = (__webpack_require__(2).Output);
var InputScanner = (__webpack_require__(8).InputScanner);
var Directives = (__webpack_require__(13).Directives);
var directives_core = new Directives(/\/\*/, /\*\//);
@ -5114,6 +5128,10 @@ function Beautifier(source_text, options) {
"@supports": true,
"@document": true
};
this.NON_SEMICOLON_NEWLINE_PROPERTY = [
"grid-template-areas",
"grid-template"
];
}
@ -5238,7 +5256,9 @@ Beautifier.prototype.beautify = function() {
var enteringConditionalGroup = false;
var insideAtExtend = false;
var insideAtImport = false;
var insideScssMap = false;
var topCharacter = this._ch;
var insideNonSemiColonValues = false;
var whitespace;
var isAfterSpace;
var previous_ch;
@ -5290,7 +5310,7 @@ Beautifier.prototype.beautify = function() {
// Ensures any new lines following the comment are preserved
this.eatWhitespace(true);
} else if (this._ch === '@') {
} else if (this._ch === '@' || this._ch === '$') {
this.preserveSingleSpace(isAfterSpace);
// deal with less propery mixins @{...}
@ -5361,7 +5381,12 @@ Beautifier.prototype.beautify = function() {
this.indent();
this._output.set_indent(this._indentLevel);
} else {
this.indent();
// inside mixin and first param is object
if (previous_ch === '(') {
this._output.space_before_token = false;
} else if (previous_ch !== ',') {
this.indent();
}
this.print_string(this._ch);
}
@ -5393,7 +5418,21 @@ Beautifier.prototype.beautify = function() {
this._output.add_new_line(true);
}
}
if (this._input.peek() === ')') {
this._output.trim(true);
if (this._options.brace_style === "expand") {
this._output.add_new_line(true);
}
}
} else if (this._ch === ":") {
for (var i = 0; i < this.NON_SEMICOLON_NEWLINE_PROPERTY.length; i++) {
if (this._input.lookBack(this.NON_SEMICOLON_NEWLINE_PROPERTY[i])) {
insideNonSemiColonValues = true;
break;
}
}
if ((insideRule || enteringConditionalGroup) && !(this._input.lookBack("&") || this.foundNestedPseudoClass()) && !this._input.lookBack("(") && !insideAtExtend && parenLevel === 0) {
// 'property: value' delimiter
// which could be in a conditional group query
@ -5422,10 +5461,12 @@ Beautifier.prototype.beautify = function() {
}
}
} else if (this._ch === '"' || this._ch === '\'') {
this.preserveSingleSpace(isAfterSpace);
var preserveQuoteSpace = previous_ch === '"' || previous_ch === '\'';
this.preserveSingleSpace(preserveQuoteSpace || isAfterSpace);
this.print_string(this._ch + this.eatString(this._ch));
this.eatWhitespace(true);
} else if (this._ch === ';') {
insideNonSemiColonValues = false;
if (parenLevel === 0) {
if (insidePropertyValue) {
this.outdent();
@ -5465,22 +5506,39 @@ Beautifier.prototype.beautify = function() {
}
}
} else {
this.preserveSingleSpace(isAfterSpace);
var space_needed = false;
if (this._input.lookBack("with")) {
// look back is not an accurate solution, we need tokens to confirm without whitespaces
space_needed = true;
}
this.preserveSingleSpace(isAfterSpace || space_needed);
this.print_string(this._ch);
this.eatWhitespace();
parenLevel++;
this.indent();
// handle scss/sass map
if (insidePropertyValue && previous_ch === "$" && this._options.selector_separator_newline) {
this._output.add_new_line();
insideScssMap = true;
} else {
this.eatWhitespace();
parenLevel++;
this.indent();
}
}
} else if (this._ch === ')') {
if (parenLevel) {
parenLevel--;
this.outdent();
}
if (insideScssMap && this._input.peek() === ";" && this._options.selector_separator_newline) {
insideScssMap = false;
this.outdent();
this._output.add_new_line();
}
this.print_string(this._ch);
} else if (this._ch === ',') {
this.print_string(this._ch);
this.eatWhitespace(true);
if (this._options.selector_separator_newline && !insidePropertyValue && parenLevel === 0 && !insideAtImport && !insideAtExtend) {
if (this._options.selector_separator_newline && (!insidePropertyValue || insideScssMap) && parenLevel === 0 && !insideAtImport && !insideAtExtend) {
this._output.add_new_line();
} else {
this._output.space_before_token = true;
@ -5511,11 +5569,16 @@ Beautifier.prototype.beautify = function() {
this._ch = '';
}
} else if (this._ch === '!' && !this._input.lookBack("\\")) { // !important
this.print_string(' ');
this._output.space_before_token = true;
this.print_string(this._ch);
} else {
this.preserveSingleSpace(isAfterSpace);
var preserveAfterSpace = previous_ch === '"' || previous_ch === '\'';
this.preserveSingleSpace(preserveAfterSpace || isAfterSpace);
this.print_string(this._ch);
if (!this._output.just_added_newline() && this._input.peek() === '\n' && insideNonSemiColonValues) {
this._output.add_new_line();
}
}
}
@ -5561,7 +5624,7 @@ module.exports.Beautifier = Beautifier;
var BaseOptions = __webpack_require__(6).Options;
var BaseOptions = (__webpack_require__(6).Options);
function Options(options) {
BaseOptions.call(this, options, 'css');
@ -6649,10 +6712,10 @@ module.exports.InputScanner = InputScanner;
var InputScanner = __webpack_require__(8).InputScanner;
var Token = __webpack_require__(3).Token;
var TokenStream = __webpack_require__(10).TokenStream;
var WhitespacePattern = __webpack_require__(11).WhitespacePattern;
var InputScanner = (__webpack_require__(8).InputScanner);
var Token = (__webpack_require__(3).Token);
var TokenStream = (__webpack_require__(10).TokenStream);
var WhitespacePattern = (__webpack_require__(11).WhitespacePattern);
var TOKEN = {
START: 'TK_START',
@ -6879,7 +6942,7 @@ module.exports.TokenStream = TokenStream;
var Pattern = __webpack_require__(12).Pattern;
var Pattern = (__webpack_require__(12).Pattern);
function WhitespacePattern(input_scanner, parent) {
Pattern.call(this, input_scanner, parent);
@ -7158,7 +7221,7 @@ module.exports.Directives = Directives;
var Pattern = __webpack_require__(12).Pattern;
var Pattern = (__webpack_require__(12).Pattern);
var template_names = {
@ -7378,8 +7441,8 @@ module.exports.TemplatablePattern = TemplatablePattern;
var Beautifier = __webpack_require__(19).Beautifier,
Options = __webpack_require__(20).Options;
var Beautifier = (__webpack_require__(19).Beautifier),
Options = (__webpack_require__(20).Options);
function style_html(html_source, options, js_beautify, css_beautify) {
var beautifier = new Beautifier(html_source, options, js_beautify, css_beautify);
@ -7426,10 +7489,10 @@ module.exports.defaultOptions = function() {
var Options = __webpack_require__(20).Options;
var Output = __webpack_require__(2).Output;
var Tokenizer = __webpack_require__(21).Tokenizer;
var TOKEN = __webpack_require__(21).TOKEN;
var Options = (__webpack_require__(20).Options);
var Output = (__webpack_require__(2).Output);
var Tokenizer = (__webpack_require__(21).Tokenizer);
var TOKEN = (__webpack_require__(21).TOKEN);
var lineBreak = /\r\n|[\r\n]/;
var allLineBreaks = /\r\n|[\r\n]/g;
@ -8005,14 +8068,19 @@ var TagOpenParserToken = function(parent, raw_token) {
tag_check_match = raw_token.text.match(/^<([^\s>]*)/);
this.tag_check = tag_check_match ? tag_check_match[1] : '';
} else {
tag_check_match = raw_token.text.match(/^{{(?:[\^]|#\*?)?([^\s}]+)/);
tag_check_match = raw_token.text.match(/^{{~?(?:[\^]|#\*?)?([^\s}]+)/);
this.tag_check = tag_check_match ? tag_check_match[1] : '';
// handle "{{#> myPartial}}
if (raw_token.text === '{{#>' && this.tag_check === '>' && raw_token.next !== null) {
this.tag_check = raw_token.next.text;
// handle "{{#> myPartial}}" or "{{~#> myPartial}}"
if ((raw_token.text.startsWith('{{#>') || raw_token.text.startsWith('{{~#>')) && this.tag_check[0] === '>') {
if (this.tag_check === '>' && raw_token.next !== null) {
this.tag_check = raw_token.next.text.split(' ')[0];
} else {
this.tag_check = raw_token.text.split('>')[1];
}
}
}
this.tag_check = this.tag_check.toLowerCase();
if (raw_token.type === TOKEN.COMMENT) {
@ -8024,9 +8092,17 @@ var TagOpenParserToken = function(parent, raw_token) {
this.is_end_tag = !this.is_start_tag ||
(raw_token.closed && raw_token.closed.text === '/>');
// if whitespace handler ~ included (i.e. {{~#if true}}), handlebars tags start at pos 3 not pos 2
var handlebar_starts = 2;
if (this.tag_start_char === '{' && this.text.length >= 3) {
if (this.text.charAt(2) === '~') {
handlebar_starts = 3;
}
}
// handlebars tags that don't start with # or ^ are single_tags, and so also start and end.
this.is_end_tag = this.is_end_tag ||
(this.tag_start_char === '{' && (this.text.length < 3 || (/[^#\^]/.test(this.text.charAt(2)))));
(this.tag_start_char === '{' && (this.text.length < 3 || (/[^#\^]/.test(this.text.charAt(handlebar_starts)))));
}
};
@ -8297,7 +8373,7 @@ module.exports.Beautifier = Beautifier;
var BaseOptions = __webpack_require__(6).Options;
var BaseOptions = (__webpack_require__(6).Options);
function Options(options) {
BaseOptions.call(this, options, 'html');
@ -8394,11 +8470,11 @@ module.exports.Options = Options;
var BaseTokenizer = __webpack_require__(9).Tokenizer;
var BASETOKEN = __webpack_require__(9).TOKEN;
var Directives = __webpack_require__(13).Directives;
var TemplatablePattern = __webpack_require__(14).TemplatablePattern;
var Pattern = __webpack_require__(12).Pattern;
var BaseTokenizer = (__webpack_require__(9).Tokenizer);
var BASETOKEN = (__webpack_require__(9).TOKEN);
var Directives = (__webpack_require__(13).Directives);
var TemplatablePattern = (__webpack_require__(14).TemplatablePattern);
var Pattern = (__webpack_require__(12).Pattern);
var TOKEN = {
TAG_OPEN: 'TK_TAG_OPEN',

File diff suppressed because one or more lines are too long

View File

@ -143,8 +143,8 @@ var legacy_beautify_js;
var Beautifier = __webpack_require__(1).Beautifier,
Options = __webpack_require__(5).Options;
var Beautifier = (__webpack_require__(1).Beautifier),
Options = (__webpack_require__(5).Options);
function js_beautify(js_source_text, options) {
var beautifier = new Beautifier(js_source_text, options);
@ -191,14 +191,14 @@ module.exports.defaultOptions = function() {
var Output = __webpack_require__(2).Output;
var Token = __webpack_require__(3).Token;
var Output = (__webpack_require__(2).Output);
var Token = (__webpack_require__(3).Token);
var acorn = __webpack_require__(4);
var Options = __webpack_require__(5).Options;
var Tokenizer = __webpack_require__(7).Tokenizer;
var line_starters = __webpack_require__(7).line_starters;
var positionable_operators = __webpack_require__(7).positionable_operators;
var TOKEN = __webpack_require__(7).TOKEN;
var Options = (__webpack_require__(5).Options);
var Tokenizer = (__webpack_require__(7).Tokenizer);
var line_starters = (__webpack_require__(7).line_starters);
var positionable_operators = (__webpack_require__(7).positionable_operators);
var TOKEN = (__webpack_require__(7).TOKEN);
function in_array(what, arr) {
@ -348,12 +348,14 @@ Beautifier.prototype.create_flags = function(flags_base, mode) {
inline_frame: false,
if_block: false,
else_block: false,
class_start_block: false, // class A { INSIDE HERE } or class B extends C { INSIDE HERE }
do_block: false,
do_while: false,
import_block: false,
in_case_statement: false, // switch(..){ INSIDE HERE }
in_case: false, // we're on the exact line with "case 0:"
case_body: false, // the indented case-action block
case_block: false, // the indented case-action block is wrapped with {}
indentation_level: next_indent_level,
alignment: 0,
line_indent_level: flags_base ? flags_base.line_indent_level : next_indent_level,
@ -759,6 +761,8 @@ Beautifier.prototype.handle_start_expr = function(current_token) {
(peek_back_two.text === '*' && (peek_back_three.text === '{' || peek_back_three.text === ','))) {
this._output.space_before_token = true;
}
} else if (this._flags.parent && this._flags.parent.class_start_block) {
this._output.space_before_token = true;
}
}
} else {
@ -853,10 +857,10 @@ Beautifier.prototype.handle_start_block = function(current_token) {
)) {
// We don't support TypeScript,but we didn't break it for a very long time.
// We'll try to keep not breaking it.
if (!in_array(this._last_last_text, ['class', 'interface'])) {
this.set_mode(MODE.ObjectLiteral);
} else {
if (in_array(this._last_last_text, ['class', 'interface']) && !in_array(second_token.text, [':', ','])) {
this.set_mode(MODE.BlockStatement);
} else {
this.set_mode(MODE.ObjectLiteral);
}
} else if (this._flags.last_token.type === TOKEN.OPERATOR && this._flags.last_token.text === '=>') {
// arrow function: (param1, paramN) => { statements }
@ -873,6 +877,12 @@ Beautifier.prototype.handle_start_block = function(current_token) {
this.set_mode(MODE.BlockStatement);
}
if (this._flags.last_token) {
if (reserved_array(this._flags.last_token.previous, ['class', 'extends'])) {
this._flags.class_start_block = true;
}
}
var empty_braces = !next_token.comments_before && next_token.text === '}';
var empty_anonymous_function = empty_braces && this._flags.last_word === 'function' &&
this._flags.last_token.type === TOKEN.END_EXPR;
@ -972,7 +982,7 @@ Beautifier.prototype.handle_word = function(current_token) {
if (current_token.type === TOKEN.RESERVED) {
if (in_array(current_token.text, ['set', 'get']) && this._flags.mode !== MODE.ObjectLiteral) {
current_token.type = TOKEN.WORD;
} else if (current_token.text === 'import' && this._tokens.peek().text === '(') {
} else if (current_token.text === 'import' && in_array(this._tokens.peek().text, ['(', '.'])) {
current_token.type = TOKEN.WORD;
} else if (in_array(current_token.text, ['as', 'from']) && !this._flags.import_block) {
current_token.type = TOKEN.WORD;
@ -1032,7 +1042,7 @@ Beautifier.prototype.handle_word = function(current_token) {
if (this._flags.in_case_statement && reserved_array(current_token, ['case', 'default'])) {
this.print_newline();
if (this._flags.last_token.type !== TOKEN.END_BLOCK && (this._flags.case_body || this._options.jslint_happy)) {
if (!this._flags.case_block && (this._flags.case_body || this._options.jslint_happy)) {
// switch cases following one another
this.deindent();
}
@ -1313,13 +1323,6 @@ Beautifier.prototype.handle_operator = function(current_token) {
this.handle_whitespace_and_comments(current_token, preserve_statement_flags);
}
if (reserved_array(this._flags.last_token, special_words)) {
// "return" had a special handling in TK_WORD. Now we need to return the favor
this._output.space_before_token = true;
this.print_token(current_token);
return;
}
// hack for actionscript's import .*;
if (current_token.text === '*' && this._flags.last_token.type === TOKEN.DOT) {
this.print_token(current_token);
@ -1346,7 +1349,9 @@ Beautifier.prototype.handle_operator = function(current_token) {
if (this._tokens.peek().type !== TOKEN.START_BLOCK) {
this.indent();
this.print_newline();
this._flags.case_block = false;
} else {
this._flags.case_block = true;
this._output.space_before_token = true;
}
return;
@ -1444,8 +1449,12 @@ Beautifier.prototype.handle_operator = function(current_token) {
// http://www.ecma-international.org/ecma-262/5.1/#sec-7.9.1
// if there is a newline between -- or ++ and anything else we should preserve it.
if (current_token.newlines && (current_token.text === '--' || current_token.text === '++')) {
this.print_newline(false, true);
if (current_token.newlines && (current_token.text === '--' || current_token.text === '++' || current_token.text === '~')) {
var new_line_needed = reserved_array(this._flags.last_token, special_words) && current_token.newlines;
if (new_line_needed && (this._previous_flags.if_block || this._previous_flags.else_block)) {
this.restore_mode();
}
this.print_newline(new_line_needed, true);
}
if (this._flags.last_token.text === ';' && is_expression(this._flags.mode)) {
@ -1585,6 +1594,10 @@ Beautifier.prototype.handle_dot = function(current_token) {
this.handle_whitespace_and_comments(current_token, true);
}
if (this._flags.last_token.text.match('^[0-9]+$')) {
this._output.space_before_token = true;
}
if (reserved_array(this._flags.last_token, special_words)) {
this._output.space_before_token = false;
} else {
@ -2204,7 +2217,7 @@ exports.allLineBreaks = new RegExp(exports.lineBreak.source, 'g');
var BaseOptions = __webpack_require__(6).Options;
var BaseOptions = (__webpack_require__(6).Options);
var validPositionValues = ['before-newline', 'after-newline', 'preserve-newline'];
@ -2502,13 +2515,13 @@ module.exports.mergeOpts = _mergeOpts;
var InputScanner = __webpack_require__(8).InputScanner;
var BaseTokenizer = __webpack_require__(9).Tokenizer;
var BASETOKEN = __webpack_require__(9).TOKEN;
var Directives = __webpack_require__(13).Directives;
var InputScanner = (__webpack_require__(8).InputScanner);
var BaseTokenizer = (__webpack_require__(9).Tokenizer);
var BASETOKEN = (__webpack_require__(9).TOKEN);
var Directives = (__webpack_require__(13).Directives);
var acorn = __webpack_require__(4);
var Pattern = __webpack_require__(12).Pattern;
var TemplatablePattern = __webpack_require__(14).TemplatablePattern;
var Pattern = (__webpack_require__(12).Pattern);
var TemplatablePattern = (__webpack_require__(14).TemplatablePattern);
function in_array(what, arr) {
@ -2548,7 +2561,7 @@ var digit = /[0-9]/;
var dot_pattern = /[^\d\.]/;
var positionable_operators = (
">>> === !== " +
">>> === !== &&= ??= ||= " +
"<< && >= ** != == <= >> || ?? |> " +
"< / - + > : & % ? ^ | *").split(' ');
@ -2556,7 +2569,7 @@ var positionable_operators = (
// Also, you must update possitionable operators separately from punct
var punct =
">>>= " +
"... >>= <<= === >>> !== **= " +
"... >>= <<= === >>> !== **= &&= ??= ||= " +
"=> ^= :: /= << <= == && -= >= >> != -- += ** || ?? ++ %= &= *= |= |> " +
"= ! ? > < : / ^ - + * & % ~ |";
@ -2569,7 +2582,7 @@ var punct_pattern = new RegExp(punct);
// words which should always start on new line.
var line_starters = 'continue,try,throw,return,var,let,const,if,switch,case,default,for,while,break,function,import,export'.split(',');
var reserved_words = line_starters.concat(['do', 'in', 'of', 'else', 'get', 'set', 'new', 'catch', 'finally', 'typeof', 'yield', 'async', 'await', 'from', 'as']);
var reserved_words = line_starters.concat(['do', 'in', 'of', 'else', 'get', 'set', 'new', 'catch', 'finally', 'typeof', 'yield', 'async', 'await', 'from', 'as', 'class', 'extends']);
var reserved_word_pattern = new RegExp('^(?:' + reserved_words.join('|') + ')$');
// var template_pattern = /(?:(?:<\?php|<\?=)[\s\S]*?\?>)|(?:<%[\s\S]*?%>)/g;
@ -2600,7 +2613,7 @@ var Tokenizer = function(input_string, options) {
html_comment_end: pattern_reader.matching(/-->/),
include: pattern_reader.starting_with(/#include/).until_after(acorn.lineBreak),
shebang: pattern_reader.starting_with(/#!/).until_after(acorn.lineBreak),
xml: pattern_reader.matching(/[\s\S]*?<(\/?)([-a-zA-Z:0-9_.]+|{[\s\S]+?}|!\[CDATA\[[\s\S]*?\]\]|)(\s+{[\s\S]+?}|\s+[-a-zA-Z:0-9_.]+|\s+[-a-zA-Z:0-9_.]+\s*=\s*('[^']*'|"[^"]*"|{[\s\S]+?}))*\s*(\/?)\s*>/),
xml: pattern_reader.matching(/[\s\S]*?<(\/?)([-a-zA-Z:0-9_.]+|{[^}]+?}|!\[CDATA\[[^\]]*?\]\]|)(\s*{[^}]+?}|\s+[-a-zA-Z:0-9_.]+|\s+[-a-zA-Z:0-9_.]+\s*=\s*('[^']*'|"[^"]*"|{([^{}]|{[^}]+?})+?}))*\s*(\/?)\s*>/),
single_quote: templatable.until(/['\\\n\r\u2028\u2029]/),
double_quote: templatable.until(/["\\\n\r\u2028\u2029]/),
template_text: templatable.until(/[`\\$]/),
@ -2660,7 +2673,8 @@ Tokenizer.prototype._read_word = function(previous_token) {
if (!(previous_token.type === TOKEN.DOT ||
(previous_token.type === TOKEN.RESERVED && (previous_token.text === 'set' || previous_token.text === 'get'))) &&
reserved_word_pattern.test(resulting_string)) {
if (resulting_string === 'in' || resulting_string === 'of') { // hack for 'in' and 'of' operators
if ((resulting_string === 'in' || resulting_string === 'of') &&
(previous_token.type === TOKEN.WORD || previous_token.type === TOKEN.STRING)) { // hack for 'in' and 'of' operators
return this._create_token(TOKEN.OPERATOR, resulting_string);
}
return this._create_token(TOKEN.RESERVED, resulting_string);
@ -3272,10 +3286,10 @@ module.exports.InputScanner = InputScanner;
var InputScanner = __webpack_require__(8).InputScanner;
var Token = __webpack_require__(3).Token;
var TokenStream = __webpack_require__(10).TokenStream;
var WhitespacePattern = __webpack_require__(11).WhitespacePattern;
var InputScanner = (__webpack_require__(8).InputScanner);
var Token = (__webpack_require__(3).Token);
var TokenStream = (__webpack_require__(10).TokenStream);
var WhitespacePattern = (__webpack_require__(11).WhitespacePattern);
var TOKEN = {
START: 'TK_START',
@ -3502,7 +3516,7 @@ module.exports.TokenStream = TokenStream;
var Pattern = __webpack_require__(12).Pattern;
var Pattern = (__webpack_require__(12).Pattern);
function WhitespacePattern(input_scanner, parent) {
Pattern.call(this, input_scanner, parent);
@ -3781,7 +3795,7 @@ module.exports.Directives = Directives;
var Pattern = __webpack_require__(12).Pattern;
var Pattern = (__webpack_require__(12).Pattern);
var template_names = {
@ -5027,8 +5041,8 @@ module.exports.Directives = Directives;
var Beautifier = __webpack_require__(16).Beautifier,
Options = __webpack_require__(17).Options;
var Beautifier = (__webpack_require__(16).Beautifier),
Options = (__webpack_require__(17).Options);
function css_beautify(source_text, options) {
var beautifier = new Beautifier(source_text, options);
@ -5075,10 +5089,10 @@ module.exports.defaultOptions = function() {
var Options = __webpack_require__(17).Options;
var Output = __webpack_require__(2).Output;
var InputScanner = __webpack_require__(8).InputScanner;
var Directives = __webpack_require__(13).Directives;
var Options = (__webpack_require__(17).Options);
var Output = (__webpack_require__(2).Output);
var InputScanner = (__webpack_require__(8).InputScanner);
var Directives = (__webpack_require__(13).Directives);
var directives_core = new Directives(/\/\*/, /\*\//);
@ -5114,6 +5128,10 @@ function Beautifier(source_text, options) {
"@supports": true,
"@document": true
};
this.NON_SEMICOLON_NEWLINE_PROPERTY = [
"grid-template-areas",
"grid-template"
];
}
@ -5238,7 +5256,9 @@ Beautifier.prototype.beautify = function() {
var enteringConditionalGroup = false;
var insideAtExtend = false;
var insideAtImport = false;
var insideScssMap = false;
var topCharacter = this._ch;
var insideNonSemiColonValues = false;
var whitespace;
var isAfterSpace;
var previous_ch;
@ -5290,7 +5310,7 @@ Beautifier.prototype.beautify = function() {
// Ensures any new lines following the comment are preserved
this.eatWhitespace(true);
} else if (this._ch === '@') {
} else if (this._ch === '@' || this._ch === '$') {
this.preserveSingleSpace(isAfterSpace);
// deal with less propery mixins @{...}
@ -5361,7 +5381,12 @@ Beautifier.prototype.beautify = function() {
this.indent();
this._output.set_indent(this._indentLevel);
} else {
this.indent();
// inside mixin and first param is object
if (previous_ch === '(') {
this._output.space_before_token = false;
} else if (previous_ch !== ',') {
this.indent();
}
this.print_string(this._ch);
}
@ -5393,7 +5418,21 @@ Beautifier.prototype.beautify = function() {
this._output.add_new_line(true);
}
}
if (this._input.peek() === ')') {
this._output.trim(true);
if (this._options.brace_style === "expand") {
this._output.add_new_line(true);
}
}
} else if (this._ch === ":") {
for (var i = 0; i < this.NON_SEMICOLON_NEWLINE_PROPERTY.length; i++) {
if (this._input.lookBack(this.NON_SEMICOLON_NEWLINE_PROPERTY[i])) {
insideNonSemiColonValues = true;
break;
}
}
if ((insideRule || enteringConditionalGroup) && !(this._input.lookBack("&") || this.foundNestedPseudoClass()) && !this._input.lookBack("(") && !insideAtExtend && parenLevel === 0) {
// 'property: value' delimiter
// which could be in a conditional group query
@ -5422,10 +5461,12 @@ Beautifier.prototype.beautify = function() {
}
}
} else if (this._ch === '"' || this._ch === '\'') {
this.preserveSingleSpace(isAfterSpace);
var preserveQuoteSpace = previous_ch === '"' || previous_ch === '\'';
this.preserveSingleSpace(preserveQuoteSpace || isAfterSpace);
this.print_string(this._ch + this.eatString(this._ch));
this.eatWhitespace(true);
} else if (this._ch === ';') {
insideNonSemiColonValues = false;
if (parenLevel === 0) {
if (insidePropertyValue) {
this.outdent();
@ -5465,22 +5506,39 @@ Beautifier.prototype.beautify = function() {
}
}
} else {
this.preserveSingleSpace(isAfterSpace);
var space_needed = false;
if (this._input.lookBack("with")) {
// look back is not an accurate solution, we need tokens to confirm without whitespaces
space_needed = true;
}
this.preserveSingleSpace(isAfterSpace || space_needed);
this.print_string(this._ch);
this.eatWhitespace();
parenLevel++;
this.indent();
// handle scss/sass map
if (insidePropertyValue && previous_ch === "$" && this._options.selector_separator_newline) {
this._output.add_new_line();
insideScssMap = true;
} else {
this.eatWhitespace();
parenLevel++;
this.indent();
}
}
} else if (this._ch === ')') {
if (parenLevel) {
parenLevel--;
this.outdent();
}
if (insideScssMap && this._input.peek() === ";" && this._options.selector_separator_newline) {
insideScssMap = false;
this.outdent();
this._output.add_new_line();
}
this.print_string(this._ch);
} else if (this._ch === ',') {
this.print_string(this._ch);
this.eatWhitespace(true);
if (this._options.selector_separator_newline && !insidePropertyValue && parenLevel === 0 && !insideAtImport && !insideAtExtend) {
if (this._options.selector_separator_newline && (!insidePropertyValue || insideScssMap) && parenLevel === 0 && !insideAtImport && !insideAtExtend) {
this._output.add_new_line();
} else {
this._output.space_before_token = true;
@ -5511,11 +5569,16 @@ Beautifier.prototype.beautify = function() {
this._ch = '';
}
} else if (this._ch === '!' && !this._input.lookBack("\\")) { // !important
this.print_string(' ');
this._output.space_before_token = true;
this.print_string(this._ch);
} else {
this.preserveSingleSpace(isAfterSpace);
var preserveAfterSpace = previous_ch === '"' || previous_ch === '\'';
this.preserveSingleSpace(preserveAfterSpace || isAfterSpace);
this.print_string(this._ch);
if (!this._output.just_added_newline() && this._input.peek() === '\n' && insideNonSemiColonValues) {
this._output.add_new_line();
}
}
}
@ -5561,7 +5624,7 @@ module.exports.Beautifier = Beautifier;
var BaseOptions = __webpack_require__(6).Options;
var BaseOptions = (__webpack_require__(6).Options);
function Options(options) {
BaseOptions.call(this, options, 'css');
@ -6649,10 +6712,10 @@ module.exports.InputScanner = InputScanner;
var InputScanner = __webpack_require__(8).InputScanner;
var Token = __webpack_require__(3).Token;
var TokenStream = __webpack_require__(10).TokenStream;
var WhitespacePattern = __webpack_require__(11).WhitespacePattern;
var InputScanner = (__webpack_require__(8).InputScanner);
var Token = (__webpack_require__(3).Token);
var TokenStream = (__webpack_require__(10).TokenStream);
var WhitespacePattern = (__webpack_require__(11).WhitespacePattern);
var TOKEN = {
START: 'TK_START',
@ -6879,7 +6942,7 @@ module.exports.TokenStream = TokenStream;
var Pattern = __webpack_require__(12).Pattern;
var Pattern = (__webpack_require__(12).Pattern);
function WhitespacePattern(input_scanner, parent) {
Pattern.call(this, input_scanner, parent);
@ -7158,7 +7221,7 @@ module.exports.Directives = Directives;
var Pattern = __webpack_require__(12).Pattern;
var Pattern = (__webpack_require__(12).Pattern);
var template_names = {
@ -7378,8 +7441,8 @@ module.exports.TemplatablePattern = TemplatablePattern;
var Beautifier = __webpack_require__(19).Beautifier,
Options = __webpack_require__(20).Options;
var Beautifier = (__webpack_require__(19).Beautifier),
Options = (__webpack_require__(20).Options);
function style_html(html_source, options, js_beautify, css_beautify) {
var beautifier = new Beautifier(html_source, options, js_beautify, css_beautify);
@ -7426,10 +7489,10 @@ module.exports.defaultOptions = function() {
var Options = __webpack_require__(20).Options;
var Output = __webpack_require__(2).Output;
var Tokenizer = __webpack_require__(21).Tokenizer;
var TOKEN = __webpack_require__(21).TOKEN;
var Options = (__webpack_require__(20).Options);
var Output = (__webpack_require__(2).Output);
var Tokenizer = (__webpack_require__(21).Tokenizer);
var TOKEN = (__webpack_require__(21).TOKEN);
var lineBreak = /\r\n|[\r\n]/;
var allLineBreaks = /\r\n|[\r\n]/g;
@ -8005,14 +8068,19 @@ var TagOpenParserToken = function(parent, raw_token) {
tag_check_match = raw_token.text.match(/^<([^\s>]*)/);
this.tag_check = tag_check_match ? tag_check_match[1] : '';
} else {
tag_check_match = raw_token.text.match(/^{{(?:[\^]|#\*?)?([^\s}]+)/);
tag_check_match = raw_token.text.match(/^{{~?(?:[\^]|#\*?)?([^\s}]+)/);
this.tag_check = tag_check_match ? tag_check_match[1] : '';
// handle "{{#> myPartial}}
if (raw_token.text === '{{#>' && this.tag_check === '>' && raw_token.next !== null) {
this.tag_check = raw_token.next.text;
// handle "{{#> myPartial}}" or "{{~#> myPartial}}"
if ((raw_token.text.startsWith('{{#>') || raw_token.text.startsWith('{{~#>')) && this.tag_check[0] === '>') {
if (this.tag_check === '>' && raw_token.next !== null) {
this.tag_check = raw_token.next.text.split(' ')[0];
} else {
this.tag_check = raw_token.text.split('>')[1];
}
}
}
this.tag_check = this.tag_check.toLowerCase();
if (raw_token.type === TOKEN.COMMENT) {
@ -8024,9 +8092,17 @@ var TagOpenParserToken = function(parent, raw_token) {
this.is_end_tag = !this.is_start_tag ||
(raw_token.closed && raw_token.closed.text === '/>');
// if whitespace handler ~ included (i.e. {{~#if true}}), handlebars tags start at pos 3 not pos 2
var handlebar_starts = 2;
if (this.tag_start_char === '{' && this.text.length >= 3) {
if (this.text.charAt(2) === '~') {
handlebar_starts = 3;
}
}
// handlebars tags that don't start with # or ^ are single_tags, and so also start and end.
this.is_end_tag = this.is_end_tag ||
(this.tag_start_char === '{' && (this.text.length < 3 || (/[^#\^]/.test(this.text.charAt(2)))));
(this.tag_start_char === '{' && (this.text.length < 3 || (/[^#\^]/.test(this.text.charAt(handlebar_starts)))));
}
};
@ -8297,7 +8373,7 @@ module.exports.Beautifier = Beautifier;
var BaseOptions = __webpack_require__(6).Options;
var BaseOptions = (__webpack_require__(6).Options);
function Options(options) {
BaseOptions.call(this, options, 'html');
@ -8394,11 +8470,11 @@ module.exports.Options = Options;
var BaseTokenizer = __webpack_require__(9).Tokenizer;
var BASETOKEN = __webpack_require__(9).TOKEN;
var Directives = __webpack_require__(13).Directives;
var TemplatablePattern = __webpack_require__(14).TemplatablePattern;
var Pattern = __webpack_require__(12).Pattern;
var BaseTokenizer = (__webpack_require__(9).Tokenizer);
var BASETOKEN = (__webpack_require__(9).TOKEN);
var Directives = (__webpack_require__(13).Directives);
var TemplatablePattern = (__webpack_require__(14).TemplatablePattern);
var Pattern = (__webpack_require__(12).Pattern);
var TOKEN = {
TAG_OPEN: 'TK_TAG_OPEN',

View File

@ -1003,8 +1003,8 @@ module.exports.Directives = Directives;
var Beautifier = __webpack_require__(16).Beautifier,
Options = __webpack_require__(17).Options;
var Beautifier = (__webpack_require__(16).Beautifier),
Options = (__webpack_require__(17).Options);
function css_beautify(source_text, options) {
var beautifier = new Beautifier(source_text, options);
@ -1051,10 +1051,10 @@ module.exports.defaultOptions = function() {
var Options = __webpack_require__(17).Options;
var Output = __webpack_require__(2).Output;
var InputScanner = __webpack_require__(8).InputScanner;
var Directives = __webpack_require__(13).Directives;
var Options = (__webpack_require__(17).Options);
var Output = (__webpack_require__(2).Output);
var InputScanner = (__webpack_require__(8).InputScanner);
var Directives = (__webpack_require__(13).Directives);
var directives_core = new Directives(/\/\*/, /\*\//);
@ -1090,6 +1090,10 @@ function Beautifier(source_text, options) {
"@supports": true,
"@document": true
};
this.NON_SEMICOLON_NEWLINE_PROPERTY = [
"grid-template-areas",
"grid-template"
];
}
@ -1214,7 +1218,9 @@ Beautifier.prototype.beautify = function() {
var enteringConditionalGroup = false;
var insideAtExtend = false;
var insideAtImport = false;
var insideScssMap = false;
var topCharacter = this._ch;
var insideNonSemiColonValues = false;
var whitespace;
var isAfterSpace;
var previous_ch;
@ -1266,7 +1272,7 @@ Beautifier.prototype.beautify = function() {
// Ensures any new lines following the comment are preserved
this.eatWhitespace(true);
} else if (this._ch === '@') {
} else if (this._ch === '@' || this._ch === '$') {
this.preserveSingleSpace(isAfterSpace);
// deal with less propery mixins @{...}
@ -1337,7 +1343,12 @@ Beautifier.prototype.beautify = function() {
this.indent();
this._output.set_indent(this._indentLevel);
} else {
this.indent();
// inside mixin and first param is object
if (previous_ch === '(') {
this._output.space_before_token = false;
} else if (previous_ch !== ',') {
this.indent();
}
this.print_string(this._ch);
}
@ -1369,7 +1380,21 @@ Beautifier.prototype.beautify = function() {
this._output.add_new_line(true);
}
}
if (this._input.peek() === ')') {
this._output.trim(true);
if (this._options.brace_style === "expand") {
this._output.add_new_line(true);
}
}
} else if (this._ch === ":") {
for (var i = 0; i < this.NON_SEMICOLON_NEWLINE_PROPERTY.length; i++) {
if (this._input.lookBack(this.NON_SEMICOLON_NEWLINE_PROPERTY[i])) {
insideNonSemiColonValues = true;
break;
}
}
if ((insideRule || enteringConditionalGroup) && !(this._input.lookBack("&") || this.foundNestedPseudoClass()) && !this._input.lookBack("(") && !insideAtExtend && parenLevel === 0) {
// 'property: value' delimiter
// which could be in a conditional group query
@ -1398,10 +1423,12 @@ Beautifier.prototype.beautify = function() {
}
}
} else if (this._ch === '"' || this._ch === '\'') {
this.preserveSingleSpace(isAfterSpace);
var preserveQuoteSpace = previous_ch === '"' || previous_ch === '\'';
this.preserveSingleSpace(preserveQuoteSpace || isAfterSpace);
this.print_string(this._ch + this.eatString(this._ch));
this.eatWhitespace(true);
} else if (this._ch === ';') {
insideNonSemiColonValues = false;
if (parenLevel === 0) {
if (insidePropertyValue) {
this.outdent();
@ -1441,22 +1468,39 @@ Beautifier.prototype.beautify = function() {
}
}
} else {
this.preserveSingleSpace(isAfterSpace);
var space_needed = false;
if (this._input.lookBack("with")) {
// look back is not an accurate solution, we need tokens to confirm without whitespaces
space_needed = true;
}
this.preserveSingleSpace(isAfterSpace || space_needed);
this.print_string(this._ch);
this.eatWhitespace();
parenLevel++;
this.indent();
// handle scss/sass map
if (insidePropertyValue && previous_ch === "$" && this._options.selector_separator_newline) {
this._output.add_new_line();
insideScssMap = true;
} else {
this.eatWhitespace();
parenLevel++;
this.indent();
}
}
} else if (this._ch === ')') {
if (parenLevel) {
parenLevel--;
this.outdent();
}
if (insideScssMap && this._input.peek() === ";" && this._options.selector_separator_newline) {
insideScssMap = false;
this.outdent();
this._output.add_new_line();
}
this.print_string(this._ch);
} else if (this._ch === ',') {
this.print_string(this._ch);
this.eatWhitespace(true);
if (this._options.selector_separator_newline && !insidePropertyValue && parenLevel === 0 && !insideAtImport && !insideAtExtend) {
if (this._options.selector_separator_newline && (!insidePropertyValue || insideScssMap) && parenLevel === 0 && !insideAtImport && !insideAtExtend) {
this._output.add_new_line();
} else {
this._output.space_before_token = true;
@ -1487,11 +1531,16 @@ Beautifier.prototype.beautify = function() {
this._ch = '';
}
} else if (this._ch === '!' && !this._input.lookBack("\\")) { // !important
this.print_string(' ');
this._output.space_before_token = true;
this.print_string(this._ch);
} else {
this.preserveSingleSpace(isAfterSpace);
var preserveAfterSpace = previous_ch === '"' || previous_ch === '\'';
this.preserveSingleSpace(preserveAfterSpace || isAfterSpace);
this.print_string(this._ch);
if (!this._output.just_added_newline() && this._input.peek() === '\n' && insideNonSemiColonValues) {
this._output.add_new_line();
}
}
}
@ -1537,7 +1586,7 @@ module.exports.Beautifier = Beautifier;
var BaseOptions = __webpack_require__(6).Options;
var BaseOptions = (__webpack_require__(6).Options);
function Options(options) {
BaseOptions.call(this, options, 'css');

View File

@ -999,10 +999,10 @@ module.exports.InputScanner = InputScanner;
var InputScanner = __webpack_require__(8).InputScanner;
var Token = __webpack_require__(3).Token;
var TokenStream = __webpack_require__(10).TokenStream;
var WhitespacePattern = __webpack_require__(11).WhitespacePattern;
var InputScanner = (__webpack_require__(8).InputScanner);
var Token = (__webpack_require__(3).Token);
var TokenStream = (__webpack_require__(10).TokenStream);
var WhitespacePattern = (__webpack_require__(11).WhitespacePattern);
var TOKEN = {
START: 'TK_START',
@ -1229,7 +1229,7 @@ module.exports.TokenStream = TokenStream;
var Pattern = __webpack_require__(12).Pattern;
var Pattern = (__webpack_require__(12).Pattern);
function WhitespacePattern(input_scanner, parent) {
Pattern.call(this, input_scanner, parent);
@ -1508,7 +1508,7 @@ module.exports.Directives = Directives;
var Pattern = __webpack_require__(12).Pattern;
var Pattern = (__webpack_require__(12).Pattern);
var template_names = {
@ -1728,8 +1728,8 @@ module.exports.TemplatablePattern = TemplatablePattern;
var Beautifier = __webpack_require__(19).Beautifier,
Options = __webpack_require__(20).Options;
var Beautifier = (__webpack_require__(19).Beautifier),
Options = (__webpack_require__(20).Options);
function style_html(html_source, options, js_beautify, css_beautify) {
var beautifier = new Beautifier(html_source, options, js_beautify, css_beautify);
@ -1776,10 +1776,10 @@ module.exports.defaultOptions = function() {
var Options = __webpack_require__(20).Options;
var Output = __webpack_require__(2).Output;
var Tokenizer = __webpack_require__(21).Tokenizer;
var TOKEN = __webpack_require__(21).TOKEN;
var Options = (__webpack_require__(20).Options);
var Output = (__webpack_require__(2).Output);
var Tokenizer = (__webpack_require__(21).Tokenizer);
var TOKEN = (__webpack_require__(21).TOKEN);
var lineBreak = /\r\n|[\r\n]/;
var allLineBreaks = /\r\n|[\r\n]/g;
@ -2355,14 +2355,19 @@ var TagOpenParserToken = function(parent, raw_token) {
tag_check_match = raw_token.text.match(/^<([^\s>]*)/);
this.tag_check = tag_check_match ? tag_check_match[1] : '';
} else {
tag_check_match = raw_token.text.match(/^{{(?:[\^]|#\*?)?([^\s}]+)/);
tag_check_match = raw_token.text.match(/^{{~?(?:[\^]|#\*?)?([^\s}]+)/);
this.tag_check = tag_check_match ? tag_check_match[1] : '';
// handle "{{#> myPartial}}
if (raw_token.text === '{{#>' && this.tag_check === '>' && raw_token.next !== null) {
this.tag_check = raw_token.next.text;
// handle "{{#> myPartial}}" or "{{~#> myPartial}}"
if ((raw_token.text.startsWith('{{#>') || raw_token.text.startsWith('{{~#>')) && this.tag_check[0] === '>') {
if (this.tag_check === '>' && raw_token.next !== null) {
this.tag_check = raw_token.next.text.split(' ')[0];
} else {
this.tag_check = raw_token.text.split('>')[1];
}
}
}
this.tag_check = this.tag_check.toLowerCase();
if (raw_token.type === TOKEN.COMMENT) {
@ -2374,9 +2379,17 @@ var TagOpenParserToken = function(parent, raw_token) {
this.is_end_tag = !this.is_start_tag ||
(raw_token.closed && raw_token.closed.text === '/>');
// if whitespace handler ~ included (i.e. {{~#if true}}), handlebars tags start at pos 3 not pos 2
var handlebar_starts = 2;
if (this.tag_start_char === '{' && this.text.length >= 3) {
if (this.text.charAt(2) === '~') {
handlebar_starts = 3;
}
}
// handlebars tags that don't start with # or ^ are single_tags, and so also start and end.
this.is_end_tag = this.is_end_tag ||
(this.tag_start_char === '{' && (this.text.length < 3 || (/[^#\^]/.test(this.text.charAt(2)))));
(this.tag_start_char === '{' && (this.text.length < 3 || (/[^#\^]/.test(this.text.charAt(handlebar_starts)))));
}
};
@ -2647,7 +2660,7 @@ module.exports.Beautifier = Beautifier;
var BaseOptions = __webpack_require__(6).Options;
var BaseOptions = (__webpack_require__(6).Options);
function Options(options) {
BaseOptions.call(this, options, 'html');
@ -2744,11 +2757,11 @@ module.exports.Options = Options;
var BaseTokenizer = __webpack_require__(9).Tokenizer;
var BASETOKEN = __webpack_require__(9).TOKEN;
var Directives = __webpack_require__(13).Directives;
var TemplatablePattern = __webpack_require__(14).TemplatablePattern;
var Pattern = __webpack_require__(12).Pattern;
var BaseTokenizer = (__webpack_require__(9).Tokenizer);
var BASETOKEN = (__webpack_require__(9).TOKEN);
var Directives = (__webpack_require__(13).Directives);
var TemplatablePattern = (__webpack_require__(14).TemplatablePattern);
var Pattern = (__webpack_require__(12).Pattern);
var TOKEN = {
TAG_OPEN: 'TK_TAG_OPEN',

View File

@ -125,8 +125,8 @@ var legacy_beautify_js;
var Beautifier = __webpack_require__(1).Beautifier,
Options = __webpack_require__(5).Options;
var Beautifier = (__webpack_require__(1).Beautifier),
Options = (__webpack_require__(5).Options);
function js_beautify(js_source_text, options) {
var beautifier = new Beautifier(js_source_text, options);
@ -173,14 +173,14 @@ module.exports.defaultOptions = function() {
var Output = __webpack_require__(2).Output;
var Token = __webpack_require__(3).Token;
var Output = (__webpack_require__(2).Output);
var Token = (__webpack_require__(3).Token);
var acorn = __webpack_require__(4);
var Options = __webpack_require__(5).Options;
var Tokenizer = __webpack_require__(7).Tokenizer;
var line_starters = __webpack_require__(7).line_starters;
var positionable_operators = __webpack_require__(7).positionable_operators;
var TOKEN = __webpack_require__(7).TOKEN;
var Options = (__webpack_require__(5).Options);
var Tokenizer = (__webpack_require__(7).Tokenizer);
var line_starters = (__webpack_require__(7).line_starters);
var positionable_operators = (__webpack_require__(7).positionable_operators);
var TOKEN = (__webpack_require__(7).TOKEN);
function in_array(what, arr) {
@ -330,12 +330,14 @@ Beautifier.prototype.create_flags = function(flags_base, mode) {
inline_frame: false,
if_block: false,
else_block: false,
class_start_block: false, // class A { INSIDE HERE } or class B extends C { INSIDE HERE }
do_block: false,
do_while: false,
import_block: false,
in_case_statement: false, // switch(..){ INSIDE HERE }
in_case: false, // we're on the exact line with "case 0:"
case_body: false, // the indented case-action block
case_block: false, // the indented case-action block is wrapped with {}
indentation_level: next_indent_level,
alignment: 0,
line_indent_level: flags_base ? flags_base.line_indent_level : next_indent_level,
@ -741,6 +743,8 @@ Beautifier.prototype.handle_start_expr = function(current_token) {
(peek_back_two.text === '*' && (peek_back_three.text === '{' || peek_back_three.text === ','))) {
this._output.space_before_token = true;
}
} else if (this._flags.parent && this._flags.parent.class_start_block) {
this._output.space_before_token = true;
}
}
} else {
@ -835,10 +839,10 @@ Beautifier.prototype.handle_start_block = function(current_token) {
)) {
// We don't support TypeScript,but we didn't break it for a very long time.
// We'll try to keep not breaking it.
if (!in_array(this._last_last_text, ['class', 'interface'])) {
this.set_mode(MODE.ObjectLiteral);
} else {
if (in_array(this._last_last_text, ['class', 'interface']) && !in_array(second_token.text, [':', ','])) {
this.set_mode(MODE.BlockStatement);
} else {
this.set_mode(MODE.ObjectLiteral);
}
} else if (this._flags.last_token.type === TOKEN.OPERATOR && this._flags.last_token.text === '=>') {
// arrow function: (param1, paramN) => { statements }
@ -855,6 +859,12 @@ Beautifier.prototype.handle_start_block = function(current_token) {
this.set_mode(MODE.BlockStatement);
}
if (this._flags.last_token) {
if (reserved_array(this._flags.last_token.previous, ['class', 'extends'])) {
this._flags.class_start_block = true;
}
}
var empty_braces = !next_token.comments_before && next_token.text === '}';
var empty_anonymous_function = empty_braces && this._flags.last_word === 'function' &&
this._flags.last_token.type === TOKEN.END_EXPR;
@ -954,7 +964,7 @@ Beautifier.prototype.handle_word = function(current_token) {
if (current_token.type === TOKEN.RESERVED) {
if (in_array(current_token.text, ['set', 'get']) && this._flags.mode !== MODE.ObjectLiteral) {
current_token.type = TOKEN.WORD;
} else if (current_token.text === 'import' && this._tokens.peek().text === '(') {
} else if (current_token.text === 'import' && in_array(this._tokens.peek().text, ['(', '.'])) {
current_token.type = TOKEN.WORD;
} else if (in_array(current_token.text, ['as', 'from']) && !this._flags.import_block) {
current_token.type = TOKEN.WORD;
@ -1014,7 +1024,7 @@ Beautifier.prototype.handle_word = function(current_token) {
if (this._flags.in_case_statement && reserved_array(current_token, ['case', 'default'])) {
this.print_newline();
if (this._flags.last_token.type !== TOKEN.END_BLOCK && (this._flags.case_body || this._options.jslint_happy)) {
if (!this._flags.case_block && (this._flags.case_body || this._options.jslint_happy)) {
// switch cases following one another
this.deindent();
}
@ -1295,13 +1305,6 @@ Beautifier.prototype.handle_operator = function(current_token) {
this.handle_whitespace_and_comments(current_token, preserve_statement_flags);
}
if (reserved_array(this._flags.last_token, special_words)) {
// "return" had a special handling in TK_WORD. Now we need to return the favor
this._output.space_before_token = true;
this.print_token(current_token);
return;
}
// hack for actionscript's import .*;
if (current_token.text === '*' && this._flags.last_token.type === TOKEN.DOT) {
this.print_token(current_token);
@ -1328,7 +1331,9 @@ Beautifier.prototype.handle_operator = function(current_token) {
if (this._tokens.peek().type !== TOKEN.START_BLOCK) {
this.indent();
this.print_newline();
this._flags.case_block = false;
} else {
this._flags.case_block = true;
this._output.space_before_token = true;
}
return;
@ -1426,8 +1431,12 @@ Beautifier.prototype.handle_operator = function(current_token) {
// http://www.ecma-international.org/ecma-262/5.1/#sec-7.9.1
// if there is a newline between -- or ++ and anything else we should preserve it.
if (current_token.newlines && (current_token.text === '--' || current_token.text === '++')) {
this.print_newline(false, true);
if (current_token.newlines && (current_token.text === '--' || current_token.text === '++' || current_token.text === '~')) {
var new_line_needed = reserved_array(this._flags.last_token, special_words) && current_token.newlines;
if (new_line_needed && (this._previous_flags.if_block || this._previous_flags.else_block)) {
this.restore_mode();
}
this.print_newline(new_line_needed, true);
}
if (this._flags.last_token.text === ';' && is_expression(this._flags.mode)) {
@ -1567,6 +1576,10 @@ Beautifier.prototype.handle_dot = function(current_token) {
this.handle_whitespace_and_comments(current_token, true);
}
if (this._flags.last_token.text.match('^[0-9]+$')) {
this._output.space_before_token = true;
}
if (reserved_array(this._flags.last_token, special_words)) {
this._output.space_before_token = false;
} else {
@ -2186,7 +2199,7 @@ exports.allLineBreaks = new RegExp(exports.lineBreak.source, 'g');
var BaseOptions = __webpack_require__(6).Options;
var BaseOptions = (__webpack_require__(6).Options);
var validPositionValues = ['before-newline', 'after-newline', 'preserve-newline'];
@ -2484,13 +2497,13 @@ module.exports.mergeOpts = _mergeOpts;
var InputScanner = __webpack_require__(8).InputScanner;
var BaseTokenizer = __webpack_require__(9).Tokenizer;
var BASETOKEN = __webpack_require__(9).TOKEN;
var Directives = __webpack_require__(13).Directives;
var InputScanner = (__webpack_require__(8).InputScanner);
var BaseTokenizer = (__webpack_require__(9).Tokenizer);
var BASETOKEN = (__webpack_require__(9).TOKEN);
var Directives = (__webpack_require__(13).Directives);
var acorn = __webpack_require__(4);
var Pattern = __webpack_require__(12).Pattern;
var TemplatablePattern = __webpack_require__(14).TemplatablePattern;
var Pattern = (__webpack_require__(12).Pattern);
var TemplatablePattern = (__webpack_require__(14).TemplatablePattern);
function in_array(what, arr) {
@ -2530,7 +2543,7 @@ var digit = /[0-9]/;
var dot_pattern = /[^\d\.]/;
var positionable_operators = (
">>> === !== " +
">>> === !== &&= ??= ||= " +
"<< && >= ** != == <= >> || ?? |> " +
"< / - + > : & % ? ^ | *").split(' ');
@ -2538,7 +2551,7 @@ var positionable_operators = (
// Also, you must update possitionable operators separately from punct
var punct =
">>>= " +
"... >>= <<= === >>> !== **= " +
"... >>= <<= === >>> !== **= &&= ??= ||= " +
"=> ^= :: /= << <= == && -= >= >> != -- += ** || ?? ++ %= &= *= |= |> " +
"= ! ? > < : / ^ - + * & % ~ |";
@ -2551,7 +2564,7 @@ var punct_pattern = new RegExp(punct);
// words which should always start on new line.
var line_starters = 'continue,try,throw,return,var,let,const,if,switch,case,default,for,while,break,function,import,export'.split(',');
var reserved_words = line_starters.concat(['do', 'in', 'of', 'else', 'get', 'set', 'new', 'catch', 'finally', 'typeof', 'yield', 'async', 'await', 'from', 'as']);
var reserved_words = line_starters.concat(['do', 'in', 'of', 'else', 'get', 'set', 'new', 'catch', 'finally', 'typeof', 'yield', 'async', 'await', 'from', 'as', 'class', 'extends']);
var reserved_word_pattern = new RegExp('^(?:' + reserved_words.join('|') + ')$');
// var template_pattern = /(?:(?:<\?php|<\?=)[\s\S]*?\?>)|(?:<%[\s\S]*?%>)/g;
@ -2582,7 +2595,7 @@ var Tokenizer = function(input_string, options) {
html_comment_end: pattern_reader.matching(/-->/),
include: pattern_reader.starting_with(/#include/).until_after(acorn.lineBreak),
shebang: pattern_reader.starting_with(/#!/).until_after(acorn.lineBreak),
xml: pattern_reader.matching(/[\s\S]*?<(\/?)([-a-zA-Z:0-9_.]+|{[\s\S]+?}|!\[CDATA\[[\s\S]*?\]\]|)(\s+{[\s\S]+?}|\s+[-a-zA-Z:0-9_.]+|\s+[-a-zA-Z:0-9_.]+\s*=\s*('[^']*'|"[^"]*"|{[\s\S]+?}))*\s*(\/?)\s*>/),
xml: pattern_reader.matching(/[\s\S]*?<(\/?)([-a-zA-Z:0-9_.]+|{[^}]+?}|!\[CDATA\[[^\]]*?\]\]|)(\s*{[^}]+?}|\s+[-a-zA-Z:0-9_.]+|\s+[-a-zA-Z:0-9_.]+\s*=\s*('[^']*'|"[^"]*"|{([^{}]|{[^}]+?})+?}))*\s*(\/?)\s*>/),
single_quote: templatable.until(/['\\\n\r\u2028\u2029]/),
double_quote: templatable.until(/["\\\n\r\u2028\u2029]/),
template_text: templatable.until(/[`\\$]/),
@ -2642,7 +2655,8 @@ Tokenizer.prototype._read_word = function(previous_token) {
if (!(previous_token.type === TOKEN.DOT ||
(previous_token.type === TOKEN.RESERVED && (previous_token.text === 'set' || previous_token.text === 'get'))) &&
reserved_word_pattern.test(resulting_string)) {
if (resulting_string === 'in' || resulting_string === 'of') { // hack for 'in' and 'of' operators
if ((resulting_string === 'in' || resulting_string === 'of') &&
(previous_token.type === TOKEN.WORD || previous_token.type === TOKEN.STRING)) { // hack for 'in' and 'of' operators
return this._create_token(TOKEN.OPERATOR, resulting_string);
}
return this._create_token(TOKEN.RESERVED, resulting_string);
@ -3254,10 +3268,10 @@ module.exports.InputScanner = InputScanner;
var InputScanner = __webpack_require__(8).InputScanner;
var Token = __webpack_require__(3).Token;
var TokenStream = __webpack_require__(10).TokenStream;
var WhitespacePattern = __webpack_require__(11).WhitespacePattern;
var InputScanner = (__webpack_require__(8).InputScanner);
var Token = (__webpack_require__(3).Token);
var TokenStream = (__webpack_require__(10).TokenStream);
var WhitespacePattern = (__webpack_require__(11).WhitespacePattern);
var TOKEN = {
START: 'TK_START',
@ -3484,7 +3498,7 @@ module.exports.TokenStream = TokenStream;
var Pattern = __webpack_require__(12).Pattern;
var Pattern = (__webpack_require__(12).Pattern);
function WhitespacePattern(input_scanner, parent) {
Pattern.call(this, input_scanner, parent);
@ -3763,7 +3777,7 @@ module.exports.Directives = Directives;
var Pattern = __webpack_require__(12).Pattern;
var Pattern = (__webpack_require__(12).Pattern);
var template_names = {

View File

@ -1,7 +1,7 @@
Description of importing the js-beautify library into Moodle.
* Download the latest version from https://github.com/beautify-web/js-beautify/releases
* Copy lib/beautify*.js into lib/yui/src/beautify/js
* Copy LICENSE into lib/yui/src/beautify
* Update lib/thirdpartylibs.xml
* Copy lib/beautify*.js into lib/editor/atto/plugins/html/yui/src/beautify/js
* Copy LICENSE into lib/editor/atto/plugins/html/yui/src/beautify
* Update lib/editor/atto/plugins/html/thirdpartylibs.xml
* Rebuild the module