tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

HighlightRegistry-iteration.html (12446B)


      1 <!doctype html>
      2 <title>HighlightRegistry iteration</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 <script>
      7  'use strict';
      8  let customHighlight1 = new Highlight();
      9  let customHighlight2 = new Highlight();
     10  let highlightName1 = "example1";
     11  let highlightName2 = "example2";
     12 
     13  function getIterator(iterationInitialization){
     14    var iterator;
     15    if(iterationInitialization === "CSS.highlights[Symbol.iterator]()"){
     16      iterator = CSS.highlights[Symbol.iterator]();
     17    }
     18    else if(iterationInitialization === "CSS.highlights.entries()"){
     19      iterator = CSS.highlights.entries();
     20    }
     21    else if(iterationInitialization === "CSS.highlights.values()"){
     22      iterator = CSS.highlights.values();
     23    }
     24    else if(iterationInitialization === "CSS.highlights.keys()"){
     25      iterator = CSS.highlights.keys();
     26    }
     27    return iterator;
     28  }
     29 
     30 
     31  // Test .keys()
     32 
     33  let iterationInitialization = "CSS.highlights.keys()";
     34  test(() => {
     35    let iterator = getIterator(iterationInitialization);
     36    let element = iterator.next();
     37    assert_true(element.done, 'HighlightRegistry is iterable and .next() returns an element with .done===true when there are no more highlight names to iterate');
     38    assert_equals(element.value, undefined, 'HighlightRegistry is iterable and .next() returns an element with .value undefined when there are no more highlight names to iterate.');
     39  }, 'HighlightRegistry can be iterated when it\'s empty initializing the iterator with ' + iterationInitialization);
     40 
     41  test(() => {
     42    CSS.highlights.set(highlightName1, customHighlight1);
     43    let iterator = getIterator(iterationInitialization);
     44    let element = iterator.next();
     45    assert_false(element.done, 'HighlightRegistry is iterable and .next() returns an element with .done===false when the iteration didn\'t go past the last highlight name');
     46    assert_equals(element.value, highlightName1, '.next() returns an element with .value corresponding to the first highlight name added to the HighlightRegistry');
     47    element = iterator.next();
     48    assert_true(element.done, 'HighlightRegistry is iterable and .next() returns an element with .done===true when there are no more highlight names to iterate');
     49    assert_equals(element.value, undefined, 'HighlightRegistry is iterable and .next() returns an element with .value undefined when there are no more highlight names to iterate.');
     50  }, 'HighlightRegistry can be iterated over all of its highlight names after adding one of them initializing the iterator with ' + iterationInitialization);
     51 
     52  test(() => {
     53    CSS.highlights.set(highlightName2, customHighlight2);
     54    let iterator = getIterator(iterationInitialization);
     55    let element = iterator.next();
     56    assert_false(element.done, 'HighlightRegistry is iterable and .next() returns an element with .done===false when the iteration didn\'t go past the last highlight');
     57    assert_equals(element.value, highlightName1, '.next() returns an element with .value corresponding to the first highlight name added to the HighlightRegistry');
     58    element = iterator.next();
     59    assert_false(element.done, 'HighlightRegistry is iterable and .next() returns an element with .done===false when the iteration didn\'t go past the last highlight');
     60    assert_equals(element.value, highlightName2, '.next() returns an element with .value corresponding to the second highlight name added to the HighlightRegistry');
     61    element = iterator.next();
     62    assert_true(element.done, 'HighlightRegistry is iterable and .next() returns an element with .done===true when there are no more highlight names to iterate');
     63    assert_equals(element.value, undefined, 'HighlightRegistry is iterable and .next() returns an element with .value undefined when there are no more highlight names to iterate.');
     64  }, 'HighlightRegistry can be iterated over all of its highlight names after adding two of them initializing the iterator with ' + iterationInitialization);
     65 
     66  CSS.highlights.clear();
     67 
     68 
     69  // Test .values()
     70 
     71  iterationInitialization = "CSS.highlights.values()";
     72  test(() => {
     73    let iterator = getIterator(iterationInitialization);
     74    let element = iterator.next();
     75    assert_true(element.done, 'HighlightRegistry is iterable and .next() returns an element with .done===true when there are no more highlights to iterate');
     76    assert_equals(element.value, undefined, 'HighlightRegistry is iterable and .next() returns an element with .value undefined when there are no more highlights to iterate.');
     77  }, 'HighlightRegistry can be iterated when it\'s empty initializing the iterator with ' + iterationInitialization);
     78 
     79  test(() => {
     80    CSS.highlights.set(highlightName1, customHighlight1);
     81    let iterator = getIterator(iterationInitialization);
     82    let element = iterator.next();
     83    assert_false(element.done, 'HighlightRegistry is iterable and .next() returns an element with .done===false when the iteration didn\'t go past the last highlight');
     84    assert_equals(element.value, customHighlight1, '.next() returns an element with .value corresponding to the first Highlight added to the HighlightRegistry');
     85    element = iterator.next();
     86    assert_true(element.done, 'HighlightRegistry is iterable and .next() returns an element with .done===true when there are no more highlights to iterate');
     87    assert_equals(element.value, undefined, 'HighlightRegistry is iterable and .next() returns an element with .value undefined when there are no more highlights to iterate.');
     88  }, 'HighlightRegistry can be iterated over all of its Highlights after adding one of them initializing the iterator with ' + iterationInitialization);
     89 
     90  test(() => {
     91    CSS.highlights.set(highlightName2, customHighlight2);
     92    let iterator = getIterator(iterationInitialization);
     93    let element = iterator.next();
     94    assert_false(element.done, 'HighlightRegistry is iterable and .next() returns an element with .done===false when the iteration didn\'t go past the last highlight');
     95    assert_equals(element.value, customHighlight1, '.next() returns an element with .value corresponding to the first Highlight added to the HighlightRegistry');
     96    element = iterator.next();
     97    assert_false(element.done, 'HighlightRegistry is iterable and .next() returns an element with .done===false when the iteration didn\'t go past the last highlight');
     98    assert_equals(element.value, customHighlight2, '.next() returns an element with .value corresponding to the second Highlight added to the HighlightRegistry');
     99    element = iterator.next();
    100    assert_true(element.done, 'HighlightRegistry is iterable and .next() returns an element with .done===true when there are no more highlights to iterate');
    101    assert_equals(element.value, undefined, 'HighlightRegistry is iterable and .next() returns an element with .value undefined when there are no more highlights to iterate.');
    102  }, 'HighlightRegistry can be iterated over all of its Highlights after adding two of them initializing the iterator with ' + iterationInitialization);
    103 
    104  CSS.highlights.clear();
    105 
    106 
    107  // Test [Symbol.iterator]() and .entries()
    108 
    109  let iterationInitializationCollection = ["CSS.highlights[Symbol.iterator]()", "CSS.highlights.entries()"];
    110  for(let iterationInitialization of iterationInitializationCollection){
    111    test(() => {
    112      let iterator = getIterator(iterationInitialization);
    113      let element = iterator.next();
    114      assert_true(element.done, 'HighlightRegistry is iterable and .next() returns an element with .done===true when there are no more highlights to iterate');
    115      assert_equals(element.value, undefined, 'HighlightRegistry is iterable and .next() returns an element with .value undefined when there are no more highlights to iterate.');
    116    }, 'HighlightRegistry can be iterated when it\'s empty initializing the iterator with ' + iterationInitialization);
    117 
    118    test(() => {
    119      CSS.highlights.set(highlightName1, customHighlight1);
    120      let iterator = getIterator(iterationInitialization);
    121      let element = iterator.next();
    122      assert_false(element.done, 'HighlightRegistry is iterable and .next() returns an element with .done===false when the iteration didn\'t go past the last highlight');
    123      assert_equals(element.value[0], highlightName1, '.next() returns an element with .value[0] corresponding to the first Highlight name added to the HighlightRegistry');
    124      assert_equals(element.value[1], customHighlight1, '.next() returns an element with .value[1] corresponding to the first Highlight added to the HighlightRegistry');
    125      element = iterator.next();
    126      assert_true(element.done, 'HighlightRegistry is iterable and .next() returns an element with .done===true when there are no more highlights to iterate');
    127      assert_equals(element.value, undefined, 'HighlightRegistry is iterable and .next() returns an element with .value undefined when there are no more highlights to iterate.');
    128    }, 'HighlightRegistry can be iterated over all of its Highlights after adding one of them initializing the iterator with ' + iterationInitialization);
    129 
    130    test(() => {
    131      CSS.highlights.set(highlightName2, customHighlight2);
    132      let iterator = getIterator(iterationInitialization);
    133      let element = iterator.next();
    134      assert_false(element.done, 'HighlightRegistry is iterable and .next() returns an element with .done===false when the iteration didn\'t go past the last highlight');
    135      assert_equals(element.value[0], highlightName1, '.next() returns an element with .value[0] corresponding to the first Highlight name added to the HighlightRegistry');
    136      assert_equals(element.value[1], customHighlight1, '.next() returns an element with .value[1] corresponding to the first Highlight added to the HighlightRegistry');
    137      element = iterator.next();
    138      assert_false(element.done, 'HighlightRegistry is iterable and .next() returns an element with .done===false when the iteration didn\'t go past the last highlight');
    139      assert_equals(element.value[0], highlightName2, '.next() returns an element with .value[0] corresponding to the second Highlight name added to the HighlightRegistry');
    140      assert_equals(element.value[1], customHighlight2, '.next() returns an element with .value[1] corresponding to the second Highlight added to the HighlightRegistry');
    141      element = iterator.next();
    142      assert_true(element.done, 'HighlightRegistry is iterable and .next() returns an element with .done===true when there are no more highlights to iterate');
    143      assert_equals(element.value, undefined, 'HighlightRegistry is iterable and .next() returns an element with .value undefined when there are no more highlights to iterate.');
    144    }, 'HighlightRegistry can be iterated over all of its Highlights after adding two of them initializing the iterator with ' + iterationInitialization);
    145 
    146    CSS.highlights.clear();
    147  }
    148 
    149 
    150  // Test .forEach
    151 
    152  function compareArrays(array1, array2){
    153    if(array1.length != array2.length){
    154      return false;
    155    }
    156    for(let index=0; index<array1.length; ++index){
    157      if(array1[index] != array2[index])
    158        return false;
    159    }
    160    return true;
    161  }
    162 
    163  let expectedResult = [];
    164 
    165  test(() => {
    166    let actualResult = [];
    167    CSS.highlights.forEach((highlight) => {actualResult.push(highlight);});
    168    assert_true(compareArrays(actualResult, expectedResult), 'The highlights seen match the highlights added');
    169  }, 'HighlightRegistry can be iterated through when it\'s empty using forEach.');
    170 
    171  test(() => {
    172    CSS.highlights.set(highlightName1, customHighlight1);
    173    expectedResult.push(customHighlight1);
    174    let actualResult = [];
    175    CSS.highlights.forEach((highlight) => {actualResult.push(highlight);});
    176    assert_true(compareArrays(actualResult, expectedResult), 'The highlights seen match the highlights added');
    177  }, 'HighlightRegistry can be iterated through when it has one Highlight using forEach.');
    178 
    179  test(() => {
    180    CSS.highlights.set(highlightName2, customHighlight2);
    181    expectedResult.push(customHighlight2);
    182    let actualResult = [];
    183    CSS.highlights.forEach((highlight) => {actualResult.push(highlight);});
    184    assert_true(compareArrays(actualResult, expectedResult), 'The highlights seen match the highlights added');
    185  }, 'HighlightRegistry can be iterated through when it has two Highlights using forEach.');
    186 </script>