Skip to content

Commit 2f54e79

Browse files
committed
Fixes attribute parsing bug where comments include braces
Also refactored out the dynamic bit in favor of present?/absent? logic. Slowly getting the hang of those conditionals. refs: zmoazeni#29
1 parent 787fb7b commit 2f54e79

File tree

3 files changed

+19
-17
lines changed

3 files changed

+19
-17
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## (Unreleased) ##
2+
3+
* Fixes attribute parsing bug that includes comments with braces
4+
15
## 1.3.1 - 4/20/2013 ##
26

37
* Fixes --ignore-sass-mixins bug with @importing

lib/csscss/parser/css.rb

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,22 @@ def parse(source)
1212
class Parser < Parslet::Parser
1313
include Common
1414

15-
rule(:comment) {
16-
(space? >> str('/*') >> (str('*/').absent? >> any).repeat >> str('*/') >> space?).as(:comment)
17-
}
18-
19-
rule(:css_space?) {
20-
comment.repeat(1) | space?
15+
rule(:raw_comment) {
16+
space? >> str('/*') >> (str('*/').absent? >> any).repeat >> str('*/') >> space?
2117
}
18+
rule(:comment) { raw_comment.as(:comment) }
2219

2320
rule(:blank_attribute) { str(";") >> space? }
2421

22+
rule(:attribute_value) { (str('/*').absent? >> match["^;}"]) | raw_comment }
23+
2524
rule(:attribute) {
2625
match["^:{}"].repeat(1).as(:property) >>
2726
str(":") >>
28-
(match["^;}"].repeat(1).capture(:stuff) >> dynamic {|source, context|
29-
if context.captures[:stuff].to_s =~ /data:/
30-
str(";") >> match["^;}"].repeat(1)
31-
else
32-
any.present?
33-
end
34-
}).as(:value) >>
27+
(
28+
(stri("data:").absent? >> attribute_value) |
29+
(stri("data:").present? >> attribute_value.repeat(1) >> str(";") >> attribute_value.repeat(1))
30+
).repeat(1).as(:value) >>
3531
str(";").maybe >>
3632
space?
3733
}

test/csscss/parser/css_test.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ module Css
2323
end
2424

2525
it "parses comments" do
26-
@parser.css_space?.must_parse "/* foo */"
27-
@parser.css_space?.must_parse %$
26+
@parser.comment.must_parse "/* foo */"
27+
@parser.comment.must_parse %$
2828
/* foo
2929
* bar
3030
*/
3131
$
3232

33-
@parser.css_space?.must_parse %$
33+
@parser.comment.repeat(1).must_parse %$
3434
/* foo */
3535
/* bar */
3636
$
@@ -63,11 +63,13 @@ module Css
6363
*/
6464
.bar { border: 1px solid black /* sdflk */ }
6565
.baz { background: white /* sdflk */ }
66+
.baz2 { background: white /* {sdflk} */ }
6667
$
6768

6869
trans(css).must_equal([
6970
rs(sel(".bar"), [dec("border", "1px solid black /* sdflk */")]),
70-
rs(sel(".baz"), [dec("background", "white /* sdflk */")])
71+
rs(sel(".baz"), [dec("background", "white /* sdflk */")]),
72+
rs(sel(".baz2"), [dec("background", "white /* {sdflk} */")])
7173
])
7274
end
7375

0 commit comments

Comments
 (0)