Class: InlineCssString::CSS

Inherits:
Object
  • Object
show all
Defined in:
lib/html_inline_css.rb

Class Method Summary collapse

Class Method Details

.add_style(tag, arr) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/html_inline_css.rb', line 38

def self.add_style(tag, arr)
  unless arr.nil?; 
    @doc_to.css("#{tag}").each do |y|
      unless y.nil? 
        y.to_s.scan(/style="([^"]*)"|style='([^"]*)'/).flatten.select{ |i| !i.nil?; unless i.nil?; y['style'] = "#{arr}#{i}" end }        
      end
    end
  end
end

.add_style_by_id_or_class(id_or_class_name, arr, id_or_class) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/html_inline_css.rb', line 48

def self.add_style_by_id_or_class(id_or_class_name, arr, id_or_class)
  unless arr.nil?; 
    if id_or_class == "class"
      @html_tags.each do |tag|
        @doc_to.xpath("#{tag}[@class = '#{id_or_class_name}']").each do |y|
          unless y.nil? 
            y.to_s.scan(/style="([^"]*)"|style='([^"]*)'/).flatten.select{ |i| !i.nil?; unless i.nil?; y['style'] = "#{arr}#{i}" end }        
          end
        end
      end
    else
      @html_tags.each do |tag|
        @doc_to.xpath("#{tag}[@class = '#{id_or_class_name}']").each do |y|
          unless y.nil? 
            y.to_s.scan(/style="([^"]*)"|style='([^"]*)'/).flatten.select{ |i| !i.nil?; unless i.nil?; y['style'] = "#{arr}#{i}" end }        
           end
        end
      end
    end
  end
end

.add_style_by_id_or_class_to_child(id_or_class_name, arr, id_or_class) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/html_inline_css.rb', line 70

def self.add_style_by_id_or_class_to_child(id_or_class_name, arr, id_or_class)
  unless arr.nil?; 
    if id_or_class == "class"
      @html_tags.each do |tag|
        @doc_to.xpath("#{tag}[@class = '#{id_or_class_name}']").each do |y|
          unless y.nil? 
            y.xpath("#{tag}").each do |c|
              c.to_s.scan(/style="([^"]*)"|style='([^"]*)'/).flatten.select{ |i| !i.nil?; unless i.nil?; c['style'] = "#{arr}#{i}" end }        
            end
          end
        end
      end
    else
      @html_tags.each do |tag|
        @doc_to.xpath("#{tag}[@id = '#{id_or_class_name}']").each do |y|
          unless y.nil? 
            y.xpath("#{tag}").each do |c|
              c.to_s.scan(/style="([^"]*)"|style='([^"]*)'/).flatten.select{ |i| !i.nil?; unless i.nil?; c['style'] = "#{arr}#{i}" end }        
            end
          end
        end
      end
    end
  end
end

.add_style_tag_to_html(html) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/html_inline_css.rb', line 26

def self.add_style_tag_to_html(html)
  @html_tags.each do |tag|
    html.css("#{tag}").each do |x| 
      unless x.nil? 
        unless x.to_s.match(/<#{Regexp.escape(tag)}\s(.*?)style=('|")(.*?)('|")(.*?)>/)
          x['style'] = ''
        end
      end
    end
  end
end

.get_all_class_and_idsObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/html_inline_css.rb', line 7

def self.get_all_class_and_ids
  @html_tags.each do |tag|
    @doc_to.css("#{tag}").each do |y|
      unless y.nil? 
        if @classes.empty?
          @classes << y["class"].to_s;
        else
          @classes.each do |css_class|; unless css_class == y["class"] || y["class"].nil?; @classes << y["class"].to_s; break; end; end;
        end
        if @ids.empty?
          @ids << y["id"].to_s;
        else
          @ids.each do |css_id|; unless css_id == y["id"] || y["id"].nil?; @ids << y["id"].to_s; break; end; end;
        end
      end
    end
  end
end

.inline_css(html) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/html_inline_css.rb', line 141

def self.inline_css(html)
  @html_tags = ["div","span","b","a","i","abbr","acronym","address","applet","area","article","aside","bdi","big","blockquote","caption","center","cite","code","col","colgroup","datalist","dd","del","details","dfn","dialog","dir","dl","dt","em","footer","form","frame","frameset","h1","h2","h3","h4","h5","h6","hr","iframe","img","input","ins","kbd","keygen","label","legend","li","link","main","map","mark","menu","menuitem","meter","nav","object","ol","optgroup","option","output","p","param","pre","progress","q","rp","rt","ruby","s","samp","section","select","small","source","strike","strong","sub","summary","sup","table","tbody","td","textarea","tfoot","th","thead","time","tr","track","tt","u","ul","var","wbr"]
  @classes = Array.new
  @ids = Array.new
  @html_without_skeleton = html.gsub(/<style>(.*?)\n\t\t<\/style>|<style>(.*?)<\/style>|<html>|<\/html>|<head>|<\/head>|<body>|<\/body>|<script>|<\/script>/,"")
  @doc = Nokogiri::HTML::DocumentFragment.parse(html)
  @doc_to = Nokogiri::HTML::DocumentFragment.parse(@html_without_skeleton)
  self.get_all_class_and_ids
  self.add_style_tag_to_html(@doc_to)
  self.inline_css_with_class_and_html(@doc)
  self.inline_css_from_style_tag(@doc)
  return @doc_to.to_html
end

.inline_css_from_style_tag(html) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/html_inline_css.rb', line 118

def self.inline_css_from_style_tag(html)  
  style_tags = html.search('style').map { |n| n.inner_text }
  style_tags.each do |tag|
    @html_tags.each do |html_tag|
      unless html_tag == "a" || html_tag == "b" || html_tag == "i" || html_tag == "u" || html_tag == "s" || html_tag == "q"
        tag.scan(/#{Regexp.escape(html_tag)}(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("#{html_tag}", x) }
      else
        tag.scan(/#{Regexp.escape(html_tag)}{(.*?)}|u\s{(.*?)}/).flatten.select{ |x| !x.nil?; add_style("#{html_tag}", x) }
      end       
    end
    # CLASS
    @classes.each do |css_class|
      regex = /\.#{Regexp.escape(css_class)}\s{(.*?)}|\.#{Regexp.escape(css_class)}{(.*?)}/
      tag.scan(regex).flatten.select{ |x| !x.nil?; add_style_by_id_or_class(css_class, x, 'class'); }
    end
    # ID
    @ids.each do |css_id|
      regex = /\##{Regexp.escape(css_id)}\s{(.*?)}|\##{Regexp.escape(css_id)}{(.*?)}/
      tag.scan(regex).flatten.select{ |x| !x.nil?; add_style_by_id_or_class(css_id, x, 'id') }
    end
  end
end

.inline_css_with_class_and_html(html) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/html_inline_css.rb', line 96

def self.inline_css_with_class_and_html(html)
  style_tags = html.search('style').map { |n| n.inner_text }
  style_tags.each do |tag|
    # CLASS
    @classes.each do |css_class|
      @html_tags.each do |tags|
        regex = /\.#{Regexp.escape(css_class)}\s#{Regexp.escape(tags)}\s{(.*?)}|\.#{Regexp.escape(css_class)}\s#{Regexp.escape(tags)}{(.*?)}/
        tag.scan(regex).flatten.select{ |x| !x.nil?; add_style_by_id_or_class_to_child(css_class, x, 'class'); }
        new_html = @doc.to_s; @doc = Nokogiri::HTML::DocumentFragment.parse(new_html.gsub(regex,""))
      end
    end
    # ID
    @ids.each do |css_id|
      @html_tags.each do |tag|
        regex = /\##{Regexp.escape(css_id)}\s#{Regexp.escape(tag)}\s{(.*?)}|\##{Regexp.escape(css_id)}\s#{Regexp.escape(tag)}{(.*?)}/
        tag.scan(regex).flatten.select{ |x| !x.nil?; add_style_by_id_or_class_to_child(css_id, x, 'id'); x.gsub("",""); }
        new_html = @doc.to_s; @doc = Nokogiri::HTML::DocumentFragment.parse(new_html.gsub(regex,""))
      end
    end
  end
end