summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/domxpath/resolver-callback-interface.html
blob: b43695f4aa56d2e7b8a05d0798657ff1057e3c9b (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<!DOCTYPE html>
<meta charset=utf-8>
<title>XPathNSResolver implements callback interface</title>
<link rel="help" href="https://www.w3.org/TR/DOM-Level-3-XPath/xpath.html#XPathEvaluator">
<link rel="help" href="https://webidl.spec.whatwg.org/#call-a-user-objects-operation">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/invalid_namespace_test.js"></script>
<div id=log></div>
<script>
"use strict";

test(() => {
  let resolverCalls = 0;
  document.evaluate("/foo:bar", document.documentElement, () => {
    resolverCalls++;
    return "";
  });

  assert_equals(resolverCalls, 1);
}, "callable resolver");

test(() => {
  let resolverCalls = 0;
  const resolver = () => {
    resolverCalls++;
    return "";
  };

  document.evaluate("/foo:bar", document.documentElement, resolver);
  document.evaluate("/foo:bar", document.documentElement, resolver);

  assert_equals(resolverCalls, 2);
}, "callable resolver: result is not cached");

promise_test(t => {
  const testError = { name: "test" };
  const resolver = () => {
    throw testError;
  };

  return promise_rejects_exactly(t, testError,
    invalid_namespace_test(t, resolver)
  );
}, "callable resolver: abrupt completion from Call");

test(() => {
  let resolverCalls = 0;
  const resolver = () => {
    resolverCalls++;
    return "";
  };

  let resolverGets = 0;
  Object.defineProperty(resolver, "lookupNamespaceURI", {
    get() {
      resolverGets++;
    },
  });

  document.evaluate("/foo:bar", document.documentElement, resolver);

  assert_equals(resolverCalls, 1);
  assert_equals(resolverGets, 0);
}, "callable resolver: no 'lookupNamespaceURI' lookups");

test(() => {
  let resolverCalls = 0;
  document.evaluate("/foo:bar", document.documentElement, {
    lookupNamespaceURI() {
      resolverCalls++;
      return "";
    },
  });

  assert_equals(resolverCalls, 1);
}, "object resolver");

test(() => {
  let thisValue, prefixArg;
  const resolver = {
    lookupNamespaceURI(prefix) {
      thisValue = this;
      prefixArg = prefix;
      return "";
    },
  };

  document.evaluate("/foo:bar", document.documentElement, resolver);

  assert_equals(thisValue, resolver);
  assert_equals(prefixArg, "foo");
}, "object resolver: this value and `prefix` argument");

test(() => {
  let resolverCalls = 0;
  const lookupNamespaceURI = () => {
    resolverCalls++;
    return "";
  };

  let resolverGets = 0;
  const resolver = {
    get lookupNamespaceURI() {
      resolverGets++;
      return lookupNamespaceURI;
    },
  };

  document.evaluate("/foo:bar", document.documentElement, resolver);
  document.evaluate("/foo:bar", document.documentElement, resolver);

  assert_equals(resolverCalls, 2);
  assert_equals(resolverGets, 2);
}, "object resolver: 'lookupNamespaceURI' is not cached");

promise_test(t => {
  const testError = { name: "test" };
  const resolver = {
    get lookupNamespaceURI() {
      throw testError;
    },
  };

  return promise_rejects_exactly(t, testError,
    invalid_namespace_test(t, resolver)
  );
}, "object resolver: abrupt completion from Get");

promise_test(t => {
  const resolver = {
    lookupNamespaceURI: {},
  };

  return promise_rejects_js(t, TypeError,
    invalid_namespace_test(t, resolver)
  );
}, "object resolver: 'lookupNamespaceURI' is thruthy and not callable");

promise_test(t => {
  return promise_rejects_js(t, TypeError,
    invalid_namespace_test(t, {})
  );
}, "object resolver: 'lookupNamespaceURI' is falsy and not callable");
</script>
</body>