Skip to content

Commit

Permalink
Better handle HTML input, especially regarding comments and certain s…
Browse files Browse the repository at this point in the history
…elf-closing HTML tags
  • Loading branch information
Foorack committed Apr 17, 2020
1 parent 5d6d2a7 commit 23c8cd7
Show file tree
Hide file tree
Showing 2 changed files with 456 additions and 24 deletions.
48 changes: 26 additions & 22 deletions UdonXML.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,16 +197,9 @@ private object[] FindCurrentLevel(object[] data, int[] position)

var current = data;

#if DEBUG
Debug.Log("FCL Start");
#endif

// [ 1, 0, 1]
while (position.Length != 0)
{
#if DEBUG
Debug.Log("FCL: " + position[0] + " " + ((object[]) current[2]).Length);
#endif
current = (object[]) ((object[]) current[2])[position[0]];
position = RemoveFirstIntegerArray(position);
}
Expand Down Expand Up @@ -370,6 +363,15 @@ private object[] Parse(char[] input)
if (c == ' ' && !hasNodeNameEnded)
{
hasNodeNameEnded = true;
var nodeNameLow = nodeName.ToLower();
if (nodeNameLow == "area" || nodeNameLow == "base" || nodeNameLow == "br" ||
nodeNameLow == "embed" || nodeNameLow == "hr" || nodeNameLow == "iframe" ||
nodeNameLow == "img" || nodeNameLow == "input" || nodeNameLow == "link" ||
nodeNameLow == "meta" || nodeNameLow == "param" || nodeNameLow == "source" ||
nodeNameLow == "track")
{
isSelfClosingNode = true;
}
}

if (hasNodeNameEnded)
Expand Down Expand Up @@ -458,11 +460,7 @@ private string Serialize(object[] data, string padding)
var tagList = "";
for (var i = 0; i != tagNames.Length; i++)
{
var tagValue = (string) tagValues[i];
Debug.Log(tagValue);
Debug.Log((tagValue == null) + " " + (null == tagValue));
Debug.Log("");
if (null == tagValue)
if (null == tagValues[i])
{
// Tags without value, such as bordered in table, or html in doctype
tagList += " " + tagNames[i];
Expand All @@ -477,17 +475,23 @@ private string Serialize(object[] data, string padding)
{
if (nodeValue.Trim().Length == 0)
{
if (nodeName == "?xml")
{
output += $"<{nodeName}{tagList}?>"; // ?xml has an extra ? at the end
}
else if (nodeName.ToUpper() == "!DOCTYPE")
{
output += $"<{nodeName}{tagList}>"; // doc types are self closing without the usual slash
}
else
var nodeNameLow = nodeName.ToLower();
switch (nodeNameLow)
{
output += $"<{nodeName}{tagList} />";
// ?xml has an extra ? at the end
case "?xml":
output += $"<{nodeName}{tagList}?>";
break;
// doc types are self closing without the usual slash
case "!doctype":
output += $"<{nodeName}{tagList}>";
break;
case "!--":
output += $"<{nodeName}{tagList} -->";
break;
default:
output += $"<{nodeName}{tagList} />";
break;
}
}
else
Expand Down
Loading

0 comments on commit 23c8cd7

Please sign in to comment.