List of usage examples for com.google.gwt.dom.client Range setStart
public final native void setStart(Node node, int startOffset) ;
From source file:org.rstudio.core.client.dom.impl.DomUtilsStandardImpl.java
License:Open Source License
public void setSelectionOffsets(Element container, int start, int end) { NodeRelativePosition startp = NodeRelativePosition.toPosition(container, start); NodeRelativePosition endp = NodeRelativePosition.toPosition(container, end); Document doc = container.getOwnerDocument(); Range rng = Range.create(doc); rng.setStart(startp.node, startp.offset); rng.setEnd(endp.node, endp.offset);/*from w ww . j av a 2 s.co m*/ Selection.get(NativeWindow.get(doc)).setRange(rng); }
From source file:org.xwiki.gwt.dom.client.DOMUtils.java
License:Open Source License
/** * Computes the longest text range included in the specified range. By text range we understand any range that * starts and ends in a text node. The end points of a text range can be in different text nodes. * //w w w .j a v a2s . co m * @param range any range * @return the longest text range included in the given range. */ public Range getTextRange(Range range) { Range textRange = range.cloneRange(); Node firstLeaf = getFirstLeaf(range); if (firstLeaf != null) { Node lastLeaf = getLastLeaf(range); // Find the first text node in the range and start the range there. while (firstLeaf != lastLeaf && firstLeaf.getNodeType() != Node.TEXT_NODE) { firstLeaf = getNextLeaf(firstLeaf); } if (firstLeaf.getNodeType() == Node.TEXT_NODE && firstLeaf != textRange.getStartContainer()) { textRange.setStart(firstLeaf, 0); } // Find the last text node in the range and end the range there. while (lastLeaf != firstLeaf && lastLeaf.getNodeType() != Node.TEXT_NODE) { lastLeaf = getPreviousLeaf(lastLeaf); } if (lastLeaf.getNodeType() == Node.TEXT_NODE && lastLeaf != textRange.getEndContainer()) { textRange.setEnd(lastLeaf, lastLeaf.getNodeValue().length()); } } return textRange; }
From source file:org.xwiki.gwt.dom.client.DOMUtils.java
License:Open Source License
/** * Computes the maximal <strong>sub-range</strong> of the given range that satisfies the following two conditions: * <ul>//from w w w .ja v a 2 s . c o m * <li>the start point is before a <strong>leaf</strong> element node that can't have child nodes (e.g. an image) or * inside a leaf node at position 0 (e.g. an empty span or a text node)</li> * <li>the end point is after a leaf element node that can't have child nodes (e.g. an image) or inside a leaf node * at the end (e.g. inside an empty span or at the end of a text node)</li> * </ul> * . If no such sub-range exists (because the given range doesn't wrap any leaf node and none of its end points * satisfies the corresponding condition) then the given range is returned unmodified. * * @param range a DOM range * @return the maximal sub-range that selects the same content as the given range */ public Range shrinkRange(Range range) { if (range == null || range.isCollapsed()) { return range; } // Find the start and end points that satisfy the conditions. Range start = getShrunkenRangeStart(range); Range end = getShrunkenRangeEnd(range); // If at least one of the end points moved and the range is still valid. if ((start != range || end != range) && start.compareBoundaryPoints(RangeCompare.END_TO_START, end) <= 0) { Range result = range.cloneRange(); result.setStart(start.getEndContainer(), start.getEndOffset()); result.setEnd(end.getStartContainer(), end.getStartOffset()); return result; } return range; }
From source file:org.xwiki.gwt.dom.client.DOMUtils.java
License:Open Source License
/** * Utility method to get the end point of the shrunken range (obtained with {@link #shrinkRange(Range)}). * /*w ww .ja v a 2s .c om*/ * @param range a DOM range * @return the end point of the range returned by {@link #shrinkRange(Range)} * @see #shrinkRange(Range) */ private Range getShrunkenRangeEnd(Range range) { Node endContainer = range.getEndContainer(); if (endContainer.hasChildNodes()) { if (range.getEndOffset() > 0) { // After a child node of an element. endContainer = getLastLeaf(endContainer.getChild(range.getEndOffset() - 1)); } else { // Before the first child of an element. endContainer = getPreviousLeaf(endContainer); } } else if (range.getEndOffset() == 0 && getLength(endContainer) > 0) { // At the start of a non-empty text node. endContainer = getPreviousLeaf(endContainer); } if (endContainer != null) { int endOffset = getLength(endContainer); if (endContainer.getNodeType() == Node.ELEMENT_NODE && !canHaveChildren(endContainer)) { endOffset = getNodeIndex(endContainer) + 1; endContainer = endContainer.getParentNode(); } // Return a new range only if we managed to move the end. if (endContainer != range.getEndContainer()) { Range end = range.cloneRange(); end.setStart(endContainer, endOffset); end.collapse(true); return end; } } return range; }