November Happy Hour will be moved to Thursday December 5th.
AI OnAI Off
November Happy Hour will be moved to Thursday December 5th.
Hi Muller,
I think you are mention about XhtmlString validator, isn't it?
If yes, then I would suggest you:
That is client side, otherwise you also can listen content saving event and validate word cout for the property as well.
// Ha Bui
Hi,
For simplicity another approach would just be to go with a ValidationAttribute.
Definitely not saying this is perfect, but it should give you a start:
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public class MaxWordsAttribute : ValidationAttribute
{
private readonly int _maxWords;
public MaxWordsAttribute(int maxWords)
{
_maxWords = maxWords;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
// Allow null
if (!(value is XhtmlString xhtmlString))
{
return ValidationResult.Success;
}
var doc = new HtmlDocument();
doc.LoadHtml(xhtmlString.ToHtmlString());
var words = new List<string>();
foreach (var node in doc.DocumentNode.SelectNodes("//text()"))
{
if (string.IsNullOrEmpty(node.InnerText))
{
continue;
}
var text = HttpUtility.HtmlDecode(node.InnerText);
words.AddRange(text.Split(new[] {' ', '\r', '\n'}, StringSplitOptions.RemoveEmptyEntries)
.Where(x => !string.IsNullOrWhiteSpace(x)));
}
return words.Count <= _maxWords
? ValidationResult.Success
: new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
}
public override string FormatErrorMessage(string name)
{
if (string.IsNullOrEmpty(ErrorMessage))
{
ErrorMessage = "{0} must have {1} words or fewer.";
}
return string.Format(CultureInfo.InvariantCulture, ErrorMessageString, name, _maxWords);
}
}
You'll need to install HtmlAgilityPack as it takes advantage of it's HTML parsing functionality.
Then it's as simple as:
[MaxWords(10)]
public virtual XhtmlString MainBody { get; set; }
How to limit word count in XhtmlString property which displays in TinyMCE editor?