mjml-rb 0.3.2 → 0.3.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/lib/mjml-rb/renderer.rb +61 -5
- 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: c87c74bfccdb5ffe347fba7099a9f17855e1c42446b1edd0d013dbfa08fe5c67
|
|
4
|
+
data.tar.gz: b18e8124ded52ecc49a7d43f00da55ff902068f3b1593d195aaecb95e042346f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 0e4714a86a42c8270a6ec66a8b7d50bc21f54ea9d0aebb8cbf5424fac2af621de1ab8fa79d8b51cdc92954b615e8c66dd55e499593dec4861a65052b6c7eacab
|
|
7
|
+
data.tar.gz: 80f36f9bea2094a8a13b8bffea1b8b3c3f62b67bfefb44a540ecdc96b479dc50366f7ca182f5f601387f31e8dc44cebf6c9c60c0f8761dfa530ad27aca9e6419
|
data/lib/mjml-rb/renderer.rb
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
require "cgi"
|
|
2
2
|
require "nokogiri"
|
|
3
|
+
require "set"
|
|
3
4
|
require_relative "components/accordion"
|
|
4
5
|
require_relative "components/attributes"
|
|
5
6
|
require_relative "components/body"
|
|
@@ -539,6 +540,7 @@ module MjmlRb
|
|
|
539
540
|
existing[property] = merged
|
|
540
541
|
end
|
|
541
542
|
normalize_background_fallbacks!(node, existing)
|
|
543
|
+
sync_html_attributes!(node, existing)
|
|
542
544
|
node["style"] = serialize_css_declarations(existing)
|
|
543
545
|
end
|
|
544
546
|
|
|
@@ -552,12 +554,59 @@ module MjmlRb
|
|
|
552
554
|
important: declarations.fetch("background-color", {}).fetch(:important, false)
|
|
553
555
|
}
|
|
554
556
|
end
|
|
557
|
+
end
|
|
558
|
+
|
|
559
|
+
# Sync HTML attributes from inlined CSS declarations.
|
|
560
|
+
# Mirrors Juice's attribute syncing: width/height on TABLE/TD/TH/IMG,
|
|
561
|
+
# and style-to-attribute mappings (bgcolor, background, align, valign)
|
|
562
|
+
# on table-related elements.
|
|
563
|
+
# See: https://github.com/Automattic/juice/blob/master/lib/inline.js
|
|
564
|
+
WIDTH_HEIGHT_ELEMENTS = Set.new(%w[table td th img]).freeze
|
|
565
|
+
TABLE_ELEMENTS = Set.new(%w[table th tr td caption colgroup col thead tbody tfoot]).freeze
|
|
566
|
+
STYLE_TO_ATTRIBUTE = {
|
|
567
|
+
"background-color" => "bgcolor",
|
|
568
|
+
"background-image" => "background",
|
|
569
|
+
"text-align" => "align",
|
|
570
|
+
"vertical-align" => "valign"
|
|
571
|
+
}.freeze
|
|
572
|
+
|
|
573
|
+
def sync_html_attributes!(node, declarations)
|
|
574
|
+
tag = node.name.downcase
|
|
555
575
|
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
576
|
+
# Sync width/height on TABLE, TD, TH, IMG
|
|
577
|
+
if WIDTH_HEIGHT_ELEMENTS.include?(tag)
|
|
578
|
+
%w[width height].each do |prop|
|
|
579
|
+
css_value = declaration_value(declarations[prop])
|
|
580
|
+
next if css_value.nil? || css_value.empty?
|
|
581
|
+
|
|
582
|
+
# Convert CSS px values to plain numbers for HTML attributes;
|
|
583
|
+
# keep other values (auto, %) as-is.
|
|
584
|
+
html_value = css_value.sub(/px\z/i, "")
|
|
585
|
+
node[prop] = html_value
|
|
586
|
+
end
|
|
587
|
+
end
|
|
559
588
|
|
|
560
|
-
|
|
589
|
+
# Sync style-to-attribute mappings on table elements
|
|
590
|
+
if TABLE_ELEMENTS.include?(tag)
|
|
591
|
+
STYLE_TO_ATTRIBUTE.each do |css_prop, html_attr|
|
|
592
|
+
css_value = declaration_value(declarations[css_prop])
|
|
593
|
+
next if css_value.nil? || css_value.empty?
|
|
594
|
+
|
|
595
|
+
case html_attr
|
|
596
|
+
when "bgcolor"
|
|
597
|
+
next if %w[none transparent].include?(css_value.downcase)
|
|
598
|
+
node[html_attr] = css_value
|
|
599
|
+
when "background"
|
|
600
|
+
# Extract url(...) from background-image
|
|
601
|
+
url = css_value[/url\(['"]?([^'")]+)['"]?\)/i, 1]
|
|
602
|
+
node[html_attr] = url if url
|
|
603
|
+
when "align"
|
|
604
|
+
node[html_attr] = css_value
|
|
605
|
+
when "valign"
|
|
606
|
+
node[html_attr] = css_value
|
|
607
|
+
end
|
|
608
|
+
end
|
|
609
|
+
end
|
|
561
610
|
end
|
|
562
611
|
|
|
563
612
|
def syncable_background?(value)
|
|
@@ -781,9 +830,16 @@ module MjmlRb
|
|
|
781
830
|
CGI.escapeHTML(value.to_s)
|
|
782
831
|
end
|
|
783
832
|
|
|
833
|
+
# HTML attributes that are meaningful even when empty.
|
|
834
|
+
# `alt=""` signals a decorative image to browsers, suppressing the
|
|
835
|
+
# broken-image placeholder icon.
|
|
836
|
+
KEEP_WHEN_EMPTY = Set.new(%w[alt]).freeze
|
|
837
|
+
|
|
784
838
|
def html_attrs(hash)
|
|
785
839
|
attrs = hash.each_with_object([]) do |(key, value), memo|
|
|
786
|
-
|
|
840
|
+
if value.nil? || value.to_s.empty?
|
|
841
|
+
next unless KEEP_WHEN_EMPTY.include?(key) && !value.nil?
|
|
842
|
+
end
|
|
787
843
|
memo << %(#{key}="#{escape_attr(value)}")
|
|
788
844
|
end
|
|
789
845
|
return "" if attrs.empty?
|
data/lib/mjml-rb/version.rb
CHANGED