html_inline_css 0.1.7 → 0.1.8
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 +4 -4
- data/README.md +1 -1
- data/lib/html_inline_css.rb +20 -14
- data/lib/html_inline_css/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8a11571fd50066291d051dbb36a07f47787a985c
|
4
|
+
data.tar.gz: 6cde6a673c9bbc54d175aa83dd9f0a774af0dcc9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2880aa1a83d9b13f63a8fa6fa5307b211227bf664cf62e583f203afb836dfe20c13bdb2e73aee2a45bc9c8ceae1154ba6b9441b549cd30fce4d3f79905016297
|
7
|
+
data.tar.gz: dea71c06523f097f1e4e1d795f026b3a79c9340666eab002a9d91d386889dd075fabfa2619d95226e8b31161719c29dfb3bf81008551050eebc8e9ddbe3d23e6
|
data/README.md
CHANGED
@@ -25,7 +25,7 @@ Or install it yourself as:
|
|
25
25
|
html = "<html><head><style>div {border: 1px solid #e5e5e5;}</style></head><body><div> Foo bar </div></body></html>"
|
26
26
|
html_with_inline_css = InlineCssString::CSS.inline_css(html)
|
27
27
|
|
28
|
-
html_with_inline_css
|
28
|
+
html_with_inline_css => "<div style='border:1px solid #e5e5e5;'> Foo Bar </div>"
|
29
29
|
```
|
30
30
|
|
31
31
|
```ruby
|
data/lib/html_inline_css.rb
CHANGED
@@ -36,10 +36,12 @@ module InlineCssString
|
|
36
36
|
end
|
37
37
|
|
38
38
|
def self.add_style(tag, arr)
|
39
|
-
unless arr.nil?;
|
39
|
+
unless arr.nil?;
|
40
|
+
regex = /\n|\r|\t|(?<=;)\s+|(?<=")\s+/m
|
41
|
+
no_newline_arr = arr.gsub(regex,"")
|
40
42
|
@doc_to.css("#{tag}").each do |y|
|
41
43
|
unless y.nil?
|
42
|
-
y.to_s.scan(/style="([^"]*)"|style='([^"]*)'/).flatten.select{ |i| !i.nil?; unless i.nil?; y['style'] = "#{
|
44
|
+
y.to_s.scan(/style="([^"]*)"|style='([^"]*)'/).flatten.select{ |i| !i.nil?; unless i.nil?; y['style'] = "#{no_newline_arr}#{i}".gsub(regex,"") end }
|
43
45
|
end
|
44
46
|
end
|
45
47
|
end
|
@@ -47,11 +49,13 @@ module InlineCssString
|
|
47
49
|
|
48
50
|
def self.add_style_by_id_or_class(id_or_class_name, arr, id_or_class)
|
49
51
|
unless arr.nil?;
|
52
|
+
regex = /\n|\r|\t|(?<=;)\s+|(?<=")\s+/m
|
53
|
+
no_newline_arr = arr.gsub(regex,"")
|
50
54
|
if id_or_class == "class"
|
51
55
|
@html_tags.each do |tag|
|
52
56
|
@doc_to.xpath("#{tag}[@class = '#{id_or_class_name}']").each do |y|
|
53
57
|
unless y.nil?
|
54
|
-
y.to_s.scan(/style="([^"]*)"|style='([^"]*)'/).flatten.select{ |i| !i.nil?; unless i.nil?; y['style'] = "#{
|
58
|
+
y.to_s.scan(/style="([^"]*)"|style='([^"]*)'/).flatten.select{ |i| !i.nil?; unless i.nil?; y['style'] = "#{no_newline_arr}#{i}".gsub(regex,"") end }
|
55
59
|
end
|
56
60
|
end
|
57
61
|
end
|
@@ -59,7 +63,7 @@ module InlineCssString
|
|
59
63
|
@html_tags.each do |tag|
|
60
64
|
@doc_to.xpath("#{tag}[@class = '#{id_or_class_name}']").each do |y|
|
61
65
|
unless y.nil?
|
62
|
-
y.to_s.scan(/style="([^"]*)"|style='([^"]*)'/).flatten.select{ |i| !i.nil?; unless i.nil?; y['style'] = "#{
|
66
|
+
y.to_s.scan(/style="([^"]*)"|style='([^"]*)'/).flatten.select{ |i| !i.nil?; unless i.nil?; y['style'] = "#{no_newline_arr}#{i}".gsub(regex,"") end }
|
63
67
|
end
|
64
68
|
end
|
65
69
|
end
|
@@ -69,12 +73,14 @@ module InlineCssString
|
|
69
73
|
|
70
74
|
def self.add_style_by_id_or_class_to_child(id_or_class_name, arr, id_or_class)
|
71
75
|
unless arr.nil?;
|
76
|
+
regex = /\n|\r|\t|(?<=;)\s+|(?<=")\s+/m
|
77
|
+
no_newline_arr = arr.gsub(regex,"")
|
72
78
|
if id_or_class == "class"
|
73
79
|
@html_tags.each do |tag|
|
74
80
|
@doc_to.xpath("#{tag}[@class = '#{id_or_class_name}']").each do |y|
|
75
81
|
unless y.nil?
|
76
82
|
y.xpath("#{tag}").each do |c|
|
77
|
-
c.to_s.scan(/style="([^"]*)"|style='([^"]*)'/).flatten.select{ |i| !i.nil?; unless i.nil?; c['style'] = "#{
|
83
|
+
c.to_s.scan(/style="([^"]*)"|style='([^"]*)'/).flatten.select{ |i| !i.nil?; unless i.nil?; c['style'] = "#{no_newline_arr}#{i}".gsub(regex,"") end }
|
78
84
|
end
|
79
85
|
end
|
80
86
|
end
|
@@ -84,7 +90,7 @@ module InlineCssString
|
|
84
90
|
@doc_to.xpath("#{tag}[@id = '#{id_or_class_name}']").each do |y|
|
85
91
|
unless y.nil?
|
86
92
|
y.xpath("#{tag}").each do |c|
|
87
|
-
c.to_s.scan(/style="([^"]*)"|style='([^"]*)'/).flatten.select{ |i| !i.nil?; unless i.nil?; c['style'] = "#{
|
93
|
+
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 }
|
88
94
|
end
|
89
95
|
end
|
90
96
|
end
|
@@ -99,7 +105,7 @@ module InlineCssString
|
|
99
105
|
# CLASS
|
100
106
|
@classes.each do |css_class|
|
101
107
|
@html_tags.each do |tags|
|
102
|
-
regex = /\.#{Regexp.escape(css_class)}\s#{Regexp.escape(tags)}\s{(.*?)}|\.#{Regexp.escape(css_class)}\s#{Regexp.escape(tags)}{(.*?)}/
|
108
|
+
regex = /\.#{Regexp.escape(css_class)}\s#{Regexp.escape(tags)}\s{(.*?)}|\.#{Regexp.escape(css_class)}\s#{Regexp.escape(tags)}{(.*?)}/m
|
103
109
|
tag.scan(regex).flatten.select{ |x| !x.nil?; add_style_by_id_or_class_to_child(css_class, x, 'class'); }
|
104
110
|
new_html = @doc.to_s; @doc = Nokogiri::HTML::DocumentFragment.parse(new_html.gsub(regex,""))
|
105
111
|
end
|
@@ -107,7 +113,7 @@ module InlineCssString
|
|
107
113
|
# ID
|
108
114
|
@ids.each do |css_id|
|
109
115
|
@html_tags.each do |tag|
|
110
|
-
regex = /\##{Regexp.escape(css_id)}\s#{Regexp.escape(tag)}\s{(.*?)}|\##{Regexp.escape(css_id)}\s#{Regexp.escape(tag)}{(.*?)}/
|
116
|
+
regex = /\##{Regexp.escape(css_id)}\s#{Regexp.escape(tag)}\s{(.*?)}|\##{Regexp.escape(css_id)}\s#{Regexp.escape(tag)}{(.*?)}/m
|
111
117
|
tag.scan(regex).flatten.select{ |x| !x.nil?; add_style_by_id_or_class_to_child(css_id, x, 'id'); x.gsub("",""); }
|
112
118
|
new_html = @doc.to_s; @doc = Nokogiri::HTML::DocumentFragment.parse(new_html.gsub(regex,""))
|
113
119
|
end
|
@@ -119,20 +125,20 @@ module InlineCssString
|
|
119
125
|
style_tags = html.search('style').map { |n| n.inner_text }
|
120
126
|
style_tags.each do |tag|
|
121
127
|
@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) }
|
128
|
+
unless html_tag == "a" || html_tag == "b" || html_tag == "i" || html_tag == "u" || html_tag == "s" || html_tag == "q" || html_tag == "p"
|
129
|
+
tag.scan(/#{Regexp.escape(html_tag)}(.*?){(.*?)}/m).flatten.select{ |x| !x.nil?; add_style("#{html_tag}", x) }
|
124
130
|
else
|
125
|
-
tag.scan(/#{Regexp.escape(html_tag)}{(.*?)}|u\s{(.*?)}/).flatten.select{ |x| !x.nil?; add_style("#{html_tag}", x) }
|
131
|
+
tag.scan(/#{Regexp.escape(html_tag)}{(.*?)}|u\s{(.*?)}/m).flatten.select{ |x| !x.nil?; add_style("#{html_tag}", x) }
|
126
132
|
end
|
127
133
|
end
|
128
134
|
# CLASS
|
129
135
|
@classes.each do |css_class|
|
130
|
-
regex = /\.#{Regexp.escape(css_class)}\s{(.*?)}|\.#{Regexp.escape(css_class)}{(.*?)}/
|
136
|
+
regex = /\.#{Regexp.escape(css_class)}\s{(.*?)}|\.#{Regexp.escape(css_class)}{(.*?)}/m
|
131
137
|
tag.scan(regex).flatten.select{ |x| !x.nil?; add_style_by_id_or_class(css_class, x, 'class'); }
|
132
138
|
end
|
133
139
|
# ID
|
134
140
|
@ids.each do |css_id|
|
135
|
-
regex = /\##{Regexp.escape(css_id)}\s{(.*?)}|\##{Regexp.escape(css_id)}{(.*?)}/
|
141
|
+
regex = /\##{Regexp.escape(css_id)}\s{(.*?)}|\##{Regexp.escape(css_id)}{(.*?)}/m
|
136
142
|
tag.scan(regex).flatten.select{ |x| !x.nil?; add_style_by_id_or_class(css_id, x, 'id') }
|
137
143
|
end
|
138
144
|
end
|
@@ -142,7 +148,7 @@ module InlineCssString
|
|
142
148
|
@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
149
|
@classes = Array.new
|
144
150
|
@ids = Array.new
|
145
|
-
@html_without_skeleton = html.gsub(/<style>(.*?)
|
151
|
+
@html_without_skeleton = html.gsub(/<style>(.*?)<\/style>|<title>(.*?)<\/title>|<meta(.*?)>|<html>|<\/html>|<head>(.*?)<\/head>|<body>|<\/body>|<script>|<\/script>/m,"")
|
146
152
|
@doc = Nokogiri::HTML::DocumentFragment.parse(html)
|
147
153
|
@doc_to = Nokogiri::HTML::DocumentFragment.parse(@html_without_skeleton)
|
148
154
|
self.get_all_class_and_ids
|