<!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>
