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
accept_host
clean_properties
clean_value
filter
new
parse
remove_blanklines
remove_comments
remove_nullvalues
remove_urls
remove_whitespace
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. |
Attributes
[RW] | allowed_hosts | url hosts which will be allowed. |
[RW] | allowed_scheme | url schemes which will be allowed (http, ftp, mailto) |
[RW] | allowed_urls | urls which will be allowed. (NOT YET USED) |
[RW] | rewrite | Complete parse and rewrite of CSS document. This does a complete "cleaning" but note that is not yet a perfect parser. |
[RW] | strip_blanklines | remove blank lines. |
[RW] | strip_comments | should we remove comments? (true, false) |
[RW] | strip_urls | should we remove urls? (true, false) |
[RW] | strip_whitespace | remove blank lines. |
[RW] | substitute_urls | substitue urls (NOT YET USED) |
Public Class methods
[ + ]
# 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
[ + ]
# 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!
[ + ]
# File lib/more/facets/cssfilter.rb, line 178 def clean_properties(atts) atts end
[ + ]
# 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
[ + ]
# 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.
[ + ]
# 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
[ + ]
# File lib/more/facets/cssfilter.rb, line 143 def remove_blanklines(data) data = data.gsub(/^\s*\n/,'') end
[ + ]
# File lib/more/facets/cssfilter.rb, line 115 def remove_comments(data) data.gsub(/\/\*(.8?)\*\//,'') end
[ + ]
# File lib/more/facets/cssfilter.rb, line 149 def remove_nullvalues(data); data = data.gsub(/\w+[:](\s+)[;]/,'') end
TODO: allowed_urls
[ + ]
# 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
[ + ]
# File lib/more/facets/cssfilter.rb, line 136 def remove_whitespace(data) data = data.gsub(/^\s*/,'') data = data.gsub(/\s*$/,'') end