Example usage for org.w3c.dom.ranges Range getEndContainer

List of usage examples for org.w3c.dom.ranges Range getEndContainer

Introduction

In this page you can find the example usage for org.w3c.dom.ranges Range getEndContainer.

Prototype

public Node getEndContainer() throws DOMException;

Source Link

Document

Node within which the Range ends

Usage

From source file:com.gargoylesoftware.htmlunit.javascript.host.dom.TextRange.java

/**
 * 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//from www  .  ja  va  2s.  co  m
 * @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);
    }
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.dom.TextRange.java

/**
 * Indicates if a range is contained in current one.
 * @param other the other range/*ww w  . j av a 2  s  .  com*/
 * @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;
}