Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds html parser check for void elements #2217

Merged
merged 1 commit into from
Feb 18, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading