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
47
48
# File 'lib/html_inline_css.rb', line 38

def self.add_style(tag, arr)
  unless arr.nil?;
  	regex = /\n|\r|\t|(?<=;)\s+|(?<=")\s+/m
  	no_newline_arr = arr.gsub(regex,"")
    @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'] = "#{no_newline_arr}#{i}".gsub(regex,"") end }				
      end
    end
  end
end

.add_style_by_id_or_class(id_or_class_name, arr, id_or_class) ⇒ Object



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

def self.add_style_by_id_or_class(id_or_class_name, arr, id_or_class)
  unless arr.nil?; 
  	regex = /\n|\r|\t|(?<=;)\s+|(?<=")\s+/m
  	no_newline_arr = arr.gsub(regex,"")
    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'] = "#{no_newline_arr}#{i}".gsub(regex,"") 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'] = "#{no_newline_arr}#{i}".gsub(regex,"") end }				
           end
        end
      end
    end
  end
end

.add_style_by_id_or_class_to_child(id_or_class_name, arr, id_or_class) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/html_inline_css.rb', line 74

def self.add_style_by_id_or_class_to_child(id_or_class_name, arr, id_or_class)
  unless arr.nil?; 
  	regex = /\n|\r|\t|(?<=;)\s+|(?<=")\s+/m
  	no_newline_arr = arr.gsub(regex,"")
    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'] = "#{no_newline_arr}#{i}".gsub(regex,"") 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'] = "#{no_newline_arr}#{i}".gsub(/\n|\r|\t|(?<=;)\s+|(?<=")\s+/,"") 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



147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/html_inline_css.rb', line 147

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>(.*?)<\/style>|<title>(.*?)<\/title>|<meta(.*?)>|<html>|<\/html>|<head>(.*?)<\/head>|<body>|<\/body>|<script>|<\/script>/m,"")
  @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



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/html_inline_css.rb', line 124

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" || html_tag == "p"
        tag.scan(/#{Regexp.escape(html_tag)}(.*?){(.*?)}/m).flatten.select{ |x| !x.nil?; add_style("#{html_tag}", x) }
      else
        tag.scan(/#{Regexp.escape(html_tag)}{(.*?)}|u\s{(.*?)}/m).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)}{(.*?)}/m
      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)}{(.*?)}/m
      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



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/html_inline_css.rb', line 102

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)}{(.*?)}/m
        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)}{(.*?)}/m
        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