@Override public N getFirstChild(N node) { return getDelegate().getFirstChild(node); }
/** * Called when an element has been inserted into the document. * * @param e The inserted element. */ public void handleInsertedElement(E e) { tagElements.add(e); String addedTagText = doc.getData(doc.asText(doc.getFirstChild(e))); notifyAddTag(addedTagText); }
/** * 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)); } }
/** * @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); }
/** * 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())); } });
public void testGetOrCreateSucceedsOnOldEmptyDocument() { MutableDocument<Node, Element, Text> doc = getDoc(""); Element top = DocHelper.getOrCreateFirstTopLevelElement(doc, "foo"); assertEquals(doc.getFirstChild(doc.getDocumentElement()), top); }
public void testGetOrCreateSucceedsOnOldEmptyDocument() { MutableDocument<Node, Element, Text> doc = getDoc(""); Element top = DocHelper.getOrCreateFirstTopLevelElement(doc, "foo"); assertEquals(doc.getFirstChild(doc.getDocumentElement()), top); }
public <N, E extends N, T extends N> void exec(MutableDocument<N, E, T> doc) { Point<N> startText = doc.locate(locateAfterLineElement(doc)); doc.insertText(startText, "cd"); // Insert reply between c|d. N bodyNode = DocHelper.getElementWithTagName(doc, Blips.BODY_TAGNAME); N textNode = doc.getFirstChild(bodyNode); textNode = doc.getNextSibling(textNode); int replyLocation = doc.getLocation(Point.inText(textNode, 1)); blip.addReplyThread(replyLocation); // Insert text to give abc|d startText = Point.before(doc, textNode); doc.insertText(startText, "ab"); int newLocation = blip.locateReplyThreads().iterator().next().getLocation(); assertEquals(replyLocation + 2, newLocation); } });
public <N, E extends N, T extends N> void exec(MutableDocument<N, E, T> doc) { Point<N> startText = doc.locate(locateAfterLineElement(doc)); doc.insertText(startText, "cd"); // Insert reply between c|d. N bodyNode = DocHelper.getElementWithTagName(doc, Blips.BODY_TAGNAME); N textNode = doc.getFirstChild(bodyNode); textNode = doc.getNextSibling(textNode); int replyLocation = doc.getLocation(Point.inText(textNode, 1)); blip.addReplyThread(replyLocation); // Insert text to give abc|d startText = Point.before(doc, textNode); doc.insertText(startText, "ab"); int newLocation = blip.locateReplyThreads().iterator().next().getLocation(); assertEquals(replyLocation + 2, newLocation); } });
public <N, E extends N, T extends N> void exec(MutableDocument<N, E, T> doc) { Point<N> startText = doc.locate(locateAfterLineElement(doc)); doc.insertText(startText, "cd"); // Insert reply between c|d. N bodyNode = DocHelper.getElementWithTagName(doc, Blips.BODY_TAGNAME); N textNode = doc.getFirstChild(bodyNode); textNode = doc.getNextSibling(textNode); int replyLocation = doc.getLocation(Point.inText(textNode, 1)); ConversationThread replyThread = blip.addReplyThread(replyLocation); // Delete text and anchor. doc.deleteRange(Point.before(doc, textNode), Point.inElement(bodyNode, null)); int newLocation = blip.locateReplyThreads().iterator().next().getLocation(); assertEquals(Blips.INVALID_INLINE_LOCATION, newLocation); } });
public <N, E extends N, T extends N> void exec(MutableDocument<N, E, T> doc) { Point<N> startText = doc.locate(locateAfterLineElement(doc)); doc.insertText(startText, "cd"); // Insert reply between c|d. N bodyNode = DocHelper.getElementWithTagName(doc, Blips.BODY_TAGNAME); N textNode = doc.getFirstChild(bodyNode); textNode = doc.getNextSibling(textNode); int replyLocation = doc.getLocation(Point.inText(textNode, 1)); ConversationThread replyThread = blip.addReplyThread(replyLocation); // Delete text and anchor. doc.deleteRange(Point.before(doc, textNode), Point.inElement(bodyNode, null)); int newLocation = blip.locateReplyThreads().iterator().next().getLocation(); assertEquals(Blips.INVALID_INLINE_LOCATION, newLocation); } });
/** * 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 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 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)); }
@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)); }
public void testSplitTextNeverReturnsSibling() { TestDocumentContext<Node, Element, Text> cxt = ContextProviders.createTestPojoContext( DocProviders.POJO.parse("ab").asOperation(), null, null, null, DocumentSchema.NO_SCHEMA_CONSTRAINTS); TextNodeOrganiser<Text> organiser = cxt.textNodeOrganiser(); MutableDocument<Node, Element, Text> doc = cxt.document(); Text first = (Text) doc.getFirstChild(doc.getDocumentElement()); Text text = organiser.splitText(first, 1); LocalDocument<Node, Element, Text> local = cxt.annotatableContent(); Element tr = local.transparentCreate("l", Attributes.EMPTY_MAP, doc.getDocumentElement(), text); local.transparentMove(tr, text, null, null); assertNull(cxt.getIndexedDoc().splitText(first, 1)); assertNull(organiser.splitText(first, 1)); assertSame(first, organiser.splitText(first, 0)); assertSame(first, organiser.splitText(first, 0)); assertEquals("a<l>b</l>", XmlStringBuilder.innerXml(local).toString()); assertEquals("ab", XmlStringBuilder.innerXml(doc).toString()); }
public void testSplitTextNeverReturnsSibling() { TestDocumentContext<Node, Element, Text> cxt = ContextProviders.createTestPojoContext( DocProviders.POJO.parse("ab").asOperation(), null, null, null, DocumentSchema.NO_SCHEMA_CONSTRAINTS); TextNodeOrganiser<Text> organiser = cxt.textNodeOrganiser(); MutableDocument<Node, Element, Text> doc = cxt.document(); Text first = (Text) doc.getFirstChild(doc.getDocumentElement()); Text text = organiser.splitText(first, 1); LocalDocument<Node, Element, Text> local = cxt.annotatableContent(); Element tr = local.transparentCreate("l", Attributes.EMPTY_MAP, doc.getDocumentElement(), text); local.transparentMove(tr, text, null, null); assertNull(cxt.getIndexedDoc().splitText(first, 1)); assertNull(organiser.splitText(first, 1)); assertSame(first, organiser.splitText(first, 0)); assertSame(first, organiser.splitText(first, 0)); assertEquals("a<l>b</l>", XmlStringBuilder.innerXml(local).toString()); assertEquals("ab", XmlStringBuilder.innerXml(doc).toString()); }