Skip to content

Commit

Permalink
Delete at-rules that shouldn't have blocks if they have blocks.
Browse files Browse the repository at this point in the history
  • Loading branch information
rgrove committed Apr 20, 2015
1 parent 69f152b commit 6555424
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
8 changes: 4 additions & 4 deletions lib/sanitize/css.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,9 @@ def self.tree!(tree, config = {})
def initialize(config = {})
@config = Config.merge(Config::DEFAULT[:css], config[:css] || config)

@at_rules = Set.new(@config[:at_rules])
@at_rules_with_properties = Set.new(@config[:at_rules_with_properties])
@at_rules_with_styles = Set.new(@config[:at_rules_with_styles])

@at_rules = @at_rules_with_properties + @at_rules_with_styles + @config[:at_rules]
end

# Sanitizes inline CSS style properties.
Expand Down Expand Up @@ -204,7 +203,6 @@ def tree!(tree)
# current config doesn't allow this at-rule.
def at_rule!(rule)
name = rule[:name].downcase
return nil unless @at_rules.include?(name)

if @at_rules_with_styles.include?(name)
styles = Crass::Parser.parse_rules(rule[:block],
Expand All @@ -220,8 +218,10 @@ def at_rule!(rule)

rule[:block] = tree!(props)

elsif @at_rules.include?(name)
return nil if rule.has_key?(:block)
else
rule.delete(:block)
return nil
end

rule
Expand Down
58 changes: 57 additions & 1 deletion test/test_sanitize_css.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@
end
end

describe 'bugs' do
describe 'functionality' do
before do
@default = Sanitize::CSS.new
@relaxed = Sanitize::CSS.new(Sanitize::Config::RELAXED[:css])
Expand All @@ -235,13 +235,21 @@
@media (max-width: 720px) {
p.foo > .bar { float: right; width: expression(body.scrollLeft + 50 + 'px'); }
#baz { color: green; }
@media (orientation: portrait) {
#baz { color: red; }
}
}
].strip

@relaxed.stylesheet(css).must_equal %[
@media (max-width: 720px) {
p.foo > .bar { float: right; }
#baz { color: green; }
@media (orientation: portrait) {
#baz { color: red; }
}
}
].strip
end
Expand Down Expand Up @@ -270,5 +278,53 @@

@relaxed.stylesheet(css).must_equal css
end

describe ":at_rules" do
it "should remove blockless at-rules that aren't whitelisted" do
css = %[
@charset 'utf-8';
@import url('foo.css');
.foo { color: green; }
].strip

@relaxed.stylesheet(css).strip.must_equal %[
.foo { color: green; }
].strip
end

describe "when blockless at-rules are whitelisted" do
before do
@scss = Sanitize::CSS.new(Sanitize::Config.merge(Sanitize::Config::RELAXED[:css], {
:at_rules => ['charset', 'import']
}))
end

it "should not remove them" do
css = %[
@charset 'utf-8';
@import url('foo.css');
.foo { color: green; }
].strip

@scss.stylesheet(css).must_equal %[
@charset 'utf-8';
@import url('foo.css');
.foo { color: green; }
].strip
end

it "should remove them if they have invalid blocks" do
css = %[
@charset { color: green }
@import { color: green }
.foo { color: green; }
].strip

@scss.stylesheet(css).strip.must_equal %[
.foo { color: green; }
].strip
end
end
end
end
end

0 comments on commit 6555424

Please sign in to comment.