Skip to content

Commit

Permalink
Fix incorrect parsing of certain selectors inside a CSS @media block.
Browse files Browse the repository at this point in the history
Fixes #121.
  • Loading branch information
rgrove committed Oct 29, 2014
1 parent c62e83b commit bbb51e6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
10 changes: 10 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
Sanitize History
================================================================================

Version 3.0.3 (git)
-------------------

* Fixed: Some CSS selectors weren't parsed correctly inside the body of a
`@media` block, causing them to be removed even when whitelist rules should
have allowed them to remain. [#121][121]

[121]:https://github.com/rgrove/sanitize/issues/121


Version 3.0.2 (2014-09-02)
--------------------------

Expand Down
5 changes: 4 additions & 1 deletion lib/sanitize/css.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,10 @@ def at_rule!(rule)
return nil unless @config[:at_rules].include?(name)

if AT_RULES_WITH_STYLES.include?(name)
styles = Crass::Parser.parse_rules(rule[:block][:value],
# Remove the { and } tokens surrounding the @media block.
tokens = rule[:block][:tokens][1...-1]

styles = Crass::Parser.parse_rules(tokens,
:preserve_comments => @config[:allow_comments],
:preserve_hacks => @config[:allow_hacks])

Expand Down
27 changes: 27 additions & 0 deletions test/test_sanitize_css.rb
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,31 @@
end
end
end

describe 'bugs' do
before do
@default = Sanitize::CSS.new
@relaxed = Sanitize::CSS.new(Sanitize::Config::RELAXED[:css])
end

# https://github.com/rgrove/sanitize/issues/121
it 'should parse the contents of @media rules properly' do
css = '@media { p[class="center"] { text-align: center; }}'
@relaxed.stylesheet(css).must_equal css

css = %[
@media (max-width: 720px) {
p.foo > .bar { float: right; width: expression(body.scrollLeft + 50 + 'px'); }
#baz { color: green; }
}
].strip

@relaxed.stylesheet(css).must_equal %[
@media (max-width: 720px) {
p.foo > .bar { float: right; }
#baz { color: green; }
}
].strip
end
end
end

0 comments on commit bbb51e6

Please sign in to comment.