Class CssFilter
In: lib/more/facets/cssfilter.rb
Parent: Object

CSS Filter

The CssFilter class will clean up a cascading style sheet. It can be used to remove whitespace and most importantly remove urls.

Methods

Constants

DEFAULT = { 'strip_comments' => true, 'strip_urls' => true, 'allowed_urls' => [], 'allowed_hosts' => [], 'allowed_scheme' => [], 'strip_whitespace' => false, 'strip_blanklines' => true, 'rewrite' => false, 'substitute_urls' => {}   CssFilter option defaults.

External Aliases

allowed_scheme -> allowed_protocols
  alias for allowed_scheme
allowed_scheme= -> allowed_protocols=

Attributes

allowed_hosts  [RW]  url hosts which will be allowed.
allowed_scheme  [RW]  url schemes which will be allowed (http, ftp, mailto)
allowed_urls  [RW]  urls which will be allowed. (NOT YET USED)
rewrite  [RW]  Complete parse and rewrite of CSS document. This does a complete "cleaning" but note that is not yet a perfect parser.
strip_blanklines  [RW]  remove blank lines.
strip_comments  [RW]  should we remove comments? (true, false)
strip_urls  [RW]  should we remove urls? (true, false)
strip_whitespace  [RW]  remove blank lines.
substitute_urls  [RW]  substitue urls (NOT YET USED)

Public Class methods

[Source]

# File lib/more/facets/cssfilter.rb, line 78
  def initialize(options=nil)
    if options
      h = DEFAULT.dup
      options.each do |k,v|
        h[k.to_s] = v
      end
      options = h
    else
      options = DEFAULT.dup
    end

    options.each{ |k,v| send("#{k}=",v) }
  end

Public Instance methods

[Source]

# File lib/more/facets/cssfilter.rb, line 94
  def accept_host(host)
    @hosts << host
  end

Takes a css entry and ensures it is valid (as best it can). It will fix trival mistakes, and raise an error when it is beyond repair.

TODO: So far this does absolutely nothing!

[Source]

# File lib/more/facets/cssfilter.rb, line 178
  def clean_properties(atts)
    atts
  end

[Source]

# File lib/more/facets/cssfilter.rb, line 184
  def clean_value(val)
    val = val.strip

    if urls
      uris = URI.extract(val)
      uris.each do |u|
        val.sub!(u.to_s, urls)
      end
    end

    return val
  end

[Source]

# File lib/more/facets/cssfilter.rb, line 100
  def filter(css)
    css = remove_comments(css)    if strip_comments
    css = remove_urls(css)        if strip_urls

    css = remove_nullvalues(css)

    css = remove_whitespace(css)  if strip_whitespace
    css = remove_blanklines(css)  if strip_blanklines

    css = parse(css).to_css       if rewrite
    css
  end

Breaks a css document up into a hash. This can be used completely rewritting the css.

TODO: Not complete, does not work with "@xxx foo;" for example.

[Source]

# File lib/more/facets/cssfilter.rb, line 158
  def parse(css)
    tree = CssTree.new
    entries = css.scan(/^(.*?)\{(.*?)\}/m)
    entries.each do |ref, props|
      tree[ref.strip] ||= {}
      props = clean_properties(props)
      props = props.scan(/(.*?)[:](.*?)([;]|\s*\Z)/)
      props.each do |(key,val)|
        tree[ref.strip][key.strip] = clean_value(val)
      end
    end
    return tree
  end

[Source]

# File lib/more/facets/cssfilter.rb, line 143
  def remove_blanklines(data)
    data = data.gsub(/^\s*\n/,'')
  end

[Source]

# File lib/more/facets/cssfilter.rb, line 115
  def remove_comments(data)
    data.gsub(/\/\*(.8?)\*\//,'')
  end

[Source]

# File lib/more/facets/cssfilter.rb, line 149
  def remove_nullvalues(data);
    data = data.gsub(/\w+[:](\s+)[;]/,'')
  end

TODO: allowed_urls

[Source]

# File lib/more/facets/cssfilter.rb, line 121
  def remove_urls(data)
    urls = data.scan(/url\((.*?)\)/).flatten
    uris = urls.collect{ |u| URI.extract(u) }.flatten
    uris.each do |u|
      uri = URI.parse(u)
      unless allowed_hosts.include?(uri.host) or
             allowed_scheme.include?(uri.scheme)
        data.sub!(u.to_s, '')
      end
    end
    data.gsub(/url\(\s*\)/, '')
  end

[Source]

# File lib/more/facets/cssfilter.rb, line 136
  def remove_whitespace(data)
    data = data.gsub(/^\s*/,'')
    data = data.gsub(/\s*$/,'')
  end

[Validate]