Skip to content

Commit 23c8cd7

Browse files
committed
Better handle HTML input, especially regarding comments and certain self-closing HTML tags
1 parent 5d6d2a7 commit 23c8cd7

File tree

2 files changed

+456
-24
lines changed

2 files changed

+456
-24
lines changed

UdonXML.cs

+26-22
Original file line numberDiff line numberDiff line change
@@ -197,16 +197,9 @@ private object[] FindCurrentLevel(object[] data, int[] position)
197197

198198
var current = data;
199199

200-
#if DEBUG
201-
Debug.Log("FCL Start");
202-
#endif
203-
204200
// [ 1, 0, 1]
205201
while (position.Length != 0)
206202
{
207-
#if DEBUG
208-
Debug.Log("FCL: " + position[0] + " " + ((object[]) current[2]).Length);
209-
#endif
210203
current = (object[]) ((object[]) current[2])[position[0]];
211204
position = RemoveFirstIntegerArray(position);
212205
}
@@ -370,6 +363,15 @@ private object[] Parse(char[] input)
370363
if (c == ' ' && !hasNodeNameEnded)
371364
{
372365
hasNodeNameEnded = true;
366+
var nodeNameLow = nodeName.ToLower();
367+
if (nodeNameLow == "area" || nodeNameLow == "base" || nodeNameLow == "br" ||
368+
nodeNameLow == "embed" || nodeNameLow == "hr" || nodeNameLow == "iframe" ||
369+
nodeNameLow == "img" || nodeNameLow == "input" || nodeNameLow == "link" ||
370+
nodeNameLow == "meta" || nodeNameLow == "param" || nodeNameLow == "source" ||
371+
nodeNameLow == "track")
372+
{
373+
isSelfClosingNode = true;
374+
}
373375
}
374376

375377
if (hasNodeNameEnded)
@@ -458,11 +460,7 @@ private string Serialize(object[] data, string padding)
458460
var tagList = "";
459461
for (var i = 0; i != tagNames.Length; i++)
460462
{
461-
var tagValue = (string) tagValues[i];
462-
Debug.Log(tagValue);
463-
Debug.Log((tagValue == null) + " " + (null == tagValue));
464-
Debug.Log("");
465-
if (null == tagValue)
463+
if (null == tagValues[i])
466464
{
467465
// Tags without value, such as bordered in table, or html in doctype
468466
tagList += " " + tagNames[i];
@@ -477,17 +475,23 @@ private string Serialize(object[] data, string padding)
477475
{
478476
if (nodeValue.Trim().Length == 0)
479477
{
480-
if (nodeName == "?xml")
481-
{
482-
output += $"<{nodeName}{tagList}?>"; // ?xml has an extra ? at the end
483-
}
484-
else if (nodeName.ToUpper() == "!DOCTYPE")
485-
{
486-
output += $"<{nodeName}{tagList}>"; // doc types are self closing without the usual slash
487-
}
488-
else
478+
var nodeNameLow = nodeName.ToLower();
479+
switch (nodeNameLow)
489480
{
490-
output += $"<{nodeName}{tagList} />";
481+
// ?xml has an extra ? at the end
482+
case "?xml":
483+
output += $"<{nodeName}{tagList}?>";
484+
break;
485+
// doc types are self closing without the usual slash
486+
case "!doctype":
487+
output += $"<{nodeName}{tagList}>";
488+
break;
489+
case "!--":
490+
output += $"<{nodeName}{tagList} -->";
491+
break;
492+
default:
493+
output += $"<{nodeName}{tagList} />";
494+
break;
491495
}
492496
}
493497
else

0 commit comments

Comments
 (0)