summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/css/css-highlight-api/HighlightRegistry-iteration-with-modifications.html
blob: 7e1a0c08f3c795fa7d71cc77f531a10d865e0506 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<!doctype html>
<title>HighlightRegistry iteration with insertions and deletions inbetween</title>
<link rel="help" href="https://drafts.csswg.org/css-highlight-api-1/">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
  'use strict';
  let customHighlight1 = new Highlight();
  let customHighlight2 = new Highlight();
  let highlightName1 = "example1";
  let highlightName2 = "example2";

  // Test insertions using .add
  test(() => {
    let iterator = CSS.highlights[Symbol.iterator]();
    CSS.highlights.set(highlightName1, customHighlight1);
    let element = iterator.next();
    assert_true(element.done, 'The iteration ends although we added a new Highlight after starting the iteration');
    assert_equals(element.value, undefined, 'A Highlight added after starting the iteration is not found during the current iteration');
  }, 'HighlightRegistry iteration is not modified when a new Highlight is added after starting the iteration');

  CSS.highlights.clear();

  test(() => {
    CSS.highlights.set(highlightName1, customHighlight1);
    let iterator = CSS.highlights[Symbol.iterator]();
    CSS.highlights.set(highlightName2, customHighlight2);
    let element = iterator.next();
    assert_false(element.done, 'The iteration doesn\'t end although there was a second Highlight added to the HighlightRegistry after starting the iteration');
    assert_equals(element.value[0], highlightName1, 'The highlight name that was pointed to by the iterator is returned although a second Highlight was added after starting the iteration');
    assert_equals(element.value[1], customHighlight1, 'The Highlight that was pointed to by the iterator is returned although a second Highlight was added after starting the iteration');
    element = iterator.next();
    assert_true(element.done, 'The iteration ends after going through all the Highlights that were in the HighlightRegistry when the iteration started although there was a Highlight addition after starting the iteration');
    assert_equals(element.value, undefined, 'A Highlight added after starting the iteration is not found during the current iteration');
  }, 'HighlightRegistry iteration is not modified when a new Highlight is added after starting the iteration with one Highlight in the HighlightRegistry');

  CSS.highlights.clear();

  // Test deletions using .delete
  test(() => {
    CSS.highlights.set(highlightName1, customHighlight1);
    let iterator = CSS.highlights[Symbol.iterator]();
    CSS.highlights.delete(highlightName1);
    let element = iterator.next();
    assert_false(element.done, 'The iteration doesn\'t end although the Highlight that was pointed to by the iterator was deleted');
    assert_equals(element.value[0], highlightName1, 'The highlight name that was pointed to by the iterator is returned although it was deleted after starting the iteration');
    assert_equals(element.value[1], customHighlight1, 'The Highlight that was pointed to by the iterator is returned although it was deleted after starting the iteration');
    element = iterator.next();
    assert_true(element.done, 'The iteration ends after going through all the highlights although the Highlight that was pointed to by the iterator was deleted after starting the iteration');
    assert_equals(element.value, undefined, '.next() returns undefined when the iteration ends');
  }, 'HighlightRegistry iteration is not modified when the Highlight that was pointed to by the iterator was deleted after starting the iteration');

  CSS.highlights.clear();

  test(() => {
    CSS.highlights.set(highlightName1, customHighlight1);
    CSS.highlights.set(highlightName2, customHighlight2);
    let iterator = CSS.highlights[Symbol.iterator]();
    CSS.highlights.delete(highlightName2);
    let element = iterator.next();
    assert_false(element.done, 'The iteration doesn\'t end although the Highlight following to the one that was pointed to by the iterator was deleted');
    assert_equals(element.value[0], highlightName1, 'The highlight name that was pointed to by the iterator is returned as it should although the next Highlight was deleted immediately after starting the iteration');
    assert_equals(element.value[1], customHighlight1, 'The Highlight that was pointed to by the iterator is returned as it should although the next Highlight was deleted immediately after starting the iteration');
    element = iterator.next();
    assert_false(element.done, 'The iteration doesn\'t end when you call .next twice since the beginning of the iteration although the second Highlight was deleted');
    assert_equals(element.value[0], highlightName2, 'The highlight name that was pointed to by the iterator is returned as it should although the next Highlight was deleted immediately after starting the iteration');
    assert_equals(element.value[1], customHighlight2, 'The Highlight that was pointed to by the iterator is returned as it should although the next Highlight was deleted immediately after starting the iteration');
    element = iterator.next();
    assert_true(element.done, 'The iteration ends after going through all the highlights although the second Highlight was deleted immediately after starting the iteration');
    assert_equals(element.value, undefined, '.next() returns undefined when the iteration ends');
  }, 'HighlightRegistry iteration is not modified when the Highlight that was immediately after the one pointed to by the iterator was deleted after starting the iteration');

  CSS.highlights.clear();

  test(() => {
    CSS.highlights.set(highlightName1, customHighlight1);
    CSS.highlights.set(highlightName2, customHighlight2);
    let iterator = CSS.highlights[Symbol.iterator]();
    let element = iterator.next();
    assert_false(element.done, 'The iteration doesn\'t end when there are still two Highlights to visit');
    assert_equals(element.value[0], highlightName1, 'The highlight name that was pointed to by the iterator is returned as it should');
    assert_equals(element.value[1], customHighlight1, 'The Highlight that was pointed to by the iterator is returned as it should');
    CSS.highlights.delete(highlightName1);
    element = iterator.next();
    assert_false(element.done, 'The iteration doesn\'t end when the Highlight previously visited is deleted and there is still a Highlight to visit');
    assert_equals(element.value[0], highlightName2, 'The highlight name that was pointed to by the iterator is returned as it should although the previous Highlight was deleted after calling .next');
    assert_equals(element.value[1], customHighlight2, 'The Highlight that was pointed to by the iterator is returned as it should although the previous Highlight was deleted after calling .next');
    element = iterator.next();
    assert_true(element.done, 'The iteration ends after going through all the highlights although the first Highlight was deleted after the first call to .next');
    assert_equals(element.value, undefined, '.next() returns undefined when the iteration ends');
  }, 'HighlightRegistry iteration is not modified when a Highlight that was already visited is deleted and there are still Highlights to visit');

  CSS.highlights.clear();

  // Test deletions using .clear
  test(() => {
    CSS.highlights.set(highlightName1, customHighlight1);
    let iterator = CSS.highlights[Symbol.iterator]();
    CSS.highlights.clear();
    let element = iterator.next();
    assert_false(element.done, 'The iteration doesn\'t end although the Highlight that was pointed to by the iterator was deleted using .clear()');
    assert_equals(element.value[0], highlightName1, 'The highlight name that was pointed to by the iterator is returned although it was deleted using .clear() after starting the iteration');
    assert_equals(element.value[1], customHighlight1, 'The Highlight that was pointed to by the iterator is returned although it was deleted using .clear() after starting the iteration');
    element = iterator.next();
    assert_true(element.done, 'The iteration ends after going through all the highlights although the Highlight that was pointed to by the iterator was deleted using .clear() after starting the iteration');
    assert_equals(element.value, undefined, '.next() returns undefined when the iteration ends');
  }, 'HighlightRegistry iteration is not modified when the Highlight that was pointed to by the iterator was deleted using .clear() after starting the iteration');

</script>