Truncate with trimming should now work, added some mpore tests for truncate by words methods

Fix: Original Truncate method show now be able to trim
Add: Added some more tests for Truncate by words to cater for tagsAsContent parameter
Fix: In original truncate, currentTextLength prop will be increased everytime a tag is added, this will fix an issue when tagsAsContent is set to true (it would have added an extra char per tag, which would have went over to the next word)
This commit is contained in:
Robert
2017-08-25 11:39:13 +02:00
parent 54f86c06f0
commit 84f684ed31
2 changed files with 58 additions and 7 deletions

View File

@@ -95,37 +95,72 @@ namespace Umbraco.Tests.FrontEnd
}
[Test]
public void Truncate_By_Words()
public void Truncate_By_Words_TagsAsContentOff()
{
var text = "Hello world, this is some text <a href='blah'>with a link</a>";
var helper = new UmbracoHelper();
var result = helper.TruncateByWords(text, 4).ToString();
var result = helper.TruncateByWords(text, 4, true, false).ToString();
Assert.AreEqual("Hello world, this is&hellip;", result);
}
[Test]
public void Truncate_By_Words_With_Tag()
public void Truncate_By_Words_With_Tag_TagsAsContentOff()
{
var text = "Hello world, <b>this</b> is some text <a href='blah'>with a link</a>";
var helper = new UmbracoHelper();
var result = helper.TruncateByWords(text, 4).ToString();
var result = helper.TruncateByWords(text, 4, true, false).ToString();
Assert.AreEqual("Hello world, <b>this</b> is&hellip;", result);
}
[Test]
public void Truncate_By_Words_Mid_Tag()
public void Truncate_By_Words_Mid_Tag_TagsAsContentOff()
{
var text = "Hello world, this is some text <a href='blah'>with a link</a>";
var helper = new UmbracoHelper();
var result = helper.TruncateByWords(text, 7).ToString();
var result = helper.TruncateByWords(text, 7, true, false).ToString();
Assert.AreEqual("Hello world, this is some text <a href='blah'>with&hellip;</a>", result);
}
[Test]
public void Truncate_By_Words_TagsAsContentOn()
{
var text = "Hello world, this is some text <a href='blah'>with a link</a>";
var helper = new UmbracoHelper();
var result = helper.TruncateByWords(text, 4, true, true).ToString();
Assert.AreEqual("Hello world, this is&hellip;", result);
}
[Test]
public void Truncate_By_Words_With_Tag_TagsAsContentOn()
{
var text = "Hello world, <b>this</b> is some text <a href='blah'>with a link</a>";
var helper = new UmbracoHelper();
var result = helper.TruncateByWords(text, 4, true, true).ToString();
Assert.AreEqual("Hello world, <b>this</b> is&hellip;", result);
}
[Test]
public void Truncate_By_Words_Mid_Tag_TagsAsContentOn()
{
var text = "Hello world, this is some text <a href='blah'>with a link</a>";
var helper = new UmbracoHelper();
var result = helper.TruncateByWords(text, 7, true, true).ToString();
Assert.AreEqual("Hello world, this is some text <a href='blah'>with&hellip;</a>", result);
}

View File

@@ -6,6 +6,7 @@ using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using HtmlAgilityPack;
using Umbraco.Web.WebApi.Filters;
namespace Umbraco.Web
{
@@ -142,6 +143,10 @@ namespace Umbraco.Web
{
string thisTag = tagStack.Pop();
outputtw.Write("</" + thisTag + ">");
if (treatTagsAsContent)
{
currentTextLength++;
}
}
if (!isTagClose && currentTag.Length > 0)
{
@@ -149,6 +154,10 @@ namespace Umbraco.Web
{
tagStack.Push(currentTag);
outputtw.Write("<" + currentTag);
if (treatTagsAsContent)
{
currentTextLength++;
}
if (!string.IsNullOrEmpty(tagContents))
{
if (tagContents.EndsWith("/"))
@@ -236,7 +245,14 @@ namespace Umbraco.Web
outputms.Position = 0;
using (TextReader outputtr = new StreamReader(outputms))
{
return new HtmlString(outputtr.ReadToEnd().Replace(" ", " ").Trim());
string result = String.Empty;
string firsTrim = outputtr.ReadToEnd().Replace(" ", " ").Trim();
//Check to see if there is an empty char between the hellip and the output string
//if there is, remove it
result = firsTrim[firsTrim.Length -9] == ' ' ? firsTrim.Remove(firsTrim.Length - 9, 1) : firsTrim;
return new HtmlString(result);
}
}
}