From cc1f69f8580fb501bb268ef047de2ff0a5bc6065 Mon Sep 17 00:00:00 2001 From: Cris Date: Thu, 20 Aug 2026 10:44:02 -0500 Subject: [PATCH] update mrml to 6.0.1 and enable inline styles Enable the css-inline feature so CSS in `mj-style inline="inline"` is merged into the style attributes of the elements it matches, which is what most email clients need. Plain mj-style tags keep rendering into a +``` + +Note that `to_mjml` does not currently round-trip the `inline` attribute, so +re-parsing the output of `to_mjml` loses style inlining. `to_json`/`to_hash` and +`clone`/`dup` preserve it. + ## Benchmark ``` diff --git a/ext/mrml/Cargo.toml b/ext/mrml/Cargo.toml index bb3b81b..4f7cc8e 100644 --- a/ext/mrml/Cargo.toml +++ b/ext/mrml/Cargo.toml @@ -5,7 +5,7 @@ authors = ["Jonian Guveli "] edition = "2018" [dependencies] -mrml = "5.1" +mrml = { version = "6.0.1", features = ["css-inline"] } magnus = "0.8" [dependencies.serde] diff --git a/ext/mrml/src/lib.rs b/ext/mrml/src/lib.rs index bdd063b..2112866 100644 --- a/ext/mrml/src/lib.rs +++ b/ext/mrml/src/lib.rs @@ -72,7 +72,7 @@ impl Template { impl Clone for Template { fn clone(&self) -> Self { - Self::new(self.to_mjml()).unwrap() + Self { res: self.res.clone() } } } diff --git a/test/fixtures/inline.mjml b/test/fixtures/inline.mjml new file mode 100644 index 0000000..19a8e24 --- /dev/null +++ b/test/fixtures/inline.mjml @@ -0,0 +1,18 @@ + + + + .highlight { color: #F45E43; } + + + .footer { color: #888888; } + + + + + + Hello World + Goodbye World + + + + diff --git a/test/mrml_test.rb b/test/mrml_test.rb index 59b77e8..ecebfe6 100644 --- a/test/mrml_test.rb +++ b/test/mrml_test.rb @@ -35,6 +35,34 @@ def test_that_it_generates_html assert_match 'Hello World', result end + def test_that_it_inlines_styles + result = ::MRML.to_html(inline_template) + + assert_match %r{]*style="[^"]*color: ?#F45E43}, result + refute_match %r{]*>[^<]*\.highlight}, result + end + + def test_that_it_keeps_non_inline_styles + result = ::MRML.to_html(inline_template) + + assert_match %r{]*>[^<]*\.footer}, result + refute_match %r{]*style="[^"]*color: ?#888888}, result + end + + def test_that_it_inlines_styles_after_clone + template = ::MRML::Template.new(inline_template) + + assert_equal template.to_html, template.clone.to_html + assert_equal template.to_html, template.dup.to_html + end + + def test_that_it_inlines_styles_from_json + template = ::MRML::Template.new(inline_template) + result = ::MRML::Template.from_json(template.to_json) + + assert_equal template.to_html, result.to_html + end + def test_that_it_generates_json result = ::MRML.to_json(valid_template) assert_match '"type":"mjml"', result @@ -72,6 +100,12 @@ def invalid_template ) end + def inline_template + @inline_template ||= File.read( + File.join(__dir__, 'fixtures/inline.mjml') + ) + end + def json_template @json_template ||= File.read( File.join(__dir__, 'fixtures/value.json')