Fix parsing html tags with starting with slash

This commit is contained in:
Wojciech Figat
2022-08-03 10:15:42 +02:00
parent e59de73948
commit dbd48ac5b4
2 changed files with 46 additions and 13 deletions

View File

@@ -33,10 +33,20 @@ namespace FlaxEngine.Utilities
/// </summary>
public Dictionary<string, string> Attributes;
/// <summary>
/// True if this tag contained a leading forward slash (begin of the tag).
/// </summary>
public bool IsLeadingSlash;
/// <summary>
/// True if this tag contained a trailing forward slash (end of the tag).
/// </summary>
public bool IsTrailingSlash;
public bool IsEndingSlash;
/// <summary>
/// True if this tag contained a leading or trailing forward slash.
/// </summary>
public bool IsSlash => IsLeadingSlash || IsEndingSlash;
};
/// <summary>
@@ -102,7 +112,6 @@ namespace FlaxEngine.Utilities
// Skip opening '<'
Move();
// Examine first tag character
char c = Peek();
if (c == '!' && Peek(1) == '-' && Peek(2) == '-')
{
@@ -112,15 +121,13 @@ namespace FlaxEngine.Utilities
NormalizePosition();
Move(endComment.Length);
}
else if (c == '/')
{
// Skip over closing tags
_pos = _html.IndexOf('>', _pos);
NormalizePosition();
Move();
}
else
{
// Skip leading slash
bool isLeadingSlash = c == '/';
if (isLeadingSlash)
Move();
// Parse tag
bool result = ParseTag(ref tag, name);
@@ -136,9 +143,16 @@ namespace FlaxEngine.Utilities
Move();
}
// Return true if requested tag was found
if (result)
{
if (isLeadingSlash)
{
// Tag starts with '/'
tag.StartPosition--;
tag.IsLeadingSlash = true;
}
return true;
}
}
}
@@ -188,7 +202,7 @@ namespace FlaxEngine.Utilities
{
// Handle trailing forward slash
if (requested)
tag.IsTrailingSlash = true;
tag.IsEndingSlash = true;
Move();
SkipWhitespace();
@@ -250,7 +264,7 @@ namespace FlaxEngine.Utilities
private string ParseAttributeName()
{
int start = _pos;
while (!EOF && !char.IsWhiteSpace(Peek()) && Peek() != '>' && Peek() != '=')
while (!EOF && char.IsLetterOrDigit(Peek()))
Move();
return _html.Substring(start, _pos - start);
}
@@ -284,7 +298,7 @@ namespace FlaxEngine.Utilities
{
// Parse unquoted value
start = _pos;
while (!EOF && !char.IsWhiteSpace(c) && c != '>')
while (!EOF && !char.IsWhiteSpace(c) && c != '>' && c != '/')
{
Move();
c = Peek();

View File

@@ -68,8 +68,27 @@ namespace FlaxEngine.Tests
return result;
}
[TestCase("b<a>", ExpectedResult = false)]
[TestCase("b<a/>", ExpectedResult = true)]
[TestCase("b<a />", ExpectedResult = true)]
[TestCase("b<a />", ExpectedResult = true)]
[TestCase("b<a size=50>", ExpectedResult = false)]
[TestCase("b<a size=50/>", ExpectedResult = true)]
[TestCase("b</a>", ExpectedResult = true)]
public bool TestEnding(string html)
{
var parser = new HtmlParser(html);
while (parser.ParseNext(out var tag))
{
return tag.IsSlash;
}
throw new System.Exception();
}
[TestCase("b<a size=50>", ExpectedResult = 1)]
[TestCase("sd b <a>", ExpectedResult = 5)]
[TestCase("sd b </a>", ExpectedResult = 5)]
[TestCase("sd b <a/>", ExpectedResult = 5)]
public int TestStartPosition(string html)
{
var parser = new HtmlParser(html);