summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/css/css-highlight-api/Highlight-setlike.html
blob: 0093f9be232b5544c635ee343e660c4ee4b489e8 (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
<!doctype html>
<title> Highlight has a setlike interface </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>
<div id='testDiv'>abc</div>
<script>
  'use strict';
  let priority = 123;

  test(() => {
    let customHighlight = new Highlight();
    assert_equals(customHighlight.priority, 0, 'Highlight uses 0 as priority by default.');

    customHighlight.priority = priority;
    assert_equals(customHighlight.priority, priority, 'Highlight sets priority correctly.');

    assert_equals(customHighlight.size, 0, 'Highlight starts empty');
  }, 'Highlight initializes empty (if no ranges are provided) and with priority 0.');

  let range0 = new Range();
  let range1 = new Range();
  let range2 = new Range();

  let container = document.getElementById('testDiv');
  let staticRange0 = new StaticRange({startContainer: container, startOffset: 0, endContainer: container, endOffset: 0});
  let staticRange1 = new StaticRange({startContainer: container, startOffset: 0, endContainer: container, endOffset: 0});
  let staticRange2 = new StaticRange({startContainer: container, startOffset: 0, endContainer: container, endOffset: 0});

  let rangeCollections = [[range0,range1,range2], [staticRange0,staticRange1,staticRange2], [range0,staticRange1,range2], [staticRange0,range1,staticRange2]]

  var i;
  for(i=0; i<rangeCollections.length; i++){
    let rangesCombinationDescription = " (using the following combination of ranges [";
    var j;
    for(j=0; j<rangeCollections[i].length; j++){
      if(j!=0) rangesCombinationDescription += ", ";
      rangesCombinationDescription = rangesCombinationDescription + Object.prototype.toString.call(rangeCollections[i][j]);
    }
    rangesCombinationDescription += "])";

    test(() => {
      let customHighlight = new Highlight();
      assert_false(customHighlight.has(rangeCollections[i][0]), 'Highlight.has returns false when it doesn\'t contain the range which is passed as the argument');
      assert_false(customHighlight.has(rangeCollections[i][1]), 'Highlight.has returns false when it doesn\'t contain the range which is passed as the argument');
      assert_false(customHighlight.has(rangeCollections[i][2]), 'Highlight.has returns false when it doesn\'t contain the range which is passed as the argument');
      customHighlight.add(rangeCollections[i][0]);
      assert_true(customHighlight.has(rangeCollections[i][0]), 'Highlight.has returns true when it contains the range which is passed as the argument');
      assert_false(customHighlight.has(rangeCollections[i][1]), 'Highlight.has returns false when it doesn\'t contain the range which is passed as the argument');
      assert_false(customHighlight.has(rangeCollections[i][2]), 'Highlight.has returns false when it doesn\'t contain the range which is passed as the argument');

      assert_equals(customHighlight.size, 1, 'Highlight.size is 1 after only adding 1 range');
      customHighlight.add(rangeCollections[i][0]);
      assert_equals(customHighlight.size, 1, 'Highlight.size is 1 after only adding same range twice');

      customHighlight.add(rangeCollections[i][1]);
      assert_true(customHighlight.has(rangeCollections[i][0]), 'Highlight.has returns true when it contains the range which is passed as the argument');
      assert_true(customHighlight.has(rangeCollections[i][1]), 'Highlight.has returns true when it contains the range which is passed as the argument');
      assert_false(customHighlight.has(rangeCollections[i][2]), 'Highlight.has returns false when it doesn\'t contain the range which is passed as the argument');

      assert_equals(customHighlight.size, 2, 'Highlight.size is 2 after only adding two different ranges');
    }, 'Highlight add and has methods work as expected' + rangesCombinationDescription);

    test(() => {
      let customHighlight = new Highlight(rangeCollections[i][0], rangeCollections[i][1]);
      assert_false(customHighlight.delete(rangeCollections[i][2]), 'Highlight.delete returns false when trying to delete a range that is not in the highlight');
      assert_true(customHighlight.delete(rangeCollections[i][1]), 'Highlight.delete returns true when trying to delete a range that is in the highlight');
      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');
      assert_true(customHighlight.delete(rangeCollections[i][0]), 'Highlight.delete returns true when trying to delete a range that is in the highlight');
      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');
    }, 'Highlight delete method works as expected' + rangesCombinationDescription);

    test(() => {
      let customHighlight = new Highlight(rangeCollections[i][0], rangeCollections[i][0]);
      assert_true(customHighlight.has(rangeCollections[i][0]), 'Highlight.has returns true when it is called with the range used twice in the constructor');
      assert_equals(customHighlight.size, 1, 'Highlight behaves like a set when constructing it with two equal ranges.');

      customHighlight = new Highlight(rangeCollections[i][0], rangeCollections[i][1], rangeCollections[i][0], rangeCollections[i][1]);
      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');
      assert_true(customHighlight.has(rangeCollections[i][1]), 'Highlight.has returns true when it is called with the other range used twice in the constructor');
      assert_equals(customHighlight.size, 2, 'Highlight behaves like a set when constructing it with two pairs of equal ranges.');
    }, 'Highlight constructor behaves like a set when using equal ranges' + rangesCombinationDescription);

    test(() => {
      let customHighlight = new Highlight(rangeCollections[i][0]);
      assert_true(customHighlight.has(rangeCollections[i][0]), 'Highlight.has returns true when it is called with the range used in its constructor');
      assert_equals(customHighlight.size, 1, 'Highlight.size is 1 after constructing it with one range');
    }, 'Highlight constructor works as expected when called with one range' + rangesCombinationDescription);

    test(() => {
      let customHighlight = new Highlight(rangeCollections[i][0], rangeCollections[i][1]);
      assert_true(customHighlight.has(rangeCollections[i][0]), 'Highlight.has returns true when it is called with the first range used in its constructor');
      assert_true(customHighlight.has(rangeCollections[i][1]), 'Highlight.has returns true when it is called with the second range used in its constructor');
      assert_equals(customHighlight.size, 2, 'Highlight.size is 2 after constructing it with two ranges');
    }, 'Highlight constructor works as expected when called with two ranges' + rangesCombinationDescription);

    test(() => {
      let customHighlight = new Highlight(rangeCollections[i][0], rangeCollections[i][1]);
      assert_equals(customHighlight.size, 2, 'Highlight has size 2 after constructing it with two ranges');
      customHighlight.clear();
      assert_equals(customHighlight.size, 0, 'Highlight becomes empty after executing clear()');
      customHighlight.clear();
      assert_equals(customHighlight.size, 0, 'Highlight is still empty after executing clear() twice');
    }, 'Highlight clear method works as expected' + rangesCombinationDescription);
  }
</script>