@Override public <N, E extends N, T extends N> void exec(MutableDocument<N, E, T> doc) { Point<N> point = doc.locate(location); doc.createElement(point, Blips.THREAD_INLINE_ANCHOR_TAGNAME, Collections.singletonMap(Blips.THREAD_INLINE_ANCHOR_ID_ATTR, threadId)); } });
/** * Helper method to satisfy type requirements when adding new elements. */ static <N, E extends N> E prependChild( MutableDocument<N, E, ?> doc, E container, String tag, Map<String, String> attrs) { return doc.createElement(Point.start(doc, container), tag, attrs); }
@Override public E createElement(Point<N> point, String tag, Map<String, String> attributes) { return getDelegate().createElement(point, tag, attributes); }
/** * Try to insert text into a new text node, by first inserting an element, and * then removing it after the new text is inserted. * * NOTE(user): We assume the document doesn't try to join the text nodes * after the dummy element is removed. * * @param <N> * @param at * @param text */ // TODO(user): Move somewhere common for TextLocatorTest static <N, E extends N, T extends N> void insertTextInNewTextNodeHelper( MutableDocument<N, E, T> doc, Point<N> at, String text) { E e = doc.createElement(at, "a", Collections.<String, String> emptyMap()); doc.insertText(Point.after(doc, e), text); doc.deleteNode(e); }
/** * Inserts a line at the given location * * @param doc * @param rounding rightwards rounding to apply to the given location * @param location * @param attributes * @return the new line element * * Temporarily supports paragraphs as well */ public static <N, E extends N, T extends N> E insertLine(final MutableDocument<N, E, T> doc, Rounding rounding, Point<N> location, Attributes attributes) { Preconditions.checkNotNull(rounding, "rounding must not be null"); location = roundLocation(doc, rounding, location, RoundDirection.RIGHT); Preconditions.checkArgument(location != null, "location is not a valid place to insert a line"); checkNotParagraphDocument(doc); // Make sure this is a valid place to insert the line, even if it means // dishonouring the rounding. Line rounding should already have done this. if (rounding != Rounding.LINE) { location = jumpOutToContainer(doc, location); } return doc.createElement(location, LINE_TAGNAME, attributes); }
/** * Try to insert text into a new text node, by first inserting an element, and * then removing it after the new text is inserted. * * NOTE(user): We assume the document doesn't try to join the text nodes * after the dummy element is removed. * * @param <N> * @param at * @param text */ // TODO(user): Move somewhere common for TextLocatorTest static <N, E extends N, T extends N> void insertTextInNewTextNodeHelper( MutableDocument<N, E, T> doc, Point<N> at, String text) { E e = doc.createElement(at, "a", Collections.<String, String> emptyMap()); doc.insertText(Point.after(doc, e), text); doc.deleteNode(e); }
/** * 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)); }
/** * 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)); }