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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4c0d1fcf5bc3d3da63f1e883fbf9fc1c5d596f23e2d1845b52fac3a2795c15ec
4
- data.tar.gz: 96945245aea91b2d4cb94336a2d29a58eb6c5cbf332ac76e2828f581f4e31a4f
3
+ metadata.gz: 4bb6667d87d9376e60199a47e987e10cfb496be78c4bd549f71fa049c2618aff
4
+ data.tar.gz: 98f63a171ea6a2d5b1dd6212c86a13cdd2eabf71c1ab9f12d15f8c83a04e30d2
5
5
  SHA512:
6
- metadata.gz: 2ab219c36a7dfb49bc89ce1f7b6da2dc8d5089e60bfb640b2f2606783d8f27ee5560133274935d0f6aee96ac076cdfac346c4d85775452a2933f2a47ff9d87e0
7
- data.tar.gz: d3fea2c9c4cd07a967d62dc07ff16328c0b61cc7f20b108209f21c7d5f8eb061040eaf5085875c0bb09a81d56607e08e9efa3cfd83d147f296a5a8308687cca9
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:
@@ -271,14 +271,10 @@ module MjmlRb
271
271
  rules.each do |selector, attrs|
272
272
  next if selector.empty? || attrs.empty?
273
273
 
274
- begin
275
- document.css(selector).each do |node|
276
- attrs.each do |name, value|
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
- begin
297
- document.css(selector).each do |node|
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)
@@ -1,3 +1,3 @@
1
1
  module MjmlRb
2
- VERSION = "0.2.3".freeze
2
+ VERSION = "0.2.4".freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mjml-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrei Andriichuk