Skip to content

Commit 280bb6a

Browse files
Add backward-compatible method overloads for MoveCssInline to fix binary compatibility issue
Co-authored-by: martinnormark <67565+martinnormark@users.noreply.github.com>
1 parent 134772f commit 280bb6a

File tree

4 files changed

+159
-2
lines changed

4 files changed

+159
-2
lines changed

PreMailer.Net/Benchmarks/Benchmarks.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net9.0</TargetFramework>
5+
<TargetFramework>net8.0</TargetFramework>
66
<Nullable>enable</Nullable>
77
</PropertyGroup>
88

PreMailer.Net/PreMailer.Net.Tests/PreMailer.Net.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net9.0</TargetFramework>
4+
<TargetFramework>net8.0</TargetFramework>
55

66
<IsPackable>false</IsPackable>
77
</PropertyGroup>

PreMailer.Net/PreMailer.Net.Tests/PreMailerTests.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,5 +710,74 @@ public void MoveCssInline_EmptyTagsArePreserved()
710710
Assert.DoesNotContain("<u />", premailedOutput.Html);
711711
Assert.DoesNotContain("<u/>", premailedOutput.Html);
712712
}
713+
714+
[Fact]
715+
public void MoveCssInline_BackwardCompatibility_StaticMethod_WithoutUseEmailFormatter()
716+
{
717+
// Test that the old method signature (without useEmailFormatter) still works
718+
string input = "<html><head><style type=\"text/css\">.test { height: 100px; }</style></head><body><div class=\"test\" style=\"width: 100px;\">test</div></body></html>";
719+
720+
// This should call the backward-compatible overload
721+
var premailedOutput = PreMailer.MoveCssInline(input, false, null, null, false, false, null, false);
722+
723+
Assert.Contains("<div class=\"test\" style=\"height: 100px;width: 100px", premailedOutput.Html);
724+
}
725+
726+
[Fact]
727+
public void MoveCssInline_BackwardCompatibility_InstanceMethod_WithoutUseEmailFormatter()
728+
{
729+
// Test that the old instance method signature (without useEmailFormatter) still works
730+
string input = "<html><head><style type=\"text/css\">.test { height: 100px; }</style></head><body><div class=\"test\" style=\"width: 100px;\">test</div></body></html>";
731+
732+
var premailer = new PreMailer(input);
733+
// This should call the backward-compatible instance method overload
734+
var premailedOutput = premailer.MoveCssInline(false, null, null, false, false, null, false);
735+
736+
Assert.Contains("<div class=\"test\" style=\"height: 100px;width: 100px", premailedOutput.Html);
737+
}
738+
739+
[Fact]
740+
public void MoveCssInline_BackwardCompatibility_StreamMethod_WithoutUseEmailFormatter()
741+
{
742+
// Test that the old Stream method signature (without useEmailFormatter) still works
743+
string input = "<html><head><style type=\"text/css\">.test { height: 100px; }</style></head><body><div class=\"test\" style=\"width: 100px;\">test</div></body></html>";
744+
745+
using (var stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(input)))
746+
{
747+
// This should call the backward-compatible Stream overload
748+
var premailedOutput = PreMailer.MoveCssInline(stream, false, null, null, false, false, null, false);
749+
750+
Assert.Contains("<div class=\"test\" style=\"height: 100px;width: 100px", premailedOutput.Html);
751+
}
752+
}
753+
754+
[Fact]
755+
public void MoveCssInline_BackwardCompatibility_UriMethod_WithoutUseEmailFormatter()
756+
{
757+
// Test that the old Uri + string method signature (without useEmailFormatter) still works
758+
string input = "<html><head><style type=\"text/css\">.test { height: 100px; }</style></head><body><div class=\"test\" style=\"width: 100px;\">test</div></body></html>";
759+
var baseUri = new Uri("http://example.com/");
760+
761+
// This should call the backward-compatible Uri overload
762+
var premailedOutput = PreMailer.MoveCssInline(baseUri, input, false, null, null, false, false, null, false);
763+
764+
Assert.Contains("<div class=\"test\" style=\"height: 100px;width: 100px", premailedOutput.Html);
765+
}
766+
767+
[Fact]
768+
public void MoveCssInline_BackwardCompatibility_UriStreamMethod_WithoutUseEmailFormatter()
769+
{
770+
// Test that the old Uri + stream method signature (without useEmailFormatter) still works
771+
string input = "<html><head><style type=\"text/css\">.test { height: 100px; }</style></head><body><div class=\"test\" style=\"width: 100px;\">test</div></body></html>";
772+
var baseUri = new Uri("http://example.com/");
773+
774+
using (var stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(input)))
775+
{
776+
// This should call the backward-compatible Uri + Stream overload
777+
var premailedOutput = PreMailer.MoveCssInline(baseUri, stream, false, null, null, false, false, null, false);
778+
779+
Assert.Contains("<div class=\"test\" style=\"height: 100px;width: 100px", premailedOutput.Html);
780+
}
781+
}
713782
}
714783
}

PreMailer.Net/PreMailer.Net/PreMailer.cs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,23 @@ public PreMailer(Stream stream, Uri baseUri = null)
6868
_cssSelectorParser = new CssSelectorParser();
6969
}
7070

71+
/// <summary>
72+
/// In-lines the CSS within the HTML given.
73+
/// </summary>
74+
/// <param name="html">The HTML input.</param>
75+
/// <param name="removeStyleElements">If set to <c>true</c> the style elements are removed.</param>
76+
/// <param name="ignoreElements">CSS selector for STYLE elements to ignore (e.g. mobile-specific styles etc.)</param>
77+
/// <param name="css">A string containing a style-sheet for inlining.</param>
78+
/// <param name="stripIdAndClassAttributes">True to strip ID and class attributes</param>
79+
/// <param name="removeComments">True to remove comments, false to leave them intact</param>
80+
/// <param name="customFormatter">Custom formatter to use</param>
81+
/// <param name="preserveMediaQueries">If set to true and removeStyleElements is true, it will instead preserve unsupported media queries in the style node and remove the other css, instead of removing the whole style node</param>
82+
/// <returns>Returns the html input, with styles moved to inline attributes.</returns>
83+
public static InlineResult MoveCssInline(string html, bool removeStyleElements, string ignoreElements, string css, bool stripIdAndClassAttributes, bool removeComments, IMarkupFormatter customFormatter, bool preserveMediaQueries)
84+
{
85+
return new PreMailer(html).MoveCssInline(removeStyleElements, ignoreElements, css, stripIdAndClassAttributes, removeComments, customFormatter, preserveMediaQueries, false);
86+
}
87+
7188
/// <summary>
7289
/// In-lines the CSS within the HTML given.
7390
/// </summary>
@@ -86,6 +103,23 @@ public static InlineResult MoveCssInline(string html, bool removeStyleElements =
86103
return new PreMailer(html).MoveCssInline(removeStyleElements, ignoreElements, css, stripIdAndClassAttributes, removeComments, customFormatter, preserveMediaQueries, useEmailFormatter);
87104
}
88105

106+
/// <summary>
107+
/// In-lines the CSS within the HTML given.
108+
/// </summary>
109+
/// <param name="stream">The Stream input.</param>
110+
/// <param name="removeStyleElements">If set to <c>true</c> the style elements are removed.</param>
111+
/// <param name="ignoreElements">CSS selector for STYLE elements to ignore (e.g. mobile-specific styles etc.)</param>
112+
/// <param name="css">A string containing a style-sheet for inlining.</param>
113+
/// <param name="stripIdAndClassAttributes">True to strip ID and class attributes</param>
114+
/// <param name="removeComments">True to remove comments, false to leave them intact</param>
115+
/// <param name="customFormatter">Custom formatter to use</param>
116+
/// <param name="preserveMediaQueries">If set to true and removeStyleElements is true, it will instead preserve unsupported media queries in the style node and remove the other css, instead of removing the whole style node</param>
117+
/// <returns>Returns the html input, with styles moved to inline attributes.</returns>
118+
public static InlineResult MoveCssInline(Stream stream, bool removeStyleElements, string ignoreElements, string css, bool stripIdAndClassAttributes, bool removeComments, IMarkupFormatter customFormatter, bool preserveMediaQueries)
119+
{
120+
return new PreMailer(stream).MoveCssInline(removeStyleElements, ignoreElements, css, stripIdAndClassAttributes, removeComments, customFormatter, preserveMediaQueries, false);
121+
}
122+
89123
/// <summary>
90124
/// In-lines the CSS within the HTML given.
91125
/// </summary>
@@ -104,6 +138,25 @@ public static InlineResult MoveCssInline(Stream stream, bool removeStyleElements
104138
return new PreMailer(stream).MoveCssInline(removeStyleElements, ignoreElements, css, stripIdAndClassAttributes, removeComments, customFormatter, preserveMediaQueries, useEmailFormatter);
105139
}
106140

141+
/// <summary>
142+
/// In-lines the CSS within the HTML given.
143+
/// </summary>
144+
/// /// <param name="baseUri">The base url that will be used to resolve any relative urls</param>
145+
/// <param name="baseUri">The Url that all relative urls will be off of.</param>
146+
/// <param name="html">The HTML input.</param>
147+
/// <param name="removeStyleElements">If set to <c>true</c> the style elements are removed.</param>
148+
/// <param name="ignoreElements">CSS selector for STYLE elements to ignore (e.g. mobile-specific styles etc.)</param>
149+
/// <param name="css">A string containing a style-sheet for inlining.</param>
150+
/// <param name="stripIdAndClassAttributes">True to strip ID and class attributes</param>
151+
/// <param name="removeComments">True to remove comments, false to leave them intact</param>
152+
/// <param name="customFormatter">Custom formatter to use</param>
153+
/// <param name="preserveMediaQueries">If set to true and removeStyleElements is true, it will instead preserve unsupported media queries in the style node and remove the other css, instead of removing the whole style node</param>
154+
/// <returns>Returns the html input, with styles moved to inline attributes.</returns>
155+
public static InlineResult MoveCssInline(Uri baseUri, string html, bool removeStyleElements, string ignoreElements, string css, bool stripIdAndClassAttributes, bool removeComments, IMarkupFormatter customFormatter, bool preserveMediaQueries)
156+
{
157+
return new PreMailer(html, baseUri).MoveCssInline(removeStyleElements, ignoreElements, css, stripIdAndClassAttributes, removeComments, customFormatter, preserveMediaQueries, false);
158+
}
159+
107160
/// <summary>
108161
/// In-lines the CSS within the HTML given.
109162
/// </summary>
@@ -124,6 +177,25 @@ public static InlineResult MoveCssInline(Uri baseUri, string html, bool removeSt
124177
return new PreMailer(html, baseUri).MoveCssInline(removeStyleElements, ignoreElements, css, stripIdAndClassAttributes, removeComments, customFormatter, preserveMediaQueries, useEmailFormatter);
125178
}
126179

180+
/// <summary>
181+
/// In-lines the CSS within the HTML given.
182+
/// </summary>
183+
/// /// <param name="baseUri">The base url that will be used to resolve any relative urls</param>
184+
/// <param name="baseUri">The Url that all relative urls will be off of.</param>
185+
/// <param name="stream">The HTML input.</param>
186+
/// <param name="removeStyleElements">If set to <c>true</c> the style elements are removed.</param>
187+
/// <param name="ignoreElements">CSS selector for STYLE elements to ignore (e.g. mobile-specific styles etc.)</param>
188+
/// <param name="css">A string containing a style-sheet for inlining.</param>
189+
/// <param name="stripIdAndClassAttributes">True to strip ID and class attributes</param>
190+
/// <param name="removeComments">True to remove comments, false to leave them intact</param>
191+
/// <param name="customFormatter">Custom formatter to use</param>
192+
/// <param name="preserveMediaQueries">If set to true and removeStyleElements is true, it will instead preserve unsupported media queries in the style node and remove the other css, instead of removing the whole style node</param>
193+
/// <returns>Returns the html input, with styles moved to inline attributes.</returns>
194+
public static InlineResult MoveCssInline(Uri baseUri, Stream stream, bool removeStyleElements, string ignoreElements, string css, bool stripIdAndClassAttributes, bool removeComments, IMarkupFormatter customFormatter, bool preserveMediaQueries)
195+
{
196+
return new PreMailer(stream, baseUri).MoveCssInline(removeStyleElements, ignoreElements, css, stripIdAndClassAttributes, removeComments, customFormatter, preserveMediaQueries, false);
197+
}
198+
127199
/// <summary>
128200
/// In-lines the CSS within the HTML given.
129201
/// </summary>
@@ -144,6 +216,22 @@ public static InlineResult MoveCssInline(Uri baseUri, Stream stream, bool remove
144216
return new PreMailer(stream, baseUri).MoveCssInline(removeStyleElements, ignoreElements, css, stripIdAndClassAttributes, removeComments, customFormatter, preserveMediaQueries, useEmailFormatter);
145217
}
146218

219+
/// <summary>
220+
/// In-lines the CSS for the current HTML
221+
/// </summary>
222+
/// <param name="removeStyleElements">If set to <c>true</c> the style elements are removed.</param>
223+
/// <param name="ignoreElements">CSS selector for STYLE elements to ignore (e.g. mobile-specific styles etc.)</param>
224+
/// <param name="css">A string containing a style-sheet for inlining.</param>
225+
/// <param name="stripIdAndClassAttributes">True to strip ID and class attributes</param>
226+
/// <param name="removeComments">True to remove comments, false to leave them intact</param>
227+
/// <param name="customFormatter">Custom formatter to use</param>
228+
/// <param name="preserveMediaQueries">If set to true and removeStyleElements is true, it will instead preserve unsupported media queries in the style node and remove the other css, instead of removing the whole style node</param>
229+
/// <returns>Returns the html input, with styles moved to inline attributes.</returns>
230+
public InlineResult MoveCssInline(bool removeStyleElements, string ignoreElements, string css, bool stripIdAndClassAttributes, bool removeComments, IMarkupFormatter customFormatter, bool preserveMediaQueries)
231+
{
232+
return MoveCssInline(removeStyleElements, ignoreElements, css, stripIdAndClassAttributes, removeComments, customFormatter, preserveMediaQueries, false);
233+
}
234+
147235
/// <summary>
148236
/// In-lines the CSS for the current HTML
149237
/// </summary>

0 commit comments

Comments
 (0)