Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
50 changes: 50 additions & 0 deletions PreMailer.Net/PreMailer.Net.Tests/Base64UrlTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using Xunit;

namespace PreMailer.Net.Tests
{
public class Base64UrlTests
{
[Fact]
public void MoveCssInline_WithUnquotedBase64BackgroundImage_ShouldPreserveBase64Data()
{
string input = "<style>.pt-logo { background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA); background-repeat: no-repeat; }</style><span class=\"pt-logo\"></span>";

var result = PreMailer.MoveCssInline(input);

Assert.Contains("background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA)", result.Html);
Assert.Contains("background-repeat: no-repeat", result.Html);
}

[Fact]
public void MoveCssInline_WithQuotedBase64BackgroundImage_ShouldPreserveBase64Data()
{
string input = "<style>.pt-logo { background-image: url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA\"); background-repeat: no-repeat; }</style><span class=\"pt-logo\"></span>";

var result = PreMailer.MoveCssInline(input);

Assert.Contains("background-image: url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA\")", result.Html);
Assert.Contains("background-repeat: no-repeat", result.Html);
}

[Fact]
public void MoveCssInline_WithSvgXmlBackgroundImage_ShouldPreserveEncodedData()
{
string input = "<style>.my-icon { background-image: url(\"data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='%23fff' width='8' height='8' viewBox='0 0 8 8'%3e%3cpath d='M5.25 0l-4 4 4 4 1.5-1.5L4.25 4l2.5-2.5L5.25 0z'/%3e%3c/svg%3e\"); }</style><span class=\"my-icon\"></span>";

var result = PreMailer.MoveCssInline(input);

Assert.Contains("background-image: url(\"data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='%23fff' width='8' height='8' viewBox='0 0 8 8'%3e%3cpath d='M5.25 0l-4 4 4 4 1.5-1.5L4.25 4l2.5-2.5L5.25 0z'/%3e%3c/svg%3e\")", result.Html);
}

[Fact]
public void MoveCssInline_WithBase64DataWithDoubleEquals_ShouldPreserveEnding()
{
string input = "<style>.pt-logo { background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA==); }</style><span class=\"pt-logo\"></span>";

var result = PreMailer.MoveCssInline(input);

Assert.Contains("background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA==)", result.Html);
}
}
}
7 changes: 5 additions & 2 deletions PreMailer.Net/PreMailer.Net/CssParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ private static string CleanUp(string s)
temp = Regex.Replace(temp, "(['\"])([^'\"\\\\]*(?:\\\\.[^'\"\\\\]*)*?)http://([^'\"]*?)\\1", m =>
m.Groups[1].Value + m.Groups[2].Value + "http:" + httpProtocolMarker + m.Groups[3].Value + m.Groups[1].Value);

temp = Regex.Replace(temp, "(data:[^;]+;base64,[^)\"']*?)//([^)\"']*)", m =>
m.Groups[1].Value + dataUrlMarker + m.Groups[2].Value);
temp = Regex.Replace(temp, "(data:[^;]+;base64,)([^)\"']*?)//([^)\"']*)", m =>
m.Groups[1].Value + m.Groups[2].Value + dataUrlMarker + m.Groups[3].Value);

temp = _cssCommentRegex.Replace(temp, "");
temp = _unsupportedAtRuleRegex.Replace(temp, "");
Expand All @@ -141,6 +141,9 @@ private static string CleanUp(string s)
temp = temp.Replace(httpProtocolMarker, "//");
temp = temp.Replace(dataUrlMarker, "//");

temp = Regex.Replace(temp, @"url\s*\(\s*(['""]?)data:([^;]+);base64,([^)'""]*)(['""]?)\s*\)", m =>
"url(" + m.Groups[1].Value + "data:" + m.Groups[2].Value + ";base64," + m.Groups[3].Value + m.Groups[4].Value + ")");

return temp;
}

Expand Down
9 changes: 7 additions & 2 deletions PreMailer.Net/PreMailer.Net/StyleClassApplier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,15 @@ private static void SetAttribute(IElement domElement, AttributeToCss attributeTo
value = value.Replace("px", string.Empty);
}

// Quotation marks in CSS is common, but when applied to the style attribute we will use single quotes instead.
if (value.Contains("\""))
{
value = value.Replace("\"", "'");
if (value.Contains("data:") && value.Contains(";base64,"))
{
}
else
{
value = value.Replace("\"", "'");
}
}

domElement.SetAttribute(name, value);
Expand Down