mjml-rb 0.3.2 → 0.3.3

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: 2a961ff4aa96b2bc79520f261eb233c0e8b77f363afa3d7e0c670fa622359ab7
4
- data.tar.gz: 295a1289237c408dbcd2752c26bc45c9b16dd80edfe00933bcec864bf250bebf
3
+ metadata.gz: 99c98b34eee9aea7822dc4f59250b3b8ca008b98a17cd7e5063eb04595460c31
4
+ data.tar.gz: 6541eb2234e61dd3399dce72b6f0f45f4e810601ecf1c7ff2ccc9a6405d74b84
5
5
  SHA512:
6
- metadata.gz: e544b134c2db7296c58cd9a5c6235b5780448a3673618ae5392ca6eeada84cafa67d88ad7cf1e8aab99aa41ab92feabcc3255ec7eb30ccdbd386b52cc73515ba
7
- data.tar.gz: 3751e0e61fcfd95378c03d1024caa4dcc4ccc0e7dd1f92a08108afe98c5858cff3d2c31e8a551185a145c44e36e96d7d934fcfc4cd86168fad8ec74650192661
6
+ metadata.gz: 3ae18240675e7e9bf5572634a09cb2ae1079e93c874088b917181245a8b20947f59368b07d911fc781cd96676a7953e9f2aaa650bf69ed7eae9e4e5ff2962782
7
+ data.tar.gz: d6ae46c90053abdfeeb2445c87c7ebc07fbd9e36ac47b335577b03d2bfa0debb2dd5c6f9ce9208910c338ae7e7798d29b0c98ed4fc2b9a8b2aaf9eedd045a2f4
@@ -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
575
+
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?
555
581
 
556
- return unless node.name == "td"
557
- return unless node["bgcolor"]
558
- return if %w[none transparent].include?(background_color.downcase)
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
- node["bgcolor"] = background_color
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)
@@ -1,3 +1,3 @@
1
1
  module MjmlRb
2
- VERSION = "0.3.2".freeze
2
+ VERSION = "0.3.3".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.3.2
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrei Andriichuk