summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/css/css-highlight-api/HighlightRegistry-maplike-tampered-Map-prototype.html
blob: 3b38bd8f162ed108853abd1752c1e9f93841bcf2 (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
<!doctype html>
<title>HighlightRegistry is a maplike interface that works as expected even if Map.prototype is tampered</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>

<body>
<script>
"use strict";

function tamperMapPrototype() {
  delete Map.prototype.size;

  Map.prototype.entries = null;
  Map.prototype.forEach = undefined;
  Map.prototype.get = "foo";
  Map.prototype.has = 0;
  Map.prototype.keys = Symbol();
  Map.prototype.values = 1;
  Map.prototype[Symbol.iterator] = true;
  Map.prototype.clear = false;
  Map.prototype.delete = "";
  Map.prototype.set = 3.14;

  Object.freeze(Map.prototype);
}

test(() => {
  tamperMapPrototype();

  const highlight = new Highlight(new StaticRange({startContainer: document.body, endContainer: document.body, startOffset: 0, endOffset: 0}));
  const highlightRegister = new HighlightRegister();

  assert_equals(highlightRegister.size, 0);
  highlightRegister.set("foo", highlight);
  assert_equals(highlightRegister.size, 1);

  assert_true(highlightRegister.has("foo"));
  assert_equals([...highlightRegister.entries()][0][0], "foo");

  highlightRegister.clear();
  assert_equals(highlightRegister.size, 0);
  assert_equals(highlightRegister.get("foo"), undefined);

  highlightRegister.set("bar", highlight);
  assert_equals(highlightRegister.get("bar"), highlight);
  assert_equals([...highlightRegister][0][1], highlight);

  highlightRegister.delete("bar");
  assert_equals(highlightRegister.size, 0);
  assert_false(highlightRegister.has("bar"));

  highlightRegister.set("baz", highlight);
  assert_equals([...highlightRegister.keys()][0], "baz");
  assert_equals([...highlightRegister.values()][0], highlight);

  let callbackCalled = false;
  highlightRegister.forEach(() => { callbackCalled = true; });
  assert_true(callbackCalled);
}, "HighlightRegistry is a maplike interface that works as expected even if Map.prototype is tampered.");
</script>