Skip to content
Open
Changes from 3 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
27 changes: 23 additions & 4 deletions OpenDreamClient/Interface/Html/HtmlParser.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Text;
using OpenDreamShared.Dream;
using OpenDreamShared.Dream;
using Robust.Shared.Utility;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace OpenDreamClient.Interface.Html;

Expand All @@ -11,6 +13,9 @@
private static readonly ISawmill Sawmill;
private static readonly HashSet<string> WarnedAttributes = new();

private static Regex? _attributeMatchRegex = null;

Check warning

Code scanning / InspectCode

Redundant member initializer Warning

Initializing field by default value is redundant
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
Comment thread
PowerfulBacon marked this conversation as resolved.
Outdated
private static Regex AttributeMatchRegex => _attributeMatchRegex ??= new Regex(@"[^ =]+(?:=(?:\w+|""[^""]*""|'[^']*'))?");

static HtmlParser() {
Sawmill = IoCManager.Resolve<ILogManager>().GetSawmill("opendream.html_parser");
}
Expand All @@ -33,6 +38,11 @@
currentText.Clear();
}

void SkipComment() {
while ((i - 2 < 0 || (text[i - 2] != '-' || text[i - 1] != '-' || text[i] != '>')) && text.Length > 2)
i++;
}

for (i = 0; i < text.Length; i++) {
char c = text[i];

Expand Down Expand Up @@ -67,7 +77,10 @@
}

string insideTag = currentText.ToString();
string[] attributes = insideTag.Split(' ', StringSplitOptions.RemoveEmptyEntries);
string[] attributes = AttributeMatchRegex.Matches(insideTag)
.Where(x => x.Length > 0)
.Select(x => x.Value)
.ToArray();
string tagType = attributes[0].ToLowerInvariant();

currentText.Clear();
Expand Down Expand Up @@ -98,6 +111,12 @@
appendTo.Pop();
tags.Pop();
} else {
// If a comment contained other HTML tags, we need to make sure those also
// get skipped.
if (tagType == "!--") {
SkipComment();
continue;
}
if (!isSelfClosing) {
tags.Push(tagType);
}
Expand All @@ -123,7 +142,7 @@

if (insideEntity.StartsWith('#')) {
if (int.TryParse(insideEntity.Substring(1), out int result)) {
currentText.Append((char) result);
currentText.Append((char)result);
}
} else {
switch (insideEntity) {
Expand Down
Loading