Added TruncateByWords method + overloads

This commit is contained in:
Robert
2017-07-12 10:35:09 +02:00
parent 1d3279fe07
commit 769f0d97d2
2 changed files with 122 additions and 8 deletions

View File

@@ -240,5 +240,39 @@ namespace Umbraco.Web
}
}
}
/// <summary>
/// Returns the length of the words from a html block
/// </summary>
/// <param name="html">Html text</param>
/// <param name="words">Amount of words you would like to measure</param>
/// <returns></returns>
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;
}
}
}

View File

@@ -1457,18 +1457,98 @@ namespace Umbraco.Web
/// </summary>
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);
}
/// <summary>
/// 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
/// </summary>
public IHtmlString TruncateByWords(DynamicNull html, int words, bool addElipsis)
{
return new HtmlString(string.Empty);
}
#endregion
/// <summary>
/// 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
/// </summary>
public IHtmlString TruncateByWords(DynamicNull html, int words, bool addElipsis, bool treatTagsAsContent)
{
return new HtmlString(string.Empty);
}
#region If
/// <summary>
/// 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
/// </summary>
public IHtmlString TruncateByWords(string html, int words)
{
int length = _stringUtilities.WordsToLength(html, words);
/// <summary>
/// If the test is true, the string valueIfTrue will be returned, otherwise the valueIfFalse will be returned.
/// </summary>
public HtmlString If(bool test, string valueIfTrue, string valueIfFalse)
return Truncate(html, length, true, false);
}
/// <summary>
/// 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
/// </summary>
public IHtmlString TruncateByWords(string html, int words, bool addElipsis)
{
int length = _stringUtilities.WordsToLength(html, words);
return Truncate(html, length, addElipsis, false);
}
/// <summary>
/// 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
/// </summary>
public IHtmlString TruncateByWords(string html, int words, bool addElipsis, bool treatTagsAsContent)
{
int length =_stringUtilities.WordsToLength(html, words);
return Truncate(html, length, addElipsis, treatTagsAsContent);
}
/// <summary>
/// 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
/// </summary>
public IHtmlString TruncateByWords(IHtmlString html, int words)
{
int length = _stringUtilities.WordsToLength(html.ToHtmlString(), words);
return Truncate(html, length, true, false);
}
/// <summary>
/// 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
/// </summary>
public IHtmlString TruncateByWords(IHtmlString html, int words, bool addElipsis)
{
int length = _stringUtilities.WordsToLength(html.ToHtmlString(), words);
return Truncate(html, length, addElipsis, false);
}
/// <summary>
/// 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
/// </summary>
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
/// <summary>
/// If the test is true, the string valueIfTrue will be returned, otherwise the valueIfFalse will be returned.
/// </summary>
public HtmlString If(bool test, string valueIfTrue, string valueIfFalse)
{
return test ? new HtmlString(valueIfTrue) : new HtmlString(valueIfFalse);
}