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.
Defined Under Namespace
Classes: Tree
Constant Summary collapse
- VERSION =
Library version.
"1.2.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
Instance Attribute Details
#allowed_hosts ⇒ Object
url hosts which will be allowed.
39 40 41 |
# File 'lib/cssfilter.rb', line 39 def allowed_hosts @allowed_hosts end |
#allowed_scheme ⇒ Object Also known as: allowed_protocols
url schemes which will be allowed (http, ftp, mailto)
32 33 34 |
# File 'lib/cssfilter.rb', line 32 def allowed_scheme @allowed_scheme end |
#allowed_urls ⇒ Object
urls which will be allowed. (NOT YET USED)
42 43 44 |
# File 'lib/cssfilter.rb', line 42 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.
56 57 58 |
# File 'lib/cssfilter.rb', line 56 def rewrite @rewrite end |
#strip_blanklines ⇒ Object
remove blank lines.
51 52 53 |
# File 'lib/cssfilter.rb', line 51 def strip_blanklines @strip_blanklines end |
#strip_comments ⇒ Object
should we remove comments? (true, false)
26 27 28 |
# File 'lib/cssfilter.rb', line 26 def strip_comments @strip_comments end |
#strip_urls ⇒ Object
should we remove urls? (true, false)
29 30 31 |
# File 'lib/cssfilter.rb', line 29 def strip_urls @strip_urls end |
#strip_whitespace ⇒ Object
remove blank lines.
48 49 50 |
# File 'lib/cssfilter.rb', line 48 def strip_whitespace @strip_whitespace end |
#substitute_urls ⇒ Object
substitue urls (NOT YET USED)
45 46 47 |
# File 'lib/cssfilter.rb', line 45 def substitute_urls @substitute_urls end |
Instance Method Details
#accept_host(host) ⇒ Object
90 91 92 |
# File 'lib/cssfilter.rb', line 90 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!
174 175 176 |
# File 'lib/cssfilter.rb', line 174 def clean_properties(atts) atts end |
#clean_value(val) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/cssfilter.rb', line 180 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
96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/cssfilter.rb', line 96 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.
154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/cssfilter.rb', line 154 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
139 140 141 |
# File 'lib/cssfilter.rb', line 139 def remove_blanklines(data) data = data.gsub(/^\s*\n/,'') end |
#remove_comments(data) ⇒ Object
111 112 113 |
# File 'lib/cssfilter.rb', line 111 def remove_comments(data) data.gsub(/\/\*(.8?)\*\//,'') end |
#remove_nullvalues(data) ⇒ Object
145 146 147 |
# File 'lib/cssfilter.rb', line 145 def remove_nullvalues(data); data = data.gsub(/\w+[:](\s+)[;]/,'') end |
#remove_urls(data) ⇒ Object
TODO: allowed_urls
117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/cssfilter.rb', line 117 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
132 133 134 135 |
# File 'lib/cssfilter.rb', line 132 def remove_whitespace(data) data = data.gsub(/^\s*/,'') data = data.gsub(/\s*$/,'') end |