mount-observer 0.1.10 → 0.1.11
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/EnhanceMountedElementHandler.js +25 -12
- package/EnhanceMountedElementHandler.ts +24 -13
- package/README.md +5 -3
- package/package.json +2 -2
|
@@ -1,18 +1,20 @@
|
|
|
1
1
|
import { EvtRt } from './EvtRt.js';
|
|
2
|
+
//import { buildCSSQuery } from 'assign-gingerly/buildCSSQuery.js';
|
|
3
|
+
import 'assign-gingerly/object-extension.js';
|
|
2
4
|
/**
|
|
3
5
|
* Handler for automatically enhancing mounted elements using assign-gingerly.
|
|
4
6
|
* Searches the first imported module for an export with a "spawn" property
|
|
5
7
|
* and uses element.enh.get() to spawn the enhancement.
|
|
6
8
|
*/
|
|
7
9
|
export class EnhanceMountedElementHandler extends EvtRt {
|
|
8
|
-
mount(mountedElement, MountConfig, context) {
|
|
10
|
+
async mount(mountedElement, MountConfig, context) {
|
|
9
11
|
// Check if modules are specified
|
|
10
12
|
if (!context.modules || context.modules.length === 0) {
|
|
11
13
|
throw new Error('Must specify an ES Module with import property');
|
|
12
14
|
}
|
|
13
15
|
const module = context.modules[0];
|
|
14
16
|
// Find registry item (object with spawn property)
|
|
15
|
-
const registryItem = this.
|
|
17
|
+
const registryItem = await this._findRegistryItem(module, mountedElement);
|
|
16
18
|
if (!registryItem) {
|
|
17
19
|
throw new Error('No registry item found in module. Expected an export with a "spawn" property.');
|
|
18
20
|
}
|
|
@@ -21,15 +23,13 @@ export class EnhanceMountedElementHandler extends EvtRt {
|
|
|
21
23
|
throw new Error('Registry item "spawn" property must be a constructor function');
|
|
22
24
|
}
|
|
23
25
|
// Spawn the enhancement
|
|
24
|
-
this.
|
|
26
|
+
this._spawnEnhancement(mountedElement, registryItem, context);
|
|
25
27
|
}
|
|
26
28
|
/**
|
|
27
29
|
* Spawn the enhancement using element.enh.get().
|
|
28
30
|
* Polyfills customElementRegistry if needed for browsers without scoped registry support.
|
|
29
31
|
*/
|
|
30
|
-
|
|
31
|
-
// Import assign-gingerly object-extension to enable enh property
|
|
32
|
-
await import('assign-gingerly/object-extension.js');
|
|
32
|
+
_spawnEnhancement(element, registryItem, context) {
|
|
33
33
|
// Polyfill element.customElementRegistry if it doesn't exist (for browsers without scoped registries)
|
|
34
34
|
if (!element.customElementRegistry) {
|
|
35
35
|
Object.defineProperty(element, 'customElementRegistry', {
|
|
@@ -52,14 +52,19 @@ export class EnhanceMountedElementHandler extends EvtRt {
|
|
|
52
52
|
* @param module - The imported module
|
|
53
53
|
* @returns The registry item or null if not found
|
|
54
54
|
*/
|
|
55
|
-
|
|
55
|
+
async _findRegistryItem(module, el) {
|
|
56
56
|
// Check default export first
|
|
57
|
-
if (module.default && this.
|
|
57
|
+
if (module.default && await this._isRegistryItem(module.default, el)) {
|
|
58
58
|
return module.default;
|
|
59
59
|
}
|
|
60
60
|
// Search all exports for a registry item
|
|
61
|
-
const
|
|
62
|
-
|
|
61
|
+
const exports = Object.values(module);
|
|
62
|
+
const registryItems = [];
|
|
63
|
+
for (const e of exports) {
|
|
64
|
+
const isRegistryItem = await this._isRegistryItem(e, el);
|
|
65
|
+
if (isRegistryItem)
|
|
66
|
+
registryItems.push(e);
|
|
67
|
+
}
|
|
63
68
|
if (registryItems.length === 0) {
|
|
64
69
|
return null;
|
|
65
70
|
}
|
|
@@ -73,10 +78,18 @@ export class EnhanceMountedElementHandler extends EvtRt {
|
|
|
73
78
|
* @param exp - The export to check
|
|
74
79
|
* @returns True if the export is a registry item
|
|
75
80
|
*/
|
|
76
|
-
|
|
77
|
-
|
|
81
|
+
async _isRegistryItem(exp, mountedElement) {
|
|
82
|
+
let test = exp !== null
|
|
78
83
|
&& typeof exp === 'object'
|
|
79
84
|
&& 'spawn' in exp
|
|
80
85
|
&& typeof exp.spawn === 'function';
|
|
86
|
+
if (!test)
|
|
87
|
+
return false;
|
|
88
|
+
const emc = exp;
|
|
89
|
+
if (emc.withAttrs !== undefined) {
|
|
90
|
+
const cssQuery = (await import('assign-gingerly/buildCSSQuery.js')).buildCSSQuery(emc);
|
|
91
|
+
return mountedElement.matches(cssQuery);
|
|
92
|
+
}
|
|
93
|
+
return true;
|
|
81
94
|
}
|
|
82
95
|
}
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { EvtRt } from './EvtRt.js';
|
|
2
|
+
import {EnhancementConfig} from './types/assign-gingerly/types.js';
|
|
2
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';
|
|
3
6
|
|
|
4
7
|
/**
|
|
5
8
|
* Handler for automatically enhancing mounted elements using assign-gingerly.
|
|
@@ -7,7 +10,7 @@ import { MountConfig, MountContext } from './types/mount-observer/types.js';
|
|
|
7
10
|
* and uses element.enh.get() to spawn the enhancement.
|
|
8
11
|
*/
|
|
9
12
|
export class EnhanceMountedElementHandler extends EvtRt {
|
|
10
|
-
mount(mountedElement: Element, MountConfig: MountConfig, context: MountContext)
|
|
13
|
+
async mount(mountedElement: Element, MountConfig: MountConfig, context: MountContext){
|
|
11
14
|
// Check if modules are specified
|
|
12
15
|
if (!context.modules || context.modules.length === 0) {
|
|
13
16
|
throw new Error('Must specify an ES Module with import property');
|
|
@@ -16,7 +19,7 @@ export class EnhanceMountedElementHandler extends EvtRt {
|
|
|
16
19
|
const module = context.modules[0];
|
|
17
20
|
|
|
18
21
|
// Find registry item (object with spawn property)
|
|
19
|
-
const registryItem = this.
|
|
22
|
+
const registryItem = await this._findRegistryItem(module, mountedElement);
|
|
20
23
|
|
|
21
24
|
if (!registryItem) {
|
|
22
25
|
throw new Error('No registry item found in module. Expected an export with a "spawn" property.');
|
|
@@ -28,17 +31,14 @@ export class EnhanceMountedElementHandler extends EvtRt {
|
|
|
28
31
|
}
|
|
29
32
|
|
|
30
33
|
// Spawn the enhancement
|
|
31
|
-
this.
|
|
34
|
+
this._spawnEnhancement(mountedElement, registryItem, context);
|
|
32
35
|
}
|
|
33
36
|
|
|
34
37
|
/**
|
|
35
38
|
* Spawn the enhancement using element.enh.get().
|
|
36
39
|
* Polyfills customElementRegistry if needed for browsers without scoped registry support.
|
|
37
40
|
*/
|
|
38
|
-
|
|
39
|
-
// Import assign-gingerly object-extension to enable enh property
|
|
40
|
-
await import('assign-gingerly/object-extension.js');
|
|
41
|
-
|
|
41
|
+
protected _spawnEnhancement(element: Element, registryItem: any, context: MountContext): void {
|
|
42
42
|
// Polyfill element.customElementRegistry if it doesn't exist (for browsers without scoped registries)
|
|
43
43
|
if (!(element as any).customElementRegistry) {
|
|
44
44
|
Object.defineProperty(element, 'customElementRegistry', {
|
|
@@ -64,15 +64,19 @@ export class EnhanceMountedElementHandler extends EvtRt {
|
|
|
64
64
|
* @param module - The imported module
|
|
65
65
|
* @returns The registry item or null if not found
|
|
66
66
|
*/
|
|
67
|
-
|
|
67
|
+
protected async _findRegistryItem(module: any, el: Element): Promise<any | null> {
|
|
68
68
|
// Check default export first
|
|
69
|
-
if (module.default && this.
|
|
69
|
+
if (module.default && await this._isRegistryItem(module.default, el)) {
|
|
70
70
|
return module.default;
|
|
71
71
|
}
|
|
72
72
|
|
|
73
73
|
// Search all exports for a registry item
|
|
74
|
-
const
|
|
75
|
-
|
|
74
|
+
const exports = Object.values(module);
|
|
75
|
+
const registryItems = [];
|
|
76
|
+
for(const e of exports){
|
|
77
|
+
const isRegistryItem = await this._isRegistryItem(e, el);
|
|
78
|
+
if(isRegistryItem) registryItems.push(e);
|
|
79
|
+
}
|
|
76
80
|
|
|
77
81
|
if (registryItems.length === 0) {
|
|
78
82
|
return null;
|
|
@@ -90,10 +94,17 @@ export class EnhanceMountedElementHandler extends EvtRt {
|
|
|
90
94
|
* @param exp - The export to check
|
|
91
95
|
* @returns True if the export is a registry item
|
|
92
96
|
*/
|
|
93
|
-
|
|
94
|
-
|
|
97
|
+
protected async _isRegistryItem(exp: any, mountedElement: Element): Promise<boolean> {
|
|
98
|
+
let test = exp !== null
|
|
95
99
|
&& typeof exp === 'object'
|
|
96
100
|
&& 'spawn' in exp
|
|
97
101
|
&& typeof exp.spawn === 'function';
|
|
102
|
+
if(!test) return false;
|
|
103
|
+
const emc = exp as EnhancementConfig;
|
|
104
|
+
if(emc.withAttrs !== undefined){
|
|
105
|
+
const cssQuery = (await import('assign-gingerly/buildCSSQuery.js')).buildCSSQuery(emc);
|
|
106
|
+
return mountedElement.matches(cssQuery);
|
|
107
|
+
}
|
|
108
|
+
return true;
|
|
98
109
|
}
|
|
99
110
|
}
|
package/README.md
CHANGED
|
@@ -42,9 +42,9 @@ The following features have been implemented and tested:
|
|
|
42
42
|
|
|
43
43
|
Author: Bruce B. Anderson (with valuable feedback from @doeixd)
|
|
44
44
|
|
|
45
|
-
Issues / PRs / polyfill: [mount-observer](https://github.com/bahrus/mount-observer)
|
|
45
|
+
Issues / PRs / polyfill: [mount-observer](https://github.com/bahrus/mount-observer/tree/v2)
|
|
46
46
|
|
|
47
|
-
Last Update:
|
|
47
|
+
Last Update: Feb 23, 2026
|
|
48
48
|
|
|
49
49
|
## Benefits of this API
|
|
50
50
|
|
|
@@ -100,7 +100,7 @@ The extra flexibility this new primitive would provide could be quite useful to
|
|
|
100
100
|
|
|
101
101
|
Before getting into the weeds, let's demonstrate the two most prominent use cases:
|
|
102
102
|
|
|
103
|
-
### Use Case 1: Custom Attribute Enhancement
|
|
103
|
+
### Use Case 1: Custom Attribute Enhancement [TODO]: out of date
|
|
104
104
|
|
|
105
105
|
```html
|
|
106
106
|
<body>
|
|
@@ -108,6 +108,8 @@ Before getting into the weeds, let's demonstrate the two most prominent use case
|
|
|
108
108
|
|
|
109
109
|
<script type=module>
|
|
110
110
|
import 'mount-observer/ElementMountExtension.js';
|
|
111
|
+
|
|
112
|
+
|
|
111
113
|
document.body.mount([{
|
|
112
114
|
withAttrs:{base: 'log-to-console'},
|
|
113
115
|
spawn: function(el){
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mount-observer",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.11",
|
|
4
4
|
"description": "Observe and act on css matches.",
|
|
5
5
|
"main": "MountObserver.js",
|
|
6
6
|
"module": "MountObserver.js",
|
|
7
7
|
"dependencies": {
|
|
8
|
-
"assign-gingerly": "0.0.
|
|
8
|
+
"assign-gingerly": "0.0.18"
|
|
9
9
|
},
|
|
10
10
|
"devDependencies": {
|
|
11
11
|
"@playwright/test": "1.58.2",
|