Skip to content

Commit

Permalink
Strip trailing newlines, minor support for <!--
Browse files Browse the repository at this point in the history
  • Loading branch information
einars committed May 15, 2009
1 parent ae81f81 commit a8bc9e5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
8 changes: 5 additions & 3 deletions beautify-tests.js
Expand Up @@ -120,9 +120,9 @@ function test_js_beautify()
bt('a = 1e-10', "a = 1e-10");
bt('a = e - 10', "a = e - 10");
bt('a = 11-10', "a = 11 - 10");
bt("a = 1;// comment\n", "a = 1; // comment\n");
bt("a = 1; // comment\n", "a = 1; // comment\n");
bt("a = 1;\n // comment\n", "a = 1;\n// comment\n");
bt("a = 1;// comment\n", "a = 1; // comment");
bt("a = 1; // comment\n", "a = 1; // comment");
bt("a = 1;\n // comment\n", "a = 1;\n// comment");

bt("if (a) {\n do();\n}"); // was: extra space appended
bt("if\n(a)\nb()", "if (a) b()"); // test for proper newline removal
Expand Down Expand Up @@ -159,6 +159,8 @@ function test_js_beautify()
bt('{a:#1', '{\n a: #1'); // incomplete
bt('{a:#', '{\n a: #'); // incomplete

bt('<!--\nvoid();\n// -->', '<!--\nvoid();\n// -->');

bt('a=/regexp', 'a = /regexp'); // incomplete regexp

bt('{a:#1=[],b:#1#,c:#999999#}', '{\n a: #1=[],\n b: #1#,\n c: #999999#\n}');
Expand Down
10 changes: 9 additions & 1 deletion beautify.js
Expand Up @@ -326,6 +326,11 @@ function js_beautify(js_source_text, options)
}
}

if (c == '<' && input.substring(0, 4) === '<!--') {
parser_pos += 3;
return ['<!--', 'TK_COMMENT'];
}

if (in_array(c, punct)) {
while (parser_pos < input.length && in_array(c + input.charAt(parser_pos), punct)) {
c += input.charAt(parser_pos);
Expand All @@ -334,6 +339,7 @@ function js_beautify(js_source_text, options)
break;
}
}

return [c, 'TK_OPERATOR'];
}

Expand Down Expand Up @@ -364,6 +370,8 @@ function js_beautify(js_source_text, options)
whitespace = "\n\r\t ".split('');
wordchar = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$'.split('');
digits = '0123456789'.split('');

// <!-- is a special case (ok, it's a minor hack actually)
punct = '+ - * / % & ++ -- = += -= *= /= %= == === != !== > < >= <= >> << >>> >>>= >>= <<= && &= | || ! !! , : ? ^ ^= |= ::'.split(' ');

// words which should always start on new line.
Expand Down Expand Up @@ -668,6 +676,6 @@ function js_beautify(js_source_text, options)
last_text = token_text;
}

return output.join('');
return output.join('').replace(/\n+$/, '');

}

0 comments on commit a8bc9e5

Please sign in to comment.