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

The following examples show how to use org.w3c.dom.ranges.Range#getStartContainer() . 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: Selection.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the first selection range in the current document, by document position.
 * @return the first selection range in the current document, by document position
 */
private Range getFirstRange() {
    // avoid concurrent modification exception
    final List<Range> ranges = new ArrayList<>(getRanges());

    Range first = null;
    for (final Range range : ranges) {
        if (first == null) {
            first = range;
        }
        else {
            final org.w3c.dom.Node firstStart = first.getStartContainer();
            final org.w3c.dom.Node rangeStart = range.getStartContainer();
            if ((firstStart.compareDocumentPosition(rangeStart) & Node.DOCUMENT_POSITION_PRECEDING) != 0) {
                first = range;
            }
        }
    }
    return first;
}
 
Example 3
Source File: Selection.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the last selection range in the current document, by document position.
 * @return the last selection range in the current document, by document position
 */
private Range getLastRange() {
    // avoid concurrent modification exception
    final List<Range> ranges = new ArrayList<>(getRanges());

    Range last = null;
    for (final Range range : ranges) {
        if (last == null) {
            last = range;
        }
        else {
            final org.w3c.dom.Node lastStart = last.getStartContainer();
            final org.w3c.dom.Node rangeStart = range.getStartContainer();
            if ((lastStart.compareDocumentPosition(rangeStart) & Node.DOCUMENT_POSITION_FOLLOWING) != 0) {
                last = range;
            }
        }
    }
    return last;
}
 
Example 4
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 5
Source File: Selection.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the first selection range in the current document, by document position.
 * @return the first selection range in the current document, by document position
 */
private Range getFirstRange() {
    // avoid concurrent modification exception
    final List<Range> ranges = new ArrayList<>(getRanges());

    Range first = null;
    for (final Range range : ranges) {
        if (first == null) {
            first = range;
        }
        else {
            final Node firstStart = (Node) first.getStartContainer();
            final Node rangeStart = (Node) range.getStartContainer();
            if ((firstStart.compareDocumentPosition(rangeStart) & Node.DOCUMENT_POSITION_PRECEDING) != 0) {
                first = range;
            }
        }
    }
    return first;
}
 
Example 6
Source File: Selection.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the last selection range in the current document, by document position.
 * @return the last selection range in the current document, by document position
 */
private Range getLastRange() {
    // avoid concurrent modification exception
    final List<Range> ranges = new ArrayList<>(getRanges());

    Range last = null;
    for (final Range range : ranges) {
        if (last == null) {
            last = range;
        }
        else {
            final Node lastStart = (Node) last.getStartContainer();
            final Node rangeStart = (Node) range.getStartContainer();
            if ((lastStart.compareDocumentPosition(rangeStart) & Node.DOCUMENT_POSITION_FOLLOWING) != 0) {
                last = range;
            }
        }
    }
    return last;
}
 
Example 7
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 8
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;
}