chromium/third_party/blink/web_tests/fast/dom/shadow/compare-document-position.html

<!DOCTYPE html>
<html>
<head>
<script src="../../../resources/js-test.js"></script>
<script src="resources/shadow-dom.js"></script>
</head>
<body>
<div id="console"></div>
<div id="sandbox"></div>
<script>
description("Tests for Node.compareDocumentPosition() for nodes in shadow tree.");

document.getElementById('sandbox').appendChild(
    createDOM('div', {'id': 'a1'},
              createDOM('div', {'id': 'a2'},
                        createShadowRoot(createDOM('div', {'id': 'b1'},
                                                   createDOM('div', {'id': 'b2'}),
                                                   createDOM('div', {'id': 'b3'})))),
              createDOM('div', {'id': 'a3'},
                        createShadowRoot(createDOM('div', {'id': 'c1'})))));

var a1, a2, a3, b1, b2, b3, c1;

function testCompareDocumentPosition()
{
    var sandbox = document.getElementById('sandbox');
    document.body.offsetLeft;

    a1 = document.getElementById('a1');
    a2 = document.getElementById('a2');
    a3 = document.getElementById('a3');
    b1 = getNodeInComposedTree('a2/b1');
    b2 = getNodeInComposedTree('a2/b2');
    b3 = getNodeInComposedTree('a2/b3');
    c1 = getNodeInComposedTree('a3/c1');

    shouldBe('a1.compareDocumentPosition(a2)', 'Node.DOCUMENT_POSITION_CONTAINED_BY | Node.DOCUMENT_POSITION_FOLLOWING');
    shouldBe('a2.compareDocumentPosition(a1)', 'Node.DOCUMENT_POSITION_CONTAINS | Node.DOCUMENT_POSITION_PRECEDING');
    shouldBe('a2.compareDocumentPosition(a3)', 'Node.DOCUMENT_POSITION_FOLLOWING');

    shouldBe('b1.compareDocumentPosition(b2)', 'Node.DOCUMENT_POSITION_CONTAINED_BY | Node.DOCUMENT_POSITION_FOLLOWING');
    shouldBe('b2.compareDocumentPosition(b1)', 'Node.DOCUMENT_POSITION_CONTAINS | Node.DOCUMENT_POSITION_PRECEDING');
    shouldBe('b2.compareDocumentPosition(b3)', 'Node.DOCUMENT_POSITION_FOLLOWING');

    // Nodes in different shadow trees.
    shouldBeNonZero('a1.compareDocumentPosition(b1) & Node.DOCUMENT_POSITION_PRECEDING || a1.compareDocumentPosition(b1) & Node.DOCUMENT_POSITION_FOLLOWING');
    shouldBe('a1.compareDocumentPosition(b1) & Node.DOCUMENT_POSITION_DISCONNECTED', 'Node.DOCUMENT_POSITION_DISCONNECTED');
    shouldBe('a1.compareDocumentPosition(b1) & Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC', 'Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC');
    shouldBe('a1.compareDocumentPosition(b1)', 'a1.compareDocumentPosition(b1)');
    shouldBeNonZero('b1.compareDocumentPosition(c1) & Node.DOCUMENT_POSITION_PRECEDING || b1.compareDocumentPosition(c1) & Node.DOCUMENT_POSITION_FOLLOWING');
    shouldBe('b1.compareDocumentPosition(c1) & Node.DOCUMENT_POSITION_DISCONNECTED', 'Node.DOCUMENT_POSITION_DISCONNECTED');
    shouldBe('b1.compareDocumentPosition(c1) & Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC', 'Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC');
    shouldBe('b1.compareDocumentPosition(c1)', 'b1.compareDocumentPosition(c1)');
}

testCompareDocumentPosition();
</script>
</body>
</html>