From 84f684ed3125e9f6df9cb8ba647acff27cd95c6f Mon Sep 17 00:00:00 2001 From: Robert Date: Fri, 25 Aug 2017 11:39:13 +0200 Subject: [PATCH] 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) --- .../FrontEnd/UmbracoHelperTests.cs | 47 ++++++++++++++++--- src/Umbraco.Web/HtmlStringUtilities.cs | 18 ++++++- 2 files changed, 58 insertions(+), 7 deletions(-) diff --git a/src/Umbraco.Tests/FrontEnd/UmbracoHelperTests.cs b/src/Umbraco.Tests/FrontEnd/UmbracoHelperTests.cs index d886009e00..1e808ab8b2 100644 --- a/src/Umbraco.Tests/FrontEnd/UmbracoHelperTests.cs +++ b/src/Umbraco.Tests/FrontEnd/UmbracoHelperTests.cs @@ -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 with a link"; 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…", result); } [Test] - public void Truncate_By_Words_With_Tag() + public void Truncate_By_Words_With_Tag_TagsAsContentOff() { var text = "Hello world, this is some text with a link"; 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…", 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 with a link"; 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 with…", result); + } + [Test] + public void Truncate_By_Words_TagsAsContentOn() + { + var text = "Hello world, this is some text with a link"; + + var helper = new UmbracoHelper(); + + var result = helper.TruncateByWords(text, 4, true, true).ToString(); + + Assert.AreEqual("Hello world, this is…", result); + } + + [Test] + public void Truncate_By_Words_With_Tag_TagsAsContentOn() + { + var text = "Hello world, this is some text with a link"; + + var helper = new UmbracoHelper(); + + var result = helper.TruncateByWords(text, 4, true, true).ToString(); + + Assert.AreEqual("Hello world, this is…", result); + } + + [Test] + public void Truncate_By_Words_Mid_Tag_TagsAsContentOn() + { + var text = "Hello world, this is some text with a link"; + + var helper = new UmbracoHelper(); + + var result = helper.TruncateByWords(text, 7, true, true).ToString(); Assert.AreEqual("Hello world, this is some text with…", result); } diff --git a/src/Umbraco.Web/HtmlStringUtilities.cs b/src/Umbraco.Web/HtmlStringUtilities.cs index 08e73e389d..166ba44667 100644 --- a/src/Umbraco.Web/HtmlStringUtilities.cs +++ b/src/Umbraco.Web/HtmlStringUtilities.cs @@ -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(""); + 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); } } }