html_inline_css 0.1.6 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d47ba20463100eb48864902018b709e5534e6f55
4
- data.tar.gz: 435b6eceba951c0278a06c5950fa42c9c09e623d
3
+ metadata.gz: afb64561a01ccaca09a140685a20b5fd756cdb72
4
+ data.tar.gz: d931e7902757eebe2851dbcc12623eb284a5b332
5
5
  SHA512:
6
- metadata.gz: 700b3a5a5c219939b868a11d5105aa4fb0a1f25d5911c21efd81691630c499ac5c2c4036b68bc161b6cd74e61c2246681b056fc7857d265e95fc0ff4afc01150
7
- data.tar.gz: f3a3d892c7866577baf63b50efc1858d9d5f3d97ee64ca611008abe05ef9393e2108f111bada3d1657676337157123bec47191b24b6ba7388b2615e46e5f4dd3
6
+ metadata.gz: ca0dd8b05bfaccaea61a8c872bc7671102ca85a88e7780592d3ce023fe7c57f62841e654f4153dbab6a5fdcc9507a60d0b808972e4e82b9fe10e9d5a4237b0d6
7
+ data.tar.gz: 04c4bcf560141f2c4706d26a97bc099321c343d2f9b8e70c0afe2bed319211fb706cd3d0cccf02051e4a6351e288f058920de17aa4b211e5f9865dfc9322995a
@@ -2,400 +2,155 @@ require "html_inline_css/version"
2
2
  require "nokogiri"
3
3
 
4
4
  module InlineCssString
5
- class CSS
6
-
7
- def self.get_all_class_and_ids
8
- @html_tags.each do |tag|
9
- @doc_to.css("#{tag}").each do |y|
10
- unless y.nil?
11
- if @classes.empty?
12
- @classes << y["class"].to_s;
13
- else
14
- @classes.each do |css_class|; unless css_class == y["class"] || y["class"].nil?; @classes << y["class"].to_s; break; end; end;
15
- end
16
- if @ids.empty?
17
- @ids << y["id"].to_s;
18
- else
19
- @ids.each do |css_id|; unless css_id == y["id"] || y["id"].nil?; @ids << y["id"].to_s; break; end; end;
20
- end
21
- end
22
- end
23
- end
24
- end
25
-
26
- def self.add_style_tag_to_html(html)
27
- @html_tags.each do |tag|
28
- html.css("#{tag}").each do |x|
29
- unless x.nil?
30
- unless x.to_s.match(/<#{Regexp.escape(tag)}\s(.*?)style=('|")(.*?)('|")(.*?)>/)
31
- x['style'] = ''
32
- end
33
- end
34
- end
35
- end
36
- end
37
-
38
- def self.add_style(tag, arr)
39
- unless arr.nil?;
40
- @doc_to.css("#{tag}").each do |y|
41
- unless y.nil?
42
- y.to_s.scan(/style="([^"]*)"|style='([^"]*)'/).flatten.select{ |i| !i.nil?; unless i.nil?; y['style'] = "#{arr}#{i}" end }
43
- end
44
- end
45
- end
46
- end
47
-
48
- def self.add_style_by_id_or_class(id_or_class_name, arr, id_or_class)
49
- unless arr.nil?;
50
- if id_or_class == "class"
51
- @html_tags.each do |tag|
52
- @doc_to.xpath("#{tag}[@class = '#{id_or_class_name}']").each do |y|
53
- unless y.nil?
54
- y.to_s.scan(/style="([^"]*)"|style='([^"]*)'/).flatten.select{ |i| !i.nil?; unless i.nil?; y['style'] = "#{arr}#{i}" end }
55
- end
56
- end
57
- end
58
- else
59
- @html_tags.each do |tag|
60
- @doc_to.xpath("#{tag}[@class = '#{id_or_class_name}']").each do |y|
61
- unless y.nil?
62
- y.to_s.scan(/style="([^"]*)"|style='([^"]*)'/).flatten.select{ |i| !i.nil?; unless i.nil?; y['style'] = "#{arr}#{i}" end }
63
- end
64
- end
65
- end
66
- end
67
- end
68
- end
69
-
70
- def self.inline_css_from_style_tag(html)
71
-
72
- style_tags = html.search('style').map { |n| n.inner_text }
73
-
74
- style_tags.each do |tag|
75
-
76
- # DIV
77
- tag.scan(/div(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("div", x) }
78
-
79
- # PARAGRAPH
80
- tag.scan(/p\s{(.*?)}|p{(.*?)}/).flatten.select{ |x| !x.nil?; add_style("p", x) }
81
-
82
- # BOLD
83
- tag.scan(/b\s{(.*?)}|b{(.*?)}/).flatten.select{ |x| !x.nil?; add_style("b", x) }
84
-
85
- # ITALIC
86
- tag.scan(/i{(.*?)}|i\s{(.*?)}/).flatten.select{ |x| !x.nil?; add_style("i", x) }
87
-
88
- # SPAN
89
- tag.scan(/span(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("span", x) }
90
-
91
- # H1
92
- tag.scan(/h1(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("h1", x) }
93
-
94
- # H2
95
- tag.scan(/h2(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("h2", x) }
96
-
97
- # H3
98
- tag.scan(/h3(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("h3", x) }
99
-
100
- # H4
101
- tag.scan(/h4(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("h4", x) }
102
-
103
- # H5
104
- tag.scan(/h5(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("h5", x) }
105
-
106
- # H6
107
- tag.scan(/h6(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("h6", x) }
108
-
109
- # A
110
- tag.scan(/a(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("a", x) }
111
-
112
- # ABBR
113
- tag.scan(/abbr(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("abbr", x) }
114
-
115
- # ACRONYM
116
- tag.scan(/acronym(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("acronym", x) }
117
-
118
- # ADDRESS
119
- tag.scan(/address(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("address", x) }
120
-
121
- # APPLET
122
- tag.scan(/applet(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("applet", x) }
123
-
124
- # AREA
125
- tag.scan(/area(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("area", x) }
126
-
127
- # ARTICLE
128
- tag.scan(/article(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("article", x) }
129
-
130
- # ASIDE
131
- tag.scan(/aside(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("aside", x) }
132
-
133
- # BDI
134
- tag.scan(/bdi(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("bdi", x) }
135
-
136
- # BIG
137
- tag.scan(/big(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("big", x) }
138
-
139
- # BLOCKQUOTE
140
- tag.scan(/blockquote(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("blockquote", x) }
141
-
142
- # CAPTION
143
- tag.scan(/caption(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("caption", x) }
144
-
145
- # CENTER
146
- tag.scan(/center(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("center", x) }
147
-
148
- # CITE
149
- tag.scan(/cite(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("cite", x) }
150
-
151
- # CODE
152
- tag.scan(/code(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("code", x) }
153
-
154
- # COL
155
- tag.scan(/col(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("col", x) }
156
-
157
- # COLGROUP
158
- tag.scan(/colgroup(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("colgroup", x) }
159
-
160
- # DATALIST
161
- tag.scan(/datalist(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("datalist", x) }
162
-
163
- # DD
164
- tag.scan(/dd(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("dd", x) }
165
-
166
- # DEL
167
- tag.scan(/del(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("del", x) }
168
-
169
- # details
170
- tag.scan(/details(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("details", x) }
171
-
172
- # DFN
173
- tag.scan(/dfn(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("dfn", x) }
174
-
175
- # DIALOG
176
- tag.scan(/dialog(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("dialog", x) }
177
-
178
- # DIR
179
- tag.scan(/dir(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("dir", x) }
180
-
181
- # DL
182
- tag.scan(/dl(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("dl", x) }
183
-
184
- # DT
185
- tag.scan(/dt(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("dt", x) }
186
-
187
- # EM
188
- tag.scan(/em(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("em", x) }
189
-
190
- # FOOTER
191
- tag.scan(/footer(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("footer", x) }
192
-
193
- # FORM
194
- tag.scan(/form(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("form", x) }
195
-
196
- # FRAME
197
- tag.scan(/frame(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("frame", x) }
198
-
199
- # FRAMESET
200
- tag.scan(/frameset(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("frameset", x) }
201
-
202
- # HR
203
- tag.scan(/hr(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("hr", x) }
204
-
205
- # IFRAME
206
- tag.scan(/iframe(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("iframe", x) }
207
-
208
- # IMG
209
- tag.scan(/img(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("img", x) }
210
-
211
- # INPUT
212
- tag.scan(/input(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("input", x) }
213
-
214
- # INS
215
- tag.scan(/ins(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("ins", x) }
216
-
217
- # KBD
218
- tag.scan(/kbd(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("kbd", x) }
219
-
220
- # KEYGEN
221
- tag.scan(/keygen(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("keygen", x) }
222
-
223
- # LABEL
224
- tag.scan(/label(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("label", x) }
225
-
226
- # LEGEND
227
- tag.scan(/legend(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("legend", x) }
228
-
229
- # LI
230
- tag.scan(/li(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("li", x) }
231
-
232
- # LINK
233
- tag.scan(/link(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("link", x) }
234
-
235
- # MAIN
236
- tag.scan(/main(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("main", x) }
237
-
238
- # MAP
239
- tag.scan(/map(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("map", x) }
240
-
241
- # MARK
242
- tag.scan(/mark(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("mark", x) }
243
-
244
- # MENU
245
- tag.scan(/menu(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("menu", x) }
246
-
247
- # MENUITEM
248
- tag.scan(/menuitem(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("menuitem", x) }
249
-
250
- # METER
251
- tag.scan(/meter(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("meter", x) }
252
-
253
- # NAV
254
- tag.scan(/nav(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("nav", x) }
255
-
256
- # OBJECT
257
- tag.scan(/object(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("object", x) }
258
-
259
- # OL
260
- tag.scan(/ol(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("ol", x) }
261
-
262
- # OPTGROUP
263
- tag.scan(/optgroup(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("optgroup", x) }
264
-
265
- # OPTION
266
- tag.scan(/option(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("option", x) }
267
-
268
- # OUTPUT
269
- tag.scan(/output(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("output", x) }
270
-
271
- # PARAM
272
- tag.scan(/param(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("param", x) }
273
-
274
- # PRE
275
- tag.scan(/pre(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("pre", x) }
276
-
277
- # PROGRESS
278
- tag.scan(/progress(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("progress", x) }
279
-
280
- # Q
281
- tag.scan(/q{(.*?)}|q\s{(.*?)}/).flatten.select{ |x| !x.nil?; add_style("q", x) }
282
-
283
- # RP
284
- tag.scan(/rp(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("rp", x) }
285
-
286
- # RT
287
- tag.scan(/rt(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("rt", x) }
288
-
289
- # RUBY
290
- tag.scan(/ruby(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("ruby", x) }
291
-
292
- # S
293
- tag.scan(/s{(.*?)}|s\s{(.*?)}/).flatten.select{ |x| !x.nil?; add_style("ruby", x) }
294
-
295
- # SAMP
296
- tag.scan(/samp(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("samp", x) }
297
-
298
- # SECTION
299
- tag.scan(/section(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("section", x) }
300
-
301
- # SELECT
302
- tag.scan(/select(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("select", x) }
303
-
304
- # SMALL
305
- tag.scan(/small(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("small", x) }
306
-
307
- # SOURCE
308
- tag.scan(/source(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("source", x) }
309
-
310
- # STRIKE
311
- tag.scan(/strike(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("strike", x) }
312
-
313
- # STRONG
314
- tag.scan(/strong(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("strong", x) }
315
-
316
- # SUB
317
- tag.scan(/sub(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("sub", x) }
318
-
319
- # SUMMARY
320
- tag.scan(/summary(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("summary", x) }
321
-
322
- # SUP
323
- tag.scan(/sup(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("sup", x) }
324
-
325
- # TABLE
326
- tag.scan(/table(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("table", x) }
327
-
328
- # TBODY
329
- tag.scan(/tbody(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("tbody", x) }
330
-
331
- # TD
332
- tag.scan(/td(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("td", x) }
333
-
334
- # TEXTAREA
335
- tag.scan(/textarea(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("textarea", x) }
336
-
337
- # TFOOT
338
- tag.scan(/tfoot(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("tfoot", x) }
339
-
340
- # TH
341
- tag.scan(/th(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("th", x) }
342
-
343
- # THEAD
344
- tag.scan(/thead(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("thead", x) }
345
-
346
- # TIME
347
- tag.scan(/time(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("time", x) }
348
-
349
- # TR
350
- tag.scan(/tr(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("tr", x) }
351
-
352
- # TRACK
353
- tag.scan(/track(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("track", x) }
354
-
355
- # TT
356
- tag.scan(/tt(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("tt", x) }
357
-
358
- # U
359
- tag.scan(/u{(.*?)}|u\s{(.*?)}/).flatten.select{ |x| !x.nil?; add_style("u", x) }
360
-
361
- # UL
362
- tag.scan(/ul(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("ul", x) }
363
-
364
- # VAR
365
- tag.scan(/var(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("var", x) }
366
-
367
- # WBR
368
- tag.scan(/wbr(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("wbr", x) }
369
-
370
- # CLASS
371
- @classes.each do |css_class|
372
- regex = /\.#{Regexp.escape(css_class)}\s{(.*?)}|\.#{Regexp.escape(css_class)}{(.*?)}/
373
- tag.scan(regex).flatten.select{ |x| !x.nil?; add_style_by_id_or_class(css_class, x, 'class'); }
374
- end
375
-
376
- # ID
377
- @ids.each do |css_id|
378
- regex = /\##{Regexp.escape(css_id)}\s{(.*?)}|\##{Regexp.escape(css_id)}{(.*?)}/
379
- tag.scan(regex).flatten.select{ |x| !x.nil?; add_style_by_id_or_class(css_id, x, 'id') }
380
- end
381
-
382
- end
383
- end
384
-
385
- def self.inline_css(html)
386
- @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"]
387
- @classes = Array.new
388
- @ids = Array.new
389
- @html_without_skeleton = html.gsub(/<html>|<\/html>|<head>|<\/head>|<body>|<\/body>|<script>|<\/script>/,"")
390
- @html_without_skeleton = html.gsub(/<script(.*?)>(.*?)<\/script>|<script(.*?)>(.*?)<\/script>/m,"")
391
- @html_without_skeleton = html.gsub(/<style(.*?)>(.*?)<\/style>|<style(.*?)>(.*?)<\/style>/m,"")
392
- @doc = Nokogiri::HTML::DocumentFragment.parse(html)
393
- @doc_to = Nokogiri::HTML::DocumentFragment.parse(@html_without_skeleton)
394
- self.get_all_class_and_ids
395
- self.add_style_tag_to_html(@doc_to)
396
- self.inline_css_from_style_tag(@doc)
397
- return @doc_to.to_html
398
- end
399
-
400
- end
5
+ class CSS
6
+
7
+ def self.get_all_class_and_ids
8
+ @html_tags.each do |tag|
9
+ @doc_to.css("#{tag}").each do |y|
10
+ unless y.nil?
11
+ if @classes.empty?
12
+ @classes << y["class"].to_s;
13
+ else
14
+ @classes.each do |css_class|; unless css_class == y["class"] || y["class"].nil?; @classes << y["class"].to_s; break; end; end;
15
+ end
16
+ if @ids.empty?
17
+ @ids << y["id"].to_s;
18
+ else
19
+ @ids.each do |css_id|; unless css_id == y["id"] || y["id"].nil?; @ids << y["id"].to_s; break; end; end;
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ def self.add_style_tag_to_html(html)
27
+ @html_tags.each do |tag|
28
+ html.css("#{tag}").each do |x|
29
+ unless x.nil?
30
+ unless x.to_s.match(/<#{Regexp.escape(tag)}\s(.*?)style=('|")(.*?)('|")(.*?)>/)
31
+ x['style'] = ''
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ def self.add_style(tag, arr)
39
+ unless arr.nil?;
40
+ @doc_to.css("#{tag}").each do |y|
41
+ unless y.nil?
42
+ y.to_s.scan(/style="([^"]*)"|style='([^"]*)'/).flatten.select{ |i| !i.nil?; unless i.nil?; y['style'] = "#{arr}#{i}" end }
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ def self.add_style_by_id_or_class(id_or_class_name, arr, id_or_class)
49
+ unless arr.nil?;
50
+ if id_or_class == "class"
51
+ @html_tags.each do |tag|
52
+ @doc_to.xpath("#{tag}[@class = '#{id_or_class_name}']").each do |y|
53
+ unless y.nil?
54
+ y.to_s.scan(/style="([^"]*)"|style='([^"]*)'/).flatten.select{ |i| !i.nil?; unless i.nil?; y['style'] = "#{arr}#{i}" end }
55
+ end
56
+ end
57
+ end
58
+ else
59
+ @html_tags.each do |tag|
60
+ @doc_to.xpath("#{tag}[@class = '#{id_or_class_name}']").each do |y|
61
+ unless y.nil?
62
+ y.to_s.scan(/style="([^"]*)"|style='([^"]*)'/).flatten.select{ |i| !i.nil?; unless i.nil?; y['style'] = "#{arr}#{i}" end }
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
69
+
70
+ def self.add_style_by_id_or_class_to_child(id_or_class_name, arr, id_or_class)
71
+ unless arr.nil?;
72
+ if id_or_class == "class"
73
+ @html_tags.each do |tag|
74
+ @doc_to.xpath("#{tag}[@class = '#{id_or_class_name}']").each do |y|
75
+ unless y.nil?
76
+ y.xpath("#{tag}").each do |c|
77
+ c.to_s.scan(/style="([^"]*)"|style='([^"]*)'/).flatten.select{ |i| !i.nil?; unless i.nil?; c['style'] = "#{arr}#{i}" end }
78
+ end
79
+ end
80
+ end
81
+ end
82
+ else
83
+ @html_tags.each do |tag|
84
+ @doc_to.xpath("#{tag}[@id = '#{id_or_class_name}']").each do |y|
85
+ unless y.nil?
86
+ y.xpath("#{tag}").each do |c|
87
+ c.to_s.scan(/style="([^"]*)"|style='([^"]*)'/).flatten.select{ |i| !i.nil?; unless i.nil?; c['style'] = "#{arr}#{i}" end }
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
95
+
96
+ def self.inline_css_with_class_and_html(html)
97
+ style_tags = html.search('style').map { |n| n.inner_text }
98
+ style_tags.each do |tag|
99
+ # CLASS
100
+ @classes.each do |css_class|
101
+ @html_tags.each do |tags|
102
+ regex = /\.#{Regexp.escape(css_class)}\s#{Regexp.escape(tags)}\s{(.*?)}|\.#{Regexp.escape(css_class)}\s#{Regexp.escape(tags)}{(.*?)}/
103
+ tag.scan(regex).flatten.select{ |x| !x.nil?; add_style_by_id_or_class_to_child(css_class, x, 'class'); }
104
+ new_html = @doc.to_s; @doc = Nokogiri::HTML::DocumentFragment.parse(new_html.gsub(regex,""))
105
+ end
106
+ end
107
+ # ID
108
+ @ids.each do |css_id|
109
+ @html_tags.each do |tag|
110
+ regex = /\##{Regexp.escape(css_id)}\s#{Regexp.escape(tag)}\s{(.*?)}|\##{Regexp.escape(css_id)}\s#{Regexp.escape(tag)}{(.*?)}/
111
+ tag.scan(regex).flatten.select{ |x| !x.nil?; add_style_by_id_or_class_to_child(css_id, x, 'id'); x.gsub("",""); }
112
+ new_html = @doc.to_s; @doc = Nokogiri::HTML::DocumentFragment.parse(new_html.gsub(regex,""))
113
+ end
114
+ end
115
+ end
116
+ end
117
+
118
+ def self.inline_css_from_style_tag(html)
119
+ style_tags = html.search('style').map { |n| n.inner_text }
120
+ style_tags.each do |tag|
121
+ @html_tags.each do |html_tag|
122
+ unless html_tag == "a" || html_tag == "b" || html_tag == "i" || html_tag == "u" || html_tag == "s" || html_tag == "q"
123
+ tag.scan(/#{Regexp.escape(html_tag)}(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("#{html_tag}", x) }
124
+ else
125
+ tag.scan(/#{Regexp.escape(html_tag)}{(.*?)}|u\s{(.*?)}/).flatten.select{ |x| !x.nil?; add_style("#{html_tag}", x) }
126
+ end
127
+ end
128
+ # CLASS
129
+ @classes.each do |css_class|
130
+ regex = /\.#{Regexp.escape(css_class)}\s{(.*?)}|\.#{Regexp.escape(css_class)}{(.*?)}/
131
+ tag.scan(regex).flatten.select{ |x| !x.nil?; add_style_by_id_or_class(css_class, x, 'class'); }
132
+ end
133
+ # ID
134
+ @ids.each do |css_id|
135
+ regex = /\##{Regexp.escape(css_id)}\s{(.*?)}|\##{Regexp.escape(css_id)}{(.*?)}/
136
+ tag.scan(regex).flatten.select{ |x| !x.nil?; add_style_by_id_or_class(css_id, x, 'id') }
137
+ end
138
+ end
139
+ end
140
+
141
+ def self.inline_css(html)
142
+ @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"]
143
+ @classes = Array.new
144
+ @ids = Array.new
145
+ @html_without_skeleton = html.gsub(/<style>(.*?)\n\t\t<\/style>|<style>(.*?)<\/style>|<html>|<\/html>|<head>|<\/head>|<body>|<\/body>|<script>|<\/script>/,"")
146
+ @doc = Nokogiri::HTML::DocumentFragment.parse(html)
147
+ @doc_to = Nokogiri::HTML::DocumentFragment.parse(@html_without_skeleton)
148
+ self.get_all_class_and_ids
149
+ self.add_style_tag_to_html(@doc_to)
150
+ self.inline_css_with_class_and_html(@doc)
151
+ self.inline_css_from_style_tag(@doc)
152
+ return @doc_to.to_html
153
+ end
154
+
155
+ end
401
156
  end
@@ -1,3 +1,3 @@
1
1
  module HtmlInlineCss
2
- VERSION = "0.1.6"
2
+ VERSION = "0.1.7"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: html_inline_css
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicolai Berg Andersen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-11-08 00:00:00.000000000 Z
11
+ date: 2015-11-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler