Example usage for com.google.gwt.dom.client Range setEnd

List of usage examples for com.google.gwt.dom.client Range setEnd

Introduction

In this page you can find the example usage for com.google.gwt.dom.client Range setEnd.

Prototype

public final native void setEnd(Node node, int endOffset) ;

Source Link

Document

Sets the end position of a Range.

Usage

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);
    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.
 * /*from ww  w  . jav  a 2 s  .  c  o 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 .co  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 start point of the shrunken range (obtained with {@link #shrinkRange(Range)}).
 * //from  w ww .  ja v a2s.  c o  m
 * @param range a DOM range
 * @return the start point of the range returned by {@link #shrinkRange(Range)}
 * @see #shrinkRange(Range)
 */
private Range getShrunkenRangeStart(Range range) {
    Node startContainer = range.getStartContainer();
    if (startContainer.hasChildNodes()) {
        if (range.getStartOffset() < startContainer.getChildCount()) {
            // Before a child node of an element.
            startContainer = getFirstLeaf(startContainer.getChild(range.getStartOffset()));
        } else {
            // After the last child of an element.
            startContainer = getNextLeaf(startContainer);
        }
    } else if (range.getStartOffset() > 0 && range.getStartOffset() == startContainer.getNodeValue().length()) {
        // At the end of a non-empty text node.
        startContainer = getNextLeaf(startContainer);
    }

    if (startContainer != null) {
        int startOffset = 0;
        if (startContainer.getNodeType() == Node.ELEMENT_NODE && !canHaveChildren(startContainer)) {
            startOffset = getNodeIndex(startContainer);
            startContainer = startContainer.getParentNode();
        }

        // Return a new range only if we managed to move the start.
        if (startContainer != range.getStartContainer()) {
            Range start = range.cloneRange();
            start.setEnd(startContainer, startOffset);
            start.collapse(false);
            return start;
        }
    }

    return range;
}