-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix tests. * Temp * CSS Inline. Close #111 * Fix tests * Small refactoring. * Fix build.
- Loading branch information
1 parent
103bd37
commit be97935
Showing
25 changed files
with
465 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<NeutralLanguage>en</NeutralLanguage> | ||
<LangVersion>latest</LangVersion> | ||
<RootNamespace>Mjml.Net</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="AngleSharp" Version="1.1.2" /> | ||
<PackageReference Include="AngleSharp.Css" Version="1.0.0-beta.139" /> | ||
<PackageReference Include="Meziantou.Analyzer" Version="2.0.145"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="RefactoringEssentials" Version="5.6.0" PrivateAssets="all" /> | ||
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.507" PrivateAssets="all" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<AdditionalFiles Include="..\stylecop.json" Link="stylecop.json" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Include="..\README.md"> | ||
<Pack>True</Pack> | ||
<PackagePath></PackagePath> | ||
</None> | ||
|
||
<None Include="icon.png"> | ||
<Pack>True</Pack> | ||
<PackagePath></PackagePath> | ||
</None> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Mjml.Net\Mjml.Net.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
using AngleSharp; | ||
using AngleSharp.Css; | ||
using AngleSharp.Dom; | ||
using Mjml.Net; | ||
|
||
namespace Html.Net; | ||
|
||
public sealed class InlineCssPostProcessor : IPostProcessor | ||
{ | ||
private const string FallbackStyle = "non_inline_style"; | ||
private static readonly IConfiguration HtmlConfiguration = | ||
Configuration.Default | ||
.WithCss() | ||
.Without<ICssDefaultStyleSheetProvider>(); | ||
|
||
public static readonly InlineCssPostProcessor Instance = new InlineCssPostProcessor(); | ||
|
||
private InlineCssPostProcessor() | ||
{ | ||
} | ||
|
||
public async ValueTask<string> PostProcessAsync(string html, MjmlOptions options, | ||
CancellationToken ct) | ||
{ | ||
var context = BrowsingContext.New(HtmlConfiguration); | ||
|
||
var document = await context.OpenAsync(req => req.Content(html), ct); | ||
|
||
Traverse(document, a => RenameNonInline(a, document)); | ||
Traverse(document, InlineStyle); | ||
Traverse(document, a => RestoreNonInline(a, document)); | ||
|
||
var result = document.ToHtml(); | ||
|
||
return result; | ||
} | ||
|
||
private static void Traverse(INode node, Action<IElement> action) | ||
{ | ||
foreach (var child in node.ChildNodes.ToList()) | ||
{ | ||
Traverse(child, action); | ||
} | ||
|
||
if (node is IElement element) | ||
{ | ||
action(element); | ||
} | ||
} | ||
|
||
private static void InlineStyle(IElement element) | ||
{ | ||
var currentStyle = element.ComputeCurrentStyle(); | ||
|
||
if (currentStyle.Any()) | ||
{ | ||
var css = currentStyle.ToCss(); | ||
|
||
element.SetAttribute(TagNames.Style, css); | ||
} | ||
} | ||
|
||
private static void RenameNonInline(IElement element, IDocument document) | ||
{ | ||
if (string.Equals(element.TagName, TagNames.Style, StringComparison.OrdinalIgnoreCase) && !IsInline(element)) | ||
{ | ||
RenameTag(element, FallbackStyle, document); | ||
} | ||
} | ||
|
||
private static void RestoreNonInline(IElement element, IDocument document) | ||
{ | ||
if (string.Equals(element.TagName, FallbackStyle, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
RenameTag(element, TagNames.Style, document); | ||
} | ||
|
||
if (string.Equals(element.TagName, TagNames.Style, StringComparison.OrdinalIgnoreCase) && IsInline(element)) | ||
{ | ||
element.Remove(); | ||
} | ||
} | ||
|
||
private static bool IsInline(IElement element) | ||
{ | ||
return element.HasAttribute("inline"); | ||
} | ||
|
||
private static void RenameTag(IElement node, string tagName, IDocument document) | ||
{ | ||
var clone = document.CreateElement(tagName); | ||
|
||
foreach (var attribute in node.Attributes) | ||
{ | ||
clone.SetAttribute(attribute.NamespaceUri, attribute.Name, attribute.Value); | ||
} | ||
|
||
var parent = node.Parent!; | ||
|
||
clone.InnerHtml = node.InnerHtml; | ||
|
||
parent.InsertBefore(clone, node); | ||
parent.RemoveChild(node); | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Mjml.Net; | ||
|
||
public interface IPostProcessor | ||
{ | ||
ValueTask<string> PostProcessAsync(string html, MjmlOptions options, | ||
CancellationToken ct); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.