Treat HTML comments better, fixes #99

This commit is contained in:
Einar Lielmanis 2012-03-02 17:31:44 +02:00
parent c2e8472664
commit 068ec1701a
5 changed files with 22 additions and 2 deletions

View File

@ -47,6 +47,11 @@ testj:
./tests/run-tests
echo
edit:
vim \
beautify.js python/jsbeautifier/__init__.py \
tests/beautify-tests.js python/jsbeautifier/tests/testjsbeautifier.py
tests: testp testj
test: testp testj

View File

@ -570,8 +570,13 @@ function js_beautify(js_source_text, options) {
if (c === '<' && input.substring(parser_pos - 1, parser_pos + 3) === '<!--') {
parser_pos += 3;
c = '<!--';
while (input[parser_pos] != '\n' && parser_pos < input_length) {
c += input[parser_pos];
parser_pos++;
}
flags.in_html_comment = true;
return ['<!--', 'TK_COMMENT'];
return [c, 'TK_COMMENT'];
}
if (c === '-' && flags.in_html_comment && input.substring(parser_pos - 1, parser_pos + 2) === '-->') {

View File

@ -603,8 +603,12 @@ class Beautifier:
if c == '<' and self.input[parser_pos - 1 : parser_pos + 3] == '<!--':
parser_pos += 3
c = '<!--'
while parser_pos < len(self.input) and self.input[parser_pos] != '\n':
c += self.input[parser_pos]
parser_pos += 1
self.flags.in_html_comment = True
return '<!--', 'TK_COMMENT'
return c, 'TK_COMMENT'
if c == '-' and self.flags.in_html_comment and self.input[parser_pos - 1 : parser_pos + 2] == '-->':
self.flags.in_html_comment = False

View File

@ -418,6 +418,8 @@ class TestJSBeautifier(unittest.TestCase):
bt("var x = {\n set function()\n}")
bt("var x = set\n\nfunction() {}")
bt('<!-- foo\nbar();\n-->');
bt('<!-- dont crash');
def decodesto(self, input, expectation=None):
self.assertEqual(

View File

@ -471,8 +471,12 @@ function run_beautifier_tests(test_obj)
bt("var x = {\n set function()\n}");
bt("var x = set\n\nfunction() {}");
bt('<!-- foo\nbar();\n-->');
bt('<!-- dont crash');
opts.space_before_conditional = false;
bt('if(a) b()');
return sanitytest;
}