class Sanitize::Transformers::CSS::CleanAttribute

Enforces a CSS allowlist on the contents of ‘style` attributes.

Public Class Methods

new(sanitizer_or_config) click to toggle source
# 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

Public Instance Methods

call(env) click to toggle source
# 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