Module: CSS::Model

Defined in:
lib/css_model.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#css_path(path = nil) ⇒ Object



15
16
17
18
19
# File 'lib/css_model.rb', line 15

def css_path(path = nil)    
  return @css_path if !path 
  @css_path = path
  self
end

Class Method Details

.apply_to(doc, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/css_model.rb', line 4

def self.apply_to(doc, options = {})
  doc.decorators(Nokogiri::XML::Element) << ::CSS::Element
  doc.decorate!             
  doc.extend(::CSS::Model)
  doc.css_path options[:css_path] if options[:css_path]
  doc.apply_css!(options)
  doc      
end

Instance Method Details

#apply_css!(options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/css_model.rb', line 21

def apply_css!(options = {})     
  cp = CssParserMaster::Parser.new  
  
  self.xpath('//link[@rel = "stylesheet"]').each do |stylesheet|
    file_name = css_path + stylesheet['href']
    puts "load stylesheet: #{file_name}" if options[:verbose]
    cp.load_file!(file_name)      
  end
  
  cp.each_selector do |sel| 
    self.css(sel.selector).each do |elem|
      elem.add_rule! sel.to_text
    end
      
  end

  self.css('*').each do |elem|                 
    elem.merge_declarations!
    elem.merge_style!
  end 
  self
end