Skip to content

Commit

Permalink
Adds html parser check for void elements (#2217)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruzihm authored Feb 18, 2025
1 parent 23cd63a commit 145856a
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion OpenDreamClient/Interface/Html/HtmlParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ void PushCurrentText() {
} else {
tags.Push(tagType);

appendTo.PushTag(new MarkupNode(tagType, null, ParseAttributes(attributes)), selfClosing: attributes[^1] == "/");
bool isSelfClosing = IsSelfClosing(tagType, attributes);
appendTo.PushTag(new MarkupNode(tagType, null, ParseAttributes(attributes)), selfClosing: isSelfClosing);
}

break;
Expand Down Expand Up @@ -139,6 +140,39 @@ void PushCurrentText() {
appendTo.Pop();
}

/**
* <summary>
* Returns if a tag is written in old self-closing form, or if the tag
* represents a void element, which must have no children
* </summary>
*/
private static bool IsSelfClosing(string tagType, string[] attributes) {
if (attributes[^1] == "/") {
return true;
}

switch (tagType) {
case "area":
case "base":
case "br":
case "col":
case "embed":
case "hr":
case "img":
case "input":
case "link":
case "meta":
case "param":
case "source":
case "track":
case "wbr":
return true;

default:
return false;
}
}

private static Dictionary<string, MarkupParameter> ParseAttributes(string[] attributes) {
Dictionary<string, MarkupParameter> parsedAttributes = new();

Expand Down

0 comments on commit 145856a

Please sign in to comment.