-
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.
- Loading branch information
1 parent
dc48f27
commit 812c49b
Showing
20 changed files
with
306 additions
and
26 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
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> | ||
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
<NoWarn>1591</NoWarn> | ||
<NeutralLanguage>en</NeutralLanguage> | ||
<LangVersion>latest</LangVersion> | ||
<RootNamespace>Mjml.Net</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="AngleSharp" Version="1.1.2" /> | ||
<PackageReference Include="AngleSharp.Css" Version="0.17.0" /> | ||
<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" /> | ||
</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,91 @@ | ||
using AngleSharp; | ||
using AngleSharp.Dom; | ||
using Mjml.Net; | ||
|
||
namespace Html.Net; | ||
|
||
public sealed class InlineProcessor : IPostProcessor | ||
{ | ||
public static readonly InlineProcessor Instance = new InlineProcessor(); | ||
|
||
private InlineProcessor() | ||
{ | ||
} | ||
|
||
public string PostProcess(string html, MjmlOptions options) | ||
{ | ||
var config = Configuration.Default.WithCss(); | ||
var context = BrowsingContext.New(config); | ||
|
||
var document = context.OpenAsync(req => req.Content(html)).Result; | ||
|
||
Traverse(document, a => RenameNonInline(a, document)); | ||
Traverse(document, InlineStyle); | ||
Traverse(document, a => RestoreNonInline(a, document)); | ||
|
||
return document.ToString()!; | ||
} | ||
|
||
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("style", css); | ||
} | ||
} | ||
|
||
private static void RenameNonInline(IElement element, IDocument document) | ||
{ | ||
if (string.Equals(element.TagName, "style", StringComparison.OrdinalIgnoreCase) && !element.HasAttribute("inline")) | ||
{ | ||
RenameTag(element, "non_inline_style", document); | ||
} | ||
} | ||
|
||
private static void RestoreNonInline(IElement element, IDocument document) | ||
{ | ||
if (string.Equals(element.TagName, "non_inline_style", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
RenameTag(element, "non_inline_style", document); | ||
} | ||
|
||
if (string.Equals(element.TagName, "style", StringComparison.OrdinalIgnoreCase) && element.HasAttribute("inline")) | ||
{ | ||
element.Remove(); | ||
} | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Mjml.Net; | ||
|
||
public interface IPostProcessor | ||
{ | ||
string PostProcess(string html, MjmlOptions options); | ||
} |
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 |
---|---|---|
|
@@ -14,4 +14,8 @@ | |
.red-text div { | ||
color: red !important; | ||
} | ||
</style> | ||
</style> | ||
|
||
<div class="red-text"> | ||
<div></div> | ||
</div> |
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,23 @@ | ||
<style type="text/css"> | ||
@media only screen and (min-width:480px) { | ||
} | ||
</style> | ||
|
||
<style media="screen and (min-width:480px)"> | ||
</style> | ||
|
||
<style type="text/css"> | ||
</style> | ||
|
||
<style type="text/css"> | ||
</style> | ||
|
||
<style inline="inline" type="text/css"> | ||
.red-text div { | ||
color: red !important; | ||
} | ||
</style> | ||
|
||
<div class="red-text"> | ||
<div style="color: red !important;"></div> | ||
</div> |
Oops, something went wrong.