Manually applying change from U4-10478 7.7.1 HtmlStringUtilities Truncate throws an IndexOutOfRange when input word is shorter than "…" and crash the entire site to 7.6.9 too.

This commit is contained in:
Sebastiaan Janssen
2017-10-01 12:05:40 +02:00
parent 33d7598d3c
commit 581f15b172
2 changed files with 39 additions and 2 deletions

View File

@@ -22,5 +22,38 @@ namespace Umbraco.Tests.Web.Mvc
var expected = "<div><h1>hello world</h1><p>hello world<br />hello world<br />hello world<br />hello world</p></div>";
Assert.AreEqual(expected, output);
}
[Test]
public void TruncateWithElipsis()
{
var output = _htmlStringUtilities.Truncate("hello world", 5, true, false).ToString();
var expected = "hello&hellip;";
Assert.AreEqual(expected, output);
}
[Test]
public void TruncateWithoutElipsis()
{
var output = _htmlStringUtilities.Truncate("hello world", 5, false, false).ToString();
var expected = "hello";
Assert.AreEqual(expected, output);
}
[Test]
public void TruncateShorterWordThanHellip()
{
//http://issues.umbraco.org/issue/U4-10478
var output = _htmlStringUtilities.Truncate("hi", 5, true, false).ToString();
var expected = "hi";
Assert.AreEqual(expected, output);
}
[Test]
public void TruncateAndRemoveSpaceBetweenHellipAndWord()
{
var output = _htmlStringUtilities.Truncate("hello world", 6 /* hello plus space */, true, false).ToString();
var expected = "hello&hellip;";
Assert.AreEqual(expected, output);
}
}
}

View File

@@ -92,6 +92,7 @@ namespace Umbraco.Web
using (var outputms = new MemoryStream())
{
bool lengthReached = false;
using (var outputtw = new StreamWriter(outputms))
{
using (var ms = new MemoryStream())
@@ -106,7 +107,6 @@ namespace Umbraco.Web
using (TextReader tr = new StreamReader(ms))
{
bool isInsideElement = false,
lengthReached = false,
insideTagSpaceEncountered = false,
isTagClose = false;
@@ -254,10 +254,14 @@ namespace Umbraco.Web
//Check to see if there is an empty char between the hellip and the output string
//if there is, remove it
if (string.IsNullOrWhiteSpace(firstTrim) == false)
if (addElipsis && lengthReached && string.IsNullOrWhiteSpace(firstTrim) == false)
{
result = firstTrim[firstTrim.Length - hellip.Length - 1] == ' ' ? firstTrim.Remove(firstTrim.Length - hellip.Length - 1, 1) : firstTrim;
}
else
{
result = firstTrim;
}
return new HtmlString(result);
}
}