Node-compareDocumentPosition.html (3358B)
1 <!doctype html> 2 <title>Node.compareDocumentPosition() tests</title> 3 <link rel=author title="Aryeh Gregor" href=ayg@aryeh.name> 4 <div id=log></div> 5 <script src=/resources/testharness.js></script> 6 <script src=/resources/testharnessreport.js></script> 7 <script src=../common.js></script> 8 <script> 9 "use strict"; 10 11 testNodes.forEach(function(referenceName) { 12 var reference = eval(referenceName); 13 testNodes.forEach(function(otherName) { 14 var other = eval(otherName); 15 test(function() { 16 var result = reference.compareDocumentPosition(other); 17 18 // "If other and reference are the same object, return zero and 19 // terminate these steps." 20 if (other === reference) { 21 assert_equals(result, 0); 22 return; 23 } 24 25 // "If other and reference are not in the same tree, return the result of 26 // adding DOCUMENT_POSITION_DISCONNECTED, 27 // DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC, and either 28 // DOCUMENT_POSITION_PRECEDING or DOCUMENT_POSITION_FOLLOWING, with the 29 // constraint that this is to be consistent, together and terminate these 30 // steps." 31 if (furthestAncestor(reference) !== furthestAncestor(other)) { 32 // TODO: Test that it's consistent. 33 assert_in_array(result, [Node.DOCUMENT_POSITION_DISCONNECTED + 34 Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC + 35 Node.DOCUMENT_POSITION_PRECEDING, 36 Node.DOCUMENT_POSITION_DISCONNECTED + 37 Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC + 38 Node.DOCUMENT_POSITION_FOLLOWING]); 39 return; 40 } 41 42 // "If other is an ancestor of reference, return the result of 43 // adding DOCUMENT_POSITION_CONTAINS to DOCUMENT_POSITION_PRECEDING 44 // and terminate these steps." 45 var ancestor = reference.parentNode; 46 while (ancestor && ancestor !== other) { 47 ancestor = ancestor.parentNode; 48 } 49 if (ancestor === other) { 50 assert_equals(result, Node.DOCUMENT_POSITION_CONTAINS + 51 Node.DOCUMENT_POSITION_PRECEDING); 52 return; 53 } 54 55 // "If other is a descendant of reference, return the result of adding 56 // DOCUMENT_POSITION_CONTAINED_BY to DOCUMENT_POSITION_FOLLOWING and 57 // terminate these steps." 58 ancestor = other.parentNode; 59 while (ancestor && ancestor !== reference) { 60 ancestor = ancestor.parentNode; 61 } 62 if (ancestor === reference) { 63 assert_equals(result, Node.DOCUMENT_POSITION_CONTAINED_BY + 64 Node.DOCUMENT_POSITION_FOLLOWING); 65 return; 66 } 67 68 // "If other is preceding reference return DOCUMENT_POSITION_PRECEDING 69 // and terminate these steps." 70 var prev = previousNode(reference); 71 while (prev && prev !== other) { 72 prev = previousNode(prev); 73 } 74 if (prev === other) { 75 assert_equals(result, Node.DOCUMENT_POSITION_PRECEDING); 76 return; 77 } 78 79 // "Return DOCUMENT_POSITION_FOLLOWING." 80 assert_equals(result, Node.DOCUMENT_POSITION_FOLLOWING); 81 }, referenceName + ".compareDocumentPosition(" + otherName + ")"); 82 }); 83 }); 84 85 testDiv.parentNode.removeChild(testDiv); 86 </script> 87 <!-- vim: set expandtab tabstop=2 shiftwidth=2: -->