Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6881223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:04:50+00:00 2026-05-27T05:04:50+00:00

DOM4 compareDocumentPosition I want to implement compareDocumentPosition. Resig has made a great start at

  • 0

DOM4 compareDocumentPosition

I want to implement compareDocumentPosition. Resig has made a great start at doing just this. I’ve taken his code and neatened it up

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;
}

This works for Element but does not for Text or DocumentFragment. This is because IE8 does not give .sourceIndex on those nodes. (It doesn’t give .contains either but I’ve fixed that problem already)

How do I efficiently write the +=4 and +=2 bit which correspond to DOCUMENT_POSITION_FOLLOWING and DOCUMENT_POSITION_PRECEDING.

For extra reference those two are defined by tree-order which DOM4 defines as

An object A is preceding an object B if A and B are in the same tree and A comes before B in tree order.

An object A is following an object B if A and B are in the same tree and A comes after B in tree order.

The tree order is preorder, depth-first traversal.

Most modern browsers implement this (including IE9). So you only need something that works in IE8 (I don’t care about IE6/7, but if it works awesome!)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:04:50+00:00Added an answer on May 27, 2026 at 5:04 am
    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;
        }
    }
    

    I wrote it myself. I thought this implementation was bugged but it was a bug in some other code of mine. Seems pretty solid.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm sure there's a really easy way of doing this. I'm trying to take
I want a small library that does DOM4 events . Failing that a sensible
My Predicament: I have some things on my server that I want to connect
example: os.environ['TZ'] = CST+06CDT,M4.1.0,M10.5.0 I can understand CST, 06 (offset from UTC ?? or
Example: Monte Bianco 4.807 France/Italy Monte Rosa 4.634 Italy/Switzerland Dom 4.545 Switzerland/Italy Weisshorn 4.505

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.