Class: CSS::StyleSheet
- Inherits:
-
CSS::SAC::DocumentHandler
- Object
- CSS::SAC::DocumentHandler
- CSS::StyleSheet
- Defined in:
- lib/css/stylesheet/rule.rb,
lib/css/stylesheet/stylesheet.rb
Defined Under Namespace
Classes: Rule
Instance Attribute Summary collapse
-
#rules ⇒ Object
readonly
Returns the value of attribute rules.
Instance Method Summary collapse
- #create_rule(rule) ⇒ Object
- #end_selector(selectors) ⇒ Object
-
#find_all_rules_matching(hpricot_document) ⇒ Object
Find all rules used in
hpricot_document. - #find_rule(rule) ⇒ Object (also: #[])
-
#initialize(sac) ⇒ StyleSheet
constructor
A new instance of StyleSheet.
- #property(name, value, important) ⇒ Object
-
#rules_by_property ⇒ Object
Get a hash of rules by property.
-
#rules_matching(node) ⇒ Object
(also: #=~)
Find all rules that match
node. - #start_selector(selectors) ⇒ Object
- #to_css ⇒ Object
Methods inherited from CSS::SAC::DocumentHandler
#comment, #end_document, #end_font_face, #end_media, #end_page, #ignorable_at_rule, #import_style, #namespace_declaration, #start_document, #start_font_face, #start_media, #start_page
Constructor Details
#initialize(sac) ⇒ StyleSheet
Returns a new instance of StyleSheet.
5 6 7 8 9 |
# File 'lib/css/stylesheet/stylesheet.rb', line 5 def initialize(sac) @sac = sac @rules = [] @current_rules = [] end |
Instance Attribute Details
#rules ⇒ Object (readonly)
Returns the value of attribute rules.
3 4 5 |
# File 'lib/css/stylesheet/stylesheet.rb', line 3 def rules @rules end |
Instance Method Details
#create_rule(rule) ⇒ Object
49 50 51 |
# File 'lib/css/stylesheet/stylesheet.rb', line 49 def create_rule(rule) Rule.new(@sac.parse_rule(rule).first) end |
#end_selector(selectors) ⇒ Object
17 18 19 20 21 |
# File 'lib/css/stylesheet/stylesheet.rb', line 17 def end_selector(selectors) @rules += @current_rules @current_rules = [] reduce! end |
#find_all_rules_matching(hpricot_document) ⇒ Object
Find all rules used in hpricot_document
30 31 32 33 34 35 36 37 38 |
# File 'lib/css/stylesheet/stylesheet.rb', line 30 def find_all_rules_matching(hpricot_document) used_rules = [] hpricot_document.search('//').each do |node| if matching = (rules_matching(node)) used_rules += matching end end used_rules.uniq end |
#find_rule(rule) ⇒ Object Also known as: []
23 24 25 26 |
# File 'lib/css/stylesheet/stylesheet.rb', line 23 def find_rule(rule) rule = self.create_rule(rule) if rule.is_a?(String) rules.find { |x| x.selector == rule.selector } end |
#property(name, value, important) ⇒ Object
53 54 55 56 57 |
# File 'lib/css/stylesheet/stylesheet.rb', line 53 def property(name, value, important) @current_rules.each { |selector| selector.properties << [name, value, important] } end |
#rules_by_property ⇒ Object
Get a hash of rules by property
60 61 62 63 64 65 66 67 |
# File 'lib/css/stylesheet/stylesheet.rb', line 60 def rules_by_property rules_by_property = Hash.new { |h,k| h[k] = [] } @rules.each { |sel| props = sel.properties.to_a.sort_by { |x| x.hash } # HACK? rules_by_property[props] << sel } rules_by_property end |
#rules_matching(node) ⇒ Object Also known as: =~
Find all rules that match node. node must quack like an Hpricot node.
42 43 44 45 46 |
# File 'lib/css/stylesheet/stylesheet.rb', line 42 def rules_matching(node) rules.find_all { |rule| rule.selector =~ node } end |
#start_selector(selectors) ⇒ Object
11 12 13 14 15 |
# File 'lib/css/stylesheet/stylesheet.rb', line 11 def start_selector(selectors) selectors.each { |selector| @current_rules << Rule.new(selector) } end |
#to_css ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/css/stylesheet/stylesheet.rb', line 69 def to_css rules_by_property.map do |properties, rules| rules.map { |rule| rule.selector.to_css }.sort.join(', ') + " {\n" + properties.map { |key,value,important| # Super annoying. If the property is font-family, its supposed to # be commas join_val = ('font-family' == key) ? ', ' : ' ' values = [value].flatten.join(join_val) "#{key}:#{values}#{important ? ' !important' : ''};" }.join("\n") + "\n}" end.sort.join("\n") end |