Example usage for org.w3c.dom Node compareDocumentPosition

List of usage examples for org.w3c.dom Node compareDocumentPosition

Introduction

In this page you can find the example usage for org.w3c.dom Node compareDocumentPosition.

Prototype

public short compareDocumentPosition(Node other) throws DOMException;

Source Link

Document

Compares the reference node, i.e.

Usage

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//  w w w . j a v a2 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;
}

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

/**
 * Compares an end point of a TextRange object with an end point of another range.
 * @param how how to compare/*from   w w w.  j av  a2s .  c  o m*/
 * @param sourceRange the other range
 * @return the result
 */
@JsxFunction
public int compareEndPoints(final String how, final TextRange sourceRange) {
    final org.w3c.dom.Node start;
    final org.w3c.dom.Node otherStart;
    switch (how) {
    case "StartToEnd":
        start = range_.getStartContainer();
        otherStart = sourceRange.range_.getEndContainer();
        break;

    case "StartToStart":
        start = range_.getStartContainer();
        otherStart = sourceRange.range_.getStartContainer();
        break;

    case "EndToStart":
        start = range_.getEndContainer();
        otherStart = sourceRange.range_.getStartContainer();
        break;

    default:
        start = range_.getEndContainer();
        otherStart = sourceRange.range_.getEndContainer();
        break;
    }
    if (start == null || otherStart == null) {
        return 0;
    }
    return start.compareDocumentPosition(otherStart);
}