Java Code Examples for org.w3c.dom.ranges.Range#getEndOffset()

The following examples show how to use org.w3c.dom.ranges.Range#getEndOffset() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TextRange.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Sets the endpoint of the range based on the endpoint of another range..
 * @param type end point transfer type. One of "StartToEnd", "StartToStart", "EndToStart" and "EndToEnd"
 * @param other the other range
 * @see <a href="http://msdn.microsoft.com/en-us/library/ms536745.aspx">MSDN doc</a>
 */
@JsxFunction
public void setEndPoint(final String type, final TextRange other) {
    final Range otherRange = other.range_;

    final org.w3c.dom.Node target;
    final int offset;
    if (type.endsWith("ToStart")) {
        target = otherRange.getStartContainer();
        offset = otherRange.getStartOffset();
    }
    else {
        target = otherRange.getEndContainer();
        offset = otherRange.getEndOffset();
    }

    if (type.startsWith("Start")) {
        range_.setStart(target, offset);
    }
    else {
        range_.setEnd(target, offset);
    }
}
 
Example 2
Source File: TextRange.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Sets the endpoint of the range based on the endpoint of another range..
 * @param type end point transfer type. One of "StartToEnd", "StartToStart", "EndToStart" and "EndToEnd"
 * @param other the other range
 * @see <a href="http://msdn.microsoft.com/en-us/library/ms536745.aspx">MSDN doc</a>
 */
@JsxFunction
public void setEndPoint(final String type, final TextRange other) {
    final Range otherRange = other.range_;

    final org.w3c.dom.Node target;
    final int offset;
    if (type.endsWith("ToStart")) {
        target = otherRange.getStartContainer();
        offset = otherRange.getStartOffset();
    }
    else {
        target = otherRange.getEndContainer();
        offset = otherRange.getEndOffset();
    }

    if (type.startsWith("Start")) {
        range_.setStart(target, offset);
    }
    else {
        range_.setEnd(target, offset);
    }
}
 
Example 3
Source File: TextRange.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Indicates if a range is contained in current one.
 * @param other the other range
 * @return {@code true} if <code>other</code> is contained within current range
 * @see <a href="http://msdn.microsoft.com/en-us/library/ms536371.aspx">MSDN doc</a>
 */
@JsxFunction
public boolean inRange(final TextRange other) {
    final Range otherRange = other.range_;

    final org.w3c.dom.Node start = range_.getStartContainer();
    final org.w3c.dom.Node otherStart = otherRange.getStartContainer();
    if (otherStart == null) {
        return false;
    }
    final short startComparison = start.compareDocumentPosition(otherStart);
    final boolean startNodeBefore = startComparison == 0
            || (startComparison & Node.DOCUMENT_POSITION_CONTAINS) != 0
            || (startComparison & Node.DOCUMENT_POSITION_PRECEDING) != 0;
    if (startNodeBefore && (start != otherStart || range_.getStartOffset() <= otherRange.getStartOffset())) {
        final org.w3c.dom.Node end = range_.getEndContainer();
        final org.w3c.dom.Node otherEnd = otherRange.getEndContainer();
        final short endComparison = end.compareDocumentPosition(otherEnd);
        final boolean endNodeAfter = endComparison == 0
                || (endComparison & Node.DOCUMENT_POSITION_CONTAINS) != 0
                || (endComparison & Node.DOCUMENT_POSITION_FOLLOWING) != 0;
        if (endNodeAfter && (end != otherEnd || range_.getEndOffset() >= otherRange.getEndOffset())) {
            return true;
        }
    }

    return false;
}
 
Example 4
Source File: Selection.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the number of characters that the selection's focus is offset within the focus node.
 * @return the number of characters that the selection's focus is offset within the focus node
 */
@JsxGetter
public int getFocusOffset() {
    final Range last = getLastRange();
    if (last == null) {
        return 0;
    }
    return last.getEndOffset();
}
 
Example 5
Source File: TextRange.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Indicates if a range is contained in current one.
 * @param other the other range
 * @return {@code true} if <code>other</code> is contained within current range
 * @see <a href="http://msdn.microsoft.com/en-us/library/ms536371.aspx">MSDN doc</a>
 */
@JsxFunction
public boolean inRange(final TextRange other) {
    final Range otherRange = other.range_;

    final org.w3c.dom.Node start = range_.getStartContainer();
    final org.w3c.dom.Node otherStart = otherRange.getStartContainer();
    if (otherStart == null) {
        return false;
    }
    final short startComparison = start.compareDocumentPosition(otherStart);
    final boolean startNodeBefore = startComparison == 0
            || (startComparison & Node.DOCUMENT_POSITION_CONTAINS) != 0
            || (startComparison & Node.DOCUMENT_POSITION_PRECEDING) != 0;
    if (startNodeBefore && (start != otherStart || range_.getStartOffset() <= otherRange.getStartOffset())) {
        final org.w3c.dom.Node end = range_.getEndContainer();
        final org.w3c.dom.Node otherEnd = otherRange.getEndContainer();
        final short endComparison = end.compareDocumentPosition(otherEnd);
        final boolean endNodeAfter = endComparison == 0
                || (endComparison & Node.DOCUMENT_POSITION_CONTAINS) != 0
                || (endComparison & Node.DOCUMENT_POSITION_FOLLOWING) != 0;
        if (endNodeAfter && (end != otherEnd || range_.getEndOffset() >= otherRange.getEndOffset())) {
            return true;
        }
    }

    return false;
}
 
Example 6
Source File: Selection.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the number of characters that the selection's focus is offset within the focus node.
 * @return the number of characters that the selection's focus is offset within the focus node
 */
@JsxGetter
public int getFocusOffset() {
    final Range last = getLastRange();
    if (last == null) {
        return 0;
    }
    return last.getEndOffset();
}