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