Sha256: 4046d63f4e63554fcb9d6dba9c2b4ce365fe3b53fe5f4b5f5494f3a94ceeaeea

Contents?: true

Size: 1.26 KB

Versions: 9

Compression:

Stored size: 1.26 KB

Contents

class Sanitize; module Transformers; module CSS

# Enforces a CSS allowlist on the contents of `style` attributes.
class CleanAttribute
  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

  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
end

# Enforces a CSS allowlist on the contents of `<style>` elements.
class CleanElement
  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

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

    return unless node.type == Nokogiri::XML::Node::ELEMENT_NODE &&
        env[:node_name] == 'style'

    css = @scss.stylesheet(node.content)

    if css.strip.empty?
      node.unlink
    else
      node.children.unlink
      node << Nokogiri::XML::Text.new(css, node.document)
    end
  end
end

end; end; end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
mumukit-content-type-1.12.1 vendor/bundle/ruby/2.7.0/gems/sanitize-6.0.1/lib/sanitize/transformers/clean_css.rb
mumukit-content-type-1.12.0 vendor/bundle/ruby/2.7.0/gems/sanitize-6.0.1/lib/sanitize/transformers/clean_css.rb
sanitize-6.0.1 lib/sanitize/transformers/clean_css.rb
mumukit-content-type-1.11.1 vendor/bundle/ruby/2.6.0/gems/sanitize-6.0.0/lib/sanitize/transformers/clean_css.rb
sanitize-6.0.0 lib/sanitize/transformers/clean_css.rb
sanitize-5.2.3 lib/sanitize/transformers/clean_css.rb
sanitize-5.2.2 lib/sanitize/transformers/clean_css.rb
sanitize-5.2.1 lib/sanitize/transformers/clean_css.rb
sanitize-5.2.0 lib/sanitize/transformers/clean_css.rb