fn-id.html (2057B)
1 <!DOCTYPE html> 2 <link rel="help" href="https://www.w3.org/TR/1999/REC-xpath-19991116/#function-id"> 3 <body> 4 <script src="/resources/testharness.js"></script> 5 <script src="/resources/testharnessreport.js"></script> 6 <script> 7 // Test the id() function with various scenarios 8 function testIdFunction(expression, xmlString, expectedIds) { 9 let doc = (new DOMParser()).parseFromString(xmlString, 'text/xml'); 10 test(() => { 11 let result = doc.evaluate(expression, doc.documentElement, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); 12 assert_equals(result.resultType, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE); 13 let actualIds = []; 14 for (let i = 0; i < result.snapshotLength; i++) { 15 actualIds.push(result.snapshotItem(i).getAttribute('id')); 16 } 17 actualIds.sort(); 18 expectedIds.sort(); 19 assert_array_equals(actualIds, expectedIds, `Expected IDs ${expectedIds}, got ${actualIds}`); 20 }, `${expression}: ${doc.documentElement.outerHTML}`); 21 } 22 23 // Test single ID 24 testIdFunction('id("test1")', '<root><div id="test1">Match</div></root>', ['test1']); 25 26 // Test multiple IDs in space-separated string 27 testIdFunction('id("test1 test2")', '<root><div id="test1">First</div><div id="test2">Second</div></root>', ['test1', 'test2']); 28 29 // Test non-existent ID 30 testIdFunction('id("nonexistent")', '<root><div id="test1">No match</div></root>', []); 31 32 // Test mixed case IDs (should be case-sensitive) 33 testIdFunction('id("Test1")', '<root><div id="test1">No match</div></root>', []); 34 35 // Test multiple elements with same ID (should return all) 36 testIdFunction('id("duplicate")', '<root><div id="duplicate">First</div><div id="duplicate">Second</div></root>', ['duplicate', 'duplicate']); 37 38 // Test IDs with special characters 39 testIdFunction('id("test-1")', '<root><div id="test-1">Match</div></root>', ['test-1']); 40 41 // Test empty ID string 42 testIdFunction('id("")', '<root><div id="">Empty ID</div></root>', []); 43 44 // Test whitespace in ID string 45 testIdFunction('id(" test1 ")', '<root><div id="test1">Match</div></root>', ['test1']); 46 </script> 47 </body>