public void testBlank() { TextNode one = new TextNode("", ""); TextNode two = new TextNode(" ", ""); TextNode three = new TextNode(" \n\n ", ""); TextNode four = new TextNode("Hello", ""); TextNode five = new TextNode(" \nHello ", ""); Assert.IsTrue(one.IsBlank); Assert.IsTrue(two.IsBlank); Assert.IsTrue(three.IsBlank); Assert.IsFalse(four.IsBlank); Assert.IsFalse(five.IsBlank); }
public void Insert(Token.Character characterToken) { Node node = new TextNode(characterToken.Data.ToString(), _baseUri); InsertNode(node); }
public void Insert(Token.Character characterToken) { Node node; // characters in script and style go in as datanodes, not text nodes if (StringUtil.In(CurrentElement.TagName(), "script", "style")) { node = new DataNode(characterToken.Data.ToString(), _baseUri); } else { node = new TextNode(characterToken.Data.ToString(), _baseUri); } CurrentElement.AppendChild(node); // doesn't use insertNode, because we don't foster these; and will always have a stack. }
/// <summary> /// Split this text node into two nodes at the specified string offset. After splitting, this node will contain the /// original text up to the offset, and will have a new text node sibling containing the text after the offset. /// </summary> /// <param name="offset">string offset point to split node at.</param> /// <returns>the newly created text node containing the text after the offset.</returns> public TextNode SplitText(int offset) { if (offset < 0) { throw new ArgumentOutOfRangeException("offset", "Split offset must be not be negative"); } if (offset > text.Length) { throw new ArgumentOutOfRangeException("offset", "Split offset must not be greater than current text length"); } string head = GetWholeText().Substring(0, offset); string tail = GetWholeText().Substring(offset); Text(head); TextNode tailNode = new TextNode(tail, this.BaseUri); if (ParentNode != null) { ParentNode.AddChildren(SiblingIndex + 1, tailNode); } return tailNode; }
/// <summary> /// Iterates the input and copies trusted nodes (tags, attributes, text) into the destination. /// </summary> /// <param name="source">source of HTML</param> /// <param name="dest">destination element to copy into</param> /// <returns>number of discarded elements (that were considered unsafe)</returns> private int CopySafeNodes(Element source, Element dest) { IList<Node> sourceChildren = source.ChildNodes; int numDiscarded = 0; foreach (Node sourceChild in sourceChildren) { if (sourceChild is Element) { Element sourceEl = (Element)sourceChild; if (_whitelist.IsSafeTag(sourceEl.TagName())) { // safe, clone and copy safe attrs ElementMeta meta = CreateSafeElement(sourceEl); Element destChild = meta.Element; dest.AppendChild(destChild); numDiscarded += meta.NumAttributesDiscarded; numDiscarded += CopySafeNodes(sourceEl, destChild); // recurs } else { // not a safe tag, but it may have children (els or text) that are, so recurse numDiscarded++; numDiscarded += CopySafeNodes(sourceEl, dest); } } else if (sourceChild is TextNode) { TextNode sourceText = (TextNode)sourceChild; TextNode destText = new TextNode(sourceText.GetWholeText(), sourceChild.BaseUri); dest.AppendChild(destText); } // else, we don't care about comments, xml proc instructions, etc } return numDiscarded; }