Highlight-setlike.html (8493B)
1 <!doctype html> 2 <title> Highlight has a setlike interface </title> 3 <link rel="help" href="https://drafts.csswg.org/css-highlight-api-1/"> 4 <script src="/resources/testharness.js"></script> 5 <script src="/resources/testharnessreport.js"></script> 6 <div id='testDiv'>abc</div> 7 <script> 8 'use strict'; 9 let priority = 123; 10 11 test(() => { 12 let customHighlight = new Highlight(); 13 assert_equals(customHighlight.priority, 0, 'Highlight uses 0 as priority by default.'); 14 15 customHighlight.priority = priority; 16 assert_equals(customHighlight.priority, priority, 'Highlight sets priority correctly.'); 17 18 assert_equals(customHighlight.size, 0, 'Highlight starts empty'); 19 }, 'Highlight initializes empty (if no ranges are provided) and with priority 0.'); 20 21 let range0 = new Range(); 22 let range1 = new Range(); 23 let range2 = new Range(); 24 25 let container = document.getElementById('testDiv'); 26 let staticRange0 = new StaticRange({startContainer: container, startOffset: 0, endContainer: container, endOffset: 0}); 27 let staticRange1 = new StaticRange({startContainer: container, startOffset: 0, endContainer: container, endOffset: 0}); 28 let staticRange2 = new StaticRange({startContainer: container, startOffset: 0, endContainer: container, endOffset: 0}); 29 30 let rangeCollections = [[range0,range1,range2], [staticRange0,staticRange1,staticRange2], [range0,staticRange1,range2], [staticRange0,range1,staticRange2]] 31 32 var i; 33 for(i=0; i<rangeCollections.length; i++){ 34 let rangesCombinationDescription = " (using the following combination of ranges ["; 35 var j; 36 for(j=0; j<rangeCollections[i].length; j++){ 37 if(j!=0) rangesCombinationDescription += ", "; 38 rangesCombinationDescription = rangesCombinationDescription + Object.prototype.toString.call(rangeCollections[i][j]); 39 } 40 rangesCombinationDescription += "])"; 41 42 test(() => { 43 let customHighlight = new Highlight(); 44 assert_false(customHighlight.has(rangeCollections[i][0]), 'Highlight.has returns false when it doesn\'t contain the range which is passed as the argument'); 45 assert_false(customHighlight.has(rangeCollections[i][1]), 'Highlight.has returns false when it doesn\'t contain the range which is passed as the argument'); 46 assert_false(customHighlight.has(rangeCollections[i][2]), 'Highlight.has returns false when it doesn\'t contain the range which is passed as the argument'); 47 customHighlight.add(rangeCollections[i][0]); 48 assert_true(customHighlight.has(rangeCollections[i][0]), 'Highlight.has returns true when it contains the range which is passed as the argument'); 49 assert_false(customHighlight.has(rangeCollections[i][1]), 'Highlight.has returns false when it doesn\'t contain the range which is passed as the argument'); 50 assert_false(customHighlight.has(rangeCollections[i][2]), 'Highlight.has returns false when it doesn\'t contain the range which is passed as the argument'); 51 52 assert_equals(customHighlight.size, 1, 'Highlight.size is 1 after only adding 1 range'); 53 customHighlight.add(rangeCollections[i][0]); 54 assert_equals(customHighlight.size, 1, 'Highlight.size is 1 after only adding same range twice'); 55 56 customHighlight.add(rangeCollections[i][1]); 57 assert_true(customHighlight.has(rangeCollections[i][0]), 'Highlight.has returns true when it contains the range which is passed as the argument'); 58 assert_true(customHighlight.has(rangeCollections[i][1]), 'Highlight.has returns true when it contains the range which is passed as the argument'); 59 assert_false(customHighlight.has(rangeCollections[i][2]), 'Highlight.has returns false when it doesn\'t contain the range which is passed as the argument'); 60 61 assert_equals(customHighlight.size, 2, 'Highlight.size is 2 after only adding two different ranges'); 62 }, 'Highlight add and has methods work as expected' + rangesCombinationDescription); 63 64 test(() => { 65 let customHighlight = new Highlight(); 66 customHighlight.add(rangeCollections[i][0]).add(rangeCollections[i][1]); 67 assert_true(customHighlight.has(rangeCollections[i][0]), 'Highlight.has returns true when it contains the first range added'); 68 assert_true(customHighlight.has(rangeCollections[i][1]), 'Highlight.has returns true when it contains the second range added'); 69 assert_false(customHighlight.has(rangeCollections[i][2]), 'Highlight.has returns false when it doesn\'t contain the range which is passed as the argument'); 70 assert_equals(customHighlight.size, 2, 'Highlight.size is 2 after adding two different ranges by chaining the add method'); 71 }, "Highlight add method is chainable" + rangesCombinationDescription); 72 73 test(() => { 74 let customHighlight = new Highlight(rangeCollections[i][0], rangeCollections[i][1]); 75 assert_false(customHighlight.delete(rangeCollections[i][2]), 'Highlight.delete returns false when trying to delete a range that is not in the highlight'); 76 assert_true(customHighlight.delete(rangeCollections[i][1]), 'Highlight.delete returns true when trying to delete a range that is in the highlight'); 77 assert_false(customHighlight.delete(rangeCollections[i][1]), 'Highlight.delete returns false when trying to delete a range that was in the highlight before but it\'s not there anymore'); 78 assert_true(customHighlight.delete(rangeCollections[i][0]), 'Highlight.delete returns true when trying to delete a range that is in the highlight'); 79 assert_false(customHighlight.delete(rangeCollections[i][0]), 'Highlight.delete returns false when trying to delete a range that was in the highlight before but it\'s not there anymore'); 80 }, 'Highlight delete method works as expected' + rangesCombinationDescription); 81 82 test(() => { 83 let customHighlight = new Highlight(rangeCollections[i][0], rangeCollections[i][0]); 84 assert_true(customHighlight.has(rangeCollections[i][0]), 'Highlight.has returns true when it is called with the range used twice in the constructor'); 85 assert_equals(customHighlight.size, 1, 'Highlight behaves like a set when constructing it with two equal ranges.'); 86 87 customHighlight = new Highlight(rangeCollections[i][0], rangeCollections[i][1], rangeCollections[i][0], rangeCollections[i][1]); 88 assert_true(customHighlight.has(rangeCollections[i][0]), 'Highlight.has returns true when it is called with one of the ranges used twice in the constructor'); 89 assert_true(customHighlight.has(rangeCollections[i][1]), 'Highlight.has returns true when it is called with the other range used twice in the constructor'); 90 assert_equals(customHighlight.size, 2, 'Highlight behaves like a set when constructing it with two pairs of equal ranges.'); 91 }, 'Highlight constructor behaves like a set when using equal ranges' + rangesCombinationDescription); 92 93 test(() => { 94 let customHighlight = new Highlight(rangeCollections[i][0]); 95 assert_true(customHighlight.has(rangeCollections[i][0]), 'Highlight.has returns true when it is called with the range used in its constructor'); 96 assert_equals(customHighlight.size, 1, 'Highlight.size is 1 after constructing it with one range'); 97 }, 'Highlight constructor works as expected when called with one range' + rangesCombinationDescription); 98 99 test(() => { 100 let customHighlight = new Highlight(rangeCollections[i][0], rangeCollections[i][1]); 101 assert_true(customHighlight.has(rangeCollections[i][0]), 'Highlight.has returns true when it is called with the first range used in its constructor'); 102 assert_true(customHighlight.has(rangeCollections[i][1]), 'Highlight.has returns true when it is called with the second range used in its constructor'); 103 assert_equals(customHighlight.size, 2, 'Highlight.size is 2 after constructing it with two ranges'); 104 }, 'Highlight constructor works as expected when called with two ranges' + rangesCombinationDescription); 105 106 test(() => { 107 let customHighlight = new Highlight(rangeCollections[i][0], rangeCollections[i][1]); 108 assert_equals(customHighlight.size, 2, 'Highlight has size 2 after constructing it with two ranges'); 109 customHighlight.clear(); 110 assert_equals(customHighlight.size, 0, 'Highlight becomes empty after executing clear()'); 111 customHighlight.clear(); 112 assert_equals(customHighlight.size, 0, 'Highlight is still empty after executing clear() twice'); 113 }, 'Highlight clear method works as expected' + rangesCombinationDescription); 114 } 115 </script>