Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions PreMailer.Net/PreMailer.Net.Tests/PreMailerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -710,5 +710,74 @@ public void MoveCssInline_EmptyTagsArePreserved()
Assert.DoesNotContain("<u />", premailedOutput.Html);
Assert.DoesNotContain("<u/>", premailedOutput.Html);
}

[Fact]
public void MoveCssInline_BackwardCompatibility_StaticMethod_WithoutUseEmailFormatter()
{
// Test that the old method signature (without useEmailFormatter) still works
string input = "<html><head><style type=\"text/css\">.test { height: 100px; }</style></head><body><div class=\"test\" style=\"width: 100px;\">test</div></body></html>";

// This should call the backward-compatible overload
var premailedOutput = PreMailer.MoveCssInline(input, false, null, null, false, false, null, false);

Assert.Contains("<div class=\"test\" style=\"height: 100px;width: 100px", premailedOutput.Html);
}

[Fact]
public void MoveCssInline_BackwardCompatibility_InstanceMethod_WithoutUseEmailFormatter()
{
// Test that the old instance method signature (without useEmailFormatter) still works
string input = "<html><head><style type=\"text/css\">.test { height: 100px; }</style></head><body><div class=\"test\" style=\"width: 100px;\">test</div></body></html>";

var premailer = new PreMailer(input);
// This should call the backward-compatible instance method overload
var premailedOutput = premailer.MoveCssInline(false, null, null, false, false, null, false);

Assert.Contains("<div class=\"test\" style=\"height: 100px;width: 100px", premailedOutput.Html);
}

[Fact]
public void MoveCssInline_BackwardCompatibility_StreamMethod_WithoutUseEmailFormatter()
{
// Test that the old Stream method signature (without useEmailFormatter) still works
string input = "<html><head><style type=\"text/css\">.test { height: 100px; }</style></head><body><div class=\"test\" style=\"width: 100px;\">test</div></body></html>";

using (var stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(input)))
{
// This should call the backward-compatible Stream overload
var premailedOutput = PreMailer.MoveCssInline(stream, false, null, null, false, false, null, false);

Assert.Contains("<div class=\"test\" style=\"height: 100px;width: 100px", premailedOutput.Html);
}
}

[Fact]
public void MoveCssInline_BackwardCompatibility_UriMethod_WithoutUseEmailFormatter()
{
// Test that the old Uri + string method signature (without useEmailFormatter) still works
string input = "<html><head><style type=\"text/css\">.test { height: 100px; }</style></head><body><div class=\"test\" style=\"width: 100px;\">test</div></body></html>";
var baseUri = new Uri("http://example.com/");

// This should call the backward-compatible Uri overload
var premailedOutput = PreMailer.MoveCssInline(baseUri, input, false, null, null, false, false, null, false);

Assert.Contains("<div class=\"test\" style=\"height: 100px;width: 100px", premailedOutput.Html);
}

[Fact]
public void MoveCssInline_BackwardCompatibility_UriStreamMethod_WithoutUseEmailFormatter()
{
// Test that the old Uri + stream method signature (without useEmailFormatter) still works
string input = "<html><head><style type=\"text/css\">.test { height: 100px; }</style></head><body><div class=\"test\" style=\"width: 100px;\">test</div></body></html>";
var baseUri = new Uri("http://example.com/");

using (var stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(input)))
{
// This should call the backward-compatible Uri + Stream overload
var premailedOutput = PreMailer.MoveCssInline(baseUri, stream, false, null, null, false, false, null, false);

Assert.Contains("<div class=\"test\" style=\"height: 100px;width: 100px", premailedOutput.Html);
}
}
}
}
88 changes: 88 additions & 0 deletions PreMailer.Net/PreMailer.Net/PreMailer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,23 @@ public PreMailer(Stream stream, Uri baseUri = null)
_cssSelectorParser = new CssSelectorParser();
}

/// <summary>
/// In-lines the CSS within the HTML given.
/// </summary>
/// <param name="html">The HTML input.</param>
/// <param name="removeStyleElements">If set to <c>true</c> the style elements are removed.</param>
/// <param name="ignoreElements">CSS selector for STYLE elements to ignore (e.g. mobile-specific styles etc.)</param>
/// <param name="css">A string containing a style-sheet for inlining.</param>
/// <param name="stripIdAndClassAttributes">True to strip ID and class attributes</param>
/// <param name="removeComments">True to remove comments, false to leave them intact</param>
/// <param name="customFormatter">Custom formatter to use</param>
/// <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>
/// <returns>Returns the html input, with styles moved to inline attributes.</returns>
public static InlineResult MoveCssInline(string html, bool removeStyleElements, string ignoreElements, string css, bool stripIdAndClassAttributes, bool removeComments, IMarkupFormatter customFormatter, bool preserveMediaQueries)
{
return new PreMailer(html).MoveCssInline(removeStyleElements, ignoreElements, css, stripIdAndClassAttributes, removeComments, customFormatter, preserveMediaQueries, false);
}

/// <summary>
/// In-lines the CSS within the HTML given.
/// </summary>
Expand All @@ -86,6 +103,23 @@ public static InlineResult MoveCssInline(string html, bool removeStyleElements =
return new PreMailer(html).MoveCssInline(removeStyleElements, ignoreElements, css, stripIdAndClassAttributes, removeComments, customFormatter, preserveMediaQueries, useEmailFormatter);
}

/// <summary>
/// In-lines the CSS within the HTML given.
/// </summary>
/// <param name="stream">The Stream input.</param>
/// <param name="removeStyleElements">If set to <c>true</c> the style elements are removed.</param>
/// <param name="ignoreElements">CSS selector for STYLE elements to ignore (e.g. mobile-specific styles etc.)</param>
/// <param name="css">A string containing a style-sheet for inlining.</param>
/// <param name="stripIdAndClassAttributes">True to strip ID and class attributes</param>
/// <param name="removeComments">True to remove comments, false to leave them intact</param>
/// <param name="customFormatter">Custom formatter to use</param>
/// <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>
/// <returns>Returns the html input, with styles moved to inline attributes.</returns>
public static InlineResult MoveCssInline(Stream stream, bool removeStyleElements, string ignoreElements, string css, bool stripIdAndClassAttributes, bool removeComments, IMarkupFormatter customFormatter, bool preserveMediaQueries)
{
return new PreMailer(stream).MoveCssInline(removeStyleElements, ignoreElements, css, stripIdAndClassAttributes, removeComments, customFormatter, preserveMediaQueries, false);
}

/// <summary>
/// In-lines the CSS within the HTML given.
/// </summary>
Expand All @@ -104,6 +138,25 @@ public static InlineResult MoveCssInline(Stream stream, bool removeStyleElements
return new PreMailer(stream).MoveCssInline(removeStyleElements, ignoreElements, css, stripIdAndClassAttributes, removeComments, customFormatter, preserveMediaQueries, useEmailFormatter);
}

/// <summary>
/// In-lines the CSS within the HTML given.
/// </summary>
/// /// <param name="baseUri">The base url that will be used to resolve any relative urls</param>
/// <param name="baseUri">The Url that all relative urls will be off of.</param>
/// <param name="html">The HTML input.</param>
/// <param name="removeStyleElements">If set to <c>true</c> the style elements are removed.</param>
/// <param name="ignoreElements">CSS selector for STYLE elements to ignore (e.g. mobile-specific styles etc.)</param>
/// <param name="css">A string containing a style-sheet for inlining.</param>
/// <param name="stripIdAndClassAttributes">True to strip ID and class attributes</param>
/// <param name="removeComments">True to remove comments, false to leave them intact</param>
/// <param name="customFormatter">Custom formatter to use</param>
/// <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>
/// <returns>Returns the html input, with styles moved to inline attributes.</returns>
public static InlineResult MoveCssInline(Uri baseUri, string html, bool removeStyleElements, string ignoreElements, string css, bool stripIdAndClassAttributes, bool removeComments, IMarkupFormatter customFormatter, bool preserveMediaQueries)
{
return new PreMailer(html, baseUri).MoveCssInline(removeStyleElements, ignoreElements, css, stripIdAndClassAttributes, removeComments, customFormatter, preserveMediaQueries, false);
}

/// <summary>
/// In-lines the CSS within the HTML given.
/// </summary>
Expand All @@ -124,6 +177,25 @@ public static InlineResult MoveCssInline(Uri baseUri, string html, bool removeSt
return new PreMailer(html, baseUri).MoveCssInline(removeStyleElements, ignoreElements, css, stripIdAndClassAttributes, removeComments, customFormatter, preserveMediaQueries, useEmailFormatter);
}

/// <summary>
/// In-lines the CSS within the HTML given.
/// </summary>
/// /// <param name="baseUri">The base url that will be used to resolve any relative urls</param>
/// <param name="baseUri">The Url that all relative urls will be off of.</param>
/// <param name="stream">The HTML input.</param>
/// <param name="removeStyleElements">If set to <c>true</c> the style elements are removed.</param>
/// <param name="ignoreElements">CSS selector for STYLE elements to ignore (e.g. mobile-specific styles etc.)</param>
/// <param name="css">A string containing a style-sheet for inlining.</param>
/// <param name="stripIdAndClassAttributes">True to strip ID and class attributes</param>
/// <param name="removeComments">True to remove comments, false to leave them intact</param>
/// <param name="customFormatter">Custom formatter to use</param>
/// <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>
/// <returns>Returns the html input, with styles moved to inline attributes.</returns>
public static InlineResult MoveCssInline(Uri baseUri, Stream stream, bool removeStyleElements, string ignoreElements, string css, bool stripIdAndClassAttributes, bool removeComments, IMarkupFormatter customFormatter, bool preserveMediaQueries)
{
return new PreMailer(stream, baseUri).MoveCssInline(removeStyleElements, ignoreElements, css, stripIdAndClassAttributes, removeComments, customFormatter, preserveMediaQueries, false);
}

/// <summary>
/// In-lines the CSS within the HTML given.
/// </summary>
Expand All @@ -144,6 +216,22 @@ public static InlineResult MoveCssInline(Uri baseUri, Stream stream, bool remove
return new PreMailer(stream, baseUri).MoveCssInline(removeStyleElements, ignoreElements, css, stripIdAndClassAttributes, removeComments, customFormatter, preserveMediaQueries, useEmailFormatter);
}

/// <summary>
/// In-lines the CSS for the current HTML
/// </summary>
/// <param name="removeStyleElements">If set to <c>true</c> the style elements are removed.</param>
/// <param name="ignoreElements">CSS selector for STYLE elements to ignore (e.g. mobile-specific styles etc.)</param>
/// <param name="css">A string containing a style-sheet for inlining.</param>
/// <param name="stripIdAndClassAttributes">True to strip ID and class attributes</param>
/// <param name="removeComments">True to remove comments, false to leave them intact</param>
/// <param name="customFormatter">Custom formatter to use</param>
/// <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>
/// <returns>Returns the html input, with styles moved to inline attributes.</returns>
public InlineResult MoveCssInline(bool removeStyleElements, string ignoreElements, string css, bool stripIdAndClassAttributes, bool removeComments, IMarkupFormatter customFormatter, bool preserveMediaQueries)
{
return MoveCssInline(removeStyleElements, ignoreElements, css, stripIdAndClassAttributes, removeComments, customFormatter, preserveMediaQueries, false);
}

/// <summary>
/// In-lines the CSS for the current HTML
/// </summary>
Expand Down
Loading