summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/domxpath/resolver-callback-interface-cross-realm.tentative.html
blob: 17f7ea78c3a397aeb22d7e8d09e1a4f9611b63fb (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
<!DOCTYPE html>
<meta charset="utf-8">
<title>Cross-realm XPathNSResolver throws TypeError of its associated Realm</title>
<link rel="help" href="https://webidl.spec.whatwg.org/#ref-for-prepare-to-run-script">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<iframe name="evaluateGlobalObject" src="resources/empty-document.html"></iframe>
<iframe name="resolverGlobalObject" src="resources/empty-document.html"></iframe>
<iframe name="lookupNamespaceURIGlobalObject" src="resources/empty-document.html"></iframe>
<iframe name="relevantGlobalObject" src="resources/empty-document.html"></iframe>
<iframe name="incumbentGlobalObject" src="resources/empty-document.html"></iframe>

<script>
setup({ allow_uncaught_exception: true });

const expectedDOMExceptionType = "NAMESPACE_ERR";

test_onload(() => {
  const resolver = new resolverGlobalObject.Object;

  assert_reports_exception(() => {
    assert_throws_dom(expectedDOMExceptionType, evaluateGlobalObject.DOMException, bind_evaluate(resolver));
  });
}, "XPathNSResolver is cross-realm plain object without 'lookupNamespaceURI' property");

test_onload(() => {
  const resolver = new resolverGlobalObject.Object;
  resolver.lookupNamespaceURI = new lookupNamespaceURIGlobalObject.Object;

  assert_reports_exception(() => {
    assert_throws_dom(expectedDOMExceptionType, evaluateGlobalObject.DOMException, bind_evaluate(resolver));
  });
}, "XPathNSResolver is cross-realm plain object with non-callable 'lookupNamespaceURI' property");

test_onload(() => {
  const { proxy, revoke } = resolverGlobalObject.Proxy.revocable(new resolverGlobalObject.Object, {});
  revoke();

  assert_reports_exception(() => {
    assert_throws_dom(expectedDOMExceptionType, evaluateGlobalObject.DOMException, bind_evaluate(proxy));
  });
}, "XPathNSResolver is cross-realm non-callable revoked Proxy");

test_onload(() => {
  const { proxy, revoke } = resolverGlobalObject.Proxy.revocable(new resolverGlobalObject.Function, {});
  revoke();

  assert_reports_exception(() => {
    assert_throws_dom(expectedDOMExceptionType, evaluateGlobalObject.DOMException, bind_evaluate(proxy));
  });
}, "XPathNSResolver is cross-realm callable revoked Proxy");

test_onload(() => {
  const { proxy, revoke } = lookupNamespaceURIGlobalObject.Proxy.revocable(new lookupNamespaceURIGlobalObject.Function, {});
  revoke();

  const resolver = new resolverGlobalObject.Object;
  resolver.lookupNamespaceURI = proxy;

  assert_reports_exception(() => {
    assert_throws_dom(expectedDOMExceptionType, evaluateGlobalObject.DOMException, bind_evaluate(resolver));
  });
}, "XPathNSResolver is cross-realm plain object with revoked Proxy as 'lookupNamespaceURI' property");

function test_onload(fn, desc) {
  async_test(t => { window.addEventListener("load", t.step_func_done(fn)); }, desc);
}

function assert_reports_exception(fn) {
  let error;
  const onErrorHandler = event => {
    error = event.error;
    event.preventDefault();
  };

  resolverGlobalObject.addEventListener("error", onErrorHandler);
  fn();
  resolverGlobalObject.removeEventListener("error", onErrorHandler);

  assert_equals(typeof error, "object");
  assert_equals(error.constructor, evaluateGlobalObject.TypeError);
}

function bind_evaluate(resolver) {
  const boundEvaluate = new incumbentGlobalObject.Function("evaluate", "relevantDocument", "resolver", `
    evaluate.call(relevantDocument, "/foo:bar", relevantDocument.documentElement, resolver);
  `);

  return () => {
    boundEvaluate(evaluateGlobalObject.document.evaluate, relevantGlobalObject.document, resolver);
  };
}
</script>