Skip to content

Commit

Permalink
Unbreak php and underscore.js templating
Browse files Browse the repository at this point in the history
Html beautifier no longer kills php and underscore.js templates.
IF you try to run php the the js beautifier it will still die.

Fixes #490, #417
  • Loading branch information
bitwiseman committed Jun 16, 2015
1 parent 6163246 commit b7b5f89
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
14 changes: 11 additions & 3 deletions js/lib/beautify-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@
indent_character = (options.indent_char === undefined) ? ' ' : options.indent_char;
brace_style = (options.brace_style === undefined) ? 'collapse' : options.brace_style;
wrap_line_length = parseInt(options.wrap_line_length, 10) === 0 ? 32786 : parseInt(options.wrap_line_length || 250, 10);
unformatted = options.unformatted || ['a', 'span', 'img', 'bdo', 'em', 'strong', 'dfn', 'code', 'samp', 'kbd', 'var', 'cite', 'abbr', 'acronym', 'q', 'sub', 'sup', 'tt', 'i', 'b', 'big', 'small', 'u', 's', 'strike', 'font', 'ins', 'del', 'pre', 'address', 'dt', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'];
unformatted = options.unformatted || ['a', 'span', 'img', 'bdo', 'em', 'strong', 'dfn', 'code', 'samp', 'kbd',
'var', 'cite', 'abbr', 'acronym', 'q', 'sub', 'sup', 'tt', 'i', 'b', 'big', 'small', 'u', 's', 'strike',
'font', 'ins', 'del', 'pre', 'address', 'dt', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'];
preserve_newlines = (options.preserve_newlines === undefined) ? true : options.preserve_newlines;
max_preserve_newlines = preserve_newlines ?
(isNaN(parseInt(options.max_preserve_newlines, 10)) ? 32786 : parseInt(options.max_preserve_newlines, 10))
Expand Down Expand Up @@ -151,7 +153,7 @@

this.Utils = { //Uilities made available to the various functions
whitespace: "\n\r\t ".split(''),
single_token: 'br,input,link,meta,source,!doctype,basefont,base,area,hr,wbr,param,img,isindex,?xml,embed,?php,?,?='.split(','), //all the single tags for HTML
single_token: 'br,input,link,meta,source,!doctype,basefont,base,area,hr,wbr,param,img,isindex,embed'.split(','), //all the single tags for HTML
extra_liners: extra_liners, //for tags that need a line of whitespace before them
in_array: function(what, arr) {
for (var i = 0; i < arr.length; i++) {
Expand Down Expand Up @@ -404,7 +406,7 @@
this.line_char_count++;
content.push(input_char); //inserts character at-a-time (or string)

if (content[1] && content[1] === '!') { //if we're in a comment, do something special
if (content[1] && (content[1] === '!' || content[1] === '?' || content[1] === '%')) { //if we're in a comment, do something special
// We treat all comments as literals, even more than preformatted tags
// we just look for the appropriate close tag
content = [this.get_comment(tag_start)];
Expand Down Expand Up @@ -547,6 +549,12 @@
} else if (comment.indexOf('{{!') === 0) { // {{! handlebars comment
delimiter = '}}';
matched = true;
} else if (comment.indexOf('<?') === 0) { // {{! handlebars comment
delimiter = '?>';
matched = true;
} else if (comment.indexOf('<%') === 0) { // {{! handlebars comment
delimiter = '%>';
matched = true;
}
}

Expand Down
22 changes: 22 additions & 0 deletions js/test/beautify-html-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,28 @@ function run_html_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_be



// Php formatting
test_fragment('<h1 class="content-page-header"><?=$view["name"]; ?></h1>');
test_fragment(
'<?php\n' +
'for($i = 1; $i <= 100; $i++;) {\n' +
' #count to 100!\n' +
' echo($i . "</br>");\n' +
'}\n' +
'?>');



// underscore.js formatting
test_fragment(
'<div class="col-sm-9">\n' +
' <textarea id="notes" class="form-control" rows="3">\n' +
' <%= notes %>\n' +
' </textarea>\n' +
'</div>');



// Indent with tabs
opts.indent_with_tabs = true;
test_fragment(
Expand Down
32 changes: 32 additions & 0 deletions test/data/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,38 @@ exports.test_data = {
{ fragment: true, unchanged: '<ol>\n <li>b<pre>c</pre></li>\n</ol>' },
{ fragment: true, unchanged: '<ol>\n <li>b<code>c</code></li>\n</ol>' },
]
}, {
name: "Php formatting",
description: "Php (<?php ... ?>) treated as comments.",
options: [],
tests: [
{ fragment: true, unchanged: '<h1 class="content-page-header"><?=$view["name"]; ?></h1>' },
{ fragment: true, unchanged:
[
'<?php',
'for($i = 1; $i <= 100; $i++;) {',
' #count to 100!',
' echo($i . "</br>");',
'}',
'?>'
]
},
]
}, {
name: "underscore.js formatting",
description: "underscore.js templates (<% ... %>) treated as comments.",
options: [],
tests: [
{ fragment: true, unchanged:
[
'<div class="col-sm-9">',
' <textarea id="notes" class="form-control" rows="3">',
' <%= notes %>',
' </textarea>',
'</div>'
]
},
]
}, {
name: "Indent with tabs",
description: "Use one tab instead of several spaces for indentation",
Expand Down

0 comments on commit b7b5f89

Please sign in to comment.