beautify-html.js 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. /*jshint curly:true, eqeqeq:true, laxbreak:true, noempty:false */
  2. /*
  3. The MIT License (MIT)
  4. Copyright (c) 2007-2013 Einar Lielmanis and contributors.
  5. Permission is hereby granted, free of charge, to any person
  6. obtaining a copy of this software and associated documentation files
  7. (the "Software"), to deal in the Software without restriction,
  8. including without limitation the rights to use, copy, modify, merge,
  9. publish, distribute, sublicense, and/or sell copies of the Software,
  10. and to permit persons to whom the Software is furnished to do so,
  11. subject to the following conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  18. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  19. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. SOFTWARE.
  22. Style HTML
  23. ---------------
  24. Written by Nochum Sossonko, (nsossonko@hotmail.com)
  25. Based on code initially developed by: Einar Lielmanis, <einar@jsbeautifier.org>
  26. http://jsbeautifier.org/
  27. Usage:
  28. style_html(html_source);
  29. style_html(html_source, options);
  30. The options are:
  31. indent_inner_html (default false) — indent <head> and <body> sections,
  32. indent_size (default 4) — indentation size,
  33. indent_char (default space) — character to indent with,
  34. wrap_line_length (default 250) - maximum amount of characters per line (0 = disable)
  35. brace_style (default "collapse") - "collapse" | "expand" | "end-expand" | "none"
  36. 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.
  37. unformatted (defaults to inline tags) - list of tags, that shouldn't be reformatted
  38. indent_scripts (default normal) - "keep"|"separate"|"normal"
  39. preserve_newlines (default true) - whether existing line breaks before elements should be preserved
  40. Only works before elements, not inside tags or for text.
  41. max_preserve_newlines (default unlimited) - maximum number of line breaks to be preserved in one chunk
  42. indent_handlebars (default false) - format and indent {{#foo}} and {{/foo}}
  43. end_with_newline (false) - end with a newline
  44. extra_liners (default [head,body,/html]) -List of tags that should have an extra newline before them.
  45. e.g.
  46. style_html(html_source, {
  47. 'indent_inner_html': false,
  48. 'indent_size': 2,
  49. 'indent_char': ' ',
  50. 'wrap_line_length': 78,
  51. 'brace_style': 'expand',
  52. 'unformatted': ['a', 'sub', 'sup', 'b', 'i', 'u'],
  53. 'preserve_newlines': true,
  54. 'max_preserve_newlines': 5,
  55. 'indent_handlebars': false,
  56. 'extra_liners': ['/html']
  57. });
  58. */
  59. (function() {
  60. function trim(s) {
  61. return s.replace(/^\s+|\s+$/g, '');
  62. }
  63. function ltrim(s) {
  64. return s.replace(/^\s+/g, '');
  65. }
  66. function rtrim(s) {
  67. return s.replace(/\s+$/g,'');
  68. }
  69. function style_html(html_source, options, js_beautify, css_beautify) {
  70. //Wrapper function to invoke all the necessary constructors and deal with the output.
  71. var multi_parser,
  72. indent_inner_html,
  73. indent_size,
  74. indent_character,
  75. wrap_line_length,
  76. brace_style,
  77. unformatted,
  78. preserve_newlines,
  79. max_preserve_newlines,
  80. indent_handlebars,
  81. wrap_attributes,
  82. wrap_attributes_indent_size,
  83. end_with_newline,
  84. extra_liners,
  85. eol;
  86. options = options || {};
  87. // backwards compatibility to 1.3.4
  88. if ((options.wrap_line_length === undefined || parseInt(options.wrap_line_length, 10) === 0) &&
  89. (options.max_char !== undefined && parseInt(options.max_char, 10) !== 0)) {
  90. options.wrap_line_length = options.max_char;
  91. }
  92. indent_inner_html = (options.indent_inner_html === undefined) ? false : options.indent_inner_html;
  93. indent_size = (options.indent_size === undefined) ? 4 : parseInt(options.indent_size, 10);
  94. indent_character = (options.indent_char === undefined) ? ' ' : options.indent_char;
  95. brace_style = (options.brace_style === undefined) ? 'collapse' : options.brace_style;
  96. wrap_line_length = parseInt(options.wrap_line_length, 10) === 0 ? 32786 : parseInt(options.wrap_line_length || 250, 10);
  97. unformatted = options.unformatted || ['a', 'span', 'img', 'bdo', 'em', 'strong', 'dfn', 'code', 'samp', 'kbd',
  98. 'var', 'cite', 'abbr', 'acronym', 'q', 'sub', 'sup', 'tt', 'i', 'b', 'big', 'small', 'u', 's', 'strike',
  99. 'font', 'ins', 'del', 'pre', 'address', 'dt', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'];
  100. preserve_newlines = (options.preserve_newlines === undefined) ? true : options.preserve_newlines;
  101. max_preserve_newlines = preserve_newlines ?
  102. (isNaN(parseInt(options.max_preserve_newlines, 10)) ? 32786 : parseInt(options.max_preserve_newlines, 10))
  103. : 0;
  104. indent_handlebars = (options.indent_handlebars === undefined) ? false : options.indent_handlebars;
  105. wrap_attributes = (options.wrap_attributes === undefined) ? 'auto' : options.wrap_attributes;
  106. wrap_attributes_indent_size = (options.wrap_attributes_indent_size === undefined) ? indent_size : parseInt(options.wrap_attributes_indent_size, 10) || indent_size;
  107. end_with_newline = (options.end_with_newline === undefined) ? false : options.end_with_newline;
  108. extra_liners = (typeof options.extra_liners == 'object') && options.extra_liners ?
  109. options.extra_liners.concat() : (typeof options.extra_liners === 'string') ?
  110. options.extra_liners.split(',') : 'head,body,/html'.split(',');
  111. eol = options.eol ? options.eol : '\n';
  112. if(options.indent_with_tabs){
  113. indent_character = '\t';
  114. indent_size = 1;
  115. }
  116. eol = eol.replace(/\\r/, '\r').replace(/\\n/, '\n')
  117. function Parser() {
  118. this.pos = 0; //Parser position
  119. this.token = '';
  120. this.current_mode = 'CONTENT'; //reflects the current Parser mode: TAG/CONTENT
  121. this.tags = { //An object to hold tags, their position, and their parent-tags, initiated with default values
  122. parent: 'parent1',
  123. parentcount: 1,
  124. parent1: ''
  125. };
  126. this.tag_type = '';
  127. this.token_text = this.last_token = this.last_text = this.token_type = '';
  128. this.newlines = 0;
  129. this.indent_content = indent_inner_html;
  130. this.Utils = { //Uilities made available to the various functions
  131. whitespace: "\n\r\t ".split(''),
  132. single_token: 'br,input,link,meta,source,!doctype,basefont,base,area,hr,wbr,param,img,isindex,embed'.split(','), //all the single tags for HTML
  133. extra_liners: extra_liners, //for tags that need a line of whitespace before them
  134. in_array: function(what, arr) {
  135. for (var i = 0; i < arr.length; i++) {
  136. if (what === arr[i]) {
  137. return true;
  138. }
  139. }
  140. return false;
  141. }
  142. };
  143. // Return true if the given text is composed entirely of whitespace.
  144. this.is_whitespace = function(text) {
  145. for (var n = 0; n < text.length; text++) {
  146. if (!this.Utils.in_array(text.charAt(n), this.Utils.whitespace)) {
  147. return false;
  148. }
  149. }
  150. return true;
  151. };
  152. this.traverse_whitespace = function() {
  153. var input_char = '';
  154. input_char = this.input.charAt(this.pos);
  155. if (this.Utils.in_array(input_char, this.Utils.whitespace)) {
  156. this.newlines = 0;
  157. while (this.Utils.in_array(input_char, this.Utils.whitespace)) {
  158. if (preserve_newlines && input_char === '\n' && this.newlines <= max_preserve_newlines) {
  159. this.newlines += 1;
  160. }
  161. this.pos++;
  162. input_char = this.input.charAt(this.pos);
  163. }
  164. return true;
  165. }
  166. return false;
  167. };
  168. // Append a space to the given content (string array) or, if we are
  169. // at the wrap_line_length, append a newline/indentation.
  170. this.space_or_wrap = function(content) {
  171. if (this.line_char_count >= this.wrap_line_length) { //insert a line when the wrap_line_length is reached
  172. this.print_newline(false, content);
  173. this.print_indentation(content);
  174. } else {
  175. this.line_char_count++;
  176. content.push(' ');
  177. }
  178. };
  179. this.get_content = function() { //function to capture regular content between tags
  180. var input_char = '',
  181. content = [],
  182. space = false; //if a space is needed
  183. while (this.input.charAt(this.pos) !== '<') {
  184. if (this.pos >= this.input.length) {
  185. return content.length ? content.join('') : ['', 'TK_EOF'];
  186. }
  187. if (this.traverse_whitespace()) {
  188. this.space_or_wrap(content);
  189. continue;
  190. }
  191. if (indent_handlebars) {
  192. // Handlebars parsing is complicated.
  193. // {{#foo}} and {{/foo}} are formatted tags.
  194. // {{something}} should get treated as content, except:
  195. // {{else}} specifically behaves like {{#if}} and {{/if}}
  196. var peek3 = this.input.substr(this.pos, 3);
  197. if (peek3 === '{{#' || peek3 === '{{/') {
  198. // These are tags and not content.
  199. break;
  200. } else if (peek3 === '{{!') {
  201. return [this.get_tag(), 'TK_TAG_HANDLEBARS_COMMENT'];
  202. } else if (this.input.substr(this.pos, 2) === '{{') {
  203. if (this.get_tag(true) === '{{else}}') {
  204. break;
  205. }
  206. }
  207. }
  208. input_char = this.input.charAt(this.pos);
  209. this.pos++;
  210. this.line_char_count++;
  211. content.push(input_char); //letter at-a-time (or string) inserted to an array
  212. }
  213. return content.length ? content.join('') : '';
  214. };
  215. this.get_contents_to = function(name) { //get the full content of a script or style to pass to js_beautify
  216. if (this.pos === this.input.length) {
  217. return ['', 'TK_EOF'];
  218. }
  219. var input_char = '';
  220. var content = '';
  221. var reg_match = new RegExp('</' + name + '\\s*>', 'igm');
  222. reg_match.lastIndex = this.pos;
  223. var reg_array = reg_match.exec(this.input);
  224. var end_script = reg_array ? reg_array.index : this.input.length; //absolute end of script
  225. if (this.pos < end_script) { //get everything in between the script tags
  226. content = this.input.substring(this.pos, end_script);
  227. this.pos = end_script;
  228. }
  229. return content;
  230. };
  231. this.record_tag = function(tag) { //function to record a tag and its parent in this.tags Object
  232. if (this.tags[tag + 'count']) { //check for the existence of this tag type
  233. this.tags[tag + 'count']++;
  234. this.tags[tag + this.tags[tag + 'count']] = this.indent_level; //and record the present indent level
  235. } else { //otherwise initialize this tag type
  236. this.tags[tag + 'count'] = 1;
  237. this.tags[tag + this.tags[tag + 'count']] = this.indent_level; //and record the present indent level
  238. }
  239. 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)
  240. this.tags.parent = tag + this.tags[tag + 'count']; //and make this the current parent (i.e. in the case of a div 'div1')
  241. };
  242. this.retrieve_tag = function(tag) { //function to retrieve the opening tag to the corresponding closer
  243. if (this.tags[tag + 'count']) { //if the openener is not in the Object we ignore it
  244. var temp_parent = this.tags.parent; //check to see if it's a closable tag.
  245. while (temp_parent) { //till we reach '' (the initial value);
  246. if (tag + this.tags[tag + 'count'] === temp_parent) { //if this is it use it
  247. break;
  248. }
  249. temp_parent = this.tags[temp_parent + 'parent']; //otherwise keep on climbing up the DOM Tree
  250. }
  251. if (temp_parent) { //if we caught something
  252. this.indent_level = this.tags[tag + this.tags[tag + 'count']]; //set the indent_level accordingly
  253. this.tags.parent = this.tags[temp_parent + 'parent']; //and set the current parent
  254. }
  255. delete this.tags[tag + this.tags[tag + 'count'] + 'parent']; //delete the closed tags parent reference...
  256. delete this.tags[tag + this.tags[tag + 'count']]; //...and the tag itself
  257. if (this.tags[tag + 'count'] === 1) {
  258. delete this.tags[tag + 'count'];
  259. } else {
  260. this.tags[tag + 'count']--;
  261. }
  262. }
  263. };
  264. this.indent_to_tag = function(tag) {
  265. // Match the indentation level to the last use of this tag, but don't remove it.
  266. if (!this.tags[tag + 'count']) {
  267. return;
  268. }
  269. var temp_parent = this.tags.parent;
  270. while (temp_parent) {
  271. if (tag + this.tags[tag + 'count'] === temp_parent) {
  272. break;
  273. }
  274. temp_parent = this.tags[temp_parent + 'parent'];
  275. }
  276. if (temp_parent) {
  277. this.indent_level = this.tags[tag + this.tags[tag + 'count']];
  278. }
  279. };
  280. this.get_tag = function(peek) { //function to get a full tag and parse its type
  281. var input_char = '',
  282. content = [],
  283. comment = '',
  284. space = false,
  285. first_attr = true,
  286. tag_start, tag_end,
  287. tag_start_char,
  288. orig_pos = this.pos,
  289. orig_line_char_count = this.line_char_count;
  290. peek = peek !== undefined ? peek : false;
  291. do {
  292. if (this.pos >= this.input.length) {
  293. if (peek) {
  294. this.pos = orig_pos;
  295. this.line_char_count = orig_line_char_count;
  296. }
  297. return content.length ? content.join('') : ['', 'TK_EOF'];
  298. }
  299. input_char = this.input.charAt(this.pos);
  300. this.pos++;
  301. if (this.Utils.in_array(input_char, this.Utils.whitespace)) { //don't want to insert unnecessary space
  302. space = true;
  303. continue;
  304. }
  305. if (input_char === "'" || input_char === '"') {
  306. input_char += this.get_unformatted(input_char);
  307. space = true;
  308. }
  309. if (input_char === '=') { //no space before =
  310. space = false;
  311. }
  312. if (content.length && content[content.length - 1] !== '=' && input_char !== '>' && space) {
  313. //no space after = or before >
  314. this.space_or_wrap(content);
  315. space = false;
  316. if (!first_attr && wrap_attributes === 'force' && input_char !== '/') {
  317. this.print_newline(true, content);
  318. this.print_indentation(content);
  319. for (var count = 0; count < wrap_attributes_indent_size; count++) {
  320. content.push(indent_character);
  321. }
  322. }
  323. for (var i = 0; i < content.length; i++) {
  324. if (content[i] === ' ') {
  325. first_attr = false;
  326. break;
  327. }
  328. }
  329. }
  330. if (indent_handlebars && tag_start_char === '<') {
  331. // When inside an angle-bracket tag, put spaces around
  332. // handlebars not inside of strings.
  333. if ((input_char + this.input.charAt(this.pos)) === '{{') {
  334. input_char += this.get_unformatted('}}');
  335. if (content.length && content[content.length - 1] !== ' ' && content[content.length - 1] !== '<') {
  336. input_char = ' ' + input_char;
  337. }
  338. space = true;
  339. }
  340. }
  341. if (input_char === '<' && !tag_start_char) {
  342. tag_start = this.pos - 1;
  343. tag_start_char = '<';
  344. }
  345. if (indent_handlebars && !tag_start_char) {
  346. if (content.length >= 2 && content[content.length - 1] === '{' && content[content.length - 2] === '{') {
  347. if (input_char === '#' || input_char === '/' || input_char === '!') {
  348. tag_start = this.pos - 3;
  349. } else {
  350. tag_start = this.pos - 2;
  351. }
  352. tag_start_char = '{';
  353. }
  354. }
  355. this.line_char_count++;
  356. content.push(input_char); //inserts character at-a-time (or string)
  357. if (content[1] && (content[1] === '!' || content[1] === '?' || content[1] === '%')) { //if we're in a comment, do something special
  358. // We treat all comments as literals, even more than preformatted tags
  359. // we just look for the appropriate close tag
  360. content = [this.get_comment(tag_start)];
  361. break;
  362. }
  363. if (indent_handlebars && content[1] && content[1] === '{' && content[2] && content[2] === '!') { //if we're in a comment, do something special
  364. // We treat all comments as literals, even more than preformatted tags
  365. // we just look for the appropriate close tag
  366. content = [this.get_comment(tag_start)];
  367. break;
  368. }
  369. if (indent_handlebars && tag_start_char === '{' && content.length > 2 && content[content.length - 2] === '}' && content[content.length - 1] === '}') {
  370. break;
  371. }
  372. } while (input_char !== '>');
  373. var tag_complete = content.join('');
  374. var tag_index;
  375. var tag_offset;
  376. if (tag_complete.indexOf(' ') !== -1) { //if there's whitespace, thats where the tag name ends
  377. tag_index = tag_complete.indexOf(' ');
  378. } else if (tag_complete.charAt(0) === '{') {
  379. tag_index = tag_complete.indexOf('}');
  380. } else { //otherwise go with the tag ending
  381. tag_index = tag_complete.indexOf('>');
  382. }
  383. if (tag_complete.charAt(0) === '<' || !indent_handlebars) {
  384. tag_offset = 1;
  385. } else {
  386. tag_offset = tag_complete.charAt(2) === '#' ? 3 : 2;
  387. }
  388. var tag_check = tag_complete.substring(tag_offset, tag_index).toLowerCase();
  389. if (tag_complete.charAt(tag_complete.length - 2) === '/' ||
  390. 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 /)
  391. if (!peek) {
  392. this.tag_type = 'SINGLE';
  393. }
  394. } else if (indent_handlebars && tag_complete.charAt(0) === '{' && tag_check === 'else') {
  395. if (!peek) {
  396. this.indent_to_tag('if');
  397. this.tag_type = 'HANDLEBARS_ELSE';
  398. this.indent_content = true;
  399. this.traverse_whitespace();
  400. }
  401. } else if (this.is_unformatted(tag_check, unformatted)) { // do not reformat the "unformatted" tags
  402. comment = this.get_unformatted('</' + tag_check + '>', tag_complete); //...delegate to get_unformatted function
  403. content.push(comment);
  404. tag_end = this.pos - 1;
  405. this.tag_type = 'SINGLE';
  406. } else if (tag_check === 'script' &&
  407. (tag_complete.search('type') === -1 ||
  408. (tag_complete.search('type') > -1 &&
  409. tag_complete.search(/\b(text|application)\/(x-)?(javascript|ecmascript|jscript|livescript)/) > -1))) {
  410. if (!peek) {
  411. this.record_tag(tag_check);
  412. this.tag_type = 'SCRIPT';
  413. }
  414. } else if (tag_check === 'style' &&
  415. (tag_complete.search('type') === -1 ||
  416. (tag_complete.search('type') > -1 && tag_complete.search('text/css') > -1))) {
  417. if (!peek) {
  418. this.record_tag(tag_check);
  419. this.tag_type = 'STYLE';
  420. }
  421. } else if (tag_check.charAt(0) === '!') { //peek for <! comment
  422. // for comments content is already correct.
  423. if (!peek) {
  424. this.tag_type = 'SINGLE';
  425. this.traverse_whitespace();
  426. }
  427. } else if (!peek) {
  428. if (tag_check.charAt(0) === '/') { //this tag is a double tag so check for tag-ending
  429. this.retrieve_tag(tag_check.substring(1)); //remove it and all ancestors
  430. this.tag_type = 'END';
  431. } else { //otherwise it's a start-tag
  432. this.record_tag(tag_check); //push it on the tag stack
  433. if (tag_check.toLowerCase() !== 'html') {
  434. this.indent_content = true;
  435. }
  436. this.tag_type = 'START';
  437. }
  438. // Allow preserving of newlines after a start or end tag
  439. if (this.traverse_whitespace()) {
  440. this.space_or_wrap(content);
  441. }
  442. if (this.Utils.in_array(tag_check, this.Utils.extra_liners)) { //check if this double needs an extra line
  443. this.print_newline(false, this.output);
  444. if (this.output.length && this.output[this.output.length - 2] !== '\n') {
  445. this.print_newline(true, this.output);
  446. }
  447. }
  448. }
  449. if (peek) {
  450. this.pos = orig_pos;
  451. this.line_char_count = orig_line_char_count;
  452. }
  453. return content.join(''); //returns fully formatted tag
  454. };
  455. this.get_comment = function(start_pos) { //function to return comment content in its entirety
  456. // this is will have very poor perf, but will work for now.
  457. var comment = '',
  458. delimiter = '>',
  459. matched = false;
  460. this.pos = start_pos;
  461. input_char = this.input.charAt(this.pos);
  462. this.pos++;
  463. while (this.pos <= this.input.length) {
  464. comment += input_char;
  465. // only need to check for the delimiter if the last chars match
  466. if (comment.charAt(comment.length - 1) === delimiter.charAt(delimiter.length - 1) &&
  467. comment.indexOf(delimiter) !== -1) {
  468. break;
  469. }
  470. // only need to search for custom delimiter for the first few characters
  471. if (!matched && comment.length < 10) {
  472. if (comment.indexOf('<![if') === 0) { //peek for <![if conditional comment
  473. delimiter = '<![endif]>';
  474. matched = true;
  475. } else if (comment.indexOf('<![cdata[') === 0) { //if it's a <[cdata[ comment...
  476. delimiter = ']]>';
  477. matched = true;
  478. } else if (comment.indexOf('<![') === 0) { // some other ![ comment? ...
  479. delimiter = ']>';
  480. matched = true;
  481. } else if (comment.indexOf('<!--') === 0) { // <!-- comment ...
  482. delimiter = '-->';
  483. matched = true;
  484. } else if (comment.indexOf('{{!') === 0) { // {{! handlebars comment
  485. delimiter = '}}';
  486. matched = true;
  487. } else if (comment.indexOf('<?') === 0) { // {{! handlebars comment
  488. delimiter = '?>';
  489. matched = true;
  490. } else if (comment.indexOf('<%') === 0) { // {{! handlebars comment
  491. delimiter = '%>';
  492. matched = true;
  493. }
  494. }
  495. input_char = this.input.charAt(this.pos);
  496. this.pos++;
  497. }
  498. return comment;
  499. };
  500. this.get_unformatted = function(delimiter, orig_tag) { //function to return unformatted content in its entirety
  501. if (orig_tag && orig_tag.toLowerCase().indexOf(delimiter) !== -1) {
  502. return '';
  503. }
  504. var input_char = '';
  505. var content = '';
  506. var min_index = 0;
  507. var space = true;
  508. do {
  509. if (this.pos >= this.input.length) {
  510. return content;
  511. }
  512. input_char = this.input.charAt(this.pos);
  513. this.pos++;
  514. if (this.Utils.in_array(input_char, this.Utils.whitespace)) {
  515. if (!space) {
  516. this.line_char_count--;
  517. continue;
  518. }
  519. if (input_char === '\n' || input_char === '\r') {
  520. content += '\n';
  521. /* 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'
  522. for (var i=0; i<this.indent_level; i++) {
  523. content += this.indent_string;
  524. }
  525. space = false; //...and make sure other indentation is erased
  526. */
  527. this.line_char_count = 0;
  528. continue;
  529. }
  530. }
  531. content += input_char;
  532. this.line_char_count++;
  533. space = true;
  534. if (indent_handlebars && input_char === '{' && content.length && content.charAt(content.length - 2) === '{') {
  535. // Handlebars expressions in strings should also be unformatted.
  536. content += this.get_unformatted('}}');
  537. // These expressions are opaque. Ignore delimiters found in them.
  538. min_index = content.length;
  539. }
  540. } while (content.toLowerCase().indexOf(delimiter, min_index) === -1);
  541. return content;
  542. };
  543. this.get_token = function() { //initial handler for token-retrieval
  544. var token;
  545. if (this.last_token === 'TK_TAG_SCRIPT' || this.last_token === 'TK_TAG_STYLE') { //check if we need to format javascript
  546. var type = this.last_token.substr(7);
  547. token = this.get_contents_to(type);
  548. if (typeof token !== 'string') {
  549. return token;
  550. }
  551. return [token, 'TK_' + type];
  552. }
  553. if (this.current_mode === 'CONTENT') {
  554. token = this.get_content();
  555. if (typeof token !== 'string') {
  556. return token;
  557. } else {
  558. return [token, 'TK_CONTENT'];
  559. }
  560. }
  561. if (this.current_mode === 'TAG') {
  562. token = this.get_tag();
  563. if (typeof token !== 'string') {
  564. return token;
  565. } else {
  566. var tag_name_type = 'TK_TAG_' + this.tag_type;
  567. return [token, tag_name_type];
  568. }
  569. }
  570. };
  571. this.get_full_indent = function(level) {
  572. level = this.indent_level + level || 0;
  573. if (level < 1) {
  574. return '';
  575. }
  576. return Array(level + 1).join(this.indent_string);
  577. };
  578. this.is_unformatted = function(tag_check, unformatted) {
  579. //is this an HTML5 block-level link?
  580. if (!this.Utils.in_array(tag_check, unformatted)) {
  581. return false;
  582. }
  583. if (tag_check.toLowerCase() !== 'a' || !this.Utils.in_array('a', unformatted)) {
  584. return true;
  585. }
  586. //at this point we have an tag; is its first child something we want to remain
  587. //unformatted?
  588. var next_tag = this.get_tag(true /* peek. */ );
  589. // test next_tag to see if it is just html tag (no external content)
  590. var tag = (next_tag || "").match(/^\s*<\s*\/?([a-z]*)\s*[^>]*>\s*$/);
  591. // if next_tag comes back but is not an isolated tag, then
  592. // let's treat the 'a' tag as having content
  593. // and respect the unformatted option
  594. if (!tag || this.Utils.in_array(tag, unformatted)) {
  595. return true;
  596. } else {
  597. return false;
  598. }
  599. };
  600. this.printer = function(js_source, indent_character, indent_size, wrap_line_length, brace_style) { //handles input/output and some other printing functions
  601. this.input = js_source || ''; //gets the input for the Parser
  602. // HACK: newline parsing inconsistent. This brute force normalizes the input.
  603. this.input = this.input.replace(/\r\n|[\r\u2028\u2029]/g, '\n')
  604. this.output = [];
  605. this.indent_character = indent_character;
  606. this.indent_string = '';
  607. this.indent_size = indent_size;
  608. this.brace_style = brace_style;
  609. this.indent_level = 0;
  610. this.wrap_line_length = wrap_line_length;
  611. this.line_char_count = 0; //count to see if wrap_line_length was exceeded
  612. for (var i = 0; i < this.indent_size; i++) {
  613. this.indent_string += this.indent_character;
  614. }
  615. this.print_newline = function(force, arr) {
  616. this.line_char_count = 0;
  617. if (!arr || !arr.length) {
  618. return;
  619. }
  620. if (force || (arr[arr.length - 1] !== '\n')) { //we might want the extra line
  621. if ((arr[arr.length - 1] !== '\n')) {
  622. arr[arr.length - 1] = rtrim(arr[arr.length - 1]);
  623. }
  624. arr.push('\n');
  625. }
  626. };
  627. this.print_indentation = function(arr) {
  628. for (var i = 0; i < this.indent_level; i++) {
  629. arr.push(this.indent_string);
  630. this.line_char_count += this.indent_string.length;
  631. }
  632. };
  633. this.print_token = function(text) {
  634. // Avoid printing initial whitespace.
  635. if (this.is_whitespace(text) && !this.output.length) {
  636. return;
  637. }
  638. if (text || text !== '') {
  639. if (this.output.length && this.output[this.output.length - 1] === '\n') {
  640. this.print_indentation(this.output);
  641. text = ltrim(text);
  642. }
  643. }
  644. this.print_token_raw(text);
  645. };
  646. this.print_token_raw = function(text) {
  647. // If we are going to print newlines, truncate trailing
  648. // whitespace, as the newlines will represent the space.
  649. if (this.newlines > 0) {
  650. text = rtrim(text);
  651. }
  652. if (text && text !== '') {
  653. if (text.length > 1 && text.charAt(text.length - 1) === '\n') {
  654. // unformatted tags can grab newlines as their last character
  655. this.output.push(text.slice(0, -1));
  656. this.print_newline(false, this.output);
  657. } else {
  658. this.output.push(text);
  659. }
  660. }
  661. for (var n = 0; n < this.newlines; n++) {
  662. this.print_newline(n > 0, this.output);
  663. }
  664. this.newlines = 0;
  665. };
  666. this.indent = function() {
  667. this.indent_level++;
  668. };
  669. this.unindent = function() {
  670. if (this.indent_level > 0) {
  671. this.indent_level--;
  672. }
  673. };
  674. };
  675. return this;
  676. }
  677. /*_____________________--------------------_____________________*/
  678. multi_parser = new Parser(); //wrapping functions Parser
  679. multi_parser.printer(html_source, indent_character, indent_size, wrap_line_length, brace_style); //initialize starting values
  680. while (true) {
  681. var t = multi_parser.get_token();
  682. multi_parser.token_text = t[0];
  683. multi_parser.token_type = t[1];
  684. if (multi_parser.token_type === 'TK_EOF') {
  685. break;
  686. }
  687. switch (multi_parser.token_type) {
  688. case 'TK_TAG_START':
  689. multi_parser.print_newline(false, multi_parser.output);
  690. multi_parser.print_token(multi_parser.token_text);
  691. if (multi_parser.indent_content) {
  692. multi_parser.indent();
  693. multi_parser.indent_content = false;
  694. }
  695. multi_parser.current_mode = 'CONTENT';
  696. break;
  697. case 'TK_TAG_STYLE':
  698. case 'TK_TAG_SCRIPT':
  699. multi_parser.print_newline(false, multi_parser.output);
  700. multi_parser.print_token(multi_parser.token_text);
  701. multi_parser.current_mode = 'CONTENT';
  702. break;
  703. case 'TK_TAG_END':
  704. //Print new line only if the tag has no content and has child
  705. if (multi_parser.last_token === 'TK_CONTENT' && multi_parser.last_text === '') {
  706. var tag_name = multi_parser.token_text.match(/\w+/)[0];
  707. var tag_extracted_from_last_output = null;
  708. if (multi_parser.output.length) {
  709. tag_extracted_from_last_output = multi_parser.output[multi_parser.output.length - 1].match(/(?:<|{{#)\s*(\w+)/);
  710. }
  711. if (tag_extracted_from_last_output === null ||
  712. (tag_extracted_from_last_output[1] !== tag_name && !multi_parser.Utils.in_array(tag_extracted_from_last_output[1], unformatted))) {
  713. multi_parser.print_newline(false, multi_parser.output);
  714. }
  715. }
  716. multi_parser.print_token(multi_parser.token_text);
  717. multi_parser.current_mode = 'CONTENT';
  718. break;
  719. case 'TK_TAG_SINGLE':
  720. // Don't add a newline before elements that should remain unformatted.
  721. var tag_check = multi_parser.token_text.match(/^\s*<([a-z-]+)/i);
  722. if (!tag_check || !multi_parser.Utils.in_array(tag_check[1], unformatted)) {
  723. multi_parser.print_newline(false, multi_parser.output);
  724. }
  725. multi_parser.print_token(multi_parser.token_text);
  726. multi_parser.current_mode = 'CONTENT';
  727. break;
  728. case 'TK_TAG_HANDLEBARS_ELSE':
  729. multi_parser.print_token(multi_parser.token_text);
  730. if (multi_parser.indent_content) {
  731. multi_parser.indent();
  732. multi_parser.indent_content = false;
  733. }
  734. multi_parser.current_mode = 'CONTENT';
  735. break;
  736. case 'TK_TAG_HANDLEBARS_COMMENT':
  737. multi_parser.print_token(multi_parser.token_text);
  738. multi_parser.current_mode = 'TAG';
  739. break;
  740. case 'TK_CONTENT':
  741. multi_parser.print_token(multi_parser.token_text);
  742. multi_parser.current_mode = 'TAG';
  743. break;
  744. case 'TK_STYLE':
  745. case 'TK_SCRIPT':
  746. if (multi_parser.token_text !== '') {
  747. multi_parser.print_newline(false, multi_parser.output);
  748. var text = multi_parser.token_text,
  749. _beautifier,
  750. script_indent_level = 1;
  751. if (multi_parser.token_type === 'TK_SCRIPT') {
  752. _beautifier = typeof js_beautify === 'function' && js_beautify;
  753. } else if (multi_parser.token_type === 'TK_STYLE') {
  754. _beautifier = typeof css_beautify === 'function' && css_beautify;
  755. }
  756. if (options.indent_scripts === "keep") {
  757. script_indent_level = 0;
  758. } else if (options.indent_scripts === "separate") {
  759. script_indent_level = -multi_parser.indent_level;
  760. }
  761. var indentation = multi_parser.get_full_indent(script_indent_level);
  762. if (_beautifier) {
  763. // call the Beautifier if avaliable
  764. var Child_options = function() {
  765. this.eol = '\n';
  766. };
  767. Child_options.prototype = options;
  768. var child_options = new Child_options();
  769. text = _beautifier(text.replace(/^\s*/, indentation), child_options);
  770. } else {
  771. // simply indent the string otherwise
  772. var white = text.match(/^\s*/)[0];
  773. var _level = white.match(/[^\n\r]*$/)[0].split(multi_parser.indent_string).length - 1;
  774. var reindent = multi_parser.get_full_indent(script_indent_level - _level);
  775. text = text.replace(/^\s*/, indentation)
  776. .replace(/\r\n|\r|\n/g, '\n' + reindent)
  777. .replace(/\s+$/, '');
  778. }
  779. if (text) {
  780. multi_parser.print_token_raw(text);
  781. multi_parser.print_newline(true, multi_parser.output);
  782. }
  783. }
  784. multi_parser.current_mode = 'TAG';
  785. break;
  786. default:
  787. // We should not be getting here but we don't want to drop input on the floor
  788. // Just output the text and move on
  789. if (multi_parser.token_text !== '') {
  790. multi_parser.print_token(multi_parser.token_text);
  791. }
  792. break;
  793. }
  794. multi_parser.last_token = multi_parser.token_type;
  795. multi_parser.last_text = multi_parser.token_text;
  796. }
  797. var sweet_code = multi_parser.output.join('').replace(/[\r\n\t ]+$/, '');
  798. // establish end_with_newline
  799. if (end_with_newline) {
  800. sweet_code += '\n';
  801. }
  802. if (eol != '\n') {
  803. sweet_code = sweet_code.replace(/[\n]/g, eol);
  804. }
  805. return sweet_code;
  806. }
  807. if (typeof define === "function" && define.amd) {
  808. // Add support for AMD ( https://github.com/amdjs/amdjs-api/wiki/AMD#defineamd-property- )
  809. define(["require", "./beautify", "./beautify-css"], function(requireamd) {
  810. var js_beautify = requireamd("./beautify");
  811. var css_beautify = requireamd("./beautify-css");
  812. return {
  813. html_beautify: function(html_source, options) {
  814. return style_html(html_source, options, js_beautify.js_beautify, css_beautify.css_beautify);
  815. }
  816. };
  817. });
  818. } else if (typeof exports !== "undefined") {
  819. // Add support for CommonJS. Just put this file somewhere on your require.paths
  820. // and you will be able to `var html_beautify = require("beautify").html_beautify`.
  821. var js_beautify = require('./beautify.js');
  822. var css_beautify = require('./beautify-css.js');
  823. exports.html_beautify = function(html_source, options) {
  824. return style_html(html_source, options, js_beautify.js_beautify, css_beautify.css_beautify);
  825. };
  826. } else if (typeof window !== "undefined") {
  827. // If we're running a web page and don't have either of the above, add our one global
  828. window.html_beautify = function(html_source, options) {
  829. return style_html(html_source, options, window.js_beautify, window.css_beautify);
  830. };
  831. } else if (typeof global !== "undefined") {
  832. // If we don't even have window, try global.
  833. global.html_beautify = function(html_source, options) {
  834. return style_html(html_source, options, global.js_beautify, global.css_beautify);
  835. };
  836. }
  837. }());