|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | + <title>Invalidation of iterators over XPath results</title> |
| 5 | + <link rel=" author" title=" Simon Wülker" href=" mailto:[email protected]" > |
| 6 | + <link rel="help" href="https://www.w3.org/TR/DOM-Level-3-XPath/xpath.html#XPathResult-iterateNext"> |
| 7 | + <script src="/resources/testharness.js"></script> |
| 8 | + <script src="/resources/testharnessreport.js"></script> |
| 9 | +</head> |
| 10 | +<body> |
| 11 | + <ul id="list"> |
| 12 | + <li id="first-child"></li> |
| 13 | + <li id="second-child"></li> |
| 14 | + </ul> |
| 15 | + <script> |
| 16 | + function make_xpath_query(result_type) { |
| 17 | + return document.evaluate( |
| 18 | + "//li", |
| 19 | + document, |
| 20 | + null, |
| 21 | + result_type, |
| 22 | + null |
| 23 | + ); |
| 24 | + } |
| 25 | + |
| 26 | + function invalidate_iterator(test) { |
| 27 | + let new_element = document.createElement("li"); |
| 28 | + document.getElementById("list").appendChild(new_element); |
| 29 | + test.add_cleanup(() => { |
| 30 | + new_element.remove(); |
| 31 | + }) |
| 32 | + } |
| 33 | + |
| 34 | + test((t) => { |
| 35 | + let iterator = make_xpath_query(XPathResult.ORDERED_NODE_ITERATOR_TYPE); |
| 36 | + |
| 37 | + assert_equals(iterator.iterateNext(), document.getElementById("first-child")); |
| 38 | + assert_equals(iterator.iterateNext(), document.getElementById("second-child")); |
| 39 | + assert_equals(iterator.iterateNext(), null); |
| 40 | + assert_false(iterator.invalidIteratorState); |
| 41 | + }, "Using an ordered iterator without modifying the dom should yield the expected elements in correct order without errors."); |
| 42 | + |
| 43 | + test((t) => { |
| 44 | + let iterator = make_xpath_query(XPathResult.UNORDERED_NODE_ITERATOR_TYPE); |
| 45 | + |
| 46 | + assert_not_equals(iterator.iterateNext(), null); |
| 47 | + assert_not_equals(iterator.iterateNext(), null); |
| 48 | + assert_equals(iterator.iterateNext(), null); |
| 49 | + assert_false(iterator.invalidIteratorState); |
| 50 | + }, "Using an unordered iterator without modifying the dom should yield the correct number of elements without errors."); |
| 51 | + |
| 52 | + test((t) => { |
| 53 | + let non_iterator_query = make_xpath_query(XPathResult.BOOLEAN_TYPE); |
| 54 | + |
| 55 | + assert_false(non_iterator_query.invalidIteratorState); |
| 56 | + invalidate_iterator(t); |
| 57 | + assert_false(non_iterator_query.invalidIteratorState); |
| 58 | + }, "invalidIteratorState should be false for non-iterable results."); |
| 59 | + |
| 60 | + test((t) => { |
| 61 | + let non_iterator_query = make_xpath_query(XPathResult.BOOLEAN_TYPE); |
| 62 | + |
| 63 | + assert_throws_js(TypeError, () => non_iterator_query.iterateNext()); |
| 64 | + }, "Calling iterateNext on a non-iterable XPathResult should throw a TypeError."); |
| 65 | + |
| 66 | + test((t) => { |
| 67 | + let non_iterator_query = make_xpath_query(XPathResult.BOOLEAN_TYPE); |
| 68 | + invalidate_iterator(t); |
| 69 | + assert_throws_js(TypeError, () => non_iterator_query.iterateNext()); |
| 70 | + }, "Calling iterateNext on a non-iterable XPathResult after modifying the DOM should throw a TypeError."); |
| 71 | + |
| 72 | + test((t) => { |
| 73 | + let iterator = make_xpath_query(XPathResult.ORDERED_NODE_ITERATOR_TYPE); |
| 74 | + |
| 75 | + iterator.iterateNext(); |
| 76 | + |
| 77 | + invalidate_iterator(t); |
| 78 | + |
| 79 | + assert_throws_dom( |
| 80 | + "InvalidStateError", |
| 81 | + () => iterator.iterateNext(), |
| 82 | + ); |
| 83 | + }, "Calling iterateNext after having modified the DOM should throw an exception."); |
| 84 | + |
| 85 | + test((t) => { |
| 86 | + let iterator = make_xpath_query(XPathResult.ORDERED_NODE_ITERATOR_TYPE); |
| 87 | + |
| 88 | + iterator.iterateNext(); |
| 89 | + iterator.iterateNext(); |
| 90 | + |
| 91 | + invalidate_iterator(t); |
| 92 | + |
| 93 | + assert_throws_dom( |
| 94 | + "InvalidStateError", |
| 95 | + () => iterator.iterateNext(), |
| 96 | + ); |
| 97 | + }, "Calling iterateNext after having modified the DOM should throw an exception even if the iterator is exhausted."); |
| 98 | + </script> |
| 99 | +</body> |
| 100 | +</html> |
0 commit comments