mount-observer 0.1.12 → 0.1.13

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/package.json CHANGED
@@ -1,61 +1,67 @@
1
- {
2
- "name": "mount-observer",
3
- "version": "0.1.12",
4
- "description": "Observe and act on css matches.",
5
- "main": "MountObserver.js",
6
- "module": "MountObserver.js",
7
- "dependencies": {
8
- "assign-gingerly": "0.0.18"
9
- },
10
- "devDependencies": {
11
- "@playwright/test": "1.58.2",
12
- "spa-ssi": "0.0.27"
13
- },
14
- "exports": {
15
- ".": {
16
- "default": "./index.js",
17
- "types": "./index.ts"
18
- },
19
- "./MountObserver.js": {
20
- "default": "./MountObserver.js",
21
- "types": "./MountObserver.ts"
22
- },
23
- "./withScopePerimeter.js": {
24
- "default": "./withScopePerimeter.js",
25
- "types": "./withScopePerimeter.ts"
26
- },
27
- "./emitEvents.js": {
28
- "default": "./emitEvents.js",
29
- "types": "./emitEvents.ts"
30
- },
31
- "./arr.js": {
32
- "default": "./arr.js",
33
- "types": "./arr.ts"
34
- },
35
- "./EvtRt.js": {
36
- "default": "./EvtRt.js",
37
- "types": "./EvtRt.ts"
38
- },
39
- "./DefineCustomElementHandler.js": {
40
- "default": "./DefineCustomElementHandler.js",
41
- "types": "./DefineCustomElementHandler.ts"
42
- },
43
- "./EnhanceMountedElementHandler.js": {
44
- "default": "./EnhanceMountedElementHandler.js",
45
- "types": "./EnhanceMountedElementHandler.ts"
46
- }
47
- },
48
- "files": [
49
- "*.js",
50
- "*.ts"
51
- ],
52
- "types": "./types/mount-observer/types.d.ts",
53
- "scripts": {
54
- "serve": "node ./node_modules/spa-ssi/serve.js",
55
- "test": "playwright test",
56
- "safari": "npx playwright wk http://localhost:8000",
57
- "update": "ncu -u && npm install"
58
- },
59
- "author": "Bruce B. Anderson <anderson.bruce.b@gmail.com>",
60
- "license": "MIT"
61
- }
1
+ {
2
+ "name": "mount-observer",
3
+ "version": "0.1.13",
4
+ "description": "Observe and act on css matches.",
5
+ "main": "MountObserver.js",
6
+ "module": "MountObserver.js",
7
+ "dependencies": {
8
+ "assign-gingerly": "0.0.22",
9
+ "id-generation": "0.0.4"
10
+ },
11
+ "devDependencies": {
12
+ "@playwright/test": "1.59.0-alpha-2026-02-28",
13
+ "spa-ssi": "0.0.27"
14
+ },
15
+ "exports": {
16
+ ".": {
17
+ "default": "./index.js",
18
+ "types": "./index.ts"
19
+ },
20
+ "./MountObserver.js": {
21
+ "default": "./MountObserver.js",
22
+ "types": "./MountObserver.ts"
23
+ },
24
+ "./withScopePerimeter.js": {
25
+ "default": "./withScopePerimeter.js",
26
+ "types": "./withScopePerimeter.ts"
27
+ },
28
+ "./emitEvents.js": {
29
+ "default": "./emitEvents.js",
30
+ "types": "./emitEvents.ts"
31
+ },
32
+ "./arr.js": {
33
+ "default": "./arr.js",
34
+ "types": "./arr.ts"
35
+ },
36
+ "./EvtRt.js": {
37
+ "default": "./EvtRt.js",
38
+ "types": "./EvtRt.ts"
39
+ },
40
+ "./DefineCustomElementHandler.js": {
41
+ "default": "./DefineCustomElementHandler.js",
42
+ "types": "./DefineCustomElementHandler.ts"
43
+ },
44
+ "./EnhanceMountedElementHandler.js": {
45
+ "default": "./EnhanceMountedElementHandler.js",
46
+ "types": "./EnhanceMountedElementHandler.ts"
47
+ },
48
+ "./upShadowSearch.js": {
49
+ "default": "./upShadowSearch.js",
50
+ "types": "./upShadowSearch.ts"
51
+ }
52
+ },
53
+ "files": [
54
+ "*.js",
55
+ "*.ts"
56
+ ],
57
+ "types": "./types/mount-observer/types.d.ts",
58
+ "scripts": {
59
+ "serve": "node ./node_modules/spa-ssi/serve.js",
60
+ "test": "playwright test",
61
+ "safari": "npx playwright wk http://localhost:8000",
62
+ "chrome": "npx playwright cr http://localhost:8000",
63
+ "update": "ncu -u && npm install"
64
+ },
65
+ "author": "Bruce B. Anderson <anderson.bruce.b@gmail.com>",
66
+ "license": "MIT"
67
+ }
@@ -1,6 +1,7 @@
1
1
  // playwright.config.ts
2
2
  import { PlaywrightTestConfig, devices } from '@playwright/test';
3
3
  const config: PlaywrightTestConfig = {
4
+ testIgnore: '**/experimentalCrossScopeRegistering/**',
4
5
  webServer: {
5
6
  command: 'npm run serve',
6
7
  url: 'http://localhost:8000/',
@@ -52,7 +52,7 @@ export function setupRootSizeObserver(init, rootNodeRef, mountedElements, module
52
52
  modules,
53
53
  observer: observer,
54
54
  rootNode,
55
- MountConfig: init
55
+ mountConfig: init
56
56
  };
57
57
  // Get all mounted elements from the WeakDual setWeak
58
58
  const mountedElementsList = [];
@@ -75,7 +75,7 @@ export function setupRootSizeObserver(
75
75
  modules,
76
76
  observer: observer as any,
77
77
  rootNode,
78
- MountConfig: init
78
+ mountConfig: init
79
79
  };
80
80
 
81
81
  // Get all mounted elements from the WeakDual setWeak
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Searches for an element by ID, traversing up through shadow DOM boundaries.
3
+ *
4
+ * This function searches for an element with the specified ID starting from the reference
5
+ * element's root node and continuing up through shadow DOM boundaries until the element
6
+ * is found or the document root is reached.
7
+ *
8
+ * Search order:
9
+ * 1. Check current root node using getElementById
10
+ * 2. If in shadow root, check host element's properties for the ID
11
+ * 3. Continue up to parent shadow root or document
12
+ * 4. Handle disconnected fragments via targetFragment property
13
+ *
14
+ * @param ref - The reference element to start searching from
15
+ * @param id - The ID of the element to find
16
+ * @returns The found element, or null if not found
17
+ *
18
+ * @example
19
+ * ```typescript
20
+ * const template = document.querySelector('template[src="#myId"]');
21
+ * const source = upShadowSearch(template, 'myId');
22
+ * if (source) {
23
+ * const clone = source.cloneNode(true);
24
+ * }
25
+ * ```
26
+ */
27
+ export function upShadowSearch(ref, id) {
28
+ let rn = ref.getRootNode();
29
+ while (rn) {
30
+ // Try getElementById on current root
31
+ if ('getElementById' in rn) {
32
+ const test = rn.getElementById(id);
33
+ if (test)
34
+ return test;
35
+ }
36
+ // If in shadow root, check host element
37
+ if ('host' in rn && rn.host) {
38
+ // Check if host has a property with this ID
39
+ const hostProp = rn.host[id];
40
+ if (hostProp instanceof HTMLElement)
41
+ return hostProp;
42
+ // Move up to host's root
43
+ rn = rn.host.getRootNode();
44
+ }
45
+ else if (rn === document) {
46
+ // Reached document root without finding element
47
+ return null;
48
+ }
49
+ else if (!('isConnected' in rn) || !rn.isConnected) {
50
+ // Handle disconnected fragments
51
+ if (rn.targetFragment) {
52
+ rn = rn.targetFragment;
53
+ }
54
+ else {
55
+ rn = document;
56
+ }
57
+ }
58
+ else {
59
+ // No more parents to check
60
+ return null;
61
+ }
62
+ }
63
+ return null;
64
+ }
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Searches for an element by ID, traversing up through shadow DOM boundaries.
3
+ *
4
+ * This function searches for an element with the specified ID starting from the reference
5
+ * element's root node and continuing up through shadow DOM boundaries until the element
6
+ * is found or the document root is reached.
7
+ *
8
+ * Search order:
9
+ * 1. Check current root node using getElementById
10
+ * 2. If in shadow root, check host element's properties for the ID
11
+ * 3. Continue up to parent shadow root or document
12
+ * 4. Handle disconnected fragments via targetFragment property
13
+ *
14
+ * @param ref - The reference element to start searching from
15
+ * @param id - The ID of the element to find
16
+ * @returns The found element, or null if not found
17
+ *
18
+ * @example
19
+ * ```typescript
20
+ * const template = document.querySelector('template[src="#myId"]');
21
+ * const source = upShadowSearch(template, 'myId');
22
+ * if (source) {
23
+ * const clone = source.cloneNode(true);
24
+ * }
25
+ * ```
26
+ */
27
+ export function upShadowSearch(ref: Element, id: string): Element | null {
28
+ let rn = ref.getRootNode() as DocumentFragment | ShadowRoot | Document;
29
+
30
+ while (rn) {
31
+ // Try getElementById on current root
32
+ if ('getElementById' in rn) {
33
+ const test = rn.getElementById(id);
34
+ if (test) return test;
35
+ }
36
+
37
+ // If in shadow root, check host element
38
+ if ('host' in rn && rn.host) {
39
+ // Check if host has a property with this ID
40
+ const hostProp = (rn.host as any)[id];
41
+ if (hostProp instanceof HTMLElement) return hostProp;
42
+
43
+ // Move up to host's root
44
+ rn = rn.host.getRootNode() as DocumentFragment | ShadowRoot | Document;
45
+ } else if (rn === document) {
46
+ // Reached document root without finding element
47
+ return null;
48
+ } else if (!('isConnected' in rn) || !(rn as any).isConnected) {
49
+ // Handle disconnected fragments
50
+ if ((rn as any).targetFragment) {
51
+ rn = (rn as any).targetFragment;
52
+ } else {
53
+ rn = document;
54
+ }
55
+ } else {
56
+ // No more parents to check
57
+ return null;
58
+ }
59
+ }
60
+
61
+ return null;
62
+ }
@@ -1,117 +0,0 @@
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
- }
@@ -1,111 +0,0 @@
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
- }