Class: CSSFilter
- Inherits:
-
Object
- Object
- CSSFilter
- Defined in:
- lib/cssfilter.rb
Overview
CSS Filter
The CSSFilter class will clean up a cascading style sheet. It can be used to remove whitespace and most importantly remove urls.
Issues
TODO: Allow urls to be specified per attribute type.
Copying
Copyright © 2007 Thomas Sawyer
Creative Commons Attribution-ShareAlike 3.0 License
Defined Under Namespace
Constant Summary collapse
- VERSION =
Library version.
"1.3.0"- DEFAULT =
CssFilter option defaults.
{ 'strip_comments' => true, 'strip_urls' => true, 'allowed_urls' => [], 'allowed_hosts' => [], 'allowed_scheme' => [], 'strip_whitespace' => false, 'strip_blanklines' => true, 'rewrite' => false, 'substitute_urls' => {} }
Instance Attribute Summary collapse
-
#allowed_hosts ⇒ Object
url hosts which will be allowed.
-
#allowed_scheme ⇒ Object
(also: #allowed_protocols)
url schemes which will be allowed (http, ftp, mailto).
-
#allowed_urls ⇒ Object
urls which will be allowed.
-
#rewrite ⇒ Object
Complete parse and rewrite of CSS document.
-
#strip_blanklines ⇒ Object
remove blank lines.
-
#strip_comments ⇒ Object
should we remove comments? (true, false).
-
#strip_urls ⇒ Object
should we remove urls? (true, false).
-
#strip_whitespace ⇒ Object
remove blank lines.
-
#substitute_urls ⇒ Object
substitue urls (NOT YET USED).
Instance Method Summary collapse
- #accept_host(host) ⇒ Object
-
#clean_properties(atts) ⇒ Object
Takes a css entry and ensures it is valid (as best it can).
- #clean_value(val) ⇒ Object
- #filter(css) ⇒ Object
-
#initialize(options = nil) ⇒ CSSFilter
constructor
A new instance of CSSFilter.
-
#parse(css) ⇒ Object
Breaks a css document up into a hash.
- #remove_blanklines(data) ⇒ Object
- #remove_comments(data) ⇒ Object
- #remove_nullvalues(data) ⇒ Object
-
#remove_urls(data) ⇒ Object
TODO: allowed_urls.
- #remove_whitespace(data) ⇒ Object
Constructor Details
#initialize(options = nil) ⇒ CSSFilter
Returns a new instance of CSSFilter.
76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/cssfilter.rb', line 76 def initialize(=nil) if h = DEFAULT.dup .each do |k,v| h[k.to_s] = v end = h else = DEFAULT.dup end .each{ |k,v| send("#{k}=",v) } end |
Instance Attribute Details
#allowed_hosts ⇒ Object
url hosts which will be allowed.
41 42 43 |
# File 'lib/cssfilter.rb', line 41 def allowed_hosts @allowed_hosts end |
#allowed_scheme ⇒ Object Also known as: allowed_protocols
url schemes which will be allowed (http, ftp, mailto)
34 35 36 |
# File 'lib/cssfilter.rb', line 34 def allowed_scheme @allowed_scheme end |
#allowed_urls ⇒ Object
urls which will be allowed. (NOT YET USED)
44 45 46 |
# File 'lib/cssfilter.rb', line 44 def allowed_urls @allowed_urls end |
#rewrite ⇒ Object
Complete parse and rewrite of CSS document. This does a complete “cleaning” but note that is not yet a perfect parser.
58 59 60 |
# File 'lib/cssfilter.rb', line 58 def rewrite @rewrite end |
#strip_blanklines ⇒ Object
remove blank lines.
53 54 55 |
# File 'lib/cssfilter.rb', line 53 def strip_blanklines @strip_blanklines end |
#strip_comments ⇒ Object
should we remove comments? (true, false)
28 29 30 |
# File 'lib/cssfilter.rb', line 28 def strip_comments @strip_comments end |
#strip_urls ⇒ Object
should we remove urls? (true, false)
31 32 33 |
# File 'lib/cssfilter.rb', line 31 def strip_urls @strip_urls end |
#strip_whitespace ⇒ Object
remove blank lines.
50 51 52 |
# File 'lib/cssfilter.rb', line 50 def strip_whitespace @strip_whitespace end |
#substitute_urls ⇒ Object
substitue urls (NOT YET USED)
47 48 49 |
# File 'lib/cssfilter.rb', line 47 def substitute_urls @substitute_urls end |
Instance Method Details
#accept_host(host) ⇒ Object
92 93 94 |
# File 'lib/cssfilter.rb', line 92 def accept_host(host) @hosts << host end |
#clean_properties(atts) ⇒ Object
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!
176 177 178 |
# File 'lib/cssfilter.rb', line 176 def clean_properties(atts) atts end |
#clean_value(val) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/cssfilter.rb', line 182 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 |
#filter(css) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/cssfilter.rb', line 98 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 |
#parse(css) ⇒ Object
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.
156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/cssfilter.rb', line 156 def parse(css) tree = Tree.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 |
#remove_blanklines(data) ⇒ Object
141 142 143 |
# File 'lib/cssfilter.rb', line 141 def remove_blanklines(data) data = data.gsub(/^\s*\n/,'') end |
#remove_comments(data) ⇒ Object
113 114 115 |
# File 'lib/cssfilter.rb', line 113 def remove_comments(data) data.gsub(/\/\*(.8?)\*\//,'') end |
#remove_nullvalues(data) ⇒ Object
147 148 149 |
# File 'lib/cssfilter.rb', line 147 def remove_nullvalues(data); data = data.gsub(/\w+[:](\s+)[;]/,'') end |
#remove_urls(data) ⇒ Object
TODO: allowed_urls
119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/cssfilter.rb', line 119 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 |
#remove_whitespace(data) ⇒ Object
134 135 136 137 |
# File 'lib/cssfilter.rb', line 134 def remove_whitespace(data) data = data.gsub(/^\s*/,'') data = data.gsub(/\s*$/,'') end |