private Builder emptyElement(E element, Builder builder) { for (N node = doc.getFirstChild(element); node != null; node = doc.getNextSibling(node)) { E elChild = doc.asElement(node); if (elChild != null) { deleteElement(elChild, builder); } else { builder.deleteCharacters(doc.getData(doc.asText(node)).length()); } } return builder; }
@Override public E getDocumentElement() { return doc.getDocumentElement(); }
public static <N, T extends N> int getItemSize(ReadableWDocument<N, ?, T> doc, N node) { // Short circuit if it's a text node, implementation is simpler T textNode = doc.asText(node); if (textNode != null) { return doc.getLength(textNode); } // Otherwise, calculate two locations and subtract N parent = doc.getParentElement(node); if (parent == null) { // Requesting size of the document root. // TODO(danilatos/anorth) This would change if we have multiple roots. noteCodeThatWillBreakWithMultipleRoots(); return doc.size(); } N next = doc.getNextSibling(node); int locationAfter = next != null ? doc.getLocation(next) : doc.getLocation(Point.end(parent)); return locationAfter - doc.getLocation(node); }
/***/ public void testGetNextSiblingElementBackwards() { ReadableWDocument<Node, Element, Text> doc = DocProviders.POJO.parse( "<div>abc<p>def<q>hij</q></p><p>def<q>hij</q></p></div>"); Element previous = null; Node node = doc.getFirstChild(doc.getDocumentElement()); while (node != null) { Element p = doc.asElement(node); if (p != null) { if (previous == null) { // p is the very first element among nodes of doc. assertNull(DocHelper.getPreviousSiblingElement(doc, p)); } else { // p follows a previously seen previous element. assertSame(previous, DocHelper.getPreviousSiblingElement(doc, p)); } previous = p; } node = doc.getNextSibling(node); } // TODO(user): The following fails. Uncomment and fix if it should work. // assertNull(DocHelper.getPreviousSiblingElement(doc, null)); }
/** * Tests the getItemSize method */ public void testGetItemSize() { ReadableWDocument<Node, Element, Text> doc = DocProviders.POJO.parse( "<top>abc<p>def<q>hij</q></p><p>def<q>hij</q></p></top>"); Element top = (Element) doc.getDocumentElement().getFirstChild(); assertEquals(25, DocHelper.getItemSize(doc, top)); Node text = doc.getFirstChild(top); assertEquals(3, DocHelper.getItemSize(doc, text)); Node pWithSibling = doc.getNextSibling(text); assertEquals(10, DocHelper.getItemSize(doc, pWithSibling)); Node pWithoutSibling = doc.getNextSibling(pWithSibling); assertEquals(10, DocHelper.getItemSize(doc, pWithoutSibling)); }
Point<N> start = doc.locate(startLocation); Point<N> end = doc.locate(endLocation); assert doc.isSameNode( Point.enclosingElement(doc, start.getContainer()), Point.enclosingElement(doc, end.getContainer())) if (doc.isSameNode(start.getContainer(), end.getContainer())) { int size = end.getTextOffset() - start.getTextOffset(); if (size > 0) { int size = doc.getLength(doc.asText(start.getContainer())) - start.getTextOffset(); node = doc.getNextSibling(start.getContainer()); if (size > 0) { builder.deleteCharacters(size); N next = doc.getNextSibling(node); T text = doc.asText(node); if (text != null) { builder.deleteCharacters(doc.getData(text).length()); } else { deleteElement(doc.asElement(node), builder);
@Override public E createElement(Point<N> point, String tagName, Map<String, String> attributes) { // TODO(danilatos): Validate point is in document. indexed doc should throw an exception // when calling getLocation anyway. Preconditions.checkNotNull(tagName, "createElement: tagName must not be null"); Point.checkPoint(this, point, "MutableDocumentImpl.createElement"); try { begin(); int location = doc.getLocation(point); consume(createElement(tagName, new AttributesImpl(attributes), at(location))); Point<N> result = doc.locate(location); return doc.asElement(result.isInTextNode() ? doc.getNextSibling(result.getContainer()) : result.getNodeAfter()); } finally { end(); } }
final int removeStart = doc.getLocation(Point.before(doc, from)); int removeEnd = toExcl == null ? doc.getLocation(Point.end((N) doc.getParentElement(from))) : doc.getLocation(Point.before(doc, toExcl)); int removeSize = removeEnd - removeStart; assert removeSize > 0; int addPosition = doc.getLocation(destination); if (addPosition >= removeEnd) { int remainder = doc.size() - removeSize - addPosition; // remainder after the insert for (N at = from; at != toExcl; at = doc.getNextSibling(at)) { DomOperationUtil.buildDomInitializationFromSubtree(doc, at, domOp); doc.knownKeys().each(new Proc() { // start all the keys @Override public void apply(String key) { annotOp.startAnnotation(key, null, doc.getAnnotation(removeStart, key)); doc.annotationIntervals(removeStart, removeEnd, null)) { interval.diffFromLeft().each(new ProcV<Object>() { @Override public void apply(String key, Object value) { last = interval; doc.knownKeys().each(new Proc() { // close all the keys @Override public void apply(String key) { annotOp.endAnnotation(key);
public void testGetNextSiblingElementBackwardsForInvalid() { ReadableWDocument<Node, Element, Text> doc = DocProviders.POJO.parse( "<div>abc<p>def<q>hij</q></p><p>def<q>hij</q></p></div>"); try { DocHelper.getPreviousSiblingElement(doc, null); fail("Should failed when fetching previous sibling of a null"); } catch (Exception e) { // Success! } try { DocHelper.getPreviousSiblingElement(null, doc.getFirstChild(doc.getDocumentElement())); fail("Should failed when fetching previous sibling in a null document"); } catch (Exception e) { // Success! } }
@SuppressWarnings("unchecked") @Override public int remainingCharactersInElement(int insertionPoint) { Point<N> point = doc.locate(insertionPoint); int num = 0; N node; if (point.isInTextNode()) { num += doc.getLength((T) point.getContainer()) - point.getTextOffset(); node = doc.getNextSibling(point.getContainer()); } else { node = point.getNodeAfter(); } T textNode; while ((textNode = doc.asText(node)) != null) { num += doc.getLength(textNode); node = doc.getNextSibling(node); } return num; }
/** * Gets the location before a given node. * * @param <N> The type of DOM nodes. * @param doc The indexed document. * @param node A node. * @return The location before the given node. */ public static <N> int before(ReadableWDocument<N, ?, ?> doc, N node) { return doc.getLocation(node); }
Point.checkPoint(this, end, "MutableDocumentImpl.deleteRange end point"); int startLocation = doc.getLocation(start); int endLocation = doc.getLocation(end); if (doc.isSameNode( Point.enclosingElement(this, start.getContainer()), Point.enclosingElement(this, end.getContainer()))) { newEndPoint = doc.locate(startLocation); } else { for (E el = startEl; el != null; el = doc.getParentElement(el)) { startAncestors.add(el); for (rightEl = endEl, prevRightEl = endEl; ; rightEl = doc.getParentElement(rightEl)) { boolean atCommonAncestor = startAncestors.contains(rightEl); newEndPoint = doc.locate(s); Point<N> startPoint = doc.locate(startLocation);
@Override public Range deleteRange(int start, int end) { Preconditions.checkPositionIndexes(start, end, size()); // TODO(davidbyttow/danilatos): Handle this more efficiently. PointRange<N> range = deleteRange(doc.locate(start), doc.locate(end)); return new Range(doc.getLocation(range.getFirst()), doc.getLocation(range.getSecond())); }
@Override public E asElement(N node) { return doc.asElement(node); }
@Override public N getFirstChild(N node) { return doc.getFirstChild(node); }
@Override public N getNextSibling(N node) { return doc.getNextSibling(node); }
@Override public String toXmlString() { return doc.toXmlString(); }
@Override public Iterable<AnnotationInterval<String>> annotationIntervals(int start, int end, ReadableStringSet keys) { return doc.annotationIntervals(start, end, keys); }
@Override public AnnotationCursor annotationCursor(int start, int end, ReadableStringSet keys) { return doc.annotationCursor(start, end, keys); }
@Override public T asText(N node) { return doc.asText(node); }