diff --git a/src/Umbraco.Web/HtmlStringUtilities.cs b/src/Umbraco.Web/HtmlStringUtilities.cs
index 24a643b5b0..778bc2b717 100644
--- a/src/Umbraco.Web/HtmlStringUtilities.cs
+++ b/src/Umbraco.Web/HtmlStringUtilities.cs
@@ -240,5 +240,39 @@ namespace Umbraco.Web
}
}
}
+
+ ///
+ /// Returns the length of the words from a html block
+ ///
+ /// Html text
+ /// Amount of words you would like to measure
+ ///
+ public int WordsToLength(string html, int words)
+ {
+ int wordCount = 0;
+ int length = 0;
+ int maxWords = words;
+
+ while (length < html.Length)
+ {
+ // Check to see if the current wordCount reached the maxWords allowed
+ if (wordCount.Equals(maxWords)) break;
+ // Check if current char is part of a word
+ while (length < html.Length && char.IsWhiteSpace(html[length]) == false)
+ {
+ length++;
+ }
+
+ wordCount++;
+
+ // Skip whitespace until the next word
+ while (length < html.Length && char.IsWhiteSpace(html[length]))
+ {
+ length++;
+ }
+ }
+
+ return length;
+ }
}
}
\ No newline at end of file
diff --git a/src/Umbraco.Web/UmbracoHelper.cs b/src/Umbraco.Web/UmbracoHelper.cs
index 6f3da17254..33fa907e11 100644
--- a/src/Umbraco.Web/UmbracoHelper.cs
+++ b/src/Umbraco.Web/UmbracoHelper.cs
@@ -1457,18 +1457,98 @@ namespace Umbraco.Web
///
public IHtmlString Truncate(string html, int length, bool addElipsis, bool treatTagsAsContent)
{
- return _stringUtilities.Truncate(html, length, addElipsis, treatTagsAsContent);
- }
+ return _stringUtilities.Truncate(html, length, addElipsis, treatTagsAsContent);
+ }
+ #region Truncate by Words
+ public IHtmlString TruncateByWords(DynamicNull html, int words)
+ {
+ return new HtmlString(string.Empty);
+ }
+ ///
+ /// Truncates a string to a given amount of words, can add a elipsis at the end (...). Method checks for open html tags, and makes sure to close them
+ ///
+ public IHtmlString TruncateByWords(DynamicNull html, int words, bool addElipsis)
+ {
+ return new HtmlString(string.Empty);
+ }
- #endregion
+ ///
+ /// Truncates a string to a given amount of words, can add a elipsis at the end (...). Method checks for open html tags, and makes sure to close them
+ ///
+ public IHtmlString TruncateByWords(DynamicNull html, int words, bool addElipsis, bool treatTagsAsContent)
+ {
+ return new HtmlString(string.Empty);
+ }
- #region If
+ ///
+ /// Truncates a string to a given amount of words, can add a elipsis at the end (...). Method checks for open html tags, and makes sure to close them
+ ///
+ public IHtmlString TruncateByWords(string html, int words)
+ {
+ int length = _stringUtilities.WordsToLength(html, words);
- ///
- /// If the test is true, the string valueIfTrue will be returned, otherwise the valueIfFalse will be returned.
- ///
- public HtmlString If(bool test, string valueIfTrue, string valueIfFalse)
+ return Truncate(html, length, true, false);
+ }
+
+ ///
+ /// Truncates a string to a given amount of words, can add a elipsis at the end (...). Method checks for open html tags, and makes sure to close them
+ ///
+ public IHtmlString TruncateByWords(string html, int words, bool addElipsis)
+ {
+ int length = _stringUtilities.WordsToLength(html, words);
+
+ return Truncate(html, length, addElipsis, false);
+ }
+
+ ///
+ /// Truncates a string to a given amount of words, can add a elipsis at the end (...). Method checks for open html tags, and makes sure to close them
+ ///
+ public IHtmlString TruncateByWords(string html, int words, bool addElipsis, bool treatTagsAsContent)
+ {
+ int length =_stringUtilities.WordsToLength(html, words);
+
+ return Truncate(html, length, addElipsis, treatTagsAsContent);
+ }
+
+ ///
+ /// Truncates a string to a given amount of words, can add a elipsis at the end (...). Method checks for open html tags, and makes sure to close them
+ ///
+ public IHtmlString TruncateByWords(IHtmlString html, int words)
+ {
+ int length = _stringUtilities.WordsToLength(html.ToHtmlString(), words);
+
+ return Truncate(html, length, true, false);
+ }
+
+ ///
+ /// Truncates a string to a given amount of words, can add a elipsis at the end (...). Method checks for open html tags, and makes sure to close them
+ ///
+ public IHtmlString TruncateByWords(IHtmlString html, int words, bool addElipsis)
+ {
+ int length = _stringUtilities.WordsToLength(html.ToHtmlString(), words);
+
+ return Truncate(html, length, addElipsis, false);
+ }
+
+ ///
+ /// Truncates a string to a given amount of words, can add a elipsis at the end (...). Method checks for open html tags, and makes sure to close them
+ ///
+ public IHtmlString TruncateByWords(IHtmlString html, int words, bool addElipsis, bool treatTagsAsContent)
+ {
+ int length = _stringUtilities.WordsToLength(html.ToHtmlString(), words);
+
+ return Truncate(html, length, addElipsis, treatTagsAsContent);
+ }
+ #endregion
+ #endregion
+
+ #region If
+
+ ///
+ /// If the test is true, the string valueIfTrue will be returned, otherwise the valueIfFalse will be returned.
+ ///
+ public HtmlString If(bool test, string valueIfTrue, string valueIfFalse)
{
return test ? new HtmlString(valueIfTrue) : new HtmlString(valueIfFalse);
}