@atlaskit/top-layer 0.2.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +35 -0
- package/dist/cjs/testing/polyfill.js +817 -0
- package/dist/cjs/testing/to-be-visible.js +92 -0
- package/dist/es2019/testing/polyfill.js +831 -0
- package/dist/es2019/testing/to-be-visible.js +85 -0
- package/dist/esm/testing/polyfill.js +815 -0
- package/dist/esm/testing/to-be-visible.js +89 -0
- package/dist/types/testing/polyfill.d.ts +25 -0
- package/dist/types/testing/to-be-visible.d.ts +23 -0
- package/dist/types-ts4.5/testing/polyfill.d.ts +25 -0
- package/dist/types-ts4.5/testing/to-be-visible.d.ts +23 -0
- package/package.json +3 -2
- package/testing/polyfill/package.json +17 -0
- package/testing/to-be-visible/package.json +17 -0
|
@@ -0,0 +1,817 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _bindEventListener = require("bind-event-listener");
|
|
4
|
+
/* eslint-disable compat/compat */
|
|
5
|
+
/**
|
|
6
|
+
* Popover API & HTMLDialogElement polyfills for jsdom.
|
|
7
|
+
*
|
|
8
|
+
* jsdom does not implement the Popover API or the HTMLDialogElement methods
|
|
9
|
+
* (showModal, show, close). This polyfill provides faithful implementations,
|
|
10
|
+
* including timing semantics validated against Chromium via Playwright.
|
|
11
|
+
*
|
|
12
|
+
* Behaviour:
|
|
13
|
+
* - beforetoggle: sync, cancelable on popover show only. toggle: async task, never cancelable.
|
|
14
|
+
* - Rapid show/hide coalesces into one toggle event (originalOldState to finalNewState).
|
|
15
|
+
* - Dialog: showModal/show fire beforetoggle+toggle. close fires toggle then close event.
|
|
16
|
+
* - Popover open state tracked via `data-popover-open` (no `:popover-open` in jsdom).
|
|
17
|
+
* - Auto popovers: maintain an open stack, perform light-dismiss on document mousedown
|
|
18
|
+
* outside the open chain, and dismiss the topmost on Escape.
|
|
19
|
+
* - On open, removes any inline `opacity: 0` from the popover element so callers that
|
|
20
|
+
* rely on a `ResizeObserver` reveal callback (e.g. `use-anchor-positioning`) become
|
|
21
|
+
* visible in jsdom, where layout-driven RO callbacks never fire.
|
|
22
|
+
*
|
|
23
|
+
* Companion `toBeVisible` patch lives at `./to-be-visible`.
|
|
24
|
+
*
|
|
25
|
+
* Importing this module has the side-effect of patching the relevant
|
|
26
|
+
* prototypes. It is safe to import multiple times; each branch is guarded
|
|
27
|
+
* to avoid overwriting native implementations or re-patching.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* e.g. `<div#my-id.class-a.class-b>`
|
|
32
|
+
*/
|
|
33
|
+
function describeElement(_ref) {
|
|
34
|
+
var element = _ref.element;
|
|
35
|
+
var tag = element.tagName ? element.tagName.toLowerCase() : 'unknown';
|
|
36
|
+
var id = element.id ? "#".concat(element.id) : '';
|
|
37
|
+
var classes = element.classList && element.classList.length > 0 ? ".".concat(Array.from(element.classList).join('.')) : '';
|
|
38
|
+
return "<".concat(tag).concat(id).concat(classes, ">");
|
|
39
|
+
}
|
|
40
|
+
function createToggleEvent(_ref2) {
|
|
41
|
+
var type = _ref2.type,
|
|
42
|
+
oldState = _ref2.oldState,
|
|
43
|
+
newState = _ref2.newState,
|
|
44
|
+
cancelable = _ref2.cancelable;
|
|
45
|
+
if (typeof globalThis.ToggleEvent === 'function') {
|
|
46
|
+
// ToggleEvent is not in lib.dom.d.ts at the time of writing; cast to any for construction.
|
|
47
|
+
var ToggleEventCtor = globalThis.ToggleEvent;
|
|
48
|
+
return new ToggleEventCtor(type, {
|
|
49
|
+
oldState: oldState,
|
|
50
|
+
newState: newState,
|
|
51
|
+
cancelable: cancelable,
|
|
52
|
+
bubbles: false
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
var event = new Event(type, {
|
|
56
|
+
cancelable: cancelable,
|
|
57
|
+
bubbles: false
|
|
58
|
+
});
|
|
59
|
+
event.oldState = oldState;
|
|
60
|
+
event.newState = newState;
|
|
61
|
+
return event;
|
|
62
|
+
}
|
|
63
|
+
function fireToggleEvent(_ref3) {
|
|
64
|
+
var element = _ref3.element,
|
|
65
|
+
type = _ref3.type,
|
|
66
|
+
oldState = _ref3.oldState,
|
|
67
|
+
newState = _ref3.newState,
|
|
68
|
+
cancelable = _ref3.cancelable;
|
|
69
|
+
return element.dispatchEvent(createToggleEvent({
|
|
70
|
+
type: type,
|
|
71
|
+
oldState: oldState,
|
|
72
|
+
newState: newState,
|
|
73
|
+
cancelable: cancelable
|
|
74
|
+
}));
|
|
75
|
+
}
|
|
76
|
+
function oppositeState(_ref4) {
|
|
77
|
+
var state = _ref4.state;
|
|
78
|
+
if (state === 'open') {
|
|
79
|
+
return 'closed';
|
|
80
|
+
}
|
|
81
|
+
return 'open';
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Coalesces rapid show/hide into one toggle event per element.
|
|
85
|
+
// Uses setTimeout (task) not Promise.resolve (microtask) to match browser timing.
|
|
86
|
+
function createToggleScheduler() {
|
|
87
|
+
var pending = new Map();
|
|
88
|
+
return function schedule(_ref5) {
|
|
89
|
+
var element = _ref5.element,
|
|
90
|
+
newState = _ref5.newState;
|
|
91
|
+
var existing = pending.get(element);
|
|
92
|
+
if (existing) {
|
|
93
|
+
// Coalesce: keep the original oldState, update newState
|
|
94
|
+
pending.set(element, {
|
|
95
|
+
oldState: existing.oldState,
|
|
96
|
+
newState: newState
|
|
97
|
+
});
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
pending.set(element, {
|
|
101
|
+
oldState: oppositeState({
|
|
102
|
+
state: newState
|
|
103
|
+
}),
|
|
104
|
+
newState: newState
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
// Task queue (setTimeout) — browser fires toggle after microtasks drain, not during
|
|
108
|
+
setTimeout(function () {
|
|
109
|
+
var entry = pending.get(element);
|
|
110
|
+
if (!entry) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
pending.delete(element);
|
|
114
|
+
fireToggleEvent({
|
|
115
|
+
element: element,
|
|
116
|
+
type: 'toggle',
|
|
117
|
+
oldState: entry.oldState,
|
|
118
|
+
newState: entry.newState,
|
|
119
|
+
cancelable: false
|
|
120
|
+
});
|
|
121
|
+
}, 0);
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Like createToggleScheduler but also fires a close event as a separate task after toggle.
|
|
126
|
+
function createDialogToggleScheduler() {
|
|
127
|
+
var pending = new Map();
|
|
128
|
+
return function schedule(_ref6) {
|
|
129
|
+
var dialog = _ref6.dialog,
|
|
130
|
+
newState = _ref6.newState,
|
|
131
|
+
shouldFireCloseEvent = _ref6.shouldFireCloseEvent;
|
|
132
|
+
var existing = pending.get(dialog);
|
|
133
|
+
if (existing) {
|
|
134
|
+
// Coalesce: keep the original oldState, merge shouldFireCloseEvent
|
|
135
|
+
pending.set(dialog, {
|
|
136
|
+
oldState: existing.oldState,
|
|
137
|
+
newState: newState,
|
|
138
|
+
shouldFireCloseEvent: existing.shouldFireCloseEvent || shouldFireCloseEvent
|
|
139
|
+
});
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
pending.set(dialog, {
|
|
143
|
+
oldState: oppositeState({
|
|
144
|
+
state: newState
|
|
145
|
+
}),
|
|
146
|
+
newState: newState,
|
|
147
|
+
shouldFireCloseEvent: shouldFireCloseEvent
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
// Browser fires toggle as a task, not synchronously or as a microtask
|
|
151
|
+
setTimeout(function () {
|
|
152
|
+
var entry = pending.get(dialog);
|
|
153
|
+
if (!entry) {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
pending.delete(dialog);
|
|
157
|
+
fireToggleEvent({
|
|
158
|
+
element: dialog,
|
|
159
|
+
type: 'toggle',
|
|
160
|
+
oldState: entry.oldState,
|
|
161
|
+
newState: entry.newState,
|
|
162
|
+
cancelable: false
|
|
163
|
+
});
|
|
164
|
+
if (entry.shouldFireCloseEvent) {
|
|
165
|
+
// Browser fires close as a separate task after toggle
|
|
166
|
+
setTimeout(function () {
|
|
167
|
+
dialog.dispatchEvent(new Event('close', {
|
|
168
|
+
bubbles: false,
|
|
169
|
+
cancelable: false
|
|
170
|
+
}));
|
|
171
|
+
}, 0);
|
|
172
|
+
}
|
|
173
|
+
}, 0);
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// ─── Popover API ────────────────────────────────────────────────────────────
|
|
178
|
+
// Spec: https://html.spec.whatwg.org/multipage/popover.html
|
|
179
|
+
|
|
180
|
+
// `popover` attribute valid values. Empty / "auto" -> "auto"; unknown -> "manual".
|
|
181
|
+
// Spec: https://html.spec.whatwg.org/multipage/popover.html#attr-popover
|
|
182
|
+
|
|
183
|
+
function getPopoverMode(_ref7) {
|
|
184
|
+
var element = _ref7.element;
|
|
185
|
+
if (!element.hasAttribute('popover')) {
|
|
186
|
+
return null;
|
|
187
|
+
}
|
|
188
|
+
var raw = element.getAttribute('popover');
|
|
189
|
+
if (raw === null || raw === '' || raw === 'auto') {
|
|
190
|
+
return 'auto';
|
|
191
|
+
}
|
|
192
|
+
if (raw === 'hint') {
|
|
193
|
+
return 'hint';
|
|
194
|
+
}
|
|
195
|
+
return 'manual';
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// IDL property: `el.popover` reflects the popover content attribute. Setter
|
|
199
|
+
// writes verbatim; getter canonicalises (unknown -> "manual", empty -> "auto").
|
|
200
|
+
// Spec: https://html.spec.whatwg.org/multipage/popover.html#dom-popover
|
|
201
|
+
|
|
202
|
+
if (typeof HTMLElement !== 'undefined' && !Object.getOwnPropertyDescriptor(HTMLElement.prototype, 'popover')) {
|
|
203
|
+
Object.defineProperty(HTMLElement.prototype, 'popover', {
|
|
204
|
+
configurable: true,
|
|
205
|
+
enumerable: true,
|
|
206
|
+
get: function get() {
|
|
207
|
+
return getPopoverMode({
|
|
208
|
+
element: this
|
|
209
|
+
});
|
|
210
|
+
},
|
|
211
|
+
set: function set(value) {
|
|
212
|
+
if (value === null) {
|
|
213
|
+
this.removeAttribute('popover');
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
// Per spec, the IDL setter writes the value verbatim; the getter then
|
|
217
|
+
// canonicalises (so setting an unknown value is reflected as "manual"
|
|
218
|
+
// only when read back, but the attribute keeps the raw string).
|
|
219
|
+
this.setAttribute('popover', String(value));
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// Open auto popovers in open-order. Used for light-dismiss, Escape, and
|
|
225
|
+
// hide-all-popovers-until on new auto opens.
|
|
226
|
+
// Spec: https://html.spec.whatwg.org/multipage/popover.html#showing-auto-popover-list
|
|
227
|
+
var openAutoStack = [];
|
|
228
|
+
|
|
229
|
+
// Open hint popovers in open-order. Hints have their own stack: hint closes
|
|
230
|
+
// other hints, auto closes all hints, hint never closes autos.
|
|
231
|
+
// Spec: https://html.spec.whatwg.org/multipage/popover.html#showing-hint-popover-list
|
|
232
|
+
var openHintStack = [];
|
|
233
|
+
function isPopoverOpen(_ref8) {
|
|
234
|
+
var element = _ref8.element;
|
|
235
|
+
return element.hasAttribute('data-popover-open');
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// Stack lookup: returns the stack the element belongs to (or null for manual /
|
|
239
|
+
// non-popovers). Used by hide-handlers to remove from the correct stack and by
|
|
240
|
+
// dismissal logic to find the topmost relevant popover.
|
|
241
|
+
function stackForMode(_ref9) {
|
|
242
|
+
var mode = _ref9.mode;
|
|
243
|
+
if (mode === 'auto') {
|
|
244
|
+
return openAutoStack;
|
|
245
|
+
}
|
|
246
|
+
if (mode === 'hint') {
|
|
247
|
+
return openHintStack;
|
|
248
|
+
}
|
|
249
|
+
return null;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// Polyfill-only. Real browsers reveal the popover once `ResizeObserver`
|
|
253
|
+
// callbacks fire after the first layout pass. jsdom has no layout, so we clear
|
|
254
|
+
// any inline `opacity: 0` (set by callers like `use-anchor-positioning`) once
|
|
255
|
+
// the open toggle has fired.
|
|
256
|
+
function clearOpacityOnOpenToggle(_ref0) {
|
|
257
|
+
var element = _ref0.element;
|
|
258
|
+
var unbind = (0, _bindEventListener.bind)(element, {
|
|
259
|
+
type: 'toggle',
|
|
260
|
+
listener: function listener(event) {
|
|
261
|
+
var toggleEvent = event;
|
|
262
|
+
if (toggleEvent.newState !== 'open') {
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
element.style.removeProperty('opacity');
|
|
266
|
+
unbind();
|
|
267
|
+
}
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// Spec: "topmost popover ancestor" walks both DOM ancestry AND the invoker
|
|
272
|
+
// chain (an invoker is a button whose `popovertarget` references an open
|
|
273
|
+
// popover). When opening popover B from a button inside popover A, A is
|
|
274
|
+
// considered B's ancestor and stays open. This walks ancestors of the target,
|
|
275
|
+
// hopping through any invoker references discovered along the way.
|
|
276
|
+
// Find a popover's invoker: an element with `popovertarget=<popover.id>`.
|
|
277
|
+
// Spec: https://html.spec.whatwg.org/multipage/popover.html#popover-target-element
|
|
278
|
+
function findInvokerForPopover(_ref1) {
|
|
279
|
+
var popover = _ref1.popover;
|
|
280
|
+
if (!popover.id || !popover.ownerDocument) {
|
|
281
|
+
return null;
|
|
282
|
+
}
|
|
283
|
+
var escaped = (typeof CSS !== 'undefined' && CSS.escape ? CSS.escape : String)(popover.id);
|
|
284
|
+
var selector = "[popovertarget=\"".concat(escaped, "\"]");
|
|
285
|
+
return popover.ownerDocument.querySelector(selector);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// True if `candidate` is reachable from `start` via DOM ancestry plus invoker
|
|
289
|
+
// hops. Spec: https://html.spec.whatwg.org/multipage/popover.html#topmost-popover-ancestor
|
|
290
|
+
function reachesViaAncestryOrInvoker(_ref10) {
|
|
291
|
+
var start = _ref10.start,
|
|
292
|
+
candidate = _ref10.candidate;
|
|
293
|
+
var found = walkAncestorChain({
|
|
294
|
+
start: start,
|
|
295
|
+
visitor: function visitor(_ref11) {
|
|
296
|
+
var node = _ref11.node;
|
|
297
|
+
if (node === candidate) {
|
|
298
|
+
return true;
|
|
299
|
+
}
|
|
300
|
+
// Hop into the invoked popover's chain when this node is an invoker.
|
|
301
|
+
if (node instanceof HTMLElement) {
|
|
302
|
+
var popovertarget = node.getAttribute('popovertarget');
|
|
303
|
+
if (popovertarget && node.ownerDocument) {
|
|
304
|
+
var referenced = node.ownerDocument.getElementById(popovertarget);
|
|
305
|
+
if (referenced && referenced !== node) {
|
|
306
|
+
if (reachesViaAncestryOrInvoker({
|
|
307
|
+
start: referenced,
|
|
308
|
+
candidate: candidate
|
|
309
|
+
})) {
|
|
310
|
+
return true;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
return null;
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
return found === true;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
// Is `candidate` an ancestor of `target` via DOM nesting OR invoker chain?
|
|
322
|
+
// Spec: https://html.spec.whatwg.org/multipage/popover.html#topmost-popover-ancestor
|
|
323
|
+
function isInChainTo(_ref12) {
|
|
324
|
+
var candidate = _ref12.candidate,
|
|
325
|
+
target = _ref12.target;
|
|
326
|
+
if (!(target instanceof Node)) {
|
|
327
|
+
return false;
|
|
328
|
+
}
|
|
329
|
+
if (reachesViaAncestryOrInvoker({
|
|
330
|
+
start: target,
|
|
331
|
+
candidate: candidate
|
|
332
|
+
})) {
|
|
333
|
+
return true;
|
|
334
|
+
}
|
|
335
|
+
// If target is itself a popover, also walk its invoker's chain (a button
|
|
336
|
+
// elsewhere in the DOM that has `popovertarget=<target.id>`).
|
|
337
|
+
if (target instanceof HTMLElement && target.hasAttribute('popover')) {
|
|
338
|
+
var invoker = findInvokerForPopover({
|
|
339
|
+
popover: target
|
|
340
|
+
});
|
|
341
|
+
if (invoker && reachesViaAncestryOrInvoker({
|
|
342
|
+
start: invoker,
|
|
343
|
+
candidate: candidate
|
|
344
|
+
})) {
|
|
345
|
+
return true;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
return false;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
// Hide every popover in `stack` that is not an ancestor of `target`. With a
|
|
352
|
+
// null target, closes the entire stack.
|
|
353
|
+
// Spec: https://html.spec.whatwg.org/multipage/popover.html#hide-all-popovers-until
|
|
354
|
+
function hidePopoversInStackNotInChainTo(_ref13) {
|
|
355
|
+
var stack = _ref13.stack,
|
|
356
|
+
target = _ref13.target;
|
|
357
|
+
var snapshot = stack.slice();
|
|
358
|
+
for (var i = snapshot.length - 1; i >= 0; i -= 1) {
|
|
359
|
+
var popover = snapshot[i];
|
|
360
|
+
if (isInChainTo({
|
|
361
|
+
candidate: popover,
|
|
362
|
+
target: target
|
|
363
|
+
})) {
|
|
364
|
+
break;
|
|
365
|
+
}
|
|
366
|
+
popover.hidePopover();
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
// Two-phase light-dismiss: pointerdown stores the nearest open popover for
|
|
371
|
+
// the target; pointerup dismisses popovers that are not in that popover's
|
|
372
|
+
// chain when the same target is hit. Drag-out / drag-in does not dismiss.
|
|
373
|
+
// Spec: https://html.spec.whatwg.org/multipage/popover.html#light-dismiss-open-popovers
|
|
374
|
+
var pointerdownState = {
|
|
375
|
+
target: null
|
|
376
|
+
};
|
|
377
|
+
|
|
378
|
+
// If `node` is an invoker for an open popover, return that popover.
|
|
379
|
+
// Spec: https://html.spec.whatwg.org/multipage/popover.html#popover-target-element
|
|
380
|
+
function invokedOpenPopover(_ref14) {
|
|
381
|
+
var node = _ref14.node;
|
|
382
|
+
if (!(node instanceof HTMLElement)) {
|
|
383
|
+
return null;
|
|
384
|
+
}
|
|
385
|
+
var popovertarget = node.getAttribute('popovertarget');
|
|
386
|
+
if (!popovertarget || !node.ownerDocument) {
|
|
387
|
+
return null;
|
|
388
|
+
}
|
|
389
|
+
var referenced = node.ownerDocument.getElementById(popovertarget);
|
|
390
|
+
if (!referenced || referenced === node || !isPopoverOpen({
|
|
391
|
+
element: referenced
|
|
392
|
+
})) {
|
|
393
|
+
return null;
|
|
394
|
+
}
|
|
395
|
+
return referenced;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
// Cycle-safe ancestor walker. Returns the first non-null `visitor` result.
|
|
399
|
+
function walkAncestorChain(_ref15) {
|
|
400
|
+
var start = _ref15.start,
|
|
401
|
+
visitor = _ref15.visitor;
|
|
402
|
+
var seen = new Set();
|
|
403
|
+
function step(node) {
|
|
404
|
+
if (!node || seen.has(node)) {
|
|
405
|
+
return null;
|
|
406
|
+
}
|
|
407
|
+
seen.add(node);
|
|
408
|
+
var result = visitor({
|
|
409
|
+
node: node
|
|
410
|
+
});
|
|
411
|
+
if (result !== null) {
|
|
412
|
+
return result;
|
|
413
|
+
}
|
|
414
|
+
return step(node.parentNode);
|
|
415
|
+
}
|
|
416
|
+
return step(start);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
// Spec: https://html.spec.whatwg.org/multipage/popover.html#nearest-inclusive-open-popover
|
|
420
|
+
function nearestOpenPopover(_ref16) {
|
|
421
|
+
var target = _ref16.target;
|
|
422
|
+
return walkAncestorChain({
|
|
423
|
+
start: target,
|
|
424
|
+
visitor: function visitor(_ref17) {
|
|
425
|
+
var node = _ref17.node;
|
|
426
|
+
if (node instanceof HTMLElement && isPopoverOpen({
|
|
427
|
+
element: node
|
|
428
|
+
})) {
|
|
429
|
+
return node;
|
|
430
|
+
}
|
|
431
|
+
return invokedOpenPopover({
|
|
432
|
+
node: node
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
function handleDocumentPointerdown(event) {
|
|
438
|
+
if (openAutoStack.length === 0 && openHintStack.length === 0) {
|
|
439
|
+
return;
|
|
440
|
+
}
|
|
441
|
+
var target = event.target instanceof Node ? event.target : null;
|
|
442
|
+
pointerdownState.target = nearestOpenPopover({
|
|
443
|
+
target: target
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
function handleDocumentPointerup(event) {
|
|
447
|
+
if (openAutoStack.length === 0 && openHintStack.length === 0) {
|
|
448
|
+
pointerdownState.target = null;
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
var target = event.target instanceof Node ? event.target : null;
|
|
452
|
+
var ancestor = nearestOpenPopover({
|
|
453
|
+
target: target
|
|
454
|
+
});
|
|
455
|
+
var sameTarget = ancestor === pointerdownState.target;
|
|
456
|
+
pointerdownState.target = null;
|
|
457
|
+
if (!sameTarget) {
|
|
458
|
+
// User pointerdowned inside a popover and released elsewhere (or vice
|
|
459
|
+
// versa). Treat as a drag and do not dismiss per spec.
|
|
460
|
+
return;
|
|
461
|
+
}
|
|
462
|
+
// Hints are dismissed first (they sit "above" autos for dismissal purposes
|
|
463
|
+
// even though they live in their own stack), then autos. When ancestor is a
|
|
464
|
+
// hint, autos are unaffected; when ancestor is an auto or null, hints close
|
|
465
|
+
// entirely (because no hint is the target's ancestor).
|
|
466
|
+
var ancestorMode = ancestor ? getPopoverMode({
|
|
467
|
+
element: ancestor
|
|
468
|
+
}) : null;
|
|
469
|
+
if (ancestorMode === 'hint') {
|
|
470
|
+
hidePopoversInStackNotInChainTo({
|
|
471
|
+
stack: openHintStack,
|
|
472
|
+
target: target
|
|
473
|
+
});
|
|
474
|
+
return;
|
|
475
|
+
}
|
|
476
|
+
hidePopoversInStackNotInChainTo({
|
|
477
|
+
stack: openHintStack,
|
|
478
|
+
target: null
|
|
479
|
+
});
|
|
480
|
+
hidePopoversInStackNotInChainTo({
|
|
481
|
+
stack: openAutoStack,
|
|
482
|
+
target: target
|
|
483
|
+
});
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
// Topmost still-connected popover in `stack`, pruning disconnected entries
|
|
487
|
+
// (test pollution where a popover was removed from the DOM without hidePopover).
|
|
488
|
+
// Polyfill-only defensive cleanup; not in the spec.
|
|
489
|
+
function topmostConnected(_ref18) {
|
|
490
|
+
var stack = _ref18.stack;
|
|
491
|
+
while (stack.length > 0) {
|
|
492
|
+
var candidate = stack[stack.length - 1];
|
|
493
|
+
if (candidate.isConnected) {
|
|
494
|
+
return candidate;
|
|
495
|
+
}
|
|
496
|
+
stack.pop();
|
|
497
|
+
}
|
|
498
|
+
return null;
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
// Escape closes the topmost open popover. Hints are dismissed before autos.
|
|
502
|
+
// Spec: https://html.spec.whatwg.org/multipage/popover.html#close-watcher
|
|
503
|
+
function handleDocumentKeydown(event) {
|
|
504
|
+
if (event.key !== 'Escape') {
|
|
505
|
+
return;
|
|
506
|
+
}
|
|
507
|
+
var topHint = topmostConnected({
|
|
508
|
+
stack: openHintStack
|
|
509
|
+
});
|
|
510
|
+
if (topHint) {
|
|
511
|
+
topHint.hidePopover();
|
|
512
|
+
return;
|
|
513
|
+
}
|
|
514
|
+
var topAuto = topmostConnected({
|
|
515
|
+
stack: openAutoStack
|
|
516
|
+
});
|
|
517
|
+
if (topAuto) {
|
|
518
|
+
topAuto.hidePopover();
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
if (typeof HTMLElement !== 'undefined') {
|
|
522
|
+
var schedulePopoverToggle = createToggleScheduler();
|
|
523
|
+
|
|
524
|
+
// Spec: https://html.spec.whatwg.org/multipage/popover.html#dom-showpopover
|
|
525
|
+
if (!HTMLElement.prototype.showPopover) {
|
|
526
|
+
HTMLElement.prototype.showPopover = function showPopover() {
|
|
527
|
+
if (!this.hasAttribute('popover')) {
|
|
528
|
+
throw new DOMException("Failed to execute 'showPopover' on 'HTMLElement': ".concat(describeElement({
|
|
529
|
+
element: this
|
|
530
|
+
}), " is not a popover element. Ensure the element has a 'popover' attribute (e.g. <div popover>)."), 'NotSupportedError');
|
|
531
|
+
}
|
|
532
|
+
if (!this.isConnected) {
|
|
533
|
+
throw new DOMException("Failed to execute 'showPopover' on 'HTMLElement': ".concat(describeElement({
|
|
534
|
+
element: this
|
|
535
|
+
}), " is not connected to the document. Ensure the element has been added to the DOM (e.g. via render() or document.body.appendChild()) before calling showPopover()."), 'InvalidStateError');
|
|
536
|
+
}
|
|
537
|
+
if (isPopoverOpen({
|
|
538
|
+
element: this
|
|
539
|
+
})) {
|
|
540
|
+
return; // already showing — no-op
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
// sync, cancelable on show only
|
|
544
|
+
var allowed = fireToggleEvent({
|
|
545
|
+
element: this,
|
|
546
|
+
type: 'beforetoggle',
|
|
547
|
+
oldState: 'closed',
|
|
548
|
+
newState: 'open',
|
|
549
|
+
cancelable: true
|
|
550
|
+
});
|
|
551
|
+
if (!allowed) {
|
|
552
|
+
return;
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
// Spec-driven dismissal of popovers in other stacks before opening:
|
|
556
|
+
// - opening an `auto`: close all hints; close autos not in the new
|
|
557
|
+
// popover's ancestor chain.
|
|
558
|
+
// - opening a `hint`: close hints not in the new popover's chain;
|
|
559
|
+
// leave autos alone.
|
|
560
|
+
// - opening a `manual`: leave both stacks untouched.
|
|
561
|
+
var mode = getPopoverMode({
|
|
562
|
+
element: this
|
|
563
|
+
});
|
|
564
|
+
if (mode === 'auto') {
|
|
565
|
+
hidePopoversInStackNotInChainTo({
|
|
566
|
+
stack: openHintStack,
|
|
567
|
+
target: null
|
|
568
|
+
});
|
|
569
|
+
hidePopoversInStackNotInChainTo({
|
|
570
|
+
stack: openAutoStack,
|
|
571
|
+
target: this
|
|
572
|
+
});
|
|
573
|
+
} else if (mode === 'hint') {
|
|
574
|
+
hidePopoversInStackNotInChainTo({
|
|
575
|
+
stack: openHintStack,
|
|
576
|
+
target: this
|
|
577
|
+
});
|
|
578
|
+
}
|
|
579
|
+
this.setAttribute('data-popover-open', '');
|
|
580
|
+
var stack = stackForMode({
|
|
581
|
+
mode: mode
|
|
582
|
+
});
|
|
583
|
+
if (stack && !stack.includes(this)) {
|
|
584
|
+
stack.push(this);
|
|
585
|
+
}
|
|
586
|
+
clearOpacityOnOpenToggle({
|
|
587
|
+
element: this
|
|
588
|
+
});
|
|
589
|
+
schedulePopoverToggle({
|
|
590
|
+
element: this,
|
|
591
|
+
newState: 'open'
|
|
592
|
+
});
|
|
593
|
+
};
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
// Spec: https://html.spec.whatwg.org/multipage/popover.html#dom-hidepopover
|
|
597
|
+
if (!HTMLElement.prototype.hidePopover) {
|
|
598
|
+
HTMLElement.prototype.hidePopover = function hidePopover() {
|
|
599
|
+
if (!this.hasAttribute('popover')) {
|
|
600
|
+
throw new DOMException("Failed to execute 'hidePopover' on 'HTMLElement': ".concat(describeElement({
|
|
601
|
+
element: this
|
|
602
|
+
}), " is not a popover element. Ensure the element has a 'popover' attribute (e.g. <div popover>)."), 'NotSupportedError');
|
|
603
|
+
}
|
|
604
|
+
// Browser does not throw for disconnected elements on hide. No-op.
|
|
605
|
+
if (!this.isConnected || !isPopoverOpen({
|
|
606
|
+
element: this
|
|
607
|
+
})) {
|
|
608
|
+
return;
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
// not cancelable on hide
|
|
612
|
+
fireToggleEvent({
|
|
613
|
+
element: this,
|
|
614
|
+
type: 'beforetoggle',
|
|
615
|
+
oldState: 'open',
|
|
616
|
+
newState: 'closed',
|
|
617
|
+
cancelable: false
|
|
618
|
+
});
|
|
619
|
+
this.removeAttribute('data-popover-open');
|
|
620
|
+
var stack = stackForMode({
|
|
621
|
+
mode: getPopoverMode({
|
|
622
|
+
element: this
|
|
623
|
+
})
|
|
624
|
+
});
|
|
625
|
+
if (stack) {
|
|
626
|
+
var stackIndex = stack.indexOf(this);
|
|
627
|
+
if (stackIndex !== -1) {
|
|
628
|
+
stack.splice(stackIndex, 1);
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
schedulePopoverToggle({
|
|
632
|
+
element: this,
|
|
633
|
+
newState: 'closed'
|
|
634
|
+
});
|
|
635
|
+
};
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
// Spec: https://html.spec.whatwg.org/multipage/popover.html#dom-togglepopover
|
|
639
|
+
if (!HTMLElement.prototype.togglePopover) {
|
|
640
|
+
HTMLElement.prototype.togglePopover = function togglePopover(force) {
|
|
641
|
+
var wasOpen = isPopoverOpen({
|
|
642
|
+
element: this
|
|
643
|
+
});
|
|
644
|
+
if (typeof force === 'boolean') {
|
|
645
|
+
if (force && !wasOpen) {
|
|
646
|
+
this.showPopover();
|
|
647
|
+
} else if (!force && wasOpen) {
|
|
648
|
+
this.hidePopover();
|
|
649
|
+
}
|
|
650
|
+
} else if (wasOpen) {
|
|
651
|
+
this.hidePopover();
|
|
652
|
+
} else {
|
|
653
|
+
this.showPopover();
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
// Return the actual resulting state, which differs from `force` when
|
|
657
|
+
// `beforetoggle` was preventDefaulted on show.
|
|
658
|
+
return isPopoverOpen({
|
|
659
|
+
element: this
|
|
660
|
+
});
|
|
661
|
+
};
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
// Document-level light-dismiss for `popover="auto"` and `popover="hint"`.
|
|
665
|
+
// Spec uses pointerdown + pointerup with same-target check; we also wire
|
|
666
|
+
// `mousedown` + `mouseup` because jsdom does not fire pointer events for
|
|
667
|
+
// fireEvent.click() / userEvent.click() in older jsdom versions.
|
|
668
|
+
|
|
669
|
+
if (typeof document !== 'undefined' && !document.__atTopLayerLightDismiss) {
|
|
670
|
+
document.__atTopLayerLightDismiss = true;
|
|
671
|
+
|
|
672
|
+
// capture phase so application handlers cannot stop propagation before us
|
|
673
|
+
var pointerdownAdapter = function pointerdownAdapter(event) {
|
|
674
|
+
return handleDocumentPointerdown(event);
|
|
675
|
+
};
|
|
676
|
+
var pointerupAdapter = function pointerupAdapter(event) {
|
|
677
|
+
return handleDocumentPointerup(event);
|
|
678
|
+
};
|
|
679
|
+
(0, _bindEventListener.bind)(document, {
|
|
680
|
+
type: 'pointerdown',
|
|
681
|
+
listener: pointerdownAdapter,
|
|
682
|
+
options: {
|
|
683
|
+
capture: true
|
|
684
|
+
}
|
|
685
|
+
});
|
|
686
|
+
(0, _bindEventListener.bind)(document, {
|
|
687
|
+
type: 'pointerup',
|
|
688
|
+
listener: pointerupAdapter,
|
|
689
|
+
options: {
|
|
690
|
+
capture: true
|
|
691
|
+
}
|
|
692
|
+
});
|
|
693
|
+
// Fallback for environments / fireEvent paths that fire mouse events
|
|
694
|
+
// only. The same handlers work for MouseEvent because we only read
|
|
695
|
+
// `target`. PointerEvent extends MouseEvent so the runtime cast is safe.
|
|
696
|
+
(0, _bindEventListener.bind)(document, {
|
|
697
|
+
type: 'mousedown',
|
|
698
|
+
listener: pointerdownAdapter,
|
|
699
|
+
options: {
|
|
700
|
+
capture: true
|
|
701
|
+
}
|
|
702
|
+
});
|
|
703
|
+
(0, _bindEventListener.bind)(document, {
|
|
704
|
+
type: 'mouseup',
|
|
705
|
+
listener: pointerupAdapter,
|
|
706
|
+
options: {
|
|
707
|
+
capture: true
|
|
708
|
+
}
|
|
709
|
+
});
|
|
710
|
+
(0, _bindEventListener.bind)(document, {
|
|
711
|
+
type: 'keydown',
|
|
712
|
+
listener: handleDocumentKeydown,
|
|
713
|
+
options: {
|
|
714
|
+
capture: true
|
|
715
|
+
}
|
|
716
|
+
});
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
// ─── HTMLDialogElement ──────────────────────────────────────────────────────
|
|
721
|
+
// Spec: https://html.spec.whatwg.org/multipage/interactive-elements.html#the-dialog-element
|
|
722
|
+
|
|
723
|
+
if (typeof HTMLDialogElement !== 'undefined') {
|
|
724
|
+
var proto = HTMLDialogElement.prototype;
|
|
725
|
+
var scheduleDialogToggle = createDialogToggleScheduler();
|
|
726
|
+
if (typeof proto.showModal !== 'function') {
|
|
727
|
+
proto.showModal = function showModal() {
|
|
728
|
+
if (!this.isConnected) {
|
|
729
|
+
throw new DOMException("Failed to execute 'showModal' on 'HTMLDialogElement': ".concat(describeElement({
|
|
730
|
+
element: this
|
|
731
|
+
}), " is not connected to the document. Ensure the element has been added to the DOM (e.g. via render() or document.body.appendChild()) before calling showModal()."), 'InvalidStateError');
|
|
732
|
+
}
|
|
733
|
+
if (this.hasAttribute('open')) {
|
|
734
|
+
if (this._isModal) {
|
|
735
|
+
return; // already modal — no-op
|
|
736
|
+
}
|
|
737
|
+
throw new DOMException("Failed to execute 'showModal' on 'HTMLDialogElement': ".concat(describeElement({
|
|
738
|
+
element: this
|
|
739
|
+
}), " already has an 'open' attribute, and therefore cannot be opened modally. The dialog was likely opened non-modally with .show(). Call .close() first, then .showModal()."), 'InvalidStateError');
|
|
740
|
+
}
|
|
741
|
+
fireToggleEvent({
|
|
742
|
+
element: this,
|
|
743
|
+
type: 'beforetoggle',
|
|
744
|
+
oldState: 'closed',
|
|
745
|
+
newState: 'open',
|
|
746
|
+
cancelable: false
|
|
747
|
+
});
|
|
748
|
+
this.setAttribute('open', '');
|
|
749
|
+
this._isModal = true;
|
|
750
|
+
scheduleDialogToggle({
|
|
751
|
+
dialog: this,
|
|
752
|
+
newState: 'open',
|
|
753
|
+
shouldFireCloseEvent: false
|
|
754
|
+
});
|
|
755
|
+
};
|
|
756
|
+
}
|
|
757
|
+
if (typeof proto.show !== 'function') {
|
|
758
|
+
proto.show = function show() {
|
|
759
|
+
// Browser does not throw for disconnected elements on show — just no-op
|
|
760
|
+
if (!this.isConnected || this.hasAttribute('open')) {
|
|
761
|
+
return;
|
|
762
|
+
}
|
|
763
|
+
fireToggleEvent({
|
|
764
|
+
element: this,
|
|
765
|
+
type: 'beforetoggle',
|
|
766
|
+
oldState: 'closed',
|
|
767
|
+
newState: 'open',
|
|
768
|
+
cancelable: false
|
|
769
|
+
});
|
|
770
|
+
this.setAttribute('open', '');
|
|
771
|
+
this._isModal = false;
|
|
772
|
+
scheduleDialogToggle({
|
|
773
|
+
dialog: this,
|
|
774
|
+
newState: 'open',
|
|
775
|
+
shouldFireCloseEvent: false
|
|
776
|
+
});
|
|
777
|
+
};
|
|
778
|
+
}
|
|
779
|
+
if (typeof proto.close !== 'function') {
|
|
780
|
+
proto.close = function close(returnValue) {
|
|
781
|
+
if (!this.hasAttribute('open')) {
|
|
782
|
+
return; // already closed — no-op
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
// Sync beforetoggle (not cancelable) — matches browser behavior
|
|
786
|
+
fireToggleEvent({
|
|
787
|
+
element: this,
|
|
788
|
+
type: 'beforetoggle',
|
|
789
|
+
oldState: 'open',
|
|
790
|
+
newState: 'closed',
|
|
791
|
+
cancelable: false
|
|
792
|
+
});
|
|
793
|
+
this.removeAttribute('open');
|
|
794
|
+
this._isModal = false;
|
|
795
|
+
if (returnValue !== undefined) {
|
|
796
|
+
this.returnValue = returnValue;
|
|
797
|
+
}
|
|
798
|
+
scheduleDialogToggle({
|
|
799
|
+
dialog: this,
|
|
800
|
+
newState: 'closed',
|
|
801
|
+
shouldFireCloseEvent: true
|
|
802
|
+
});
|
|
803
|
+
};
|
|
804
|
+
}
|
|
805
|
+
if (!('returnValue' in proto)) {
|
|
806
|
+
Object.defineProperty(proto, 'returnValue', {
|
|
807
|
+
get: function get() {
|
|
808
|
+
return this._returnValue === undefined ? '' : this._returnValue;
|
|
809
|
+
},
|
|
810
|
+
set: function set(value) {
|
|
811
|
+
this._returnValue = String(value);
|
|
812
|
+
},
|
|
813
|
+
configurable: true,
|
|
814
|
+
enumerable: true
|
|
815
|
+
});
|
|
816
|
+
}
|
|
817
|
+
}
|