Skip to content

Commit

Permalink
Improve rendering in non-async context
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianStehle committed Sep 18, 2024
1 parent 00b366b commit ffc4fa1
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
<RepositoryUrl>https://github.com/SebastianStehle/mjml-net.git</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<Version>4.0.0</Version>
<Version>4.1.0</Version>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public partial class SelectorComponent : Component

public override void Render(IHtmlRenderer renderer, GlobalContext context)
{
if (!context.Options.HasProcessor<AttributesPostProcessor>())
if (!context.Async || !context.Options.HasProcessor<AttributesPostProcessor>())
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public partial class HtmlAttributeComponent : Component

public override void Render(IHtmlRenderer renderer, GlobalContext context)
{
if (!context.Options.HasProcessor<AttributesPostProcessor>())
if (!context.Async || !context.Options.HasProcessor<AttributesPostProcessor>())
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public partial class HtmlAttributesComponent : Component

public override void Render(IHtmlRenderer renderer, GlobalContext context)
{
if (!context.Options.HasProcessor<AttributesPostProcessor>())
if (!context.Async || !context.Options.HasProcessor<AttributesPostProcessor>())
{
return;
}
Expand Down
9 changes: 3 additions & 6 deletions Mjml.Net/GlobalContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,15 @@ public sealed class GlobalContext

public Dictionary<string, Dictionary<string, string>> AttributesByName => attributesByName;

public MjmlOptions Options { get; private set; }
public MjmlOptions Options { get; set; }

public bool Async { get; set; }

public IFileLoader? FileLoader
{
get => fileLoader ??= Options?.FileLoader?.Invoke();
}

public void SetOptions(MjmlOptions options)
{
Options = options;
}

public void Clear()
{
GlobalData.Clear();
Expand Down
5 changes: 3 additions & 2 deletions Mjml.Net/MjmlRenderContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ public ValidationErrors Validate()
return new ValidationErrors(errors);
}

public void Setup(MjmlRenderer mjmlRenderer, MjmlOptions? mjmlOptions)
public void Setup(MjmlRenderer mjmlRenderer, bool isAsync, MjmlOptions? mjmlOptions)
{
this.mjmlRenderer = mjmlRenderer;
this.mjmlOptions = mjmlOptions ??= new MjmlOptions();

// Reuse the context and therefore do not set them over the constructor.
context.SetOptions(mjmlOptions);
context.Options = mjmlOptions;
context.Async = isAsync;

validationContext.Options = mjmlOptions;
}
Expand Down
10 changes: 5 additions & 5 deletions Mjml.Net/MjmlRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public IMjmlRenderer ClearHelpers()
/// <inheritdoc />
public RenderResult Render(string mjml, MjmlOptions? options = null)
{
return RenderCore(mjml, options).Result;
return RenderCore(mjml, false, options).Result;
}

/// <inheritdoc />
Expand All @@ -118,7 +118,7 @@ public RenderResult Render(Stream mjml, MjmlOptions? options = null)
/// <inheritdoc />
public RenderResult Render(TextReader mjml, MjmlOptions? options = null)
{
return RenderCore(mjml.ReadToEnd(), options).Result;
return RenderCore(mjml.ReadToEnd(), false, options).Result;
}

public async ValueTask<RenderResult> RenderAsync(string mjml, MjmlOptions? options = null,
Expand Down Expand Up @@ -147,7 +147,7 @@ private async ValueTask<RenderResult> RenderCoreAsync(string mjml, MjmlOptions?
CancellationToken ct)
{
#pragma warning disable MA0042 // Do not use blocking calls in an async method
var (result, actualOptions) = RenderCore(mjml, options);
var (result, actualOptions) = RenderCore(mjml, true, options);
#pragma warning restore MA0042 // Do not use blocking calls in an async method

if (actualOptions?.PostProcessors?.Length > 0 && !string.IsNullOrWhiteSpace(result.Html))
Expand All @@ -165,7 +165,7 @@ private async ValueTask<RenderResult> RenderCoreAsync(string mjml, MjmlOptions?
return result;
}

private (RenderResult Result, MjmlOptions Options) RenderCore(string mjml, MjmlOptions? options)
private (RenderResult Result, MjmlOptions Options) RenderCore(string mjml, bool isAsync, MjmlOptions? options)
{
options ??= new MjmlOptions();

Expand All @@ -174,7 +174,7 @@ private async ValueTask<RenderResult> RenderCoreAsync(string mjml, MjmlOptions?
var context = DefaultPools.RenderContexts.Get();
try
{
context.Setup(this, options);
context.Setup(this, isAsync, options);
context.StartBuffer();
context.Read(reader, null, null);

Expand Down
29 changes: 29 additions & 0 deletions Tests/Components/HtmlAttributesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,35 @@ public async Task Should_render_attributes()

AssertHelpers.HtmlFileAssert("Components.Outputs.HtmlAttributes.html", result);
}
[Fact]
public void Should_not_render_attributes_in_sync_context()
{
var source = @"
<mjml-test>
<mj-head>
<mj-html-attributes>
<mj-selector path="".custom div"">
<mj-html-attribute name=""data-id"">42</mj-html-attribute>
</mj-selector>
</mj-html-attributes>
</mj-head>
<mj-body>
<mj-raw>
<div class=""custom"">
<div></div>
</div>
</mj-raw>
</mj-body>
</mjml-test>
";

var (result, _) = TestHelper.Render(source, new MjmlOptions
{
PostProcessors = [AngleSharpPostProcessor.Default]
}, helpers: []);

AssertHelpers.HtmlFileAssert("Components.Outputs.HtmlAttributeInvalid.html", result);
}

[Fact]
public async Task Should_render_selector_only()
Expand Down
2 changes: 1 addition & 1 deletion Tests/HtmlExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public HtmlExtensionsTests()
Beautify = true
};

sut.Setup(new MjmlRenderer(), options);
sut.Setup(new MjmlRenderer(), false, options);
sut.StartBuffer();
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/HtmlRenderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public HtmlRenderTests()
Beautify = true
};

sut.Setup(new MjmlRenderer(), options);
sut.Setup(new MjmlRenderer(), false, options);
sut.StartBuffer();
}

Expand Down

0 comments on commit ffc4fa1

Please sign in to comment.