Class: Sanitize::Transformers::CSS::CleanAttribute

Inherits:
Object
  • Object
show all
Defined in:
lib/sanitize/transformers/clean_css.rb

Overview

Enforces a CSS allowlist on the contents of style attributes.

Instance Method Summary collapse

Constructor Details

#initialize(sanitizer_or_config) ⇒ CleanAttribute

Returns a new instance of CleanAttribute.



5
6
7
8
9
10
11
# File 'lib/sanitize/transformers/clean_css.rb', line 5

def initialize(sanitizer_or_config)
  if Sanitize::CSS === sanitizer_or_config
    @scss = sanitizer_or_config
  else
    @scss = Sanitize::CSS.new(sanitizer_or_config)
  end
end

Instance Method Details

#call(env) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/sanitize/transformers/clean_css.rb', line 13

def call(env)
  node = env[:node]

  return unless node.type == Nokogiri::XML::Node::ELEMENT_NODE &&
      node.key?('style') && !env[:is_allowlisted]

  attr = node.attribute('style')
  css  = @scss.properties(attr.value)

  if css.strip.empty?
    attr.unlink
  else
    attr.value = css
  end
end