mount-observer 0.1.23 → 0.1.25

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/Synthesizer.js CHANGED
@@ -101,6 +101,44 @@ export class Synthesizer extends HTMLElement {
101
101
  }
102
102
  }
103
103
  }
104
+ /**
105
+ * Check if a script element should be processed based on include/exclude attributes.
106
+ *
107
+ * Rules:
108
+ * - If passthrough attribute exists, return false (don't process any scripts)
109
+ * - If include or exclude attributes exist and script has no id, return false (security)
110
+ * - If include attribute exists, only process scripts with IDs in the include list
111
+ * - If exclude attribute exists, don't process scripts with IDs in the exclude list
112
+ * - Otherwise, return true (process the script)
113
+ *
114
+ * Made protected so subscribers can check if syndicator would allow a script.
115
+ */
116
+ checkIfAllowed(scriptElement) {
117
+ // Passthrough mode - don't process any scripts
118
+ if (this.hasAttribute('passthrough')) {
119
+ return false;
120
+ }
121
+ const scriptId = scriptElement.getAttribute('id');
122
+ // Security: If include or exclude attributes exist, script must have an ID
123
+ if ((this.hasAttribute('include') || this.hasAttribute('exclude')) && !scriptId) {
124
+ return false;
125
+ }
126
+ // Include list - only process scripts with IDs in the list
127
+ if (this.hasAttribute('include')) {
128
+ const includeList = this.getAttribute('include').split(' ').filter(s => s.trim());
129
+ if (!includeList.includes(scriptId)) {
130
+ return false;
131
+ }
132
+ }
133
+ // Exclude list - don't process scripts with IDs in the list
134
+ if (this.hasAttribute('exclude')) {
135
+ const excludeList = this.getAttribute('exclude').split(' ').filter(s => s.trim());
136
+ if (excludeList.includes(scriptId)) {
137
+ return false;
138
+ }
139
+ }
140
+ return true;
141
+ }
104
142
  /**
105
143
  * Initialize as syndicator (in document root).
106
144
  * Watches for script elements and broadcasts them to subscribers.
@@ -109,7 +147,9 @@ export class Synthesizer extends HTMLElement {
109
147
  // Process existing script elements
110
148
  const scripts = this.querySelectorAll('script[type="mountobserver"], script[type="emc"]');
111
149
  scripts.forEach(script => {
112
- this.#broadcastScript(script);
150
+ if (this.checkIfAllowed(script)) {
151
+ this.#broadcastScript(script);
152
+ }
113
153
  });
114
154
  // Watch for new script elements
115
155
  this.#mutationObserver = new MutationObserver((mutations) => {
@@ -118,7 +158,9 @@ export class Synthesizer extends HTMLElement {
118
158
  if (node instanceof HTMLScriptElement) {
119
159
  const type = node.getAttribute('type');
120
160
  if (type === 'mountobserver' || type === 'emc') {
121
- this.#broadcastScript(node);
161
+ if (this.checkIfAllowed(node)) {
162
+ this.#broadcastScript(node);
163
+ }
122
164
  }
123
165
  }
124
166
  }
@@ -147,9 +189,12 @@ export class Synthesizer extends HTMLElement {
147
189
  return;
148
190
  }
149
191
  // Process existing scripts from syndicator
192
+ // Only process scripts that pass the syndicator's filtering
150
193
  const scripts = syndicator.querySelectorAll('script[type="mountobserver"], script[type="emc"]');
151
194
  scripts.forEach(script => {
152
- this.#processScript(script);
195
+ if (syndicator.checkIfAllowed(script)) {
196
+ this.#processScript(script);
197
+ }
153
198
  });
154
199
  // Subscribe to new scripts
155
200
  syndicator.addEventListener(AddedScriptElementEvent.eventName, (e) => {
package/Synthesizer.ts CHANGED
@@ -114,6 +114,50 @@ export abstract class Synthesizer extends HTMLElement {
114
114
  }
115
115
  }
116
116
 
117
+ /**
118
+ * Check if a script element should be processed based on include/exclude attributes.
119
+ *
120
+ * Rules:
121
+ * - If passthrough attribute exists, return false (don't process any scripts)
122
+ * - If include or exclude attributes exist and script has no id, return false (security)
123
+ * - If include attribute exists, only process scripts with IDs in the include list
124
+ * - If exclude attribute exists, don't process scripts with IDs in the exclude list
125
+ * - Otherwise, return true (process the script)
126
+ *
127
+ * Made protected so subscribers can check if syndicator would allow a script.
128
+ */
129
+ protected checkIfAllowed(scriptElement: HTMLScriptElement): boolean {
130
+ // Passthrough mode - don't process any scripts
131
+ if (this.hasAttribute('passthrough')) {
132
+ return false;
133
+ }
134
+
135
+ const scriptId = scriptElement.getAttribute('id');
136
+
137
+ // Security: If include or exclude attributes exist, script must have an ID
138
+ if ((this.hasAttribute('include') || this.hasAttribute('exclude')) && !scriptId) {
139
+ return false;
140
+ }
141
+
142
+ // Include list - only process scripts with IDs in the list
143
+ if (this.hasAttribute('include')) {
144
+ const includeList = this.getAttribute('include')!.split(' ').filter(s => s.trim());
145
+ if (!includeList.includes(scriptId!)) {
146
+ return false;
147
+ }
148
+ }
149
+
150
+ // Exclude list - don't process scripts with IDs in the list
151
+ if (this.hasAttribute('exclude')) {
152
+ const excludeList = this.getAttribute('exclude')!.split(' ').filter(s => s.trim());
153
+ if (excludeList.includes(scriptId!)) {
154
+ return false;
155
+ }
156
+ }
157
+
158
+ return true;
159
+ }
160
+
117
161
  /**
118
162
  * Initialize as syndicator (in document root).
119
163
  * Watches for script elements and broadcasts them to subscribers.
@@ -122,7 +166,9 @@ export abstract class Synthesizer extends HTMLElement {
122
166
  // Process existing script elements
123
167
  const scripts = this.querySelectorAll('script[type="mountobserver"], script[type="emc"]');
124
168
  scripts.forEach(script => {
125
- this.#broadcastScript(script as HTMLScriptElement);
169
+ if (this.checkIfAllowed(script as HTMLScriptElement)) {
170
+ this.#broadcastScript(script as HTMLScriptElement);
171
+ }
126
172
  });
127
173
 
128
174
  // Watch for new script elements
@@ -132,7 +178,9 @@ export abstract class Synthesizer extends HTMLElement {
132
178
  if (node instanceof HTMLScriptElement) {
133
179
  const type = node.getAttribute('type');
134
180
  if (type === 'mountobserver' || type === 'emc') {
135
- this.#broadcastScript(node);
181
+ if (this.checkIfAllowed(node)) {
182
+ this.#broadcastScript(node);
183
+ }
136
184
  }
137
185
  }
138
186
  }
@@ -166,9 +214,12 @@ export abstract class Synthesizer extends HTMLElement {
166
214
  }
167
215
 
168
216
  // Process existing scripts from syndicator
217
+ // Only process scripts that pass the syndicator's filtering
169
218
  const scripts = syndicator.querySelectorAll('script[type="mountobserver"], script[type="emc"]');
170
219
  scripts.forEach(script => {
171
- this.#processScript(script as HTMLScriptElement);
220
+ if (syndicator.checkIfAllowed(script as HTMLScriptElement)) {
221
+ this.#processScript(script as HTMLScriptElement);
222
+ }
172
223
  });
173
224
 
174
225
  // Subscribe to new scripts
package/nudge.js ADDED
@@ -0,0 +1,23 @@
1
+ import { arr } from './arr.js';
2
+ /**
3
+ * Decrement "disabled" counter, remove when reaches 0
4
+ * @param el
5
+ * Optional select the attribute or attributes to remove or decrement
6
+ * @param attr
7
+ */
8
+ export function nudge(el, attr = 'disabled') {
9
+ const attrs = arr(attr);
10
+ for (const attr of attrs) {
11
+ const da = el.getAttribute(attr);
12
+ if (da !== null) {
13
+ if (da.length === 0 || da === "1") {
14
+ el.removeAttribute(attr);
15
+ if (attr === 'disabled')
16
+ el.disabled = false;
17
+ }
18
+ else {
19
+ el.setAttribute(attr, (parseInt(da) - 1).toString());
20
+ }
21
+ }
22
+ }
23
+ }
package/nudge.ts ADDED
@@ -0,0 +1,23 @@
1
+ import {arr} from './arr.js';
2
+ /**
3
+ * Decrement "disabled" counter, remove when reaches 0
4
+ * @param el
5
+ * Optional select the attribute or attributes to remove or decrement
6
+ * @param attr
7
+ */
8
+ export function nudge(el: Element, attr: string | Array<string> = 'disabled') { //TODO: Share with be-observant
9
+ const attrs = arr(attr);
10
+ for(const attr of attrs){
11
+ const da = el.getAttribute(attr);
12
+ if (da !== null) {
13
+ if (da.length === 0 || da === "1") {
14
+ el.removeAttribute(attr);
15
+ if(attr === 'disabled') (<any>el).disabled = false;
16
+ }
17
+ else {
18
+ el.setAttribute(attr, (parseInt(da) - 1).toString());
19
+ }
20
+ }
21
+ }
22
+
23
+ }
package/package.json CHANGED
@@ -1,15 +1,15 @@
1
1
  {
2
2
  "name": "mount-observer",
3
- "version": "0.1.23",
3
+ "version": "0.1.25",
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.23",
8
+ "assign-gingerly": "0.0.24",
9
9
  "id-generation": "0.0.4"
10
10
  },
11
11
  "devDependencies": {
12
- "@playwright/test": "1.59.0-alpha-2026-02-28",
12
+ "@playwright/test": "1.59.1",
13
13
  "spa-ssi": "0.0.27"
14
14
  },
15
15
  "exports": {
@@ -33,6 +33,10 @@
33
33
  "default": "./arr.js",
34
34
  "types": "./arr.ts"
35
35
  },
36
+ "./nudge.js": {
37
+ "default": "./nudge.js",
38
+ "types": "./nudge.ts"
39
+ },
36
40
  "./EvtRt.js": {
37
41
  "default": "./EvtRt.js",
38
42
  "types": "./EvtRt.ts"
@@ -0,0 +1,26 @@
1
+ export interface EndUserProps{
2
+ to: string;
3
+ nudges: boolean;
4
+ on: string;
5
+ }
6
+
7
+ export interface AllProps extends EndUserProps {
8
+ enhancedElement: Element;
9
+ resolved: boolean;
10
+ }
11
+
12
+ export type AP = AllProps;
13
+
14
+ export type PAP = Partial<AP>;
15
+
16
+ export type ProPAP = Promise<PAP>;
17
+
18
+ export type BAP = AllProps // & BEAllProps & RoundaboutReady;
19
+
20
+ export interface Actions{
21
+
22
+ hydrate(self: BAP): ProPAP;
23
+ init(self: BAP, initVals: PAP): Promise<void>
24
+ // findTarget(self: this): Promise<void>;
25
+ // handleCommit(self: this, e: KeyboardEvent): Promise<void>;
26
+ }
@@ -1,6 +1,6 @@
1
1
  // Core types for MountObserver v2 - Polyfill Supported Scenario I
2
2
 
3
- import {Spawner, EnhancementConfigBase, EnhKey} from '../assign-gingerly/types';
3
+ import {Spawner, EnhancementConfigBase, EnhKey, AttrPatterns} from '../assign-gingerly/types';
4
4
 
5
5
  export type Constructor = new (...args: any[]) => any;
6
6
 
@@ -53,7 +53,7 @@ export interface EnhConfig<T = any, Obj = Element> extends EnhancementConfigBase
53
53
  *
54
54
  * @template TKeys - String literal type for sub-observer keys when using the `with` property
55
55
  */
56
- export interface MountConfig<TKeys extends string = string> {
56
+ export interface MountConfig<TKeys extends string = string, TCustomData = unknown> {
57
57
  /**
58
58
  * CSS selector string to match elements.
59
59
  * Only elements matching this selector will be considered for mounting.
@@ -216,7 +216,7 @@ export interface MountConfig<TKeys extends string = string> {
216
216
  * Allows handlers to receive additional context-specific information.
217
217
  * Not used by MountObserver itself, but available in MountContext.
218
218
  */
219
- customData?: unknown;
219
+ customData?: TCustomData;
220
220
 
221
221
  /**
222
222
  * Sub-observer configurations for hierarchical composition.
@@ -246,8 +246,8 @@ export interface MountConfig<TKeys extends string = string> {
246
246
 
247
247
  export interface EMC<
248
248
  TKeys extends string = string,
249
- T = any, Obj = Element
250
- > extends MountConfig<TKeys>{
249
+ T = unknown, Obj = Element, TCustomData = unknown
250
+ > extends MountConfig<TKeys, TCustomData>{
251
251
  enhConfig: EnhConfig<T, Obj>
252
252
  }
253
253
 
@@ -0,0 +1,150 @@
1
+ export type Key<T = any> = keyof T & string;
2
+
3
+ export type Keysh<T = any> = Key<T> | Array<Key<T>>;
4
+
5
+ type PropsToProps<Props> = (x: Props) => (Promise<Partial<Props>> | Partial<Props>)
6
+
7
+ export interface LogicOp<Props = any, TActions = Props>{
8
+ /**
9
+ * Supported by trans-render
10
+ */
11
+ ifAllOf?: Keysh<Props>,
12
+
13
+ ifKeyIn?: Keysh<Props>,
14
+
15
+ ifNoneOf?: Keysh<Props>,
16
+
17
+ ifEquals?: Array<Key<Props>>,
18
+
19
+ ifAtLeastOneOf?: Keysh<Props>,
20
+
21
+ ifNotAllOf?: Keysh<Props>,
22
+
23
+ debug?: boolean,
24
+
25
+ delay?: number,
26
+
27
+ do?:
28
+ | Function
29
+ | (keyof TActions & string)
30
+ | PropsToProps<Props>
31
+
32
+ }
33
+
34
+ export type Actions<TProps = any, TActions = TProps> =
35
+ Partial<{[key in keyof TActions & string]: LogicOp<TProps>}>
36
+ //& Partial<{[key in `do_${keyof TActions & string}_on`]: Key<TActions> | Array<Key<TActions>> }>
37
+ ;
38
+
39
+ export type Compacts<TProps = any, TActions = TProps> =
40
+ //| Partial<{[key in `${keyof TProps & string}_to_${keyof TProps & string}` & string]: Operation<TProps> }>
41
+ | Partial<{[key in `negate_${keyof TProps & string}_to_${keyof TProps & string}`]: number}>
42
+ | Partial<{[key in `pass_length_of_${keyof TProps & string}_to_${keyof TProps & string}`]: number}>
43
+ | Partial<{[key in `echo_${keyof TProps & string}_to_${keyof TProps & string}`]: number}>
44
+ | Partial<{[key in `echo_${keyof TProps & string}_to_${keyof TProps & string}_after`]: keyof TProps}>
45
+ | Partial<{[key in `when_${keyof TProps & string}_changes_call_${keyof TActions & string}`]: number}>
46
+ | Partial<{[key in `when_${keyof TProps & string}_changes_toggle_${keyof TProps & string}`]: number}>
47
+ | Partial<{[key in `when_${keyof TProps & string}_changes_inc_${keyof TProps & string}_by`]: number}>
48
+ | Partial<{[key in `when_${keyof TProps & string}_changes_dispatch`]: string}> //TODO
49
+ ;
50
+
51
+ export type Hitches<TProps = any, TActions = TProps> =
52
+ | Partial<{[key in `when_${keyof TProps & string}_emits_${keyof TProps & string}_inc_${keyof TProps & string}_by`]: number}>
53
+ ;
54
+
55
+ export type Handlers<ETProps = any, TActions = ETProps> =
56
+ | Partial<{[key in `${keyof ETProps & string}_to_${keyof TActions & string}_on` & string]: string }>;
57
+
58
+ export type Positractions<TProps = any, TActions = TProps> =
59
+ | Array<Positraction<TProps, TActions>>;
60
+
61
+ export interface Positraction<TProps = any, TActions = TProps> extends LogicOp<TProps, TActions> {
62
+ do:
63
+ | Function
64
+ | (keyof TActions & string)
65
+ | PropsToProps<TProps>
66
+ ifKeyIn?: Array<keyof TProps & string>,
67
+ ifAllOf?: Array<keyof TProps & string>,
68
+ //ifNoneOf: Array<keyof TProps & string>,
69
+
70
+ pass?: Array<(keyof TProps & string) | number | boolean | '$0' | '$0+' | `\`${string}\``>,
71
+ assignTo?: Array<null | (keyof TProps & string)>
72
+ }
73
+
74
+ export interface RAConfig<TProps = unknown, TActions = TProps, ETProps = TProps> {
75
+ actions?: Actions<TProps,TActions>,
76
+ compacts?: Compacts<TProps, TActions>,
77
+ //onsets?: Onsets<TProps, TActions>,
78
+ handlers?: Handlers<ETProps, TActions>,
79
+ hitch?: Hitches<TProps, TActions>,
80
+ positractions?: Positractions<TProps>,
81
+ }
82
+
83
+ export interface RoundaboutOptions<TProps = unknown, TActions = TProps, ETProps = TProps> extends RAConfig<TProps, TActions, ETProps> {
84
+ vm?: TProps & TActions & RoundaboutReady,
85
+ //for enhanced elements, pass in the container, referenced via $0.
86
+ container?: EventTarget,
87
+ propagate?: keyof TProps & string | Array<keyof TProps & string>,
88
+
89
+ //mountObservers?: Set<IMountObserver>
90
+
91
+ /**
92
+ * Enable internal routing optimization for actions (default: false)
93
+ *
94
+ * When true: Action results are batched and cascaded before firing events.
95
+ * - Eliminates redundant action calls in diamond dependency patterns
96
+ * - More predictable: actions run once per logical change
97
+ * - Best for: Complex cascades, multiple properties returned from actions
98
+ *
99
+ * When false: Uses traditional approach with immediate event firing.
100
+ * - Simpler execution model, easier to debug
101
+ * - Slightly faster for simple linear cascades
102
+ * - Best for: Simple cascades, performance-critical paths
103
+ *
104
+ * Enable if you have:
105
+ * - Actions that return multiple properties
106
+ * - Multiple actions monitoring the same properties
107
+ * - Diamond dependencies (A→B, A→C, B→D, C→D)
108
+ */
109
+ internalRouting?: boolean,
110
+ }
111
+
112
+ export interface RoundaboutReady{
113
+ /**
114
+ * Allow for assigning to read only props via the "backdoor"
115
+ * Bypasses getters / setters, sets directly to (private) memory slots
116
+ * Doesn't do any notification
117
+ * Allows for nested property setting
118
+ */
119
+ covertAssignment(obj: any): Promise<void>;
120
+
121
+ /**
122
+ * fires event with name matching the name of the property when the value changes (but not via covertAssignment)
123
+ * when property is set via public interface, not (immediately) via an action method's return object
124
+ */
125
+ readonly propagator : EventTarget | undefined;
126
+
127
+ /**
128
+ *
129
+ * https://github.com/whatwg/dom/issues/1296
130
+ */
131
+ //readonly disconnectedSignal: AbortSignal
132
+
133
+ RAController: AbortController;
134
+
135
+ /**
136
+ * During this time, queues/buses continue to perform "bookkeeping"
137
+ * but doesn't process the queue until sleep property becomes falsy.
138
+ * If truthy, can call await awake() before processing should resume
139
+ * [TODO]
140
+ */
141
+ readonly sleep?: number | undefined;
142
+
143
+ awake(): Promise<void>;
144
+
145
+ //make the value sleep 1 step closer to be falsy
146
+ nudge(): void;
147
+
148
+ //make the value of sleep 1 step further away from being falsy
149
+ rock(): void;
150
+ }