-
Notifications
You must be signed in to change notification settings - Fork 14
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
be97935
commit b6566ab
Showing
60 changed files
with
616 additions
and
196 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,24 @@ | ||
using AngleSharp.Dom; | ||
|
||
namespace Mjml.Net; | ||
|
||
public static class AngleSharpExtensions | ||
{ | ||
public static void Traverse(this INode node, Action<IElement> action) | ||
{ | ||
foreach (var child in node.ChildNodes.ToList()) | ||
{ | ||
Traverse(child, action); | ||
} | ||
|
||
if (node is IElement element) | ||
{ | ||
action(element); | ||
} | ||
} | ||
|
||
public static IEnumerable<IElement> Children(this IElement node, string tagName) | ||
{ | ||
return node.Children.Where(x => string.Equals(x.NodeName, tagName, StringComparison.OrdinalIgnoreCase)); | ||
} | ||
} |
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,49 @@ | ||
using AngleSharp; | ||
using AngleSharp.Css; | ||
using AngleSharp.Dom; | ||
|
||
namespace Mjml.Net; | ||
|
||
public sealed class AngleSharpPostProcessor : IPostProcessor, INestingPostProcessor | ||
{ | ||
private static readonly IConfiguration HtmlConfiguration = | ||
Configuration.Default | ||
.WithCss() | ||
.Without<ICssDefaultStyleSheetProvider>(); | ||
|
||
public static readonly IPostProcessor Default = new AngleSharpPostProcessor(new InlineCssPostProcessor(), new AttributesPostProcessor()); | ||
|
||
private readonly IAngleSharpPostProcessor[] inner; | ||
|
||
public bool Has<T>() | ||
{ | ||
return inner.Any(x => x is T); | ||
} | ||
|
||
public AngleSharpPostProcessor(params IAngleSharpPostProcessor[] inner) | ||
{ | ||
this.inner = inner ?? throw new ArgumentNullException(nameof(inner)); | ||
} | ||
|
||
public async ValueTask<string> PostProcessAsync(string html, MjmlOptions options, | ||
CancellationToken ct) | ||
{ | ||
var document = await ParseAsync(html, ct); | ||
|
||
foreach (var processor in inner) | ||
{ | ||
await processor.ProcessAsync(document, options, ct); | ||
} | ||
|
||
var result = document.ToHtml(); | ||
|
||
return result; | ||
} | ||
|
||
private static async Task<IDocument> ParseAsync(string html, CancellationToken ct) | ||
{ | ||
var context = BrowsingContext.New(HtmlConfiguration); | ||
|
||
return await context.OpenAsync(req => req.Content(html), 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using AngleSharp.Dom; | ||
|
||
namespace Mjml.Net; | ||
|
||
public sealed class AttributesPostProcessor : IAngleSharpPostProcessor | ||
{ | ||
public static readonly IPostProcessor Instance = new AngleSharpPostProcessor(new AttributesPostProcessor()); | ||
|
||
public ValueTask ProcessAsync(IDocument document, MjmlOptions options, CancellationToken ct) | ||
{ | ||
foreach (var attributes in document.QuerySelectorAll("mj-html-attributes")) | ||
{ | ||
foreach (var selector in attributes.Children("mj-selector")) | ||
{ | ||
var path = selector.GetAttribute("path"); | ||
|
||
if (string.IsNullOrEmpty(path)) | ||
{ | ||
continue; | ||
} | ||
|
||
var attributeValues = selector.Children("mj-html-attribute") | ||
.Select(x => | ||
{ | ||
var attributeName = x.GetAttribute("name")!; | ||
var attributeValue = x.TextContent; | ||
return (Name: attributeName, Value: attributeValue); | ||
}) | ||
.Where(x => !string.IsNullOrWhiteSpace(x.Name)) | ||
.ToList(); | ||
|
||
foreach (var target in document.QuerySelectorAll(path)) | ||
{ | ||
foreach (var (name, value) in attributeValues) | ||
{ | ||
target.SetAttribute(name, value?.Trim()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
RemoveAll(document, "mj-html-attributes"); | ||
RemoveAll(document, "mj-html-attribute"); | ||
RemoveAll(document, "mj-selector"); | ||
|
||
return default; | ||
} | ||
|
||
private static void RemoveAll(IDocument document, string selector) | ||
{ | ||
foreach (var element in document.QuerySelectorAll(selector)) | ||
{ | ||
element.Remove(); | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
Mjml.Net.PostProcessors/Components/AttributeSelectorComponent.cs
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,30 @@ | ||
namespace Mjml.Net; | ||
|
||
public partial class SelectorComponent : Component | ||
{ | ||
private static readonly AllowedParents Parents = | ||
[ | ||
"mj-html-attributes" | ||
]; | ||
|
||
public override AllowedParents? AllowedParents => Parents; | ||
|
||
public override ContentType ContentType => ContentType.Complex; | ||
|
||
public override string ComponentName => "mj-selector"; | ||
|
||
[Bind("path", BindType.RequiredString)] | ||
public string Path; | ||
|
||
public override void Render(IHtmlRenderer renderer, GlobalContext context) | ||
{ | ||
if (!context.Options.HasProcessor<AttributesPostProcessor>()) | ||
{ | ||
return; | ||
} | ||
|
||
renderer.StartElement(ComponentName).Attr("path", Path); | ||
RenderChildren(renderer, context); | ||
renderer.EndElement(ComponentName); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
Mjml.Net.PostProcessors/Components/HtmlAttributeComponent.cs
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,33 @@ | ||
namespace Mjml.Net; | ||
|
||
public partial class HtmlAttributeComponent : Component | ||
{ | ||
private static readonly AllowedParents Parents = | ||
[ | ||
"mj-selector" | ||
]; | ||
|
||
public override AllowedParents? AllowedParents => Parents; | ||
|
||
public override ContentType ContentType => ContentType.Text; | ||
|
||
public override string ComponentName => "mj-html-attribute"; | ||
|
||
[Bind("name", BindType.RequiredString)] | ||
public string Name; | ||
|
||
[BindText] | ||
public InnerTextOrHtml? Text; | ||
|
||
public override void Render(IHtmlRenderer renderer, GlobalContext context) | ||
{ | ||
if (!context.Options.HasProcessor<AttributesPostProcessor>()) | ||
{ | ||
return; | ||
} | ||
|
||
renderer.StartElement(ComponentName).Attr("name", Name); | ||
renderer.Content(Text); | ||
renderer.EndElement(ComponentName); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
Mjml.Net.PostProcessors/Components/HtmlAttributesComponent.cs
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,27 @@ | ||
namespace Mjml.Net.Components; | ||
|
||
public partial class HtmlAttributesComponent : Component | ||
{ | ||
private static readonly AllowedParents Parents = | ||
[ | ||
"mj-head" | ||
]; | ||
|
||
public override AllowedParents? AllowedParents => Parents; | ||
|
||
public override ContentType ContentType => ContentType.Complex; | ||
|
||
public override string ComponentName => "mj-html-attributes"; | ||
|
||
public override void Render(IHtmlRenderer renderer, GlobalContext context) | ||
{ | ||
if (!context.Options.HasProcessor<AttributesPostProcessor>()) | ||
{ | ||
return; | ||
} | ||
|
||
renderer.StartElement(ComponentName); | ||
RenderChildren(renderer, context); | ||
renderer.EndElement(ComponentName); | ||
} | ||
} |
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,9 @@ | ||
using AngleSharp.Dom; | ||
|
||
namespace Mjml.Net; | ||
|
||
public interface IAngleSharpPostProcessor | ||
{ | ||
ValueTask ProcessAsync(IDocument document, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Mjml.Net; | ||
|
||
public interface INestingPostProcessor | ||
{ | ||
bool Has<T>(); | ||
} |
27 changes: 5 additions & 22 deletions
27
....PostProcessors/InlineCssPostProcessor.cs → ....PostProcessors/InlineCssPostProcessor.cs
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,43 @@ | ||
using Mjml.Net.Components; | ||
|
||
namespace Mjml.Net; | ||
|
||
public static class PostProcessorExtensions | ||
{ | ||
public static MjmlOptions WithPostProcessors(this MjmlOptions options) | ||
{ | ||
options.PostProcessors = [AngleSharpPostProcessor.Default]; | ||
return options; | ||
} | ||
|
||
public static IMjmlRenderer AddHtmlAttributes(this IMjmlRenderer renderer) | ||
{ | ||
renderer.Add<HtmlAttributeComponent>(); | ||
renderer.Add<HtmlAttributesComponent>(); | ||
renderer.Add<SelectorComponent>(); | ||
return renderer; | ||
} | ||
|
||
public static bool HasProcessor<T>(this MjmlOptions options) | ||
{ | ||
if (options.PostProcessors == null || options.PostProcessors.Length == 0) | ||
{ | ||
return false; | ||
} | ||
|
||
foreach (var processor in options.PostProcessors) | ||
{ | ||
if (processor is T) | ||
{ | ||
return true; | ||
} | ||
|
||
if (processor is INestingPostProcessor nesting && nesting.Has<T>()) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
File renamed without changes
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 |
---|---|---|
|
@@ -3,7 +3,6 @@ | |
using Mjml.Net; | ||
using Mjml.Net.Validators; | ||
using Tests.Internal; | ||
using Xunit; | ||
|
||
namespace Tests; | ||
|
||
|
Oops, something went wrong.