7

DOM4 比较文档位置

我想实现 compareDocumentPosition。Resig 在这方面取得了良好的开端。我已经拿走了他的代码并整理了它

function compareDocumentPosition(other) {
    var ret = 0;
    if (this.contains) {
        if (this !== other && this.contains(other)) {
            ret += 16;
        }
        if (this !== other && other.contains(this)) {
            ret += 8;
        }
        if (this.sourceIndex >= 0 && other.sourceIndex >= 0) {
            if (this.sourceIndex < other.sourceIndex) {
                ret += 4;
            }
            if (this.sourceIndex > other.sourceIndex) {
                ret += 2;
            }
        } else {
            ret += 1;
        }
    } 
    return ret;
}

这适用于Element但不适用于Textor DocumentFragment.sourceIndex这是因为 IE8在这些节点上没有给出。(它也没有给出.contains,但我已经解决了这个问题)

如何有效地编写与DOCUMENT_POSITION_FOLLOWINGDOCUMENT_POSITION_PRECEDING对应的+=4和位。+=2

作为额外参考,这两个由 DOM4 定义为的树顺序定义

如果 A 和 B 在同一棵树中并且 A 在树顺序中位于 B 之前,则对象 A 在对象 B 之前。

如果 A 和 B 在同一棵树中并且 A 按树顺序在 B 之后,则对象 A 跟随对象 B。

树顺序是前序,深度优先遍历。

大多数现代浏览器都实现了这一点(包括 IE9)。所以你只需要在 IE8 中工作的东西(我不关心 IE6/7,但如果它工作得很棒!)

4

2 回答 2

10
function recursivelyWalk(nodes, cb) {
    for (var i = 0, len = nodes.length; i < len; i++) {
        var node = nodes[i];
        var ret = cb(node);
        if (ret) {
            return ret;
        }
        if (node.childNodes && node.childNodes.length) {
            var ret = recursivelyWalk(node.childNodes, cb);
            if (ret) {
                return ret;
            }
        }
    }
}

function testNodeForComparePosition(node, other) {
    if (node === other) {
        return true;
    }
}

function compareDocumentPosition(other) {
    function identifyWhichIsFirst(node) {
        if (node === other) {
            return "other";
        } else if (node === reference) {
            return "reference";
        }
    }

    var reference = this,
        referenceTop = this,
        otherTop = other;

    if (this === other) {
        return 0;
    }
    while (referenceTop.parentNode) {
        referenceTop = referenceTop.parentNode;
    }
    while (otherTop.parentNode) {
        otherTop = otherTop.parentNode;
    }

    if (referenceTop !== otherTop) {
        return Node.DOCUMENT_POSITION_DISCONNECTED;
    }

    var children = reference.childNodes;
    var ret = recursivelyWalk(
        children,
        testNodeForComparePosition.bind(null, other)
    );
    if (ret) {
        return Node.DOCUMENT_POSITION_CONTAINED_BY +
            Node.DOCUMENT_POSITION_FOLLOWING;
    }

    var children = other.childNodes;
    var ret = recursivelyWalk(
        children, 
        testNodeForComparePosition.bind(null, reference)
    );
    if (ret) {
        return Node.DOCUMENT_POSITION_CONTAINS +
            Node.DOCUMENT_POSITION_PRECEDING;
    }

    var ret = recursivelyWalk(
        [referenceTop],
        identifyWhichIsFirst
    );
    if (ret === "other") {
        return Node.DOCUMENT_POSITION_PRECEDING;
    } else {
        return Node.DOCUMENT_POSITION_FOLLOWING;
    }
}

我自己写的。我认为这个实现是错误的,但这是我的其他一些代码中的错误。看起来很扎实。

于 2011-11-30T23:51:57.833 回答
0

Raynos 的答案是一个好的开始,但不是开箱即用的。Node.*无法找到并且.bind在 IE8 中不可用。

下面是可在 Internet Explorer 8 中使用的代码:

function recursivelyWalk(nodes, cb) {
    for (var i = 0, len = nodes.length; i < len; i++) {
        var node = nodes[i];
        var ret = cb(node);
        if (ret) {
            return ret;
        }
        if (node.childNodes && node.childNodes.length) {
            var ret = recursivelyWalk(node.childNodes, cb);
            if (ret) {
                return ret;
            }
        }
    }
}

function testNodeForComparePosition(node, other) {
    if (node === other) {
        return true;
    }
}

var DOCUMENT_POSITION_DISCONNECTED = 1;
var DOCUMENT_POSITION_PRECEDING = 2;
var DOCUMENT_POSITION_FOLLOWING = 4;
var DOCUMENT_POSITION_CONTAINS = 8;
var DOCUMENT_POSITION_CONTAINED_BY = 16;

function compareDocumentPosition(thisNode, other) {
    function identifyWhichIsFirst(node) {
        if (node === other) {
            return "other";
        } else if (node === reference) {
            return "reference";
        }
    }

    var reference = thisNode,
        referenceTop = thisNode,
        otherTop = other;

    if (this === other) {
        return 0;
    }
    while (referenceTop.parentNode) {
        referenceTop = referenceTop.parentNode;
    }
    while (otherTop.parentNode) {
        otherTop = otherTop.parentNode;
    }

    if (referenceTop !== otherTop) {
        return DOCUMENT_POSITION_DISCONNECTED;
    }

    var children = reference.childNodes;
    var ret = recursivelyWalk(
        children,
        function(p) {
            (function() {
                var localOther = other;
                return testNodeForComparePosition(localOther, p);
            })();
        }
    );
    if (ret) {
        return DOCUMENT_POSITION_CONTAINED_BY +
            DOCUMENT_POSITION_FOLLOWING;
    }

    var children = other.childNodes;
    var ret = recursivelyWalk(
        children,
        function(p) {
            (function() {
                var localOther = reference;
                return testNodeForComparePosition(localOther, p);
            })();
        }
    );
    if (ret) {
        return DOCUMENT_POSITION_CONTAINS +
            DOCUMENT_POSITION_PRECEDING;
    }

    var ret = recursivelyWalk(
        [referenceTop],
        identifyWhichIsFirst
    );
    if (ret === "other") {
        return DOCUMENT_POSITION_PRECEDING;
    } else {
        return DOCUMENT_POSITION_FOLLOWING;
    }
}

你这样称呼它:

compareDocumentPosition(sourceElement, elementToTest)

(就像打电话一样sourceElement.compareDocumentPosition(elementToTest)

于 2014-03-04T14:28:52.080 回答