mount-observer 0.1.14 → 0.1.15
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/{DefineCustomElementHandler.js → handlers/DefineCustomElement.js} +103 -99
- package/handlers/DefineCustomElement.ts +123 -0
- package/handlers/EnhanceMountedElement.js +99 -0
- package/handlers/EnhanceMountedElement.ts +116 -0
- package/handlers/Events.js +110 -0
- package/handlers/EvtRt.js +59 -0
- package/handlers/GenIds.js +37 -0
- package/handlers/GenIds.ts +45 -0
- package/handlers/HTMLInclude.js +393 -0
- package/handlers/HTMLInclude.ts +453 -0
- package/handlers/HoistTemplate.js +77 -0
- package/handlers/HoistTemplate.ts +89 -0
- package/handlers/MountObserver.js +941 -0
- package/handlers/MountObserverScript.js +78 -0
- package/handlers/MountObserverScript.ts +89 -0
- package/handlers/ScriptExport.js +83 -0
- package/handlers/ScriptExport.ts +97 -0
- package/handlers/SharedMutationObserver.js +78 -0
- package/handlers/arr.js +16 -0
- package/handlers/connectionMonitor.js +122 -0
- package/handlers/elementIntersection.js +73 -0
- package/handlers/emitEvents.js +187 -0
- package/handlers/getRegistryRoot.js +52 -0
- package/handlers/loadImports.js +129 -0
- package/handlers/mediaQuery.js +90 -0
- package/handlers/rootSizeObserver.js +131 -0
- package/handlers/upShadowSearch.js +70 -0
- package/handlers/withScopePerimeter.js +22 -0
- package/package.json +12 -2
- package/types/assign-gingerly/types.d.ts +244 -0
- package/types/be-a-beacon/types.d.ts +3 -0
- package/types/global.d.ts +29 -0
- package/types/id-generation/types.d.ts +26 -0
- package/types/mount-observer/types.d.ts +330 -0
|
@@ -1,99 +1,103 @@
|
|
|
1
|
-
import { EvtRt } from '
|
|
2
|
-
export class DefineCustomElementHandler extends EvtRt {
|
|
3
|
-
mount(mountedElement, MountConfig, context) {
|
|
4
|
-
this.abort();
|
|
5
|
-
// Check if modules are specified
|
|
6
|
-
if (!context.modules || context.modules.length === 0) {
|
|
7
|
-
throw new Error('Must specify an ES Module');
|
|
8
|
-
}
|
|
9
|
-
const module = context.modules[0];
|
|
10
|
-
const tagName = mountedElement.localName;
|
|
11
|
-
// Check if already defined
|
|
12
|
-
if (customElements.get(tagName)) {
|
|
13
|
-
return;
|
|
14
|
-
}
|
|
15
|
-
// Find suitable class
|
|
16
|
-
const ElementClass = this.findSuitableClass(module);
|
|
17
|
-
// Validate that ElementClass is a constructor
|
|
18
|
-
if (typeof ElementClass !== 'function') {
|
|
19
|
-
throw new Error(`Found class is not a constructor: ${typeof ElementClass}`);
|
|
20
|
-
}
|
|
21
|
-
// Create wrapper class to allow reuse
|
|
22
|
-
// Use anonymous class expression which works across all browsers
|
|
23
|
-
const WrapperClass = class extends ElementClass {
|
|
24
|
-
};
|
|
25
|
-
// Define the custom element using the define method
|
|
26
|
-
this.define(tagName, WrapperClass, mountedElement);
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* Define the custom element in the appropriate registry.
|
|
30
|
-
* Override this method in subclasses to use scoped registries.
|
|
31
|
-
* @param tagName - The custom element tag name
|
|
32
|
-
* @param ElementClass - The element class constructor
|
|
33
|
-
* @param mountedElement - The mounted element (used for scoped registry access)
|
|
34
|
-
*/
|
|
35
|
-
define(tagName, ElementClass, mountedElement) {
|
|
36
|
-
customElements.define(tagName, ElementClass);
|
|
37
|
-
}
|
|
38
|
-
findSuitableClass(module) {
|
|
39
|
-
// Check default export first
|
|
40
|
-
const defaultExport = module.default;
|
|
41
|
-
if (defaultExport && this.extendsHTMLElement(defaultExport)) {
|
|
42
|
-
return defaultExport;
|
|
43
|
-
}
|
|
44
|
-
// Find all exports that extend HTMLElement
|
|
45
|
-
const htmlElementClasses = Object.values(module)
|
|
46
|
-
.filter(exp => typeof exp === 'function' && this.extendsHTMLElement(exp));
|
|
47
|
-
if (htmlElementClasses.length === 0) {
|
|
48
|
-
throw new Error('No suitable class found in module');
|
|
49
|
-
}
|
|
50
|
-
if (htmlElementClasses.length > 1) {
|
|
51
|
-
throw new Error('More than one class found in module');
|
|
52
|
-
}
|
|
53
|
-
return htmlElementClasses[0];
|
|
54
|
-
}
|
|
55
|
-
extendsHTMLElement(cls) {
|
|
56
|
-
try {
|
|
57
|
-
// Must be a function
|
|
58
|
-
if (typeof cls !== 'function') {
|
|
59
|
-
return false;
|
|
60
|
-
}
|
|
61
|
-
// Handle direct HTMLElement export
|
|
62
|
-
if (cls === HTMLElement) {
|
|
63
|
-
return true;
|
|
64
|
-
}
|
|
65
|
-
// Check if it has a prototype and extends HTMLElement
|
|
66
|
-
if (cls.prototype && cls.prototype instanceof HTMLElement) {
|
|
67
|
-
return true;
|
|
68
|
-
}
|
|
69
|
-
return false;
|
|
70
|
-
}
|
|
71
|
-
catch {
|
|
72
|
-
return false;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Handler for defining custom elements in scoped registries.
|
|
78
|
-
* Uses the element's customElementRegistry property to define elements
|
|
79
|
-
* in the appropriate scoped registry instead of the global registry.
|
|
80
|
-
*/
|
|
81
|
-
export class DefineScopedCustomElementHandler extends DefineCustomElementHandler {
|
|
82
|
-
/**
|
|
83
|
-
* Define the custom element in the element's scoped registry.
|
|
84
|
-
* @param tagName - The custom element tag name
|
|
85
|
-
* @param ElementClass - The element class constructor
|
|
86
|
-
* @param mountedElement - The mounted element with customElementRegistry
|
|
87
|
-
*/
|
|
88
|
-
define(tagName, ElementClass, mountedElement) {
|
|
89
|
-
const registry = mountedElement.customElementRegistry;
|
|
90
|
-
if (!registry) {
|
|
91
|
-
throw new Error('Element does not have a customElementRegistry. Scoped registries require Chrome 146+ or latest WebKit/Safari.');
|
|
92
|
-
}
|
|
93
|
-
// Check if already defined in this scoped registry
|
|
94
|
-
if (registry.get(tagName)) {
|
|
95
|
-
return;
|
|
96
|
-
}
|
|
97
|
-
registry.define(tagName, ElementClass);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
1
|
+
import { EvtRt } from '../EvtRt.js';
|
|
2
|
+
export class DefineCustomElementHandler extends EvtRt {
|
|
3
|
+
mount(mountedElement, MountConfig, context) {
|
|
4
|
+
this.abort();
|
|
5
|
+
// Check if modules are specified
|
|
6
|
+
if (!context.modules || context.modules.length === 0) {
|
|
7
|
+
throw new Error('Must specify an ES Module');
|
|
8
|
+
}
|
|
9
|
+
const module = context.modules[0];
|
|
10
|
+
const tagName = mountedElement.localName;
|
|
11
|
+
// Check if already defined
|
|
12
|
+
if (customElements.get(tagName)) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
// Find suitable class
|
|
16
|
+
const ElementClass = this.findSuitableClass(module);
|
|
17
|
+
// Validate that ElementClass is a constructor
|
|
18
|
+
if (typeof ElementClass !== 'function') {
|
|
19
|
+
throw new Error(`Found class is not a constructor: ${typeof ElementClass}`);
|
|
20
|
+
}
|
|
21
|
+
// Create wrapper class to allow reuse
|
|
22
|
+
// Use anonymous class expression which works across all browsers
|
|
23
|
+
const WrapperClass = class extends ElementClass {
|
|
24
|
+
};
|
|
25
|
+
// Define the custom element using the define method
|
|
26
|
+
this.define(tagName, WrapperClass, mountedElement);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Define the custom element in the appropriate registry.
|
|
30
|
+
* Override this method in subclasses to use scoped registries.
|
|
31
|
+
* @param tagName - The custom element tag name
|
|
32
|
+
* @param ElementClass - The element class constructor
|
|
33
|
+
* @param mountedElement - The mounted element (used for scoped registry access)
|
|
34
|
+
*/
|
|
35
|
+
define(tagName, ElementClass, mountedElement) {
|
|
36
|
+
customElements.define(tagName, ElementClass);
|
|
37
|
+
}
|
|
38
|
+
findSuitableClass(module) {
|
|
39
|
+
// Check default export first
|
|
40
|
+
const defaultExport = module.default;
|
|
41
|
+
if (defaultExport && this.extendsHTMLElement(defaultExport)) {
|
|
42
|
+
return defaultExport;
|
|
43
|
+
}
|
|
44
|
+
// Find all exports that extend HTMLElement
|
|
45
|
+
const htmlElementClasses = Object.values(module)
|
|
46
|
+
.filter(exp => typeof exp === 'function' && this.extendsHTMLElement(exp));
|
|
47
|
+
if (htmlElementClasses.length === 0) {
|
|
48
|
+
throw new Error('No suitable class found in module');
|
|
49
|
+
}
|
|
50
|
+
if (htmlElementClasses.length > 1) {
|
|
51
|
+
throw new Error('More than one class found in module');
|
|
52
|
+
}
|
|
53
|
+
return htmlElementClasses[0];
|
|
54
|
+
}
|
|
55
|
+
extendsHTMLElement(cls) {
|
|
56
|
+
try {
|
|
57
|
+
// Must be a function
|
|
58
|
+
if (typeof cls !== 'function') {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
// Handle direct HTMLElement export
|
|
62
|
+
if (cls === HTMLElement) {
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
// Check if it has a prototype and extends HTMLElement
|
|
66
|
+
if (cls.prototype && cls.prototype instanceof HTMLElement) {
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Handler for defining custom elements in scoped registries.
|
|
78
|
+
* Uses the element's customElementRegistry property to define elements
|
|
79
|
+
* in the appropriate scoped registry instead of the global registry.
|
|
80
|
+
*/
|
|
81
|
+
export class DefineScopedCustomElementHandler extends DefineCustomElementHandler {
|
|
82
|
+
/**
|
|
83
|
+
* Define the custom element in the element's scoped registry.
|
|
84
|
+
* @param tagName - The custom element tag name
|
|
85
|
+
* @param ElementClass - The element class constructor
|
|
86
|
+
* @param mountedElement - The mounted element with customElementRegistry
|
|
87
|
+
*/
|
|
88
|
+
define(tagName, ElementClass, mountedElement) {
|
|
89
|
+
const registry = mountedElement.customElementRegistry;
|
|
90
|
+
if (!registry) {
|
|
91
|
+
throw new Error('Element does not have a customElementRegistry. Scoped registries require Chrome 146+ or latest WebKit/Safari.');
|
|
92
|
+
}
|
|
93
|
+
// Check if already defined in this scoped registry
|
|
94
|
+
if (registry.get(tagName)) {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
registry.define(tagName, ElementClass);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
// Register built-in handlers
|
|
101
|
+
import { MountObserver } from '../MountObserver.js';
|
|
102
|
+
MountObserver.define('builtIns.defineCustomElement', DefineCustomElementHandler);
|
|
103
|
+
MountObserver.define('buildIns.defineScopedCustomElement', DefineScopedCustomElementHandler);
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { EvtRt } from '../EvtRt.js';
|
|
2
|
+
import { MountConfig, MountContext } from '../types/mount-observer/types.js';
|
|
3
|
+
|
|
4
|
+
export class DefineCustomElementHandler extends EvtRt {
|
|
5
|
+
mount(mountedElement: Element, MountConfig: MountConfig, context: MountContext): void {
|
|
6
|
+
this.abort();
|
|
7
|
+
// Check if modules are specified
|
|
8
|
+
if (!context.modules || context.modules.length === 0) {
|
|
9
|
+
throw new Error('Must specify an ES Module');
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const module = context.modules[0];
|
|
13
|
+
const tagName = mountedElement.localName;
|
|
14
|
+
|
|
15
|
+
// Check if already defined
|
|
16
|
+
if (customElements.get(tagName)) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Find suitable class
|
|
21
|
+
const ElementClass = this.findSuitableClass(module);
|
|
22
|
+
|
|
23
|
+
// Validate that ElementClass is a constructor
|
|
24
|
+
if (typeof ElementClass !== 'function') {
|
|
25
|
+
throw new Error(`Found class is not a constructor: ${typeof ElementClass}`);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Create wrapper class to allow reuse
|
|
29
|
+
// Use anonymous class expression which works across all browsers
|
|
30
|
+
const WrapperClass = class extends ElementClass {};
|
|
31
|
+
|
|
32
|
+
// Define the custom element using the define method
|
|
33
|
+
this.define(tagName, WrapperClass, mountedElement);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Define the custom element in the appropriate registry.
|
|
38
|
+
* Override this method in subclasses to use scoped registries.
|
|
39
|
+
* @param tagName - The custom element tag name
|
|
40
|
+
* @param ElementClass - The element class constructor
|
|
41
|
+
* @param mountedElement - The mounted element (used for scoped registry access)
|
|
42
|
+
*/
|
|
43
|
+
protected define(tagName: string, ElementClass: CustomElementConstructor, mountedElement: Element): void {
|
|
44
|
+
customElements.define(tagName, ElementClass);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
private findSuitableClass(module: any): typeof HTMLElement {
|
|
48
|
+
// Check default export first
|
|
49
|
+
const defaultExport = module.default;
|
|
50
|
+
|
|
51
|
+
if (defaultExport && this.extendsHTMLElement(defaultExport)) {
|
|
52
|
+
return defaultExport;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Find all exports that extend HTMLElement
|
|
56
|
+
const htmlElementClasses = Object.values(module)
|
|
57
|
+
.filter(exp => typeof exp === 'function' && this.extendsHTMLElement(exp));
|
|
58
|
+
|
|
59
|
+
if (htmlElementClasses.length === 0) {
|
|
60
|
+
throw new Error('No suitable class found in module');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (htmlElementClasses.length > 1) {
|
|
64
|
+
throw new Error('More than one class found in module');
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return htmlElementClasses[0] as typeof HTMLElement;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
private extendsHTMLElement(cls: any): boolean {
|
|
71
|
+
try {
|
|
72
|
+
// Must be a function
|
|
73
|
+
if (typeof cls !== 'function') {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
// Handle direct HTMLElement export
|
|
77
|
+
if (cls === HTMLElement) {
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
// Check if it has a prototype and extends HTMLElement
|
|
81
|
+
if (cls.prototype && cls.prototype instanceof HTMLElement) {
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
return false;
|
|
85
|
+
} catch {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Handler for defining custom elements in scoped registries.
|
|
93
|
+
* Uses the element's customElementRegistry property to define elements
|
|
94
|
+
* in the appropriate scoped registry instead of the global registry.
|
|
95
|
+
*/
|
|
96
|
+
export class DefineScopedCustomElementHandler extends DefineCustomElementHandler {
|
|
97
|
+
/**
|
|
98
|
+
* Define the custom element in the element's scoped registry.
|
|
99
|
+
* @param tagName - The custom element tag name
|
|
100
|
+
* @param ElementClass - The element class constructor
|
|
101
|
+
* @param mountedElement - The mounted element with customElementRegistry
|
|
102
|
+
*/
|
|
103
|
+
protected define(tagName: string, ElementClass: CustomElementConstructor, mountedElement: Element): void {
|
|
104
|
+
const registry = (mountedElement as any).customElementRegistry;
|
|
105
|
+
|
|
106
|
+
if (!registry) {
|
|
107
|
+
throw new Error('Element does not have a customElementRegistry. Scoped registries require Chrome 146+ or latest WebKit/Safari.');
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Check if already defined in this scoped registry
|
|
111
|
+
if (registry.get(tagName)) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
registry.define(tagName, ElementClass);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Register built-in handlers
|
|
120
|
+
import { MountObserver } from '../MountObserver.js';
|
|
121
|
+
|
|
122
|
+
MountObserver.define('builtIns.defineCustomElement', DefineCustomElementHandler);
|
|
123
|
+
MountObserver.define('buildIns.defineScopedCustomElement', DefineScopedCustomElementHandler);
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { EvtRt } from '../EvtRt.js';
|
|
2
|
+
//import { buildCSSQuery } from 'assign-gingerly/buildCSSQuery.js';
|
|
3
|
+
import 'assign-gingerly/object-extension.js';
|
|
4
|
+
/**
|
|
5
|
+
* Handler for automatically enhancing mounted elements using assign-gingerly.
|
|
6
|
+
* Searches the first imported module for an export with a "spawn" property
|
|
7
|
+
* and uses element.enh.get() to spawn the enhancement.
|
|
8
|
+
*/
|
|
9
|
+
export class EnhanceMountedElementHandler extends EvtRt {
|
|
10
|
+
async mount(mountedElement, MountConfig, context) {
|
|
11
|
+
this.abort();
|
|
12
|
+
// Check if modules are specified
|
|
13
|
+
if (!context.modules || context.modules.length === 0) {
|
|
14
|
+
throw new Error('Must specify an ES Module with import property');
|
|
15
|
+
}
|
|
16
|
+
const module = context.modules[0];
|
|
17
|
+
// Find registry item (object with spawn property)
|
|
18
|
+
const registryItem = await this._findRegistryItem(module, mountedElement);
|
|
19
|
+
if (!registryItem) {
|
|
20
|
+
throw new Error('No registry item found in module. Expected an export with a "spawn" property.');
|
|
21
|
+
}
|
|
22
|
+
// Validate spawn is a constructor
|
|
23
|
+
if (typeof registryItem.spawn !== 'function') {
|
|
24
|
+
throw new Error('Registry item "spawn" property must be a constructor function');
|
|
25
|
+
}
|
|
26
|
+
// Spawn the enhancement
|
|
27
|
+
this.#spawnEnhancement(mountedElement, registryItem, context);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Spawn the enhancement using element.enh.get().
|
|
31
|
+
* Polyfills customElementRegistry if needed for browsers without scoped registry support.
|
|
32
|
+
*/
|
|
33
|
+
#spawnEnhancement(element, registryItem, context) {
|
|
34
|
+
// Polyfill element.customElementRegistry if it doesn't exist (for browsers without scoped registries)
|
|
35
|
+
if (!element.customElementRegistry) {
|
|
36
|
+
Object.defineProperty(element, 'customElementRegistry', {
|
|
37
|
+
value: customElements,
|
|
38
|
+
writable: true,
|
|
39
|
+
enumerable: false,
|
|
40
|
+
configurable: true
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
// Use element.enh.get() to spawn the enhancement
|
|
44
|
+
const enh = element.enh;
|
|
45
|
+
if (!enh || typeof enh.get !== 'function') {
|
|
46
|
+
throw new Error('Element does not have enh.get() method. Make sure assign-gingerly/object-extension.js is loaded.');
|
|
47
|
+
}
|
|
48
|
+
enh.get(registryItem, context);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Find a registry item in the module exports.
|
|
52
|
+
* A registry item is an object with a "spawn" property.
|
|
53
|
+
* @param module - The imported module
|
|
54
|
+
* @returns The registry item or null if not found
|
|
55
|
+
*/
|
|
56
|
+
async _findRegistryItem(module, el) {
|
|
57
|
+
// Check default export first
|
|
58
|
+
if (module.default && await this._isRegistryItem(module.default, el)) {
|
|
59
|
+
return module.default;
|
|
60
|
+
}
|
|
61
|
+
// Search all exports for a registry item
|
|
62
|
+
const exports = Object.values(module);
|
|
63
|
+
const registryItems = [];
|
|
64
|
+
for (const e of exports) {
|
|
65
|
+
const isRegistryItem = await this._isRegistryItem(e, el);
|
|
66
|
+
if (isRegistryItem)
|
|
67
|
+
registryItems.push(e);
|
|
68
|
+
}
|
|
69
|
+
if (registryItems.length === 0) {
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
if (registryItems.length > 1) {
|
|
73
|
+
throw new Error('More than one registry item found in module. Expected exactly one export with a "spawn" property.');
|
|
74
|
+
}
|
|
75
|
+
return registryItems[0];
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Check if an export is a registry item (has a spawn property).
|
|
79
|
+
* @param exp - The export to check
|
|
80
|
+
* @returns True if the export is a registry item
|
|
81
|
+
*/
|
|
82
|
+
async _isRegistryItem(exp, mountedElement) {
|
|
83
|
+
let test = exp !== null
|
|
84
|
+
&& typeof exp === 'object'
|
|
85
|
+
&& 'spawn' in exp
|
|
86
|
+
&& typeof exp.spawn === 'function';
|
|
87
|
+
if (!test)
|
|
88
|
+
return false;
|
|
89
|
+
const emc = exp;
|
|
90
|
+
if (emc.withAttrs !== undefined) {
|
|
91
|
+
const cssQuery = (await import('assign-gingerly/buildCSSQuery.js')).buildCSSQuery(emc);
|
|
92
|
+
return mountedElement.matches(cssQuery);
|
|
93
|
+
}
|
|
94
|
+
return true;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
// Register built-in handler
|
|
98
|
+
import { MountObserver } from '../MountObserver.js';
|
|
99
|
+
MountObserver.define('builtIns.enhanceMountedElement', EnhanceMountedElementHandler);
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { EvtRt } from '../EvtRt.js';
|
|
2
|
+
import {EnhancementConfig} from '../types/assign-gingerly/types.js';
|
|
3
|
+
import { MountConfig, MountContext } from '../types/mount-observer/types.js';
|
|
4
|
+
//import { buildCSSQuery } from 'assign-gingerly/buildCSSQuery.js';
|
|
5
|
+
import 'assign-gingerly/object-extension.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Handler for automatically enhancing mounted elements using assign-gingerly.
|
|
9
|
+
* Searches the first imported module for an export with a "spawn" property
|
|
10
|
+
* and uses element.enh.get() to spawn the enhancement.
|
|
11
|
+
*/
|
|
12
|
+
export class EnhanceMountedElementHandler extends EvtRt {
|
|
13
|
+
async mount(mountedElement: Element, MountConfig: MountConfig, context: MountContext){
|
|
14
|
+
this.abort();
|
|
15
|
+
// Check if modules are specified
|
|
16
|
+
if (!context.modules || context.modules.length === 0) {
|
|
17
|
+
throw new Error('Must specify an ES Module with import property');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const module = context.modules[0];
|
|
21
|
+
|
|
22
|
+
// Find registry item (object with spawn property)
|
|
23
|
+
const registryItem = await this._findRegistryItem(module, mountedElement);
|
|
24
|
+
|
|
25
|
+
if (!registryItem) {
|
|
26
|
+
throw new Error('No registry item found in module. Expected an export with a "spawn" property.');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Validate spawn is a constructor
|
|
30
|
+
if (typeof registryItem.spawn !== 'function') {
|
|
31
|
+
throw new Error('Registry item "spawn" property must be a constructor function');
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Spawn the enhancement
|
|
35
|
+
this.#spawnEnhancement(mountedElement, registryItem, context);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Spawn the enhancement using element.enh.get().
|
|
40
|
+
* Polyfills customElementRegistry if needed for browsers without scoped registry support.
|
|
41
|
+
*/
|
|
42
|
+
#spawnEnhancement(element: Element, registryItem: any, context: MountContext): void {
|
|
43
|
+
// Polyfill element.customElementRegistry if it doesn't exist (for browsers without scoped registries)
|
|
44
|
+
if (!(element as any).customElementRegistry) {
|
|
45
|
+
Object.defineProperty(element, 'customElementRegistry', {
|
|
46
|
+
value: customElements,
|
|
47
|
+
writable: true,
|
|
48
|
+
enumerable: false,
|
|
49
|
+
configurable: true
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Use element.enh.get() to spawn the enhancement
|
|
54
|
+
const enh = (element as any).enh;
|
|
55
|
+
if (!enh || typeof enh.get !== 'function') {
|
|
56
|
+
throw new Error('Element does not have enh.get() method. Make sure assign-gingerly/object-extension.js is loaded.');
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
enh.get(registryItem, context);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Find a registry item in the module exports.
|
|
64
|
+
* A registry item is an object with a "spawn" property.
|
|
65
|
+
* @param module - The imported module
|
|
66
|
+
* @returns The registry item or null if not found
|
|
67
|
+
*/
|
|
68
|
+
protected async _findRegistryItem(module: any, el: Element): Promise<any | null> {
|
|
69
|
+
// Check default export first
|
|
70
|
+
if (module.default && await this._isRegistryItem(module.default, el)) {
|
|
71
|
+
return module.default;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Search all exports for a registry item
|
|
75
|
+
const exports = Object.values(module);
|
|
76
|
+
const registryItems = [];
|
|
77
|
+
for(const e of exports){
|
|
78
|
+
const isRegistryItem = await this._isRegistryItem(e, el);
|
|
79
|
+
if(isRegistryItem) registryItems.push(e);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (registryItems.length === 0) {
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (registryItems.length > 1) {
|
|
87
|
+
throw new Error('More than one registry item found in module. Expected exactly one export with a "spawn" property.');
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return registryItems[0];
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Check if an export is a registry item (has a spawn property).
|
|
95
|
+
* @param exp - The export to check
|
|
96
|
+
* @returns True if the export is a registry item
|
|
97
|
+
*/
|
|
98
|
+
protected async _isRegistryItem(exp: any, mountedElement: Element): Promise<boolean> {
|
|
99
|
+
let test = exp !== null
|
|
100
|
+
&& typeof exp === 'object'
|
|
101
|
+
&& 'spawn' in exp
|
|
102
|
+
&& typeof exp.spawn === 'function';
|
|
103
|
+
if(!test) return false;
|
|
104
|
+
const emc = exp as EnhancementConfig;
|
|
105
|
+
if(emc.withAttrs !== undefined){
|
|
106
|
+
const cssQuery = (await import('assign-gingerly/buildCSSQuery.js')).buildCSSQuery(emc);
|
|
107
|
+
return mountedElement.matches(cssQuery);
|
|
108
|
+
}
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Register built-in handler
|
|
114
|
+
import { MountObserver } from '../MountObserver.js';
|
|
115
|
+
|
|
116
|
+
MountObserver.define('builtIns.enhanceMountedElement', EnhanceMountedElementHandler);
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.ResolvedEvent = exports.MediaUnmatchEvent = exports.MediaMatchEvent = exports.LoadEvent = exports.DisconnectEvent = exports.DismountEvent = exports.MountEvent = exports.resolvedEventName = exports.mediaunmatchEventName = exports.mediamatchEventName = exports.disconnectEventName = exports.dismountEventName = exports.mountEventName = exports.loadEventName = void 0;
|
|
19
|
+
// Event name constants
|
|
20
|
+
exports.loadEventName = 'load';
|
|
21
|
+
exports.mountEventName = 'mount';
|
|
22
|
+
exports.dismountEventName = 'dismount';
|
|
23
|
+
exports.disconnectEventName = 'disconnect';
|
|
24
|
+
exports.mediamatchEventName = 'mediamatch';
|
|
25
|
+
exports.mediaunmatchEventName = 'mediaunmatch';
|
|
26
|
+
exports.resolvedEventName = 'resolved';
|
|
27
|
+
var MountEvent = /** @class */ (function (_super) {
|
|
28
|
+
__extends(MountEvent, _super);
|
|
29
|
+
function MountEvent(mountedElement, modules, mountConfig, mountContext) {
|
|
30
|
+
var _this = _super.call(this, MountEvent.eventName) || this;
|
|
31
|
+
_this.mountedElement = mountedElement;
|
|
32
|
+
_this.modules = modules;
|
|
33
|
+
_this.mountConfig = mountConfig;
|
|
34
|
+
_this.mountContext = mountContext;
|
|
35
|
+
return _this;
|
|
36
|
+
}
|
|
37
|
+
MountEvent.eventName = exports.mountEventName;
|
|
38
|
+
return MountEvent;
|
|
39
|
+
}(Event));
|
|
40
|
+
exports.MountEvent = MountEvent;
|
|
41
|
+
var DismountEvent = /** @class */ (function (_super) {
|
|
42
|
+
__extends(DismountEvent, _super);
|
|
43
|
+
function DismountEvent(mountedElement, reason, mountConfig) {
|
|
44
|
+
var _this = _super.call(this, DismountEvent.eventName) || this;
|
|
45
|
+
_this.mountedElement = mountedElement;
|
|
46
|
+
_this.reason = reason;
|
|
47
|
+
_this.mountConfig = mountConfig;
|
|
48
|
+
return _this;
|
|
49
|
+
}
|
|
50
|
+
DismountEvent.eventName = exports.dismountEventName;
|
|
51
|
+
return DismountEvent;
|
|
52
|
+
}(Event));
|
|
53
|
+
exports.DismountEvent = DismountEvent;
|
|
54
|
+
var DisconnectEvent = /** @class */ (function (_super) {
|
|
55
|
+
__extends(DisconnectEvent, _super);
|
|
56
|
+
function DisconnectEvent(mountedElement, mountConfig) {
|
|
57
|
+
var _this = _super.call(this, DisconnectEvent.eventName) || this;
|
|
58
|
+
_this.mountedElement = mountedElement;
|
|
59
|
+
_this.mountConfig = mountConfig;
|
|
60
|
+
return _this;
|
|
61
|
+
}
|
|
62
|
+
DisconnectEvent.eventName = exports.disconnectEventName;
|
|
63
|
+
return DisconnectEvent;
|
|
64
|
+
}(Event));
|
|
65
|
+
exports.DisconnectEvent = DisconnectEvent;
|
|
66
|
+
var LoadEvent = /** @class */ (function (_super) {
|
|
67
|
+
__extends(LoadEvent, _super);
|
|
68
|
+
function LoadEvent(modules, mountConfig) {
|
|
69
|
+
var _this = _super.call(this, LoadEvent.eventName) || this;
|
|
70
|
+
_this.modules = modules;
|
|
71
|
+
_this.mountConfig = mountConfig;
|
|
72
|
+
return _this;
|
|
73
|
+
}
|
|
74
|
+
LoadEvent.eventName = exports.loadEventName;
|
|
75
|
+
return LoadEvent;
|
|
76
|
+
}(Event));
|
|
77
|
+
exports.LoadEvent = LoadEvent;
|
|
78
|
+
var MediaMatchEvent = /** @class */ (function (_super) {
|
|
79
|
+
__extends(MediaMatchEvent, _super);
|
|
80
|
+
function MediaMatchEvent(mountConfig) {
|
|
81
|
+
var _this = _super.call(this, MediaMatchEvent.eventName) || this;
|
|
82
|
+
_this.mountConfig = mountConfig;
|
|
83
|
+
return _this;
|
|
84
|
+
}
|
|
85
|
+
MediaMatchEvent.eventName = exports.mediamatchEventName;
|
|
86
|
+
return MediaMatchEvent;
|
|
87
|
+
}(Event));
|
|
88
|
+
exports.MediaMatchEvent = MediaMatchEvent;
|
|
89
|
+
var MediaUnmatchEvent = /** @class */ (function (_super) {
|
|
90
|
+
__extends(MediaUnmatchEvent, _super);
|
|
91
|
+
function MediaUnmatchEvent(mountConfig) {
|
|
92
|
+
var _this = _super.call(this, MediaUnmatchEvent.eventName) || this;
|
|
93
|
+
_this.mountConfig = mountConfig;
|
|
94
|
+
return _this;
|
|
95
|
+
}
|
|
96
|
+
MediaUnmatchEvent.eventName = exports.mediaunmatchEventName;
|
|
97
|
+
return MediaUnmatchEvent;
|
|
98
|
+
}(Event));
|
|
99
|
+
exports.MediaUnmatchEvent = MediaUnmatchEvent;
|
|
100
|
+
var ResolvedEvent = /** @class */ (function (_super) {
|
|
101
|
+
__extends(ResolvedEvent, _super);
|
|
102
|
+
function ResolvedEvent(exportValue) {
|
|
103
|
+
var _this = _super.call(this, ResolvedEvent.eventName) || this;
|
|
104
|
+
_this.export = exportValue;
|
|
105
|
+
return _this;
|
|
106
|
+
}
|
|
107
|
+
ResolvedEvent.eventName = exports.resolvedEventName;
|
|
108
|
+
return ResolvedEvent;
|
|
109
|
+
}(Event));
|
|
110
|
+
exports.ResolvedEvent = ResolvedEvent;
|