Skip to content

Commit

Permalink
Refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianStehle committed Jul 22, 2024
1 parent 6268653 commit 837f0e6
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions Mjml.Net/MjmlRenderContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,7 @@ private void ReadElement(string name, IHtmlReader reader, IComponent? parent, st
Read(reader, component, file);
}

if (!hasAddedClosingError)
{
ValidateSelfClosing(name, reader, file);
}
ValidatingClosingState(name, reader, file);

// If there is no parent, we handle the root and we can render everything top to bottom.
if (parent == null)
Expand Down Expand Up @@ -175,26 +172,39 @@ private void BindAndRender(IComponent component)
Cleanup();
}

private void ValidateSelfClosing(string name, IHtmlReader reader, string? file)
private void ValidatingClosingState(string name, IHtmlReader reader, string? file)
{
// Only show one closing error, otherwise we could get one for every item in the hierarchy.
if (hasAddedClosingError)
{
return;
}

if (reader.TokenKind == HtmlTokenKind.Tag && reader.SelfClosingElement)
{
return;
}

if (reader.TokenKind == HtmlTokenKind.EndTag && reader.Name != name)
void AddClosingError(string message)
{
errors.Add($"Unexpected end element, expected '{name}', got '{reader.Name}'.",
errors.Add(message,
ValidationErrorType.InvalidHtml,
new SourcePosition(reader.LineNumber, reader.LinePosition, file));
new SourcePosition(
reader.LineNumber,
reader.LinePosition,
file));

// Only show one closing error, otherwise we could get one for every item in the hierarchy.
hasAddedClosingError = true;
}

if (reader.TokenKind == HtmlTokenKind.EndTag && reader.Name != name)
{
AddClosingError($"Unexpected end element, expected '{name}', got '{reader.Name}'.");
}
else if (reader.TokenKind != HtmlTokenKind.EndTag)
{
errors.Add($"Unexpected end element, expected '{name}', got '{reader.TokenKind}' token.",
ValidationErrorType.InvalidHtml,
new SourcePosition(reader.LineNumber, reader.LinePosition, file));
hasAddedClosingError = true;
AddClosingError($"Unexpected end element, expected '{name}', got '{reader.TokenKind}' token.");
}
}

Expand Down

0 comments on commit 837f0e6

Please sign in to comment.