-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fixed problems with colon character #363
Changes from 7 commits
14743e1
43c06e9
306f321
792c615
7b529dc
eab7b74
b457034
2eb49a2
398843b
e0ca5ad
c80476f
10bee4d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -233,7 +233,19 @@ | |
insideRule = false; | ||
} else if (ch === ":") { | ||
eatWhitespace(); | ||
output.push(ch, " "); | ||
|
||
var text_after_pos = source_text.replace(new RegExp("('|\").*('|\")", "gm"), "").substr(pos - 1) + ";", | ||
semicolon = text_after_pos.substr(0, text_after_pos.indexOf(';')).length, | ||
closed_brace = text_after_pos.substr(0, text_after_pos.indexOf('}')).length, | ||
open_brace = text_after_pos.substr(0, text_after_pos.indexOf('{')).length, | ||
test1 = (semicolon > closed_brace) ? closed_brace : semicolon; | ||
|
||
if (test1 > open_brace && open_brace !== 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why isn't there a function that extracts all the string chopping above? It's extremely noisy and hard to follow as-is. |
||
output.push(ch); | ||
} else { | ||
output.push(ch, " "); | ||
} | ||
|
||
insideRule = true; | ||
} else if (ch === '"' || ch === '\'') { | ||
output.push(eatString(ch)); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1761,6 +1761,13 @@ function run_beautifier_tests(test_obj, Urlencoded, js_beautify, html_beautify, | |
btc("#bla, #foo{color:green}", "#bla, #foo {\n color: green\n}\n"); | ||
btc("@media print {.tab{}}", "@media print {\n .tab {}\n}\n"); | ||
btc("#bla, #foo{color:black}", "#bla, #foo {\n color: black\n}\n"); | ||
|
||
// colon character | ||
btc("#foo : hover{color:purple}","#foo:hover {\n color: purple\n}\n"); | ||
btc(": : selection {\ncolor: #ff0000;}", "::selection {\n color: #ff0000;\n}"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These inputs are somewhat nonsensical, as they don't represent valid CSS syntax. It would be clearer if you just asserted properly formatted input (and then omit the second parameter, because it defaults to the first). btc("#foo:hover {\n color: purple\n}\n");
btc("::selection {\n color: #ff0000;\n}\n"); Note that the second test has an added There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought that the first parameter was the not formatted input and the second one the formatted one. Isn't it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the input and output are expected to be identical, all the test methods support simply passing one argument. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My input and output was not identical. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Like I said initially, they should be. Who in their right mind writes invalid CSS syntax and then expects it to magically work? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I get what you mean, but if the beautifier can even remove extra spaces and make the syntax valid I can't see the problem. I didn't added extra code to do it, it's just a side effect of my patch. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, yes, I want to take a part in this bikeshed as well! The tests by themselves are ok — we have weird corner cases and some broken-code-formatting checks in the jsbeautifier code as well. The strong uneasiness I myself feel is that the tests need to clearly cover the proper input first, but currently there are no tests that make sure that the beautifier formats a clean, proper css code as well. I.e something like this would make the tests much more structured and clear:
...and then we might have a separate discussion WHY would we ever want to test the garbage cases and what do we achieve by that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair enough. Although, for the purposes of this bugfix, |
||
|
||
// particular edge case with braces and semicolons inside tags that allows custom text | ||
btc("a: not(\"foobar\\\";{}omg\"){\ncontent: 'example\\';{} text';\ncontent: \"example\\\";{} text\";}", "a: not(\"foobar\\\";{}omg\") {\n content: 'example\';{} text';\n content: \"example\\\";{} text\";\n}"); | ||
|
||
return sanitytest; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Running a global replace with a non-reusable regexp on the source content every time a colon is encountered is dramatically bad practice.