mjml-rb 0.2.3 → 0.2.4
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 +2 -0
- data/lib/mjml-rb/renderer.rb +44 -13
- data/lib/mjml-rb/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4bb6667d87d9376e60199a47e987e10cfb496be78c4bd549f71fa049c2618aff
|
|
4
|
+
data.tar.gz: 98f63a171ea6a2d5b1dd6212c86a13cdd2eabf71c1ab9f12d15f8c83a04e30d2
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 33c9d050ef8ebd560b92a513391957c9fd2ebea43023d3977c5c06fe154bc7eef1bf6646abbf555dcbf6f53d4900461cceb89daaf0e3b2be36d3e08cec445b38
|
|
7
|
+
data.tar.gz: 839a5ddbe4448b2c86b9b015d59468e8f2553a78432cf6371bdb6d061c850568850bde0316fd9be6ac548a0bc17715900d233cd1538cff03785383f29d960240
|
data/README.md
CHANGED
|
@@ -8,6 +8,8 @@
|
|
|
8
8
|
> and not all components or attributes are fully implemented yet.
|
|
9
9
|
> **Do not use in production without thorough testing of every template against
|
|
10
10
|
> the official npm renderer.** API and output format may change without notice.
|
|
11
|
+
> This is a **fully open source project**, and help is welcome:
|
|
12
|
+
> feedback, bug reports, test cases, optimizations, proposals, and pull requests.
|
|
11
13
|
> No warranty of any kind is provided.
|
|
12
14
|
|
|
13
15
|
This directory contains a Ruby-first implementation of the main MJML user-facing tooling:
|
data/lib/mjml-rb/renderer.rb
CHANGED
|
@@ -271,14 +271,10 @@ module MjmlRb
|
|
|
271
271
|
rules.each do |selector, attrs|
|
|
272
272
|
next if selector.empty? || attrs.empty?
|
|
273
273
|
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
node[name] = value.to_s
|
|
278
|
-
end
|
|
274
|
+
select_nodes(document, selector).each do |node|
|
|
275
|
+
attrs.each do |name, value|
|
|
276
|
+
node[name] = value.to_s
|
|
279
277
|
end
|
|
280
|
-
rescue Nokogiri::CSS::SyntaxError
|
|
281
|
-
next
|
|
282
278
|
end
|
|
283
279
|
end
|
|
284
280
|
|
|
@@ -293,18 +289,53 @@ module MjmlRb
|
|
|
293
289
|
parse_inline_css_rules(css_blocks.join("\n")).each do |selector, declarations|
|
|
294
290
|
next if selector.empty? || declarations.empty?
|
|
295
291
|
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
merge_inline_style!(node, declarations)
|
|
299
|
-
end
|
|
300
|
-
rescue Nokogiri::CSS::SyntaxError
|
|
301
|
-
next
|
|
292
|
+
select_nodes(document, selector).each do |node|
|
|
293
|
+
merge_inline_style!(node, declarations)
|
|
302
294
|
end
|
|
303
295
|
end
|
|
304
296
|
|
|
305
297
|
document.to_html
|
|
306
298
|
end
|
|
307
299
|
|
|
300
|
+
def select_nodes(document, selector)
|
|
301
|
+
document.css(selector)
|
|
302
|
+
rescue Nokogiri::CSS::SyntaxError, Nokogiri::XML::XPath::SyntaxError
|
|
303
|
+
fallback_select_nodes(document, selector)
|
|
304
|
+
end
|
|
305
|
+
|
|
306
|
+
def fallback_select_nodes(document, selector)
|
|
307
|
+
return [] unless selector.include?(":lang(")
|
|
308
|
+
|
|
309
|
+
lang_values = selector.scan(/:lang\(([^)]+)\)/).flatten.map do |value|
|
|
310
|
+
value.to_s.strip.gsub(/\A['"]|['"]\z/, "").downcase
|
|
311
|
+
end.reject(&:empty?)
|
|
312
|
+
return [] if lang_values.empty?
|
|
313
|
+
|
|
314
|
+
base_selector = selector.gsub(/:lang\(([^)]+)\)/, "").strip
|
|
315
|
+
base_selector = "*" if base_selector.empty?
|
|
316
|
+
|
|
317
|
+
document.css(base_selector).select do |node|
|
|
318
|
+
lang_values.all? { |lang| lang_matches?(node, lang) }
|
|
319
|
+
end
|
|
320
|
+
rescue Nokogiri::CSS::SyntaxError, Nokogiri::XML::XPath::SyntaxError
|
|
321
|
+
[]
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
def lang_matches?(node, lang)
|
|
325
|
+
current = node
|
|
326
|
+
|
|
327
|
+
while current
|
|
328
|
+
value = current["lang"]
|
|
329
|
+
if value && !value.empty?
|
|
330
|
+
normalized = value.downcase
|
|
331
|
+
return normalized == lang || normalized.start_with?("#{lang}-")
|
|
332
|
+
end
|
|
333
|
+
current = current.parent
|
|
334
|
+
end
|
|
335
|
+
|
|
336
|
+
false
|
|
337
|
+
end
|
|
338
|
+
|
|
308
339
|
def parse_inline_css_rules(css)
|
|
309
340
|
stripped_css = strip_css_comments(css.to_s)
|
|
310
341
|
plain_css = strip_css_at_rules(stripped_css)
|
data/lib/mjml-rb/version.rb
CHANGED