protected virtual void CaseCData(HtmlReader htmlReader, TextWriter result) { if (_enabled) { WriteCData(result, htmlReader.Value); } }
protected virtual void CaseText(HtmlReader htmlReader, TextWriter result) { if (_enabled) { WriteString(result, htmlReader.Value); } }
protected virtual void CaseElement(HtmlReader htmlReader, TextWriter result) { string tagName = htmlReader.Name.ToLowerInvariant(); // automatically close unclosed tag, if any if (AutoClosedTags.Contains(tagName)) { if (FuzzyPopTag(tagName)) { WriteEndElement(result, tagName); } } // reevaluate tags stack PushTag(tagName); CheckStack(); if (_enabled && !IgnoreTags.Contains(tagName)) { WriteElement(result, tagName, htmlReader.Attributes, htmlReader.IsEmptyElement); } // pop empty tags immediately if (htmlReader.IsEmptyElement || EmptyTags.Contains(tagName)) { if (FuzzyPopTag(tagName)) { CheckStack(); } } }
protected virtual void CaseEndElement(HtmlReader htmlReader, TextWriter result) { string tagName = htmlReader.Name.ToLowerInvariant(); if (FuzzyPopTag(tagName)) { // only close tag if it was previously open if (_enabled && !IgnoreTags.Contains(tagName)) { WriteEndElement(result, tagName); } CheckStack(); } }
public void Sanitize(TextReader input, TextWriter result) { if (input == null) { throw new ArgumentNullException("input"); } if (result == null) { throw new ArgumentNullException("result"); } using (var htmlReader = new HtmlReader(input)) { Sanitize(htmlReader, result); } Reset(); }
protected virtual void Sanitize(HtmlReader htmlReader, TextWriter result) { Before(result); while (htmlReader.Read()) { switch (htmlReader.NodeType) { case HtmlNodeType.Element: CaseElement(htmlReader, result); break; case HtmlNodeType.EndElement: CaseEndElement(htmlReader, result); break; case HtmlNodeType.Text: CaseText(htmlReader, result); break; case HtmlNodeType.CDATA: CaseCData(htmlReader, result); break; } } // close unclosed tags while (_tags.Count > 0) { string tagName = PopTag(); CheckStack(); if (_enabled && !IgnoreTags.Contains(tagName)) { WriteEndElement(result, tagName); } } After(result); result.Flush(); }