/** * 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); }
@Override public int getLocation(N node) { return doc.getLocation(node); }
/** * @param doc * @param point * @return true if the given location is at the end of a line */ public static <N, E extends N, T extends N> boolean isAtLineEnd( ReadableWDocument<N, E, T> doc, Point<N> point) { return doc.getLocation(point) == doc.getLocation(LineContainers.roundLocation( doc, Rounding.LINE, point, RoundDirection.RIGHT)); }
/** * Returns the location of the first matching element * * @see #findElementByAttr(ReadableDocument, String, String) * * @return the location of the first matching element or -1 if none found */ public static <N, E extends N, T extends N> int findLocationByAttr( ReadableWDocument<N, E, T> doc, String name, String value) { E el = findElementByAttr(doc, name, value); return el != null ? doc.getLocation(el) : -1; }
/** * Gets the location after a given node. * * @param <N> The type of DOM nodes. * @param doc The indexed document. * @param node A node. * @return The location after the given node. */ public static <N> int after(ReadableWDocument<N, ?, ?> doc, N node) { return doc.getLocation(Point.after(doc, node)); }
/** * Gets the location of the start of a given element. * * @param <E> The type of DOM Element nodes. * @param doc The indexed document. * @param element An element. * @return The location of the start of the given element. */ public static <N, E extends N> int start(ReadableWDocument<N, E, ?> doc, E element) { return doc.getLocation(Point.start(doc, element)); }
/** * Gets the location of the end of a given element. * * @param <N> The type of DOM nodes. * @param <E> The type of DOM Element nodes. * @param doc The indexed document. * @param element An element. * @return The location of the end of the given element. */ public static <N, E extends N> int end(ReadableWDocument<N, E, ?> doc, E element) { return doc.getLocation(Point.<N>end(element)); }
@Override public int getLocation(Point<N> point) { Preconditions.checkNotNull(point, "getLocation: Null point"); return doc.getLocation(point); }
/** * Inserts text at the given point. Triggers a single operation event matching the * executed insertion. * * @param point Point at which to insert text * @param text Text to insert */ public void insertText(Point<N> point, String text) { Point.checkPoint(this, point, "MutableDocumentImpl.insertText"); insertText(doc.getLocation(point), text); }
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); }
/** * Returns the position of the body element of the given document or 0 if the * document has no body. */ public static <N, E extends N> int getBodyPosition(ReadableWDocument<N, E, ?> doc) { N body = DocHelper.getElementWithTagName(doc, Blips.BODY_TAGNAME); if (body == null) return 0; return doc.getLocation(body); }
@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 insertXml(Point<N> point, XmlStringBuilder xml) { Point.checkPoint(this, point, "MutableDocumentImpl.insertXml"); try { begin(); int where = doc.getLocation(point); Builder builder = at(where); appendXmlToBuilder(xml, builder); consume(builder); return Point.elementAfter(this, doc.locate(where)); } finally { end(); } }
Point.checkPoint(this, end, "MutableDocumentImpl.deleteRange end point"); int startLocation = doc.getLocation(start); int endLocation = doc.getLocation(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) {
@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(); } }