@Override public N getPreviousSibling(N node) { return inner.getPreviousSibling(node); }
/** * @param doc document accessor. * @param element a document element. * @return The previous element sibling of {@code element} if there is one, * otherwise {@code null}. */ public static <N, E extends N> E getPreviousSiblingElement( ReadableDocument<N, E, ?> doc, E element) { Preconditions.checkNotNull(element, "Previous element for null element is undefined"); Preconditions.checkNotNull(doc, "Previous element for null document is undefined"); return getNextElementInclusive(doc, doc.getPreviousSibling(element), false); }
/** * @param doc * @param point * @return Node before the given point */ public static <N, E extends N, T extends N> N nodeBefore( ReadableDocument<N,E,T> doc, Point.El<N> point) { N node = point.getNodeAfter(); return node == null ? doc.getLastChild(point.getContainer()) : doc.getPreviousSibling(node); }
/** * Returns a node as an element if it is one; otherwise, finds the next * sibling of that node that is an element. * * @param doc document accessor * @param node reference node * @return the next element in the inclusive sibling chain from {@code node}. */ public static <N, E extends N> E getNextElementInclusive(ReadableDocument<N, E, ?> doc, N node, boolean forward) { E asElement = doc.asElement(node); while (node != null && asElement == null) { node = forward ? doc.getNextSibling(node) : doc.getPreviousSibling(node); asElement = doc.asElement(node); } return asElement; }
/** * @param doc * @param point * @return Node before the given point */ public static <N, E extends N, T extends N> E elementBefore( ReadableDocument<N,E,T> doc, Point<N> point) { if (point.isInTextNode()) { if (point.getTextOffset() > 0) { return null; } else { return doc.asElement(doc.getPreviousSibling(point.getContainer())); } } else { return doc.asElement(nodeBefore(doc, point.asElementPoint())); } }
/** * Checks whether a location has some text immediately to its left. * * @return true if text data precedes the given location */ public static <N, E extends N, T extends N> boolean textPrecedes( ReadableDocument<N, E, T> doc, LocationMapper<N> mapper, int location) { Point<N> point = mapper.locate(location); if (point.isInTextNode()) { return point.getTextOffset() > 0 || doc.asText(doc.getPreviousSibling(point.getContainer())) != null; } else { return doc.asText(Point.nodeBefore(doc, point.asElementPoint())) != null; } }
/** * This method returns the last point in a sequence of text node in the given direction. * * We guarantee that the return value is non-null and inside a text node. * * @return the last point in the text sequence as a text point. */ private static <N, E extends N, T extends N> Tx<N> lastPointInTextSequence( ReadableDocument<N, E, T> doc, Tx<N> start, boolean forward) { Tx<N> ret; if (forward) { T t = doc.asText(start.getCanonicalNode()); T next = doc.asText(doc.getNextSibling(t)); while (next != null) { t = next; next = doc.asText(doc.getNextSibling(t)); } ret = Point.<N> inText(t, doc.getLength(t)); } else { T t = doc.asText(start.getCanonicalNode()); T prev = doc.asText(doc.getPreviousSibling(t)); while (prev != null) { t = prev; prev = doc.asText(doc.getPreviousSibling(t)); } ret = Point.<N> inText(t, 0); } return ret; } }
N nodeBefore = point.isInTextNode() ? doc.getPreviousSibling(point.getContainer()) : Point.nodeBefore(doc, point.asElementPoint()); while (nodeBefore != null && !isLineElement(doc, nodeBefore)) { nodeBefore = doc.getPreviousSibling(nodeBefore);
next = rightwards ? doc.getNextSibling(start) : doc.getPreviousSibling(start); if (doc.isSameNode(next, stopAt)) { return null;
/** * Locates characters in contiguous of text nodes. * * Returns location when given locator matches with the data in a text node, * Returns null if not found. * * @param doc * @param start * @param locator * @param forward */ private static <N, E extends N, T extends N> Tx<N> locateCharacters( ReadableDocument<N, E, T> doc, Tx<N> start, CharacterLocator locator, boolean forward) { Tx<N> current = start; N node = start.getContainer(); Tx<N> found = null; while (true) { found = findCharacterInNode(doc, current.asTextPoint(), locator, forward); if (found != null) { return found; } node = forward ? doc.getNextSibling(node) : doc.getPreviousSibling(node); if (doc.asText(node) != null) { current = forward ? Point.inText(node, 0) : Point.inText(node, doc.getLength(doc.asText(node))); } else { return null; } } }
/** * Normalizes a point so that it is biased towards text nodes, and node ends * rather than node start. * * @param <N> * @param <E> * @param <T> * @param point * @param doc */ public static <N, E extends N, T extends N> Point<N> normalizePoint(Point<N> point, ReadableDocument<N, E, T> doc) { N previous = null; if (!point.isInTextNode()) { previous = Point.nodeBefore(doc, point.asElementPoint()); T nodeAfterAsText = doc.asText(point.getNodeAfter()); if (nodeAfterAsText != null) { point = Point.<N>inText(nodeAfterAsText, 0); } } else if (point.getTextOffset() == 0) { previous = doc.getPreviousSibling(point.getContainer()); } T previousAsText = doc.asText(previous); if (previous != null && previousAsText != null) { point = Point.inText(previous, doc.getLength(previousAsText)); } return point; }
lastBefore = fullDoc.getLastChild(parent); } else { lastBefore = fullDoc.getPreviousSibling(at);