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_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



385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/html_inline_css.rb', line 385

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(/<html>|<\/html>|<head>|<\/head>|<body>|<\/body>|<script>|<\/script>/,"")
	@html_without_skeleton	= html.gsub(/<script(.*?)>(.*?)<\/script>|<script(.*?)>(.*?)<\/script>/m,"")
	@html_without_skeleton	= html.gsub(/<style(.*?)>(.*?)<\/style>|<style(.*?)>(.*?)<\/style>/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_from_style_tag(@doc)
	return @doc_to.to_html
end

.inline_css_from_style_tag(html) ⇒ 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/html_inline_css.rb', line 70

def self.inline_css_from_style_tag(html)
	
	style_tags = html.search('style').map { |n| n.inner_text }

	style_tags.each do |tag|

		# DIV
		tag.scan(/div(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("div", x) }

		# PARAGRAPH
		tag.scan(/p\s{(.*?)}|p{(.*?)}/).flatten.select{ |x| !x.nil?; add_style("p", x) }

		# BOLD
		tag.scan(/b\s{(.*?)}|b{(.*?)}/).flatten.select{ |x| !x.nil?; add_style("b", x) }

		# ITALIC
		tag.scan(/i{(.*?)}|i\s{(.*?)}/).flatten.select{ |x| !x.nil?; add_style("i", x) }

		# SPAN
		tag.scan(/span(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("span", x) }

		# H1
		tag.scan(/h1(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("h1", x) }

		# H2
		tag.scan(/h2(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("h2", x) }

		# H3
		tag.scan(/h3(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("h3", x) }

		# H4
		tag.scan(/h4(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("h4", x) }

		# H5
		tag.scan(/h5(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("h5", x) }

		# H6
		tag.scan(/h6(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("h6", x) }

		# A
		tag.scan(/a(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("a", x) }

		# ABBR
		tag.scan(/abbr(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("abbr", x) }

		# ACRONYM
		tag.scan(/acronym(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("acronym", x) }

		# ADDRESS
		tag.scan(/address(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("address", x) }

		# APPLET
		tag.scan(/applet(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("applet", x) }

		# AREA
		tag.scan(/area(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("area", x) }

		# ARTICLE
		tag.scan(/article(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("article", x) }

		# ASIDE
		tag.scan(/aside(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("aside", x) }

		# BDI
		tag.scan(/bdi(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("bdi", x) }

		# BIG
		tag.scan(/big(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("big", x) }

		# BLOCKQUOTE
		tag.scan(/blockquote(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("blockquote", x) }

		# CAPTION
		tag.scan(/caption(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("caption", x) }

		# CENTER
		tag.scan(/center(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("center", x) }

		# CITE
		tag.scan(/cite(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("cite", x) }

		# CODE
		tag.scan(/code(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("code", x) }

		# COL
		tag.scan(/col(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("col", x) }

		# COLGROUP
		tag.scan(/colgroup(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("colgroup", x) }

		# DATALIST
		tag.scan(/datalist(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("datalist", x) }

		# DD
		tag.scan(/dd(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("dd", x) }

		# DEL
		tag.scan(/del(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("del", x) }

		# details
		tag.scan(/details(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("details", x) }

		# DFN
		tag.scan(/dfn(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("dfn", x) }

		# DIALOG
		tag.scan(/dialog(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("dialog", x) }

		# DIR
		tag.scan(/dir(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("dir", x) }

		# DL
		tag.scan(/dl(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("dl", x) }

		# DT
		tag.scan(/dt(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("dt", x) }

		# EM
		tag.scan(/em(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("em", x) }

		# FOOTER
		tag.scan(/footer(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("footer", x) }

		# FORM
		tag.scan(/form(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("form", x) }

		# FRAME
		tag.scan(/frame(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("frame", x) }

		# FRAMESET
		tag.scan(/frameset(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("frameset", x) }

		# HR
		tag.scan(/hr(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("hr", x) }

		# IFRAME
		tag.scan(/iframe(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("iframe", x) }

		# IMG
		tag.scan(/img(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("img", x) }

		# INPUT
		tag.scan(/input(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("input", x) }

		# INS
		tag.scan(/ins(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("ins", x) }

		# KBD
		tag.scan(/kbd(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("kbd", x) }

		# KEYGEN
		tag.scan(/keygen(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("keygen", x) }

		# LABEL
		tag.scan(/label(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("label", x) }

		# LEGEND
		tag.scan(/legend(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("legend", x) }

		# LI
		tag.scan(/li(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("li", x) }

		# LINK
		tag.scan(/link(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("link", x) }

		# MAIN
		tag.scan(/main(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("main", x) }

		# MAP
		tag.scan(/map(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("map", x) }

		# MARK
		tag.scan(/mark(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("mark", x) }

		# MENU
		tag.scan(/menu(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("menu", x) }

		# MENUITEM
		tag.scan(/menuitem(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("menuitem", x) }

		# METER
		tag.scan(/meter(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("meter", x) }

		# NAV
		tag.scan(/nav(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("nav", x) }

		# OBJECT
		tag.scan(/object(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("object", x) }

		# OL
		tag.scan(/ol(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("ol", x) }

		# OPTGROUP
		tag.scan(/optgroup(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("optgroup", x) }

		# OPTION
		tag.scan(/option(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("option", x) }

		# OUTPUT
		tag.scan(/output(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("output", x) }

		# PARAM
		tag.scan(/param(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("param", x) }

		# PRE
		tag.scan(/pre(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("pre", x) }

		# PROGRESS
		tag.scan(/progress(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("progress", x) }

		# Q
		tag.scan(/q{(.*?)}|q\s{(.*?)}/).flatten.select{ |x| !x.nil?; add_style("q", x) }

		# RP
		tag.scan(/rp(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("rp", x) }

		# RT
		tag.scan(/rt(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("rt", x) }

		# RUBY
		tag.scan(/ruby(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("ruby", x) }

		# S
		tag.scan(/s{(.*?)}|s\s{(.*?)}/).flatten.select{ |x| !x.nil?; add_style("ruby", x) }

		# SAMP
		tag.scan(/samp(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("samp", x) }

		# SECTION
		tag.scan(/section(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("section", x) }

		# SELECT
		tag.scan(/select(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("select", x) }

		# SMALL
		tag.scan(/small(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("small", x) }

		# SOURCE
		tag.scan(/source(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("source", x) }

		# STRIKE
		tag.scan(/strike(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("strike", x) }

		# STRONG
		tag.scan(/strong(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("strong", x) }

		# SUB
		tag.scan(/sub(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("sub", x) }

		# SUMMARY
		tag.scan(/summary(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("summary", x) }

		# SUP
		tag.scan(/sup(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("sup", x) }

		# TABLE
		tag.scan(/table(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("table", x) }

		# TBODY
		tag.scan(/tbody(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("tbody", x) }

		# TD
		tag.scan(/td(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("td", x) }

		# TEXTAREA
		tag.scan(/textarea(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("textarea", x) }

		# TFOOT
		tag.scan(/tfoot(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("tfoot", x) }

		# TH
		tag.scan(/th(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("th", x) }

		# THEAD
		tag.scan(/thead(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("thead", x) }

		# TIME
		tag.scan(/time(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("time", x) }

		# TR
		tag.scan(/tr(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("tr", x) }

		# TRACK
		tag.scan(/track(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("track", x) }

		# TT
		tag.scan(/tt(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("tt", x) }

		# U
		tag.scan(/u{(.*?)}|u\s{(.*?)}/).flatten.select{ |x| !x.nil?; add_style("u", x) }

		# UL
		tag.scan(/ul(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("ul", x) }

		# VAR
		tag.scan(/var(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("var", x) }

		# WBR
		tag.scan(/wbr(.*?){(.*?)}/).flatten.select{ |x| !x.nil?; add_style("wbr", x) }

		# 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