@Override public N getNextSibling(N node) { return doc.getNextSibling(node); }
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; }
@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; }
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); }
/** * 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)); }
/** * 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)); }
@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(); } }
/***/ 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)); }
/***/ 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)); }
} else { 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) {
for (N at = from; at != toExcl; at = doc.getNextSibling(at)) { DomOperationUtil.buildDomInitializationFromSubtree(doc, at, domOp);