Class | CssFilter |
In: |
lib/more/facets/cssfilter.rb
|
Parent: | Object |
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. |
allowed_scheme | -> | allowed_protocols |
alias for allowed_scheme | ||
allowed_scheme= | -> | allowed_protocols= |
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) |
# 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
# 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