Skip to content

Commit

Permalink
Fix bug that could not handle a text which ends with close tag
Browse files Browse the repository at this point in the history
  • Loading branch information
majecty committed Nov 18, 2018
1 parent f877505 commit 0bb1a13
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
21 changes: 17 additions & 4 deletions Assets/RichTextHelper/RichTextHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ public static int RichTextLength(this string text)
var length = 0;
while (m.IsConsumable())
{
m.Consume();
length += 1;
if (m.Consume())
{
length += 1;
}
}
return length;
}
Expand Down Expand Up @@ -97,7 +99,9 @@ public bool IsConsumable()
return consumedLength < originalText.Length;
}

public void Consume()
// Return if real character is consumed
// If line's last part is closing tag, this function return false when consumed last closing tag.
public bool Consume()
{
Debug.Assert(IsConsumable());
var peekedOriginChar = PeekNextOriginChar();
Expand All @@ -111,11 +115,20 @@ public void Consume()
{
ConsumeStartTag();
}
Consume();
if (IsConsumable())
{
Consume();
return true;
}
else
{
return false;
}
}
else
{
ConsumeRawChar();
return true;
}
}

Expand Down
11 changes: 11 additions & 0 deletions Assets/test/RichTextHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,15 @@ public void NestedTest()
Assert.AreEqual(sampleTagText.RichTextSubString(3), "<color=#000>a<i>b<b>c</b></i></color>");
Assert.AreEqual(sampleTagText.RichTextSubString(4), "<color=#000>a<i>b<b>c</b>d</i></color>");
}

[Test]
public void CloseTagIsLast()
{
var sampleTagText = "<color=#000>a<i>b<b>c</b>d</i></color>";
Assert.AreEqual(sampleTagText.RichTextSubString(5), sampleTagText);
Assert.AreEqual(sampleTagText.RichTextSubString(1), "<color=#000>a</color>");
Assert.AreEqual(sampleTagText.RichTextSubString(2), "<color=#000>a<i>b</i></color>");
Assert.AreEqual(sampleTagText.RichTextSubString(3), "<color=#000>a<i>b<b>c</b></i></color>");
Assert.AreEqual(sampleTagText.RichTextSubString(4), "<color=#000>a<i>b<b>c</b>d</i></color>");
}
}

0 comments on commit 0bb1a13

Please sign in to comment.