| 1 |
/*jshint curly:true, eqeqeq:true, laxbreak:true, noempty:false */ |
| 2 |
/* |
| 3 |
|
| 4 |
The MIT License (MIT) |
| 5 |
|
| 6 |
Copyright (c) 2007-2017 Einar Lielmanis, Liam Newman, and contributors. |
| 7 |
|
| 8 |
Permission is hereby granted, free of charge, to any person |
| 9 |
obtaining a copy of this software and associated documentation files |
| 10 |
(the "Software"), to deal in the Software without restriction, |
| 11 |
including without limitation the rights to use, copy, modify, merge, |
| 12 |
publish, distribute, sublicense, and/or sell copies of the Software, |
| 13 |
and to permit persons to whom the Software is furnished to do so, |
| 14 |
subject to the following conditions: |
| 15 |
|
| 16 |
The above copyright notice and this permission notice shall be |
| 17 |
included in all copies or substantial portions of the Software. |
| 18 |
|
| 19 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 20 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 21 |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 22 |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 23 |
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 24 |
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 25 |
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 26 |
SOFTWARE. |
| 27 |
|
| 28 |
|
| 29 |
Style HTML |
| 30 |
--------------- |
| 31 |
|
| 32 |
Written by Nochum Sossonko, (nsossonko@hotmail.com) |
| 33 |
|
| 34 |
Based on code initially developed by: Einar Lielmanis, <einar@jsbeautifier.org> |
| 35 |
http://jsbeautifier.org/ |
| 36 |
|
| 37 |
Usage: |
| 38 |
style_html(html_source); |
| 39 |
|
| 40 |
style_html(html_source, options); |
| 41 |
|
| 42 |
The options are: |
| 43 |
indent_inner_html (default false) — indent <head> and <body> sections, |
| 44 |
indent_size (default 4) — indentation size, |
| 45 |
indent_char (default space) — character to indent with, |
| 46 |
wrap_line_length (default 250) - maximum amount of characters per line (0 = disable) |
| 47 |
brace_style (default "collapse") - "collapse" | "expand" | "end-expand" | "none" |
| 48 |
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. |
| 49 |
unformatted (defaults to inline tags) - list of tags, that shouldn't be reformatted |
| 50 |
content_unformatted (defaults to pre tag) - list of tags, that its content shouldn't be reformatted |
| 51 |
indent_scripts (default normal) - "keep"|"separate"|"normal" |
| 52 |
preserve_newlines (default true) - whether existing line breaks before elements should be preserved |
| 53 |
Only works before elements, not inside tags or for text. |
| 54 |
max_preserve_newlines (default unlimited) - maximum number of line breaks to be preserved in one chunk |
| 55 |
indent_handlebars (default false) - format and indent {{#foo}} and {{/foo}} |
| 56 |
end_with_newline (false) - end with a newline |
| 57 |
extra_liners (default [head,body,/html]) -List of tags that should have an extra newline before them. |
| 58 |
|
| 59 |
e.g. |
| 60 |
|
| 61 |
style_html(html_source, { |
| 62 |
'indent_inner_html': false, |
| 63 |
'indent_size': 2, |
| 64 |
'indent_char': ' ', |
| 65 |
'wrap_line_length': 78, |
| 66 |
'brace_style': 'expand', |
| 67 |
'preserve_newlines': true, |
| 68 |
'max_preserve_newlines': 5, |
| 69 |
'indent_handlebars': false, |
| 70 |
'extra_liners': ['/html'] |
| 71 |
}); |
| 72 |
*/ |
| 73 |
|
| 74 |
(function() { |
| 75 |
|
| 76 |
// function trim(s) { |
| 77 |
// return s.replace(/^\s+|\s+$/g, ''); |
| 78 |
// } |
| 79 |
|
| 80 |
function ltrim(s) { |
| 81 |
return s.replace(/^\s+/g, ''); |
| 82 |
} |
| 83 |
|
| 84 |
function rtrim(s) { |
| 85 |
return s.replace(/\s+$/g, ''); |
| 86 |
} |
| 87 |
|
| 88 |
function mergeOpts(allOptions, targetType) { |
| 89 |
var finalOpts = {}; |
| 90 |
var name; |
| 91 |
|
| 92 |
for (name in allOptions) { |
| 93 |
if (name !== targetType) { |
| 94 |
finalOpts[name] = allOptions[name]; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
//merge in the per type settings for the targetType |
| 99 |
if (targetType in allOptions) { |
| 100 |
for (name in allOptions[targetType]) { |
| 101 |
finalOpts[name] = allOptions[targetType][name]; |
| 102 |
} |
| 103 |
} |
| 104 |
return finalOpts; |
| 105 |
} |
| 106 |
|
| 107 |
var lineBreak = /\r\n|[\n\r\u2028\u2029]/; |
| 108 |
var allLineBreaks = new RegExp(lineBreak.source, 'g'); |
| 109 |
|
| 110 |
function style_html(html_source, options, js_beautify, css_beautify) { |
| 111 |
//Wrapper function to invoke all the necessary constructors and deal with the output. |
| 112 |
|
| 113 |
var multi_parser, |
| 114 |
indent_inner_html, |
| 115 |
indent_body_inner_html, |
| 116 |
indent_head_inner_html, |
| 117 |
indent_size, |
| 118 |
indent_character, |
| 119 |
wrap_line_length, |
| 120 |
brace_style, |
| 121 |
unformatted, |
| 122 |
content_unformatted, |
| 123 |
preserve_newlines, |
| 124 |
max_preserve_newlines, |
| 125 |
indent_handlebars, |
| 126 |
wrap_attributes, |
| 127 |
wrap_attributes_indent_size, |
| 128 |
is_wrap_attributes_force, |
| 129 |
is_wrap_attributes_force_expand_multiline, |
| 130 |
is_wrap_attributes_force_aligned, |
| 131 |
end_with_newline, |
| 132 |
extra_liners, |
| 133 |
eol; |
| 134 |
|
| 135 |
options = options || {}; |
| 136 |
|
| 137 |
// Allow the setting of language/file-type specific options |
| 138 |
// with inheritance of overall settings |
| 139 |
options = mergeOpts(options, 'html'); |
| 140 |
|
| 141 |
// backwards compatibility to 1.3.4 |
| 142 |
if ((options.wrap_line_length === undefined || parseInt(options.wrap_line_length, 10) === 0) && |
| 143 |
(options.max_char !== undefined && parseInt(options.max_char, 10) !== 0)) { |
| 144 |
options.wrap_line_length = options.max_char; |
| 145 |
} |
| 146 |
|
| 147 |
indent_inner_html = (options.indent_inner_html === undefined) ? false : options.indent_inner_html; |
| 148 |
indent_body_inner_html = (options.indent_body_inner_html === undefined) ? true : options.indent_body_inner_html; |
| 149 |
indent_head_inner_html = (options.indent_head_inner_html === undefined) ? true : options.indent_head_inner_html; |
| 150 |
indent_size = (options.indent_size === undefined) ? 4 : parseInt(options.indent_size, 10); |
| 151 |
indent_character = (options.indent_char === undefined) ? ' ' : options.indent_char; |
| 152 |
brace_style = (options.brace_style === undefined) ? 'collapse' : options.brace_style; |
| 153 |
wrap_line_length = parseInt(options.wrap_line_length, 10) === 0 ? 32786 : parseInt(options.wrap_line_length || 250, 10); |
| 154 |
unformatted = options.unformatted || [ |
| 155 |
// https://www.w3.org/TR/html5/dom.html#phrasing-content |
| 156 |
'a', 'abbr', 'area', 'audio', 'b', 'bdi', 'bdo', 'br', 'button', 'canvas', 'cite', |
| 157 |
'code', 'data', 'datalist', 'del', 'dfn', 'em', 'embed', 'i', 'iframe', 'img', |
| 158 |
'input', 'ins', 'kbd', 'keygen', 'label', 'map', 'mark', 'math', 'meter', 'noscript', |
| 159 |
'object', 'output', 'progress', 'q', 'ruby', 's', 'samp', /* 'script', */ 'select', 'small', |
| 160 |
'span', 'strong', 'sub', 'sup', 'svg', 'template', 'textarea', 'time', 'u', 'var', |
| 161 |
'video', 'wbr', 'text', |
| 162 |
// prexisting - not sure of full effect of removing, leaving in |
| 163 |
'acronym', 'address', 'big', 'dt', 'ins', 'strike', 'tt', |
| 164 |
]; |
| 165 |
content_unformatted = options.content_unformatted || [ |
| 166 |
'pre', |
| 167 |
]; |
| 168 |
preserve_newlines = (options.preserve_newlines === undefined) ? true : options.preserve_newlines; |
| 169 |
max_preserve_newlines = preserve_newlines ? |
| 170 |
(isNaN(parseInt(options.max_preserve_newlines, 10)) ? 32786 : parseInt(options.max_preserve_newlines, 10)) : |
| 171 |
0; |
| 172 |
indent_handlebars = (options.indent_handlebars === undefined) ? false : options.indent_handlebars; |
| 173 |
wrap_attributes = (options.wrap_attributes === undefined) ? 'auto' : options.wrap_attributes; |
| 174 |
wrap_attributes_indent_size = (isNaN(parseInt(options.wrap_attributes_indent_size, 10))) ? indent_size : parseInt(options.wrap_attributes_indent_size, 10); |
| 175 |
is_wrap_attributes_force = wrap_attributes.substr(0, 'force'.length) === 'force'; |
| 176 |
is_wrap_attributes_force_expand_multiline = (wrap_attributes === 'force-expand-multiline'); |
| 177 |
is_wrap_attributes_force_aligned = (wrap_attributes === 'force-aligned'); |
| 178 |
end_with_newline = (options.end_with_newline === undefined) ? false : options.end_with_newline; |
| 179 |
extra_liners = (typeof options.extra_liners === 'object') && options.extra_liners ? |
| 180 |
options.extra_liners.concat() : (typeof options.extra_liners === 'string') ? |
| 181 |
options.extra_liners.split(',') : 'head,body,/html'.split(','); |
| 182 |
eol = options.eol ? options.eol : 'auto'; |
| 183 |
|
| 184 |
if (options.indent_with_tabs) { |
| 185 |
indent_character = '\t'; |
| 186 |
indent_size = 1; |
| 187 |
} |
| 188 |
|
| 189 |
if (eol === 'auto') { |
| 190 |
eol = '\n'; |
| 191 |
if (html_source && lineBreak.test(html_source || '')) { |
| 192 |
eol = html_source.match(lineBreak)[0]; |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
eol = eol.replace(/\\r/, '\r').replace(/\\n/, '\n'); |
| 197 |
|
| 198 |
// HACK: newline parsing inconsistent. This brute force normalizes the input. |
| 199 |
html_source = html_source.replace(allLineBreaks, '\n'); |
| 200 |
|
| 201 |
function Parser() { |
| 202 |
|
| 203 |
this.pos = 0; //Parser position |
| 204 |
this.token = ''; |
| 205 |
this.current_mode = 'CONTENT'; //reflects the current Parser mode: TAG/CONTENT |
| 206 |
this.tags = { //An object to hold tags, their position, and their parent-tags, initiated with default values |
| 207 |
parent: 'parent1', |
| 208 |
parentcount: 1, |
| 209 |
parent1: '' |
| 210 |
}; |
| 211 |
this.tag_type = ''; |
| 212 |
this.token_text = this.last_token = this.last_text = this.token_type = ''; |
| 213 |
this.newlines = 0; |
| 214 |
this.indent_content = indent_inner_html; |
| 215 |
this.indent_body_inner_html = indent_body_inner_html; |
| 216 |
this.indent_head_inner_html = indent_head_inner_html; |
| 217 |
|
| 218 |
this.Utils = { //Uilities made available to the various functions |
| 219 |
whitespace: "\n\r\t ".split(''), |
| 220 |
|
| 221 |
single_token: [ |
| 222 |
// HTLM void elements - aka self-closing tags - aka singletons |
| 223 |
// https://www.w3.org/html/wg/drafts/html/master/syntax.html#void-elements |
| 224 |
'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'keygen', |
| 225 |
'link', 'menuitem', 'meta', 'param', 'source', 'track', 'wbr', |
| 226 |
// NOTE: Optional tags - are not understood. |
| 227 |
// https://www.w3.org/TR/html5/syntax.html#optional-tags |
| 228 |
// The rules for optional tags are too complex for a simple list |
| 229 |
// Also, the content of these tags should still be indented in many cases. |
| 230 |
// 'li' is a good exmple. |
| 231 |
|
| 232 |
// Doctype and xml elements |
| 233 |
'!doctype', '?xml', |
| 234 |
// ?php tag |
| 235 |
'?php', |
| 236 |
// other tags that were in this list, keeping just in case |
| 237 |
'basefont', 'isindex' |
| 238 |
], |
| 239 |
extra_liners: extra_liners, //for tags that need a line of whitespace before them |
| 240 |
in_array: function(what, arr) { |
| 241 |
for (var i = 0; i < arr.length; i++) { |
| 242 |
if (what === arr[i]) { |
| 243 |
return true; |
| 244 |
} |
| 245 |
} |
| 246 |
return false; |
| 247 |
} |
| 248 |
}; |
| 249 |
|
| 250 |
// Return true if the given text is composed entirely of whitespace. |
| 251 |
this.is_whitespace = function(text) { |
| 252 |
for (var n = 0; n < text.length; n++) { |
| 253 |
if (!this.Utils.in_array(text.charAt(n), this.Utils.whitespace)) { |
| 254 |
return false; |
| 255 |
} |
| 256 |
} |
| 257 |
return true; |
| 258 |
}; |
| 259 |
|
| 260 |
this.traverse_whitespace = function() { |
| 261 |
var input_char = ''; |
| 262 |
|
| 263 |
input_char = this.input.charAt(this.pos); |
| 264 |
if (this.Utils.in_array(input_char, this.Utils.whitespace)) { |
| 265 |
this.newlines = 0; |
| 266 |
while (this.Utils.in_array(input_char, this.Utils.whitespace)) { |
| 267 |
if (preserve_newlines && input_char === '\n' && this.newlines <= max_preserve_newlines) { |
| 268 |
this.newlines += 1; |
| 269 |
} |
| 270 |
|
| 271 |
this.pos++; |
| 272 |
input_char = this.input.charAt(this.pos); |
| 273 |
} |
| 274 |
return true; |
| 275 |
} |
| 276 |
return false; |
| 277 |
}; |
| 278 |
|
| 279 |
// Append a space to the given content (string array) or, if we are |
| 280 |
// at the wrap_line_length, append a newline/indentation. |
| 281 |
// return true if a newline was added, false if a space was added |
| 282 |
this.space_or_wrap = function(content) { |
| 283 |
if (this.line_char_count >= this.wrap_line_length) { //insert a line when the wrap_line_length is reached |
| 284 |
this.print_newline(false, content); |
| 285 |
this.print_indentation(content); |
| 286 |
return true; |
| 287 |
} else { |
| 288 |
this.line_char_count++; |
| 289 |
content.push(' '); |
| 290 |
return false; |
| 291 |
} |
| 292 |
}; |
| 293 |
|
| 294 |
this.get_content = function() { //function to capture regular content between tags |
| 295 |
var input_char = '', |
| 296 |
content = [], |
| 297 |
handlebarsStarted = 0; |
| 298 |
|
| 299 |
while (this.input.charAt(this.pos) !== '<' || handlebarsStarted === 2) { |
| 300 |
if (this.pos >= this.input.length) { |
| 301 |
return content.length ? content.join('') : ['', 'TK_EOF']; |
| 302 |
} |
| 303 |
|
| 304 |
if (handlebarsStarted < 2 && this.traverse_whitespace()) { |
| 305 |
this.space_or_wrap(content); |
| 306 |
continue; |
| 307 |
} |
| 308 |
|
| 309 |
input_char = this.input.charAt(this.pos); |
| 310 |
|
| 311 |
if (indent_handlebars) { |
| 312 |
if (input_char === '{') { |
| 313 |
handlebarsStarted += 1; |
| 314 |
} else if (handlebarsStarted < 2) { |
| 315 |
handlebarsStarted = 0; |
| 316 |
} |
| 317 |
|
| 318 |
if (input_char === '}' && handlebarsStarted > 0) { |
| 319 |
if (handlebarsStarted-- === 0) { |
| 320 |
break; |
| 321 |
} |
| 322 |
} |
| 323 |
// Handlebars parsing is complicated. |
| 324 |
// {{#foo}} and {{/foo}} are formatted tags. |
| 325 |
// {{something}} should get treated as content, except: |
| 326 |
// {{else}} specifically behaves like {{#if}} and {{/if}} |
| 327 |
var peek3 = this.input.substr(this.pos, 3); |
| 328 |
if (peek3 === '{{#' || peek3 === '{{/') { |
| 329 |
// These are tags and not content. |
| 330 |
break; |
| 331 |
} else if (peek3 === '{{!') { |
| 332 |
return [this.get_tag(), 'TK_TAG_HANDLEBARS_COMMENT']; |
| 333 |
} else if (this.input.substr(this.pos, 2) === '{{') { |
| 334 |
if (this.get_tag(true) === '{{else}}') { |
| 335 |
break; |
| 336 |
} |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
this.pos++; |
| 341 |
this.line_char_count++; |
| 342 |
content.push(input_char); //letter at-a-time (or string) inserted to an array |
| 343 |
} |
| 344 |
return content.length ? content.join('') : ''; |
| 345 |
}; |
| 346 |
|
| 347 |
this.get_contents_to = function(name) { //get the full content of a script or style to pass to js_beautify |
| 348 |
if (this.pos === this.input.length) { |
| 349 |
return ['', 'TK_EOF']; |
| 350 |
} |
| 351 |
var content = ''; |
| 352 |
var reg_match = new RegExp('</' + name + '\\s*>', 'igm'); |
| 353 |
reg_match.lastIndex = this.pos; |
| 354 |
var reg_array = reg_match.exec(this.input); |
| 355 |
var end_script = reg_array ? reg_array.index : this.input.length; //absolute end of script |
| 356 |
if (this.pos < end_script) { //get everything in between the script tags |
| 357 |
content = this.input.substring(this.pos, end_script); |
| 358 |
this.pos = end_script; |
| 359 |
} |
| 360 |
return content; |
| 361 |
}; |
| 362 |
|
| 363 |
this.record_tag = function(tag) { //function to record a tag and its parent in this.tags Object |
| 364 |
if (this.tags[tag + 'count']) { //check for the existence of this tag type |
| 365 |
this.tags[tag + 'count']++; |
| 366 |
this.tags[tag + this.tags[tag + 'count']] = this.indent_level; //and record the present indent level |
| 367 |
} else { //otherwise initialize this tag type |
| 368 |
this.tags[tag + 'count'] = 1; |
| 369 |
this.tags[tag + this.tags[tag + 'count']] = this.indent_level; //and record the present indent level |
| 370 |
} |
| 371 |
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) |
| 372 |
this.tags.parent = tag + this.tags[tag + 'count']; //and make this the current parent (i.e. in the case of a div 'div1') |
| 373 |
}; |
| 374 |
|
| 375 |
this.retrieve_tag = function(tag) { //function to retrieve the opening tag to the corresponding closer |
| 376 |
if (this.tags[tag + 'count']) { //if the openener is not in the Object we ignore it |
| 377 |
var temp_parent = this.tags.parent; //check to see if it's a closable tag. |
| 378 |
while (temp_parent) { //till we reach '' (the initial value); |
| 379 |
if (tag + this.tags[tag + 'count'] === temp_parent) { //if this is it use it |
| 380 |
break; |
| 381 |
} |
| 382 |
temp_parent = this.tags[temp_parent + 'parent']; //otherwise keep on climbing up the DOM Tree |
| 383 |
} |
| 384 |
if (temp_parent) { //if we caught something |
| 385 |
this.indent_level = this.tags[tag + this.tags[tag + 'count']]; //set the indent_level accordingly |
| 386 |
this.tags.parent = this.tags[temp_parent + 'parent']; //and set the current parent |
| 387 |
} |
| 388 |
delete this.tags[tag + this.tags[tag + 'count'] + 'parent']; //delete the closed tags parent reference... |
| 389 |
delete this.tags[tag + this.tags[tag + 'count']]; //...and the tag itself |
| 390 |
if (this.tags[tag + 'count'] === 1) { |
| 391 |
delete this.tags[tag + 'count']; |
| 392 |
} else { |
| 393 |
this.tags[tag + 'count']--; |
| 394 |
} |
| 395 |
} |
| 396 |
}; |
| 397 |
|
| 398 |
this.indent_to_tag = function(tag) { |
| 399 |
// Match the indentation level to the last use of this tag, but don't remove it. |
| 400 |
if (!this.tags[tag + 'count']) { |
| 401 |
return; |
| 402 |
} |
| 403 |
var temp_parent = this.tags.parent; |
| 404 |
while (temp_parent) { |
| 405 |
if (tag + this.tags[tag + 'count'] === temp_parent) { |
| 406 |
break; |
| 407 |
} |
| 408 |
temp_parent = this.tags[temp_parent + 'parent']; |
| 409 |
} |
| 410 |
if (temp_parent) { |
| 411 |
this.indent_level = this.tags[tag + this.tags[tag + 'count']]; |
| 412 |
} |
| 413 |
}; |
| 414 |
|
| 415 |
this.get_tag = function(peek) { //function to get a full tag and parse its type |
| 416 |
var input_char = '', |
| 417 |
content = [], |
| 418 |
comment = '', |
| 419 |
space = false, |
| 420 |
first_attr = true, |
| 421 |
has_wrapped_attrs = false, |
| 422 |
tag_start, tag_end, |
| 423 |
tag_start_char, |
| 424 |
orig_pos = this.pos, |
| 425 |
orig_line_char_count = this.line_char_count, |
| 426 |
is_tag_closed = false, |
| 427 |
tail; |
| 428 |
|
| 429 |
peek = peek !== undefined ? peek : false; |
| 430 |
|
| 431 |
do { |
| 432 |
if (this.pos >= this.input.length) { |
| 433 |
if (peek) { |
| 434 |
this.pos = orig_pos; |
| 435 |
this.line_char_count = orig_line_char_count; |
| 436 |
} |
| 437 |
return content.length ? content.join('') : ['', 'TK_EOF']; |
| 438 |
} |
| 439 |
|
| 440 |
input_char = this.input.charAt(this.pos); |
| 441 |
this.pos++; |
| 442 |
|
| 443 |
if (this.Utils.in_array(input_char, this.Utils.whitespace)) { //don't want to insert unnecessary space |
| 444 |
space = true; |
| 445 |
continue; |
| 446 |
} |
| 447 |
|
| 448 |
if (input_char === "'" || input_char === '"') { |
| 449 |
input_char += this.get_unformatted(input_char); |
| 450 |
space = true; |
| 451 |
} |
| 452 |
|
| 453 |
if (input_char === '=') { //no space before = |
| 454 |
space = false; |
| 455 |
} |
| 456 |
tail = this.input.substr(this.pos - 1); |
| 457 |
if (is_wrap_attributes_force_expand_multiline && has_wrapped_attrs && !is_tag_closed && (input_char === '>' || input_char === '/')) { |
| 458 |
if (tail.match(/^\/?\s*>/)) { |
| 459 |
space = false; |
| 460 |
is_tag_closed = true; |
| 461 |
this.print_newline(false, content); |
| 462 |
this.print_indentation(content); |
| 463 |
} |
| 464 |
} |
| 465 |
if (content.length && content[content.length - 1] !== '=' && input_char !== '>' && space) { |
| 466 |
//no space after = or before > |
| 467 |
var wrapped = this.space_or_wrap(content); |
| 468 |
var indentAttrs = wrapped && input_char !== '/' && !is_wrap_attributes_force; |
| 469 |
space = false; |
| 470 |
|
| 471 |
if (is_wrap_attributes_force && input_char !== '/') { |
| 472 |
var force_first_attr_wrap = false; |
| 473 |
if (is_wrap_attributes_force_expand_multiline && first_attr) { |
| 474 |
var is_only_attribute = tail.match(/^\S*(="([^"]|\\")*")?\s*\/?\s*>/) !== null; |
| 475 |
force_first_attr_wrap = !is_only_attribute; |
| 476 |
} |
| 477 |
if (!first_attr || force_first_attr_wrap) { |
| 478 |
this.print_newline(false, content); |
| 479 |
this.print_indentation(content); |
| 480 |
indentAttrs = true; |
| 481 |
} |
| 482 |
} |
| 483 |
if (indentAttrs) { |
| 484 |
has_wrapped_attrs = true; |
| 485 |
|
| 486 |
//indent attributes an auto, forced, or forced-align line-wrap |
| 487 |
var alignment_size = wrap_attributes_indent_size; |
| 488 |
if (is_wrap_attributes_force_aligned) { |
| 489 |
alignment_size = content.indexOf(' ') + 1; |
| 490 |
} |
| 491 |
|
| 492 |
for (var count = 0; count < alignment_size; count++) { |
| 493 |
// only ever further indent with spaces since we're trying to align characters |
| 494 |
content.push(' '); |
| 495 |
} |
| 496 |
} |
| 497 |
if (first_attr) { |
| 498 |
for (var i = 0; i < content.length; i++) { |
| 499 |
if (content[i] === ' ') { |
| 500 |
first_attr = false; |
| 501 |
break; |
| 502 |
} |
| 503 |
} |
| 504 |
} |
| 505 |
} |
| 506 |
|
| 507 |
if (indent_handlebars && tag_start_char === '<') { |
| 508 |
// When inside an angle-bracket tag, put spaces around |
| 509 |
// handlebars not inside of strings. |
| 510 |
if ((input_char + this.input.charAt(this.pos)) === '{{') { |
| 511 |
input_char += this.get_unformatted('}}'); |
| 512 |
if (content.length && content[content.length - 1] !== ' ' && content[content.length - 1] !== '<') { |
| 513 |
input_char = ' ' + input_char; |
| 514 |
} |
| 515 |
space = true; |
| 516 |
} |
| 517 |
} |
| 518 |
|
| 519 |
if (input_char === '<' && !tag_start_char) { |
| 520 |
tag_start = this.pos - 1; |
| 521 |
tag_start_char = '<'; |
| 522 |
} |
| 523 |
|
| 524 |
if (indent_handlebars && !tag_start_char) { |
| 525 |
if (content.length >= 2 && content[content.length - 1] === '{' && content[content.length - 2] === '{') { |
| 526 |
if (input_char === '#' || input_char === '/' || input_char === '!') { |
| 527 |
tag_start = this.pos - 3; |
| 528 |
} else { |
| 529 |
tag_start = this.pos - 2; |
| 530 |
} |
| 531 |
tag_start_char = '{'; |
| 532 |
} |
| 533 |
} |
| 534 |
|
| 535 |
this.line_char_count++; |
| 536 |
content.push(input_char); //inserts character at-a-time (or string) |
| 537 |
|
| 538 |
if (content[1] && (content[1] === '!' || content[1] === '?' || content[1] === '%')) { //if we're in a comment, do something special |
| 539 |
// We treat all comments as literals, even more than preformatted tags |
| 540 |
// we just look for the appropriate close tag |
| 541 |
content = [this.get_comment(tag_start)]; |
| 542 |
break; |
| 543 |
} |
| 544 |
|
| 545 |
if (indent_handlebars && content[1] && content[1] === '{' && content[2] && content[2] === '!') { //if we're in a comment, do something special |
| 546 |
// We treat all comments as literals, even more than preformatted tags |
| 547 |
// we just look for the appropriate close tag |
| 548 |
content = [this.get_comment(tag_start)]; |
| 549 |
break; |
| 550 |
} |
| 551 |
|
| 552 |
if (indent_handlebars && tag_start_char === '{' && content.length > 2 && content[content.length - 2] === '}' && content[content.length - 1] === '}') { |
| 553 |
break; |
| 554 |
} |
| 555 |
} while (input_char !== '>'); |
| 556 |
|
| 557 |
var tag_complete = content.join(''); |
| 558 |
var tag_index; |
| 559 |
var tag_offset; |
| 560 |
|
| 561 |
// must check for space first otherwise the tag could have the first attribute included, and |
| 562 |
// then not un-indent correctly |
| 563 |
if (tag_complete.indexOf(' ') !== -1) { //if there's whitespace, thats where the tag name ends |
| 564 |
tag_index = tag_complete.indexOf(' '); |
| 565 |
} else if (tag_complete.indexOf('\n') !== -1) { //if there's a line break, thats where the tag name ends |
| 566 |
tag_index = tag_complete.indexOf('\n'); |
| 567 |
} else if (tag_complete.charAt(0) === '{') { |
| 568 |
tag_index = tag_complete.indexOf('}'); |
| 569 |
} else { //otherwise go with the tag ending |
| 570 |
tag_index = tag_complete.indexOf('>'); |
| 571 |
} |
| 572 |
if (tag_complete.charAt(0) === '<' || !indent_handlebars) { |
| 573 |
tag_offset = 1; |
| 574 |
} else { |
| 575 |
tag_offset = tag_complete.charAt(2) === '#' ? 3 : 2; |
| 576 |
} |
| 577 |
var tag_check = tag_complete.substring(tag_offset, tag_index).toLowerCase(); |
| 578 |
if (tag_complete.charAt(tag_complete.length - 2) === '/' || |
| 579 |
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 /) |
| 580 |
if (!peek) { |
| 581 |
this.tag_type = 'SINGLE'; |
| 582 |
} |
| 583 |
} else if (indent_handlebars && tag_complete.charAt(0) === '{' && tag_check === 'else') { |
| 584 |
if (!peek) { |
| 585 |
this.indent_to_tag('if'); |
| 586 |
this.tag_type = 'HANDLEBARS_ELSE'; |
| 587 |
this.indent_content = true; |
| 588 |
this.traverse_whitespace(); |
| 589 |
} |
| 590 |
} else if (this.is_unformatted(tag_check, unformatted) || |
| 591 |
this.is_unformatted(tag_check, content_unformatted)) { |
| 592 |
// do not reformat the "unformatted" or "content_unformatted" tags |
| 593 |
comment = this.get_unformatted('</' + tag_check + '>', tag_complete); //...delegate to get_unformatted function |
| 594 |
content.push(comment); |
| 595 |
tag_end = this.pos - 1; |
| 596 |
this.tag_type = 'SINGLE'; |
| 597 |
} else if (tag_check === 'script' && |
| 598 |
(tag_complete.search('type') === -1 || |
| 599 |
(tag_complete.search('type') > -1 && |
| 600 |
tag_complete.search(/\b(text|application|dojo)\/(x-)?(javascript|ecmascript|jscript|livescript|(ld\+)?json|method|aspect)/) > -1))) { |
| 601 |
if (!peek) { |
| 602 |
this.record_tag(tag_check); |
| 603 |
this.tag_type = 'SCRIPT'; |
| 604 |
} |
| 605 |
} else if (tag_check === 'style' && |
| 606 |
(tag_complete.search('type') === -1 || |
| 607 |
(tag_complete.search('type') > -1 && tag_complete.search('text/css') > -1))) { |
| 608 |
if (!peek) { |
| 609 |
this.record_tag(tag_check); |
| 610 |
this.tag_type = 'STYLE'; |
| 611 |
} |
| 612 |
} else if (tag_check.charAt(0) === '!') { //peek for <! comment |
| 613 |
// for comments content is already correct. |
| 614 |
if (!peek) { |
| 615 |
this.tag_type = 'SINGLE'; |
| 616 |
this.traverse_whitespace(); |
| 617 |
} |
| 618 |
} else if (!peek) { |
| 619 |
if (tag_check.charAt(0) === '/') { //this tag is a double tag so check for tag-ending |
| 620 |
this.retrieve_tag(tag_check.substring(1)); //remove it and all ancestors |
| 621 |
this.tag_type = 'END'; |
| 622 |
} else { //otherwise it's a start-tag |
| 623 |
this.record_tag(tag_check); //push it on the tag stack |
| 624 |
if (tag_check.toLowerCase() !== 'html') { |
| 625 |
this.indent_content = true; |
| 626 |
} |
| 627 |
this.tag_type = 'START'; |
| 628 |
} |
| 629 |
|
| 630 |
// Allow preserving of newlines after a start or end tag |
| 631 |
if (this.traverse_whitespace()) { |
| 632 |
this.space_or_wrap(content); |
| 633 |
} |
| 634 |
|
| 635 |
if (this.Utils.in_array(tag_check, this.Utils.extra_liners)) { //check if this double needs an extra line |
| 636 |
this.print_newline(false, this.output); |
| 637 |
if (this.output.length && this.output[this.output.length - 2] !== '\n') { |
| 638 |
this.print_newline(true, this.output); |
| 639 |
} |
| 640 |
} |
| 641 |
} |
| 642 |
|
| 643 |
if (peek) { |
| 644 |
this.pos = orig_pos; |
| 645 |
this.line_char_count = orig_line_char_count; |
| 646 |
} |
| 647 |
|
| 648 |
return content.join(''); //returns fully formatted tag |
| 649 |
}; |
| 650 |
|
| 651 |
this.get_comment = function(start_pos) { //function to return comment content in its entirety |
| 652 |
// this is will have very poor perf, but will work for now. |
| 653 |
var comment = '', |
| 654 |
delimiter = '>', |
| 655 |
matched = false; |
| 656 |
|
| 657 |
this.pos = start_pos; |
| 658 |
var input_char = this.input.charAt(this.pos); |
| 659 |
this.pos++; |
| 660 |
|
| 661 |
while (this.pos <= this.input.length) { |
| 662 |
comment += input_char; |
| 663 |
|
| 664 |
// only need to check for the delimiter if the last chars match |
| 665 |
if (comment.charAt(comment.length - 1) === delimiter.charAt(delimiter.length - 1) && |
| 666 |
comment.indexOf(delimiter) !== -1) { |
| 667 |
break; |
| 668 |
} |
| 669 |
|
| 670 |
// only need to search for custom delimiter for the first few characters |
| 671 |
if (!matched && comment.length < 10) { |
| 672 |
if (comment.indexOf('<![if') === 0) { //peek for <![if conditional comment |
| 673 |
delimiter = '<![endif]>'; |
| 674 |
matched = true; |
| 675 |
} else if (comment.indexOf('<![cdata[') === 0) { //if it's a <[cdata[ comment... |
| 676 |
delimiter = ']]>'; |
| 677 |
matched = true; |
| 678 |
} else if (comment.indexOf('<![') === 0) { // some other ![ comment? ... |
| 679 |
delimiter = ']>'; |
| 680 |
matched = true; |
| 681 |
} else if (comment.indexOf('<!--') === 0) { // <!-- comment ... |
| 682 |
delimiter = '-->'; |
| 683 |
matched = true; |
| 684 |
} else if (comment.indexOf('{{!--') === 0) { // {{!-- handlebars comment |
| 685 |
delimiter = '--}}'; |
| 686 |
matched = true; |
| 687 |
} else if (comment.indexOf('{{!') === 0) { // {{! handlebars comment |
| 688 |
if (comment.length === 5 && comment.indexOf('{{!--') === -1) { |
| 689 |
delimiter = '}}'; |
| 690 |
matched = true; |
| 691 |
} |
| 692 |
} else if (comment.indexOf('<?') === 0) { // {{! handlebars comment |
| 693 |
delimiter = '?>'; |
| 694 |
matched = true; |
| 695 |
} else if (comment.indexOf('<%') === 0) { // {{! handlebars comment |
| 696 |
delimiter = '%>'; |
| 697 |
matched = true; |
| 698 |
} |
| 699 |
} |
| 700 |
|
| 701 |
input_char = this.input.charAt(this.pos); |
| 702 |
this.pos++; |
| 703 |
} |
| 704 |
|
| 705 |
return comment; |
| 706 |
}; |
| 707 |
|
| 708 |
function tokenMatcher(delimiter) { |
| 709 |
var token = ''; |
| 710 |
|
| 711 |
var add = function(str) { |
| 712 |
var newToken = token + str.toLowerCase(); |
| 713 |
token = newToken.length <= delimiter.length ? newToken : newToken.substr(newToken.length - delimiter.length, delimiter.length); |
| 714 |
}; |
| 715 |
|
| 716 |
var doesNotMatch = function() { |
| 717 |
return token.indexOf(delimiter) === -1; |
| 718 |
}; |
| 719 |
|
| 720 |
return { |
| 721 |
add: add, |
| 722 |
doesNotMatch: doesNotMatch |
| 723 |
}; |
| 724 |
} |
| 725 |
|
| 726 |
this.get_unformatted = function(delimiter, orig_tag) { //function to return unformatted content in its entirety |
| 727 |
if (orig_tag && orig_tag.toLowerCase().indexOf(delimiter) !== -1) { |
| 728 |
return ''; |
| 729 |
} |
| 730 |
var input_char = ''; |
| 731 |
var content = ''; |
| 732 |
var space = true; |
| 733 |
|
| 734 |
var delimiterMatcher = tokenMatcher(delimiter); |
| 735 |
|
| 736 |
do { |
| 737 |
|
| 738 |
if (this.pos >= this.input.length) { |
| 739 |
return content; |
| 740 |
} |
| 741 |
|
| 742 |
input_char = this.input.charAt(this.pos); |
| 743 |
this.pos++; |
| 744 |
|
| 745 |
if (this.Utils.in_array(input_char, this.Utils.whitespace)) { |
| 746 |
if (!space) { |
| 747 |
this.line_char_count--; |
| 748 |
continue; |
| 749 |
} |
| 750 |
if (input_char === '\n' || input_char === '\r') { |
| 751 |
content += '\n'; |
| 752 |
/* 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' |
| 753 |
for (var i=0; i<this.indent_level; i++) { |
| 754 |
content += this.indent_string; |
| 755 |
} |
| 756 |
space = false; //...and make sure other indentation is erased |
| 757 |
*/ |
| 758 |
this.line_char_count = 0; |
| 759 |
continue; |
| 760 |
} |
| 761 |
} |
| 762 |
content += input_char; |
| 763 |
delimiterMatcher.add(input_char); |
| 764 |
this.line_char_count++; |
| 765 |
space = true; |
| 766 |
|
| 767 |
if (indent_handlebars && input_char === '{' && content.length && content.charAt(content.length - 2) === '{') { |
| 768 |
// Handlebars expressions in strings should also be unformatted. |
| 769 |
content += this.get_unformatted('}}'); |
| 770 |
// Don't consider when stopping for delimiters. |
| 771 |
} |
| 772 |
} while (delimiterMatcher.doesNotMatch()); |
| 773 |
|
| 774 |
return content; |
| 775 |
}; |
| 776 |
|
| 777 |
this.get_token = function() { //initial handler for token-retrieval |
| 778 |
var token; |
| 779 |
|
| 780 |
if (this.last_token === 'TK_TAG_SCRIPT' || this.last_token === 'TK_TAG_STYLE') { //check if we need to format javascript |
| 781 |
var type = this.last_token.substr(7); |
| 782 |
token = this.get_contents_to(type); |
| 783 |
if (typeof token !== 'string') { |
| 784 |
return token; |
| 785 |
} |
| 786 |
return [token, 'TK_' + type]; |
| 787 |
} |
| 788 |
if (this.current_mode === 'CONTENT') { |
| 789 |
token = this.get_content(); |
| 790 |
if (typeof token !== 'string') { |
| 791 |
return token; |
| 792 |
} else { |
| 793 |
return [token, 'TK_CONTENT']; |
| 794 |
} |
| 795 |
} |
| 796 |
|
| 797 |
if (this.current_mode === 'TAG') { |
| 798 |
token = this.get_tag(); |
| 799 |
if (typeof token !== 'string') { |
| 800 |
return token; |
| 801 |
} else { |
| 802 |
var tag_name_type = 'TK_TAG_' + this.tag_type; |
| 803 |
return [token, tag_name_type]; |
| 804 |
} |
| 805 |
} |
| 806 |
}; |
| 807 |
|
| 808 |
this.get_full_indent = function(level) { |
| 809 |
level = this.indent_level + level || 0; |
| 810 |
if (level < 1) { |
| 811 |
return ''; |
| 812 |
} |
| 813 |
|
| 814 |
return Array(level + 1).join(this.indent_string); |
| 815 |
}; |
| 816 |
|
| 817 |
this.is_unformatted = function(tag_check, unformatted) { |
| 818 |
//is this an HTML5 block-level link? |
| 819 |
if (!this.Utils.in_array(tag_check, unformatted)) { |
| 820 |
return false; |
| 821 |
} |
| 822 |
|
| 823 |
if (tag_check.toLowerCase() !== 'a' || !this.Utils.in_array('a', unformatted)) { |
| 824 |
return true; |
| 825 |
} |
| 826 |
|
| 827 |
//at this point we have an tag; is its first child something we want to remain |
| 828 |
//unformatted? |
| 829 |
var next_tag = this.get_tag(true /* peek. */ ); |
| 830 |
|
| 831 |
// test next_tag to see if it is just html tag (no external content) |
| 832 |
var tag = (next_tag || "").match(/^\s*<\s*\/?([a-z]*)\s*[^>]*>\s*$/); |
| 833 |
|
| 834 |
// if next_tag comes back but is not an isolated tag, then |
| 835 |
// let's treat the 'a' tag as having content |
| 836 |
// and respect the unformatted option |
| 837 |
if (!tag || this.Utils.in_array(tag, unformatted)) { |
| 838 |
return true; |
| 839 |
} else { |
| 840 |
return false; |
| 841 |
} |
| 842 |
}; |
| 843 |
|
| 844 |
this.printer = function(js_source, indent_character, indent_size, wrap_line_length, brace_style) { //handles input/output and some other printing functions |
| 845 |
|
| 846 |
this.input = js_source || ''; //gets the input for the Parser |
| 847 |
|
| 848 |
// HACK: newline parsing inconsistent. This brute force normalizes the input. |
| 849 |
this.input = this.input.replace(/\r\n|[\r\u2028\u2029]/g, '\n'); |
| 850 |
|
| 851 |
this.output = []; |
| 852 |
this.indent_character = indent_character; |
| 853 |
this.indent_string = ''; |
| 854 |
this.indent_size = indent_size; |
| 855 |
this.brace_style = brace_style; |
| 856 |
this.indent_level = 0; |
| 857 |
this.wrap_line_length = wrap_line_length; |
| 858 |
this.line_char_count = 0; //count to see if wrap_line_length was exceeded |
| 859 |
|
| 860 |
for (var i = 0; i < this.indent_size; i++) { |
| 861 |
this.indent_string += this.indent_character; |
| 862 |
} |
| 863 |
|
| 864 |
this.print_newline = function(force, arr) { |
| 865 |
this.line_char_count = 0; |
| 866 |
if (!arr || !arr.length) { |
| 867 |
return; |
| 868 |
} |
| 869 |
if (force || (arr[arr.length - 1] !== '\n')) { //we might want the extra line |
| 870 |
if ((arr[arr.length - 1] !== '\n')) { |
| 871 |
arr[arr.length - 1] = rtrim(arr[arr.length - 1]); |
| 872 |
} |
| 873 |
arr.push('\n'); |
| 874 |
} |
| 875 |
}; |
| 876 |
|
| 877 |
this.print_indentation = function(arr) { |
| 878 |
for (var i = 0; i < this.indent_level; i++) { |
| 879 |
arr.push(this.indent_string); |
| 880 |
this.line_char_count += this.indent_string.length; |
| 881 |
} |
| 882 |
}; |
| 883 |
|
| 884 |
this.print_token = function(text) { |
| 885 |
// Avoid printing initial whitespace. |
| 886 |
if (this.is_whitespace(text) && !this.output.length) { |
| 887 |
return; |
| 888 |
} |
| 889 |
if (text || text !== '') { |
| 890 |
if (this.output.length && this.output[this.output.length - 1] === '\n') { |
| 891 |
this.print_indentation(this.output); |
| 892 |
text = ltrim(text); |
| 893 |
} |
| 894 |
} |
| 895 |
this.print_token_raw(text); |
| 896 |
}; |
| 897 |
|
| 898 |
this.print_token_raw = function(text) { |
| 899 |
// If we are going to print newlines, truncate trailing |
| 900 |
// whitespace, as the newlines will represent the space. |
| 901 |
if (this.newlines > 0) { |
| 902 |
text = rtrim(text); |
| 903 |
} |
| 904 |
|
| 905 |
if (text && text !== '') { |
| 906 |
if (text.length > 1 && text.charAt(text.length - 1) === '\n') { |
| 907 |
// unformatted tags can grab newlines as their last character |
| 908 |
this.output.push(text.slice(0, -1)); |
| 909 |
this.print_newline(false, this.output); |
| 910 |
} else { |
| 911 |
this.output.push(text); |
| 912 |
} |
| 913 |
} |
| 914 |
|
| 915 |
for (var n = 0; n < this.newlines; n++) { |
| 916 |
this.print_newline(n > 0, this.output); |
| 917 |
} |
| 918 |
this.newlines = 0; |
| 919 |
}; |
| 920 |
|
| 921 |
this.indent = function() { |
| 922 |
this.indent_level++; |
| 923 |
}; |
| 924 |
|
| 925 |
this.unindent = function() { |
| 926 |
if (this.indent_level > 0) { |
| 927 |
this.indent_level--; |
| 928 |
} |
| 929 |
}; |
| 930 |
}; |
| 931 |
return this; |
| 932 |
} |
| 933 |
|
| 934 |
/*_____________________--------------------_____________________*/ |
| 935 |
|
| 936 |
multi_parser = new Parser(); //wrapping functions Parser |
| 937 |
multi_parser.printer(html_source, indent_character, indent_size, wrap_line_length, brace_style); //initialize starting values |
| 938 |
|
| 939 |
while (true) { |
| 940 |
var t = multi_parser.get_token(); |
| 941 |
multi_parser.token_text = t[0]; |
| 942 |
multi_parser.token_type = t[1]; |
| 943 |
|
| 944 |
if (multi_parser.token_type === 'TK_EOF') { |
| 945 |
break; |
| 946 |
} |
| 947 |
|
| 948 |
switch (multi_parser.token_type) { |
| 949 |
case 'TK_TAG_START': |
| 950 |
multi_parser.print_newline(false, multi_parser.output); |
| 951 |
multi_parser.print_token(multi_parser.token_text); |
| 952 |
if (multi_parser.indent_content) { |
| 953 |
if ((multi_parser.indent_body_inner_html || !multi_parser.token_text.match(/<body(?:.*)>/)) && |
| 954 |
(multi_parser.indent_head_inner_html || !multi_parser.token_text.match(/<head(?:.*)>/))) { |
| 955 |
|
| 956 |
multi_parser.indent(); |
| 957 |
} |
| 958 |
|
| 959 |
multi_parser.indent_content = false; |
| 960 |
} |
| 961 |
multi_parser.current_mode = 'CONTENT'; |
| 962 |
break; |
| 963 |
case 'TK_TAG_STYLE': |
| 964 |
case 'TK_TAG_SCRIPT': |
| 965 |
multi_parser.print_newline(false, multi_parser.output); |
| 966 |
multi_parser.print_token(multi_parser.token_text); |
| 967 |
multi_parser.current_mode = 'CONTENT'; |
| 968 |
break; |
| 969 |
case 'TK_TAG_END': |
| 970 |
//Print new line only if the tag has no content and has child |
| 971 |
if (multi_parser.last_token === 'TK_CONTENT' && multi_parser.last_text === '') { |
| 972 |
var tag_name = (multi_parser.token_text.match(/\w+/) || [])[0]; |
| 973 |
var tag_extracted_from_last_output = null; |
| 974 |
if (multi_parser.output.length) { |
| 975 |
tag_extracted_from_last_output = multi_parser.output[multi_parser.output.length - 1].match(/(?:<|{{#)\s*(\w+)/); |
| 976 |
} |
| 977 |
if (tag_extracted_from_last_output === null || |
| 978 |
(tag_extracted_from_last_output[1] !== tag_name && !multi_parser.Utils.in_array(tag_extracted_from_last_output[1], unformatted))) { |
| 979 |
multi_parser.print_newline(false, multi_parser.output); |
| 980 |
} |
| 981 |
} |
| 982 |
multi_parser.print_token(multi_parser.token_text); |
| 983 |
multi_parser.current_mode = 'CONTENT'; |
| 984 |
break; |
| 985 |
case 'TK_TAG_SINGLE': |
| 986 |
// Don't add a newline before elements that should remain unformatted. |
| 987 |
var tag_check = multi_parser.token_text.match(/^\s*<([a-z-]+)/i); |
| 988 |
if (!tag_check || !multi_parser.Utils.in_array(tag_check[1], unformatted)) { |
| 989 |
multi_parser.print_newline(false, multi_parser.output); |
| 990 |
} |
| 991 |
multi_parser.print_token(multi_parser.token_text); |
| 992 |
multi_parser.current_mode = 'CONTENT'; |
| 993 |
break; |
| 994 |
case 'TK_TAG_HANDLEBARS_ELSE': |
| 995 |
// Don't add a newline if opening {{#if}} tag is on the current line |
| 996 |
var foundIfOnCurrentLine = false; |
| 997 |
for (var lastCheckedOutput = multi_parser.output.length - 1; lastCheckedOutput >= 0; lastCheckedOutput--) { |
| 998 |
if (multi_parser.output[lastCheckedOutput] === '\n') { |
| 999 |
break; |
| 1000 |
} else { |
| 1001 |
if (multi_parser.output[lastCheckedOutput].match(/{{#if/)) { |
| 1002 |
foundIfOnCurrentLine = true; |
| 1003 |
break; |
| 1004 |
} |
| 1005 |
} |
| 1006 |
} |
| 1007 |
if (!foundIfOnCurrentLine) { |
| 1008 |
multi_parser.print_newline(false, multi_parser.output); |
| 1009 |
} |
| 1010 |
multi_parser.print_token(multi_parser.token_text); |
| 1011 |
if (multi_parser.indent_content) { |
| 1012 |
multi_parser.indent(); |
| 1013 |
multi_parser.indent_content = false; |
| 1014 |
} |
| 1015 |
multi_parser.current_mode = 'CONTENT'; |
| 1016 |
break; |
| 1017 |
case 'TK_TAG_HANDLEBARS_COMMENT': |
| 1018 |
multi_parser.print_token(multi_parser.token_text); |
| 1019 |
multi_parser.current_mode = 'TAG'; |
| 1020 |
break; |
| 1021 |
case 'TK_CONTENT': |
| 1022 |
multi_parser.print_token(multi_parser.token_text); |
| 1023 |
multi_parser.current_mode = 'TAG'; |
| 1024 |
break; |
| 1025 |
case 'TK_STYLE': |
| 1026 |
case 'TK_SCRIPT': |
| 1027 |
if (multi_parser.token_text !== '') { |
| 1028 |
multi_parser.print_newline(false, multi_parser.output); |
| 1029 |
var text = multi_parser.token_text, |
| 1030 |
_beautifier, |
| 1031 |
script_indent_level = 1; |
| 1032 |
if (multi_parser.token_type === 'TK_SCRIPT') { |
| 1033 |
_beautifier = typeof js_beautify === 'function' && js_beautify; |
| 1034 |
} else if (multi_parser.token_type === 'TK_STYLE') { |
| 1035 |
_beautifier = typeof css_beautify === 'function' && css_beautify; |
| 1036 |
} |
| 1037 |
|
| 1038 |
if (options.indent_scripts === "keep") { |
| 1039 |
script_indent_level = 0; |
| 1040 |
} else if (options.indent_scripts === "separate") { |
| 1041 |
script_indent_level = -multi_parser.indent_level; |
| 1042 |
} |
| 1043 |
|
| 1044 |
var indentation = multi_parser.get_full_indent(script_indent_level); |
| 1045 |
if (_beautifier) { |
| 1046 |
|
| 1047 |
// call the Beautifier if avaliable |
| 1048 |
var Child_options = function() { |
| 1049 |
this.eol = '\n'; |
| 1050 |
}; |
| 1051 |
Child_options.prototype = options; |
| 1052 |
var child_options = new Child_options(); |
| 1053 |
text = _beautifier(text.replace(/^\s*/, indentation), child_options); |
| 1054 |
} else { |
| 1055 |
// simply indent the string otherwise |
| 1056 |
var white = text.match(/^\s*/)[0]; |
| 1057 |
var _level = white.match(/[^\n\r]*$/)[0].split(multi_parser.indent_string).length - 1; |
| 1058 |
var reindent = multi_parser.get_full_indent(script_indent_level - _level); |
| 1059 |
text = text.replace(/^\s*/, indentation) |
| 1060 |
.replace(/\r\n|\r|\n/g, '\n' + reindent) |
| 1061 |
.replace(/\s+$/, ''); |
| 1062 |
} |
| 1063 |
if (text) { |
| 1064 |
multi_parser.print_token_raw(text); |
| 1065 |
multi_parser.print_newline(true, multi_parser.output); |
| 1066 |
} |
| 1067 |
} |
| 1068 |
multi_parser.current_mode = 'TAG'; |
| 1069 |
break; |
| 1070 |
default: |
| 1071 |
// We should not be getting here but we don't want to drop input on the floor |
| 1072 |
// Just output the text and move on |
| 1073 |
if (multi_parser.token_text !== '') { |
| 1074 |
multi_parser.print_token(multi_parser.token_text); |
| 1075 |
} |
| 1076 |
break; |
| 1077 |
} |
| 1078 |
multi_parser.last_token = multi_parser.token_type; |
| 1079 |
multi_parser.last_text = multi_parser.token_text; |
| 1080 |
} |
| 1081 |
var sweet_code = multi_parser.output.join('').replace(/[\r\n\t ]+$/, ''); |
| 1082 |
|
| 1083 |
// establish end_with_newline |
| 1084 |
if (end_with_newline) { |
| 1085 |
sweet_code += '\n'; |
| 1086 |
} |
| 1087 |
|
| 1088 |
if (eol !== '\n') { |
| 1089 |
sweet_code = sweet_code.replace(/[\n]/g, eol); |
| 1090 |
} |
| 1091 |
|
| 1092 |
return sweet_code; |
| 1093 |
} |
| 1094 |
|
| 1095 |
if (typeof define === "function" && define.amd) { |
| 1096 |
// Add support for AMD ( https://github.com/amdjs/amdjs-api/wiki/AMD#defineamd-property- ) |
| 1097 |
define(["require", "./beautify", "./beautify-css"], function(requireamd) { |
| 1098 |
var js_beautify = requireamd("./beautify"); |
| 1099 |
var css_beautify = requireamd("./beautify-css"); |
| 1100 |
|
| 1101 |
return { |
| 1102 |
html_beautify: function(html_source, options) { |
| 1103 |
return style_html(html_source, options, js_beautify.js_beautify, css_beautify.css_beautify); |
| 1104 |
} |
| 1105 |
}; |
| 1106 |
}); |
| 1107 |
} else if (typeof exports !== "undefined") { |
| 1108 |
// Add support for CommonJS. Just put this file somewhere on your require.paths |
| 1109 |
// and you will be able to `var html_beautify = require("beautify").html_beautify`. |
| 1110 |
var js_beautify = require('./beautify.js'); |
| 1111 |
var css_beautify = require('./beautify-css.js'); |
| 1112 |
|
| 1113 |
exports.html_beautify = function(html_source, options) { |
| 1114 |
return style_html(html_source, options, js_beautify.js_beautify, css_beautify.css_beautify); |
| 1115 |
}; |
| 1116 |
} else if (typeof window !== "undefined") { |
| 1117 |
// If we're running a web page and don't have either of the above, add our one global |
| 1118 |
window.html_beautify = function(html_source, options) { |
| 1119 |
return style_html(html_source, options, window.js_beautify, window.css_beautify); |
| 1120 |
}; |
| 1121 |
} else if (typeof global !== "undefined") { |
| 1122 |
// If we don't even have window, try global. |
| 1123 |
global.html_beautify = function(html_source, options) { |
| 1124 |
return style_html(html_source, options, global.js_beautify, global.css_beautify); |
| 1125 |
}; |
| 1126 |
} |
| 1127 |
|
| 1128 |
}()); |