@Override public E asElement(N node) { return getDelegate().asElement(node); }
/** * @param index The index of the tag we are interested in. * @return The element that contains the desired tag. */ private E getNthTagNode(int index) { N node = doc.getFirstChild(doc.getDocumentElement()); for (int i = 0; i < index; i++) { if (node == null) { return null; } node = doc.getNextSibling(node); } return doc.asElement(node); }
/** * Reads the document and sets up the initial state. If a node without text * is found, it is deleted and reported as a non-fatal error. */ public void processInitialState() { List<N> emptyNodes = new ArrayList<N>(); for (N node = doc.getFirstChild(doc.getDocumentElement()); node != null; node = doc.getNextSibling(node)) { T textNode = doc.asText(doc.getFirstChild(node)); if (textNode == null) { emptyNodes.add(node); } else { notifyAddTag(doc.getData(textNode)); tagElements.add(doc.asElement(node)); } } for (N emptyNode : emptyNodes) { doc.deleteNode(doc.asElement(emptyNode)); } }
return doc.createChildElement(doc.getDocumentElement(), tag, Attributes.EMPTY_MAP); } else { E firstElement = doc.asElement(firstNode); if (firstElement == null) { throw new IllegalArgumentException("First node is not an element: " + firstNode);
/** * Deletes a tag from the document by name. * * @param tagName the name of the tag to delete. */ public void deleteTag(String tagName) { N node = doc.getFirstChild(doc.getDocumentElement()); while (node != null) { E element = doc.asElement(node); if (element != null) { T textNode = doc.asText(doc.getFirstChild(node)); if (textNode != null && doc.getData(textNode).equals(tagName)) { doc.deleteNode(element); return; } } node = doc.getNextSibling(node); } }
@Override public <N, E extends N, T extends N> void exec(MutableDocument<N, E, T> doc) { N rootThreadNode = doc.getFirstChild(doc.getDocumentElement()); E rootThread = doc.asElement(rootThreadNode); doc.createChildElement(rootThread, "blip", Collections.singletonMap( "id", idGenerator.newBlipId())); } });
@Override public <N, E extends N, T extends N> void exec(MutableDocument<N, E, T> doc) { N rootThreadNode = doc.getFirstChild(doc.getDocumentElement()); E rootThread = doc.asElement(rootThreadNode); doc.createChildElement(rootThread, "blip", Collections.singletonMap( "id", idGenerator.newBlipId())); } });
E el = doc.asElement(node); if (el == null) { if (firstLineReached != null) {
doc.insertXml(Point.<N>end(lc), content); E line = doc.asElement(doc.getFirstChild(lc)); assert line != null; if (attributes != null) {
/** * Test normalize points between an element and a text node <a>stuff</a>"hi" */ public void testNormalizePointElementFollowedByTextNode() { MutableDocument<Node, Element, Text> doc = initializeMutableDoc(); Element p = doc.asElement(doc.getFirstChild(doc.getDocumentElement())); assert p != null; Element aElement = doc.createElement(Point.start(doc, p), "a", Collections.<String, String> emptyMap()); doc.insertText(Point.start(doc, aElement), "stuff"); doc.insertText(Point.<Node> end(p), "hi"); Text hi = doc.asText(doc.getLastChild(p)); Text stuff = doc.asText(aElement.getFirstChild()); assertEquals(Point.inText(hi, 0), DocHelper.normalizePoint(Point.<Node> inText(hi, 0), doc)); assertEquals(Point.inText(hi, 0), DocHelper.normalizePoint(Point.<Node>inElement(p, hi), doc)); // In the future, we might want to move the caret out from inline elements. assertEquals(Point.inText(stuff, stuff.getLength()), DocHelper.normalizePoint(Point .<Node> inText(stuff, stuff.getLength()), doc)); }
/** * Test normalize points between an element and a text node <a>stuff</a>"hi" */ public void testNormalizePointElementFollowedByTextNode() { MutableDocument<Node, Element, Text> doc = initializeMutableDoc(); Element p = doc.asElement(doc.getFirstChild(doc.getDocumentElement())); assert p != null; Element aElement = doc.createElement(Point.start(doc, p), "a", Collections.<String, String> emptyMap()); doc.insertText(Point.start(doc, aElement), "stuff"); doc.insertText(Point.<Node> end(p), "hi"); Text hi = doc.asText(doc.getLastChild(p)); Text stuff = doc.asText(aElement.getFirstChild()); assertEquals(Point.inText(hi, 0), DocHelper.normalizePoint(Point.<Node> inText(hi, 0), doc)); assertEquals(Point.inText(hi, 0), DocHelper.normalizePoint(Point.<Node>inElement(p, hi), doc)); // In the future, we might want to move the caret out from inline elements. assertEquals(Point.inText(stuff, stuff.getLength()), DocHelper.normalizePoint(Point .<Node> inText(stuff, stuff.getLength()), doc)); }
ci(doc.asElement(top.getFirstChild())), ti(14, moreText), cd(23, 10, dl(st("p", attrs("x", "y")), tt("abc"), st("b", attrs("a", "b")),
ci(doc.asElement(top.getFirstChild())), ti(14, moreText), cd(23, 10, dl(st("p", attrs("x", "y")), tt("abc"), st("b", attrs("a", "b")),
/** * Test normalize points between text node and element "hi"<a>stuff</a> */ public void testNormalizePointTextNodeFollowedByElement() { MutableDocument<Node, Element, Text> doc = initializeMutableDoc(); Element p = doc.asElement(doc.getFirstChild(doc.getDocumentElement())); assert p != null; doc.insertText(Point.<Node> end(p), "hi"); Element aElement = doc.createElement(Point.<Node>end(p), "a", Collections.<String, String> emptyMap()); doc.insertText(Point.start(doc, aElement), "stuff"); Text hi = doc.asText(doc.getFirstChild(p)); Text stuff = doc.asText(aElement.getFirstChild()); assertEquals(Point.inText(hi, 2), DocHelper.normalizePoint(Point.<Node> inText(hi, 2), doc)); assertEquals(Point.inText(hi, 2), DocHelper.normalizePoint(Point.<Node> inElement(p, aElement), doc)); // In the future, we might want to move the caret out from inline elements. assertEquals(Point.inText(stuff, 0), DocHelper.normalizePoint(Point.<Node> inText(stuff, 0), doc)); assertEquals(Point.inText(stuff, stuff.getLength()), DocHelper.normalizePoint(Point .<Node> inText(stuff, stuff.getLength()), doc)); }
/** * Test normalize points between text node and element "hi"<a>stuff</a> */ public void testNormalizePointTextNodeFollowedByElement() { MutableDocument<Node, Element, Text> doc = initializeMutableDoc(); Element p = doc.asElement(doc.getFirstChild(doc.getDocumentElement())); assert p != null; doc.insertText(Point.<Node> end(p), "hi"); Element aElement = doc.createElement(Point.<Node>end(p), "a", Collections.<String, String> emptyMap()); doc.insertText(Point.start(doc, aElement), "stuff"); Text hi = doc.asText(doc.getFirstChild(p)); Text stuff = doc.asText(aElement.getFirstChild()); assertEquals(Point.inText(hi, 2), DocHelper.normalizePoint(Point.<Node> inText(hi, 2), doc)); assertEquals(Point.inText(hi, 2), DocHelper.normalizePoint(Point.<Node> inElement(p, aElement), doc)); // In the future, we might want to move the caret out from inline elements. assertEquals(Point.inText(stuff, 0), DocHelper.normalizePoint(Point.<Node> inText(stuff, 0), doc)); assertEquals(Point.inText(stuff, stuff.getLength()), DocHelper.normalizePoint(Point .<Node> inText(stuff, stuff.getLength()), doc)); }
@SuppressWarnings("unchecked") public void doTestInserts() throws OperationException { String a = "<p>abc<b>def</b></p>"; create(a); Element firstEl = (Element) doc.getFirstChild(doc.getDocumentElement()); Builder b = at(0); Attributes attrs = attrs("x", "1", "y", "2"); b.replaceAttributes(attrs); b.elementStart("x", attrs("a", "1")); b.characters("hello"); b.elementStart("y", attrs("b", "2", "c", "3")); b.characters("yeah"); b.elementEnd(); b.elementEnd(); String moreText = "more text"; b.characters(moreText); consumeNindo(b.build()); checkEvents( am(firstEl, pairs("x", null, "y", null), attrs), ci(doc.asElement(firstEl.getFirstChild())), ti(14, moreText)); }
@SuppressWarnings("unchecked") public void doTestInserts() throws OperationException { String a = "<p>abc<b>def</b></p>"; create(a); Element firstEl = (Element) doc.getFirstChild(doc.getDocumentElement()); Builder b = at(0); Attributes attrs = attrs("x", "1", "y", "2"); b.replaceAttributes(attrs); b.elementStart("x", attrs("a", "1")); b.characters("hello"); b.elementStart("y", attrs("b", "2", "c", "3")); b.characters("yeah"); b.elementEnd(); b.elementEnd(); String moreText = "more text"; b.characters(moreText); consumeNindo(b.build()); checkEvents( am(firstEl, pairs("x", null, "y", null), attrs), ci(doc.asElement(firstEl.getFirstChild())), ti(14, moreText)); }
/** * Test normalize point between two text nodes, i.e. "hello""world" */ public void testNormalizePointBetweenTwoTextNodes() { MutableDocument<Node, Element, Text> doc = initializeMutableDoc(); Element p = doc.asElement(doc.getFirstChild(doc.getDocumentElement())); assert p != null; doc.insertText(Point.<Node> end(p), "hello"); insertTextInNewTextNodeHelper(doc, Point.<Node> end(p), "world"); Text world = doc.asText(doc.getLastChild(p)); Text hello = doc.asText(doc.getFirstChild(p)); assertEquals(Point.inText(hello, hello.getLength()), DocHelper.normalizePoint(Point .<Node> inText(world, 0), doc)); assertEquals(Point.<Node> inText(world, 1), DocHelper.normalizePoint(Point.<Node> inText(world, 1), doc)); assertEquals(Point.<Node> inText(world, 2), DocHelper.normalizePoint(Point.<Node> inText(world, 2), doc)); assertEquals(Point.<Node> inText(hello, 5), DocHelper.normalizePoint(Point.<Node> inText(hello, 5), doc)); assertEquals(Point.<Node> inText(hello, 4), DocHelper.normalizePoint(Point.<Node> inText(hello, 4), doc)); }
/** * Test normalize point between two text nodes, i.e. "hello""world" */ public void testNormalizePointBetweenTwoTextNodes() { MutableDocument<Node, Element, Text> doc = initializeMutableDoc(); Element p = doc.asElement(doc.getFirstChild(doc.getDocumentElement())); assert p != null; doc.insertText(Point.<Node> end(p), "hello"); insertTextInNewTextNodeHelper(doc, Point.<Node> end(p), "world"); Text world = doc.asText(doc.getLastChild(p)); Text hello = doc.asText(doc.getFirstChild(p)); assertEquals(Point.inText(hello, hello.getLength()), DocHelper.normalizePoint(Point .<Node> inText(world, 0), doc)); assertEquals(Point.<Node> inText(world, 1), DocHelper.normalizePoint(Point.<Node> inText(world, 1), doc)); assertEquals(Point.<Node> inText(world, 2), DocHelper.normalizePoint(Point.<Node> inText(world, 2), doc)); assertEquals(Point.<Node> inText(hello, 5), DocHelper.normalizePoint(Point.<Node> inText(hello, 5), doc)); assertEquals(Point.<Node> inText(hello, 4), DocHelper.normalizePoint(Point.<Node> inText(hello, 4), doc)); }