private void TextHandler(MarkupParsingContext context, string text)
{
HtmlNodeType previousNodeType = _currentNodeType;
HtmlTag tag = _currentTag ?? HtmlTag.Empty;
string tagNameInLowercase = tag.NameInLowercase;
HtmlTagFlags tagFlags = tag.Flags;
_currentNodeType = HtmlNodeType.Text;
WhitespaceMinificationMode whitespaceMinificationMode = _settings.WhitespaceMinificationMode;
if (whitespaceMinificationMode != WhitespaceMinificationMode.None)
{
if (_tagsWithNotRemovableWhitespaceQueue.Count == 0)
{
if (context.Position == 0)
{
// Processing of starting whitespace
text = text.TrimStart();
}
else if (context.Position + text.Length == context.Length)
{
// Processing of ending whitespace
text = text.TrimEnd();
}
else if (previousNodeType == HtmlNodeType.StartTag)
{
// Processing of whitespace, that followed after the start tag
bool allowTrimStart = false;
if (tagFlags.HasFlag(HtmlTagFlags.Invisible)
|| (tagFlags.HasFlag(HtmlTagFlags.NonIndependent) && tagFlags.HasFlag(HtmlTagFlags.Empty)))
{
allowTrimStart = true;
}
else
{
if (whitespaceMinificationMode == WhitespaceMinificationMode.Medium)
{
allowTrimStart = tagFlags.HasFlag(HtmlTagFlags.Block);
}
else if (whitespaceMinificationMode == WhitespaceMinificationMode.Aggressive)
{
allowTrimStart = tagFlags.HasFlag(HtmlTagFlags.Block)
|| ((tagFlags.HasFlag(HtmlTagFlags.Inline) || tagFlags.HasFlag(HtmlTagFlags.InlineBlock))
&& !tagFlags.HasFlag(HtmlTagFlags.Empty));
}
}
if (allowTrimStart)
{
text = text.TrimStart();
}
}
else if (previousNodeType == HtmlNodeType.EndTag)
{
// Processing of whitespace, that followed after the end tag
bool allowTrimStart = false;
if (tagFlags.HasFlag(HtmlTagFlags.Invisible)
|| (tagFlags.HasFlag(HtmlTagFlags.NonIndependent)
&& CanRemoveWhitespaceAfterEndNonIndependentTag(tag)))
{
allowTrimStart = true;
}
else
{
if (whitespaceMinificationMode == WhitespaceMinificationMode.Medium
|| whitespaceMinificationMode == WhitespaceMinificationMode.Aggressive)
{
allowTrimStart = tagFlags.HasFlag(HtmlTagFlags.Block);
}
}
if (allowTrimStart)
{
text = text.TrimStart();
}
}
else if (previousNodeType == HtmlNodeType.Doctype || previousNodeType == HtmlNodeType.XmlDeclaration)
{
// Processing of whitespace, that followed after the document type declaration
// or XML declaration
text = text.TrimStart();
}
if (text.Length > 0
&& !(tagFlags.HasFlag(HtmlTagFlags.Xml) && tagFlags.HasFlag(HtmlTagFlags.NonIndependent)))
{
text = Utils.CollapseWhitespace(text);
}
}
else if (previousNodeType == HtmlNodeType.StartTag && tagNameInLowercase == "textarea"
&& string.IsNullOrWhiteSpace(text))
{
text = string.Empty;
}
}
if (text.Length > 0)
{
_buffer.Add(text);
}
else
{
_currentNodeType = previousNodeType;
}
_currentText = text;
}