@simplybuilder/core-dom 2.0.0 → 2.0.1

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/lib/creator.d.ts CHANGED
@@ -4,11 +4,22 @@
4
4
  * Provides functions for creating and appending HTML and SVG elements.
5
5
  * Handles attribute assignment, dataset configuration, and shadow DOM creation.
6
6
  */
7
- import type { CreateHTMLElementOptions, CreateSVGElementOptions } from './types.js';
7
+ import type { ShadowConfig, CreateHTMLElementOptions, CreateSVGElementOptions } from './types.js';
8
+ /**
9
+ * Creates a shadow root from a string or object configuration.
10
+ * String mode creates a shadow root with that mode ('open'/'closed').
11
+ * Object mode can additionally include `styles` via CSSStyleSheet.
12
+ *
13
+ * @function createShadowFromConfig
14
+ * @param {HTMLElement} host - The element to attach the shadow root to.
15
+ * @param {ShadowConfig} shadow - Shadow configuration (string or object with mode/styles).
16
+ * @returns {ShadowRoot|undefined} The created shadow root, or undefined on failure.
17
+ */
18
+ export declare function createShadowFromConfig(host: HTMLElement, shadow: ShadowConfig): ShadowRoot | undefined;
8
19
  /**
9
20
  * Creates an HTML element and appends it to a parent.
10
- * Supports attribute assignment, dataset configuration with automatic
11
- * store registration, and optional shadow DOM creation.
21
+ * Supports attribute assignment and dataset configuration with automatic
22
+ * store registration.
12
23
  *
13
24
  * @function createHTMLElement
14
25
  * @param {Object} [options] - Element creation options.
@@ -17,10 +28,9 @@ import type { CreateHTMLElementOptions, CreateSVGElementOptions } from './types.
17
28
  * @param {string} options.element.type - HTML tag name (e.g., 'div', 'button').
18
29
  * @param {Array} [options.element.attr] - Array of {name, value} attribute pairs.
19
30
  * @param {Array} [options.element.dataset] - Array of {name, value} dataset pairs.
20
- * @param {ShadowConfig} [options.shadow] - Shadow DOM configuration.
21
- * @returns {HTMLElement|ShadowRoot|undefined} The created element, shadow root, or undefined on error.
31
+ * @returns {HTMLElement|undefined} The created element, or undefined on error.
22
32
  */
23
- export declare function createHTMLElement(data?: CreateHTMLElementOptions): HTMLElement | ShadowRoot | undefined;
33
+ export declare function createHTMLElement(data?: CreateHTMLElementOptions): HTMLElement | undefined;
24
34
  /**
25
35
  * Creates an SVG element and appends it to a parent.
26
36
  * Supports standard and namespaced attributes, and dataset configuration.
@@ -1,4 +1,4 @@
1
- /*! SBCoreDom version 2.0.0 */
1
+ /*! SBCoreDom version 2.0.1 */
2
2
  'use strict';Object.defineProperties(exports,{__esModule:{value:true},[Symbol.toStringTag]:{value:'Module'}});/**
3
3
  * @module DomStoreModule
4
4
  * @description
@@ -92,7 +92,7 @@ function clearStore() {
92
92
  const internalStore = {
93
93
  app: {
94
94
  name: 'DomModule',
95
- version: "2.0.0",
95
+ version: "2.0.1",
96
96
  },
97
97
  register: {},
98
98
  allow: {
@@ -251,8 +251,6 @@ function setData(data) {
251
251
  /**
252
252
  * Attaches a shadow root to an HTML element with the specified mode.
253
253
  *
254
- * @private
255
- * @ignore
256
254
  * @function attachShadow
257
255
  * @param {HTMLElement} host - The element to attach the shadow root to.
258
256
  * @param {'open'|'closed'} mode - The shadow DOM mode.
@@ -266,8 +264,6 @@ function attachShadow(host, mode) {
266
264
  * String mode creates a shadow root with that mode ('open'/'closed').
267
265
  * Object mode can additionally include `styles` via CSSStyleSheet.
268
266
  *
269
- * @private
270
- * @ignore
271
267
  * @function createShadowFromConfig
272
268
  * @param {HTMLElement} host - The element to attach the shadow root to.
273
269
  * @param {ShadowConfig} shadow - Shadow configuration (string or object with mode/styles).
@@ -315,8 +311,8 @@ function applyAttributes(element, data) {
315
311
  }
316
312
  /**
317
313
  * Creates an HTML element and appends it to a parent.
318
- * Supports attribute assignment, dataset configuration with automatic
319
- * store registration, and optional shadow DOM creation.
314
+ * Supports attribute assignment and dataset configuration with automatic
315
+ * store registration.
320
316
  *
321
317
  * @function createHTMLElement
322
318
  * @param {Object} [options] - Element creation options.
@@ -325,23 +321,17 @@ function applyAttributes(element, data) {
325
321
  * @param {string} options.element.type - HTML tag name (e.g., 'div', 'button').
326
322
  * @param {Array} [options.element.attr] - Array of {name, value} attribute pairs.
327
323
  * @param {Array} [options.element.dataset] - Array of {name, value} dataset pairs.
328
- * @param {ShadowConfig} [options.shadow] - Shadow DOM configuration.
329
- * @returns {HTMLElement|ShadowRoot|undefined} The created element, shadow root, or undefined on error.
324
+ * @returns {HTMLElement|undefined} The created element, or undefined on error.
330
325
  */
331
326
  function createHTMLElement(data = {}) {
332
327
  try {
333
- const { parent, element: elementData, shadow } = data;
328
+ const { parent, element: elementData } = data;
334
329
  const element = document.createElement(elementData.type);
335
330
  applyAttributes(element, elementData);
336
331
  const targetParent = parent ?? document.body;
337
332
  if (targetParent instanceof HTMLElement || targetParent instanceof SVGElement || targetParent instanceof ShadowRoot) {
338
333
  targetParent.appendChild(element);
339
334
  }
340
- if (shadow && element.dataset?.state) {
341
- const shadowRoot = createShadowFromConfig(element, shadow);
342
- if (shadowRoot)
343
- return shadowRoot;
344
- }
345
335
  return element;
346
336
  }
347
337
  catch (err) {
@@ -461,18 +451,19 @@ function createFromStruct(data) {
461
451
  attr: attrArray,
462
452
  dataset: datasetArray,
463
453
  },
464
- shadow: struct.shadow,
465
454
  };
466
- const result = createHTMLElement(htmlData);
467
- if (result instanceof ShadowRoot) {
468
- element = result.host;
469
- }
470
- else {
471
- element = result;
472
- }
455
+ element = createHTMLElement(htmlData);
473
456
  }
474
457
  if (!element)
475
458
  return false;
459
+ let shadowRoot;
460
+ const state = element.dataset.state;
461
+ if (struct.shadow && state) {
462
+ const storedEl = getElementFromStore(state);
463
+ if (storedEl) {
464
+ shadowRoot = createShadowFromConfig(storedEl, struct.shadow);
465
+ }
466
+ }
476
467
  if (struct.text !== undefined) {
477
468
  element.textContent = struct.text;
478
469
  }
@@ -481,8 +472,9 @@ function createFromStruct(data) {
481
472
  }
482
473
  createEventElement({ struct, element });
483
474
  if (struct.children && struct.children.length > 0) {
475
+ const parentNode = shadowRoot ?? element;
484
476
  for (const child of struct.children) {
485
- createFromStruct({ struct: child, parent: element });
477
+ createFromStruct({ struct: child, parent: parentNode });
486
478
  }
487
479
  }
488
480
  return element;
@@ -561,7 +553,7 @@ function removeElement(element) {
561
553
  * integration with @simplybuilder/core-event.
562
554
  */
563
555
  const name = "SBCoreDom";
564
- const version = "2.0.0";
556
+ const version = "2.0.1";
565
557
  /**
566
558
  * Frozen singleton combining all DOM manipulation capabilities.
567
559
  * Pass `domModuleExtends()` to register an EventModule for declarative
@@ -1,3 +1,3 @@
1
- /*! SBCoreDom version 2.0.0 */
2
- Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const e={};function addElementToStore(t){const{key:n,value:r}=t;n&&r&&void 0===e[n]&&(e[n]=r)}function getElementFromStore(t){if(t)return e[t]}function removeElementFromStore(t){const{key:n,mode:r=1,EventModule:o={}}=t;if(!n)return!1;try{const t=e[n];return!!t&&(1===r&&"function"==typeof o.removeAllEventsFromStore&&o.removeAllEventsFromStore(t),delete e[n],!0)}catch{return!1}}const t={app:{name:"DomModule",version:"2.0.0"},register:{},allow:{SBCoreEvent:{major:2}},clearExtensions(){for(const e of Object.keys(this.register))delete this.register[e]}};function validVersionSupport(e){const{name:n,version:r}=e,o=t.allow[n];if(o&&r){const e=r.split(".");if(e.length>=1){if(void 0!==o.major&&e[0]&&o.major>Number(e[0]))return!1;if(void 0!==o.minor&&e[1]&&o.minor>Number(e[1]))return!1;if(void 0!==o.patch&&e[2]&&o.patch>Number(e[2]))return!1}return!0}return!1}function domModuleExtends(e){try{const{name:n}=e;if(validVersionSupport(e))return t.register[n]=e,!0}catch{return!1}return!1}function getAnyExtension(){for(const e of Object.keys(t.register))return t.register[e]}function setAttr(e){const{element:t,attrs:n}=e;if(n&&0!==n.length)for(let e=n.length-1;e>=0;e--){const r=n[e];r?.name&&t.setAttribute(r.name,r.value)}}function setAttrNS(e){const{element:t,attrs:n}=e;if(n&&0!==n.length)for(let e=n.length-1;e>=0;e--){const r=n[e];r?.name&&t.setAttributeNS(null,r.name,r.value)}}function setData(e){const{element:t,dataset:n}=e;if(n&&0!==n.length)for(let e=n.length-1;e>=0;e--){const r=n[e];r?.name&&(t.dataset[r.name]=r.value,"state"===r.name&&addElementToStore({key:r.value,value:t}))}}function attachShadow(e,t){return e.attachShadow({mode:t})}function applyAttributes(e,t){t.attr?.length&&setAttr({element:e,attrs:t.attr}),"attrNS"in t&&t.attrNS?.length&&setAttrNS({element:e,attrs:t.attrNS}),t.dataset?.length&&setData({element:e,dataset:t.dataset})}function createHTMLElement(e={}){try{const{parent:t,element:n,shadow:r}=e,o=document.createElement(n.type);applyAttributes(o,n);const a=t??document.body;if((a instanceof HTMLElement||a instanceof SVGElement||a instanceof ShadowRoot)&&a.appendChild(o),r&&o.dataset?.state){const e=function createShadowFromConfig(e,t){if("string"==typeof t)return attachShadow(e,t);if("object"==typeof t&&null!==t&&"mode"in t){const n=t,r=attachShadow(e,n.mode);if(n.styles&&"undefined"!=typeof CSSStyleSheet)try{const e=new CSSStyleSheet;e.replaceSync(n.styles),r.adoptedStyleSheets=[e]}catch{}return r}}(o,r);if(e)return e}return o}catch(e){return void console.error(e)}}function createSVGElement(e={}){try{const{parent:t,element:n}=e,r=document.createElementNS("http://www.w3.org/2000/svg",n.type);return applyAttributes(r,n),t&&(t instanceof HTMLElement||t instanceof SVGElement)&&t.appendChild(r),r}catch(e){return void console.error(e)}}function createEventElement(e){const{struct:t,element:n}=e,r=getAnyExtension();if(r&&t.event?.action&&t.event?.type){const e=r.EventActions;if(e&&e[t.event.action]){const o={element:n,type:t.event.type,handler:e[t.event.action]};t.event.node&&(o.nodeId=t.event.node);(0,r.addEventToStore)(o)}}}function createFromStruct(e){try{if("object"!=typeof e||!e)return!1;const{struct:t,parent:n=document.body}=e;if(!t?.element)return!1;const r=t.type&&"svg"===t.type.toLowerCase(),o=t.attr?Object.entries(t.attr).map(([e,t])=>({name:e,value:t})):[],a=t.dataset?Object.entries(t.dataset).map(([e,t])=>({name:e,value:t})):[];let s;if(r){const e=t.attrNS?Object.entries(t.attrNS).map(([e,t])=>({name:e,value:t})):[];s=createSVGElement({parent:n,element:{type:t.element,attr:o,attrNS:e,dataset:a}})}else{const e=createHTMLElement({parent:n,element:{type:t.element,attr:o,dataset:a},shadow:t.shadow});s=e instanceof ShadowRoot?e.host:e}if(!s)return!1;if(void 0!==t.text&&(s.textContent=t.text),void 0!==t.html&&(s.innerHTML=t.html),createEventElement({struct:t,element:s}),t.children&&t.children.length>0)for(const e of t.children)createFromStruct({struct:e,parent:s});return s}catch(e){return console.error(e),!1}}function removeElementFromStoreOrEvents(e){try{const t=getAnyExtension(),n=e;if(n.dataset?.state)return removeElementFromStore({key:n.dataset.state,mode:t?1:2,EventModule:t}),!0;if(t&&"function"==typeof t.removeAllEventsFromStore){return(0,t.removeAllEventsFromStore)(e),!0}}catch{}return!1}function removeElement(e){if(!e)return;removeElementFromStoreOrEvents(e);const t=e.querySelectorAll('[listener="true"]');for(let e=t.length-1;e>=0;e--){const n=t[e];n&&removeElementFromStoreOrEvents(n)}const n=e.querySelectorAll("[data-state]");for(let e=n.length-1;e>=0;e--){const t=n[e];if(t){const e=getAnyExtension();removeElementFromStore({key:t.dataset.state,mode:e?1:2,EventModule:e}),t.removeAttribute("data-state")}}e.remove()}const n="SBCoreDom",r="2.0.0",o=Object.freeze({name:n,version:r,domModuleExtends,createHTMLElement,createSVGElement,addElementToStore,getElementFromStore,removeElementFromStore,createFromStruct,removeElement});exports.DomModule=o,exports.addElementToStore=addElementToStore,exports.clearStore=function clearStore(){for(const t of Object.keys(e))delete e[t]},exports.createEventElement=createEventElement,exports.createFromStruct=createFromStruct,exports.createHTMLElement=createHTMLElement,exports.createSVGElement=createSVGElement,exports.default=o,exports.domModuleExtends=domModuleExtends,exports.getElementFromStore=getElementFromStore,exports.name=n,exports.removeElement=removeElement,exports.removeElementFromStore=removeElementFromStore,exports.setAttr=setAttr,exports.setAttrNS=setAttrNS,exports.setData=setData,exports.validVersionSupport=validVersionSupport,exports.version=r;
1
+ /*! SBCoreDom version 2.0.1 */
2
+ Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const e={};function addElementToStore(t){const{key:n,value:r}=t;n&&r&&void 0===e[n]&&(e[n]=r)}function getElementFromStore(t){if(t)return e[t]}function removeElementFromStore(t){const{key:n,mode:r=1,EventModule:o={}}=t;if(!n)return!1;try{const t=e[n];return!!t&&(1===r&&"function"==typeof o.removeAllEventsFromStore&&o.removeAllEventsFromStore(t),delete e[n],!0)}catch{return!1}}const t={app:{name:"DomModule",version:"2.0.1"},register:{},allow:{SBCoreEvent:{major:2}},clearExtensions(){for(const e of Object.keys(this.register))delete this.register[e]}};function validVersionSupport(e){const{name:n,version:r}=e,o=t.allow[n];if(o&&r){const e=r.split(".");if(e.length>=1){if(void 0!==o.major&&e[0]&&o.major>Number(e[0]))return!1;if(void 0!==o.minor&&e[1]&&o.minor>Number(e[1]))return!1;if(void 0!==o.patch&&e[2]&&o.patch>Number(e[2]))return!1}return!0}return!1}function domModuleExtends(e){try{const{name:n}=e;if(validVersionSupport(e))return t.register[n]=e,!0}catch{return!1}return!1}function getAnyExtension(){for(const e of Object.keys(t.register))return t.register[e]}function setAttr(e){const{element:t,attrs:n}=e;if(n&&0!==n.length)for(let e=n.length-1;e>=0;e--){const r=n[e];r?.name&&t.setAttribute(r.name,r.value)}}function setAttrNS(e){const{element:t,attrs:n}=e;if(n&&0!==n.length)for(let e=n.length-1;e>=0;e--){const r=n[e];r?.name&&t.setAttributeNS(null,r.name,r.value)}}function setData(e){const{element:t,dataset:n}=e;if(n&&0!==n.length)for(let e=n.length-1;e>=0;e--){const r=n[e];r?.name&&(t.dataset[r.name]=r.value,"state"===r.name&&addElementToStore({key:r.value,value:t}))}}function attachShadow(e,t){return e.attachShadow({mode:t})}function applyAttributes(e,t){t.attr?.length&&setAttr({element:e,attrs:t.attr}),"attrNS"in t&&t.attrNS?.length&&setAttrNS({element:e,attrs:t.attrNS}),t.dataset?.length&&setData({element:e,dataset:t.dataset})}function createHTMLElement(e={}){try{const{parent:t,element:n}=e,r=document.createElement(n.type);applyAttributes(r,n);const o=t??document.body;return(o instanceof HTMLElement||o instanceof SVGElement||o instanceof ShadowRoot)&&o.appendChild(r),r}catch(e){return void console.error(e)}}function createSVGElement(e={}){try{const{parent:t,element:n}=e,r=document.createElementNS("http://www.w3.org/2000/svg",n.type);return applyAttributes(r,n),t&&(t instanceof HTMLElement||t instanceof SVGElement)&&t.appendChild(r),r}catch(e){return void console.error(e)}}function createEventElement(e){const{struct:t,element:n}=e,r=getAnyExtension();if(r&&t.event?.action&&t.event?.type){const e=r.EventActions;if(e&&e[t.event.action]){const o={element:n,type:t.event.type,handler:e[t.event.action]};t.event.node&&(o.nodeId=t.event.node);(0,r.addEventToStore)(o)}}}function createFromStruct(e){try{if("object"!=typeof e||!e)return!1;const{struct:t,parent:n=document.body}=e;if(!t?.element)return!1;const r=t.type&&"svg"===t.type.toLowerCase(),o=t.attr?Object.entries(t.attr).map(([e,t])=>({name:e,value:t})):[],a=t.dataset?Object.entries(t.dataset).map(([e,t])=>({name:e,value:t})):[];let s,l;if(r){const e=t.attrNS?Object.entries(t.attrNS).map(([e,t])=>({name:e,value:t})):[];s=createSVGElement({parent:n,element:{type:t.element,attr:o,attrNS:e,dataset:a}})}else{s=createHTMLElement({parent:n,element:{type:t.element,attr:o,dataset:a}})}if(!s)return!1;const m=s.dataset.state;if(t.shadow&&m){const e=getElementFromStore(m);e&&(l=function createShadowFromConfig(e,t){if("string"==typeof t)return attachShadow(e,t);if("object"==typeof t&&null!==t&&"mode"in t){const n=t,r=attachShadow(e,n.mode);if(n.styles&&"undefined"!=typeof CSSStyleSheet)try{const e=new CSSStyleSheet;e.replaceSync(n.styles),r.adoptedStyleSheets=[e]}catch{}return r}}(e,t.shadow))}if(void 0!==t.text&&(s.textContent=t.text),void 0!==t.html&&(s.innerHTML=t.html),createEventElement({struct:t,element:s}),t.children&&t.children.length>0){const e=l??s;for(const n of t.children)createFromStruct({struct:n,parent:e})}return s}catch(e){return console.error(e),!1}}function removeElementFromStoreOrEvents(e){try{const t=getAnyExtension(),n=e;if(n.dataset?.state)return removeElementFromStore({key:n.dataset.state,mode:t?1:2,EventModule:t}),!0;if(t&&"function"==typeof t.removeAllEventsFromStore){return(0,t.removeAllEventsFromStore)(e),!0}}catch{}return!1}function removeElement(e){if(!e)return;removeElementFromStoreOrEvents(e);const t=e.querySelectorAll('[listener="true"]');for(let e=t.length-1;e>=0;e--){const n=t[e];n&&removeElementFromStoreOrEvents(n)}const n=e.querySelectorAll("[data-state]");for(let e=n.length-1;e>=0;e--){const t=n[e];if(t){const e=getAnyExtension();removeElementFromStore({key:t.dataset.state,mode:e?1:2,EventModule:e}),t.removeAttribute("data-state")}}e.remove()}const n="SBCoreDom",r="2.0.1",o=Object.freeze({name:n,version:r,domModuleExtends,createHTMLElement,createSVGElement,addElementToStore,getElementFromStore,removeElementFromStore,createFromStruct,removeElement});exports.DomModule=o,exports.addElementToStore=addElementToStore,exports.clearStore=function clearStore(){for(const t of Object.keys(e))delete e[t]},exports.createEventElement=createEventElement,exports.createFromStruct=createFromStruct,exports.createHTMLElement=createHTMLElement,exports.createSVGElement=createSVGElement,exports.default=o,exports.domModuleExtends=domModuleExtends,exports.getElementFromStore=getElementFromStore,exports.name=n,exports.removeElement=removeElement,exports.removeElementFromStore=removeElementFromStore,exports.setAttr=setAttr,exports.setAttrNS=setAttrNS,exports.setData=setData,exports.validVersionSupport=validVersionSupport,exports.version=r;
3
3
  /*! https://simplybuilder.github.io */
@@ -1,3 +1,3 @@
1
- /*! SBCoreDom version 2.0.0 */
2
- const e={};function addElementToStore(t){const{key:n,value:r}=t;n&&r&&void 0===e[n]&&(e[n]=r)}function getElementFromStore(t){if(t)return e[t]}function removeElementFromStore(t){const{key:n,mode:r=1,EventModule:o={}}=t;if(!n)return!1;try{const t=e[n];return!!t&&(1===r&&"function"==typeof o.removeAllEventsFromStore&&o.removeAllEventsFromStore(t),delete e[n],!0)}catch{return!1}}function clearStore(){for(const t of Object.keys(e))delete e[t]}const t={app:{name:"DomModule",version:"2.0.0"},register:{},allow:{SBCoreEvent:{major:2}},clearExtensions(){for(const e of Object.keys(this.register))delete this.register[e]}};function validVersionSupport(e){const{name:n,version:r}=e,o=t.allow[n];if(o&&r){const e=r.split(".");if(e.length>=1){if(void 0!==o.major&&e[0]&&o.major>Number(e[0]))return!1;if(void 0!==o.minor&&e[1]&&o.minor>Number(e[1]))return!1;if(void 0!==o.patch&&e[2]&&o.patch>Number(e[2]))return!1}return!0}return!1}function domModuleExtends(e){try{const{name:n}=e;if(validVersionSupport(e))return t.register[n]=e,!0}catch{return!1}return!1}function getAnyExtension(){for(const e of Object.keys(t.register))return t.register[e]}function setAttr(e){const{element:t,attrs:n}=e;if(n&&0!==n.length)for(let e=n.length-1;e>=0;e--){const r=n[e];r?.name&&t.setAttribute(r.name,r.value)}}function setAttrNS(e){const{element:t,attrs:n}=e;if(n&&0!==n.length)for(let e=n.length-1;e>=0;e--){const r=n[e];r?.name&&t.setAttributeNS(null,r.name,r.value)}}function setData(e){const{element:t,dataset:n}=e;if(n&&0!==n.length)for(let e=n.length-1;e>=0;e--){const r=n[e];r?.name&&(t.dataset[r.name]=r.value,"state"===r.name&&addElementToStore({key:r.value,value:t}))}}function attachShadow(e,t){return e.attachShadow({mode:t})}function applyAttributes(e,t){t.attr?.length&&setAttr({element:e,attrs:t.attr}),"attrNS"in t&&t.attrNS?.length&&setAttrNS({element:e,attrs:t.attrNS}),t.dataset?.length&&setData({element:e,dataset:t.dataset})}function createHTMLElement(e={}){try{const{parent:t,element:n,shadow:r}=e,o=document.createElement(n.type);applyAttributes(o,n);const a=t??document.body;if((a instanceof HTMLElement||a instanceof SVGElement||a instanceof ShadowRoot)&&a.appendChild(o),r&&o.dataset?.state){const e=function createShadowFromConfig(e,t){if("string"==typeof t)return attachShadow(e,t);if("object"==typeof t&&null!==t&&"mode"in t){const n=t,r=attachShadow(e,n.mode);if(n.styles&&"undefined"!=typeof CSSStyleSheet)try{const e=new CSSStyleSheet;e.replaceSync(n.styles),r.adoptedStyleSheets=[e]}catch{}return r}}(o,r);if(e)return e}return o}catch(e){return void console.error(e)}}function createSVGElement(e={}){try{const{parent:t,element:n}=e,r=document.createElementNS("http://www.w3.org/2000/svg",n.type);return applyAttributes(r,n),t&&(t instanceof HTMLElement||t instanceof SVGElement)&&t.appendChild(r),r}catch(e){return void console.error(e)}}function createEventElement(e){const{struct:t,element:n}=e,r=getAnyExtension();if(r&&t.event?.action&&t.event?.type){const e=r.EventActions;if(e&&e[t.event.action]){const o={element:n,type:t.event.type,handler:e[t.event.action]};t.event.node&&(o.nodeId=t.event.node);(0,r.addEventToStore)(o)}}}function createFromStruct(e){try{if("object"!=typeof e||!e)return!1;const{struct:t,parent:n=document.body}=e;if(!t?.element)return!1;const r=t.type&&"svg"===t.type.toLowerCase(),o=t.attr?Object.entries(t.attr).map(([e,t])=>({name:e,value:t})):[],a=t.dataset?Object.entries(t.dataset).map(([e,t])=>({name:e,value:t})):[];let l;if(r){const e=t.attrNS?Object.entries(t.attrNS).map(([e,t])=>({name:e,value:t})):[];l=createSVGElement({parent:n,element:{type:t.element,attr:o,attrNS:e,dataset:a}})}else{const e=createHTMLElement({parent:n,element:{type:t.element,attr:o,dataset:a},shadow:t.shadow});l=e instanceof ShadowRoot?e.host:e}if(!l)return!1;if(void 0!==t.text&&(l.textContent=t.text),void 0!==t.html&&(l.innerHTML=t.html),createEventElement({struct:t,element:l}),t.children&&t.children.length>0)for(const e of t.children)createFromStruct({struct:e,parent:l});return l}catch(e){return console.error(e),!1}}function removeElementFromStoreOrEvents(e){try{const t=getAnyExtension(),n=e;if(n.dataset?.state)return removeElementFromStore({key:n.dataset.state,mode:t?1:2,EventModule:t}),!0;if(t&&"function"==typeof t.removeAllEventsFromStore){return(0,t.removeAllEventsFromStore)(e),!0}}catch{}return!1}function removeElement(e){if(!e)return;removeElementFromStoreOrEvents(e);const t=e.querySelectorAll('[listener="true"]');for(let e=t.length-1;e>=0;e--){const n=t[e];n&&removeElementFromStoreOrEvents(n)}const n=e.querySelectorAll("[data-state]");for(let e=n.length-1;e>=0;e--){const t=n[e];if(t){const e=getAnyExtension();removeElementFromStore({key:t.dataset.state,mode:e?1:2,EventModule:e}),t.removeAttribute("data-state")}}e.remove()}const n="SBCoreDom",r="2.0.0",o=Object.freeze({name:n,version:r,domModuleExtends,createHTMLElement,createSVGElement,addElementToStore,getElementFromStore,removeElementFromStore,createFromStruct,removeElement});export{o as DomModule,addElementToStore,clearStore,createEventElement,createFromStruct,createHTMLElement,createSVGElement,o as default,domModuleExtends,getElementFromStore,n as name,removeElement,removeElementFromStore,setAttr,setAttrNS,setData,validVersionSupport,r as version};
1
+ /*! SBCoreDom version 2.0.1 */
2
+ const e={};function addElementToStore(t){const{key:n,value:r}=t;n&&r&&void 0===e[n]&&(e[n]=r)}function getElementFromStore(t){if(t)return e[t]}function removeElementFromStore(t){const{key:n,mode:r=1,EventModule:o={}}=t;if(!n)return!1;try{const t=e[n];return!!t&&(1===r&&"function"==typeof o.removeAllEventsFromStore&&o.removeAllEventsFromStore(t),delete e[n],!0)}catch{return!1}}function clearStore(){for(const t of Object.keys(e))delete e[t]}const t={app:{name:"DomModule",version:"2.0.1"},register:{},allow:{SBCoreEvent:{major:2}},clearExtensions(){for(const e of Object.keys(this.register))delete this.register[e]}};function validVersionSupport(e){const{name:n,version:r}=e,o=t.allow[n];if(o&&r){const e=r.split(".");if(e.length>=1){if(void 0!==o.major&&e[0]&&o.major>Number(e[0]))return!1;if(void 0!==o.minor&&e[1]&&o.minor>Number(e[1]))return!1;if(void 0!==o.patch&&e[2]&&o.patch>Number(e[2]))return!1}return!0}return!1}function domModuleExtends(e){try{const{name:n}=e;if(validVersionSupport(e))return t.register[n]=e,!0}catch{return!1}return!1}function getAnyExtension(){for(const e of Object.keys(t.register))return t.register[e]}function setAttr(e){const{element:t,attrs:n}=e;if(n&&0!==n.length)for(let e=n.length-1;e>=0;e--){const r=n[e];r?.name&&t.setAttribute(r.name,r.value)}}function setAttrNS(e){const{element:t,attrs:n}=e;if(n&&0!==n.length)for(let e=n.length-1;e>=0;e--){const r=n[e];r?.name&&t.setAttributeNS(null,r.name,r.value)}}function setData(e){const{element:t,dataset:n}=e;if(n&&0!==n.length)for(let e=n.length-1;e>=0;e--){const r=n[e];r?.name&&(t.dataset[r.name]=r.value,"state"===r.name&&addElementToStore({key:r.value,value:t}))}}function attachShadow(e,t){return e.attachShadow({mode:t})}function applyAttributes(e,t){t.attr?.length&&setAttr({element:e,attrs:t.attr}),"attrNS"in t&&t.attrNS?.length&&setAttrNS({element:e,attrs:t.attrNS}),t.dataset?.length&&setData({element:e,dataset:t.dataset})}function createHTMLElement(e={}){try{const{parent:t,element:n}=e,r=document.createElement(n.type);applyAttributes(r,n);const o=t??document.body;return(o instanceof HTMLElement||o instanceof SVGElement||o instanceof ShadowRoot)&&o.appendChild(r),r}catch(e){return void console.error(e)}}function createSVGElement(e={}){try{const{parent:t,element:n}=e,r=document.createElementNS("http://www.w3.org/2000/svg",n.type);return applyAttributes(r,n),t&&(t instanceof HTMLElement||t instanceof SVGElement)&&t.appendChild(r),r}catch(e){return void console.error(e)}}function createEventElement(e){const{struct:t,element:n}=e,r=getAnyExtension();if(r&&t.event?.action&&t.event?.type){const e=r.EventActions;if(e&&e[t.event.action]){const o={element:n,type:t.event.type,handler:e[t.event.action]};t.event.node&&(o.nodeId=t.event.node);(0,r.addEventToStore)(o)}}}function createFromStruct(e){try{if("object"!=typeof e||!e)return!1;const{struct:t,parent:n=document.body}=e;if(!t?.element)return!1;const r=t.type&&"svg"===t.type.toLowerCase(),o=t.attr?Object.entries(t.attr).map(([e,t])=>({name:e,value:t})):[],a=t.dataset?Object.entries(t.dataset).map(([e,t])=>({name:e,value:t})):[];let l,c;if(r){const e=t.attrNS?Object.entries(t.attrNS).map(([e,t])=>({name:e,value:t})):[];l=createSVGElement({parent:n,element:{type:t.element,attr:o,attrNS:e,dataset:a}})}else{l=createHTMLElement({parent:n,element:{type:t.element,attr:o,dataset:a}})}if(!l)return!1;const m=l.dataset.state;if(t.shadow&&m){const e=getElementFromStore(m);e&&(c=function createShadowFromConfig(e,t){if("string"==typeof t)return attachShadow(e,t);if("object"==typeof t&&null!==t&&"mode"in t){const n=t,r=attachShadow(e,n.mode);if(n.styles&&"undefined"!=typeof CSSStyleSheet)try{const e=new CSSStyleSheet;e.replaceSync(n.styles),r.adoptedStyleSheets=[e]}catch{}return r}}(e,t.shadow))}if(void 0!==t.text&&(l.textContent=t.text),void 0!==t.html&&(l.innerHTML=t.html),createEventElement({struct:t,element:l}),t.children&&t.children.length>0){const e=c??l;for(const n of t.children)createFromStruct({struct:n,parent:e})}return l}catch(e){return console.error(e),!1}}function removeElementFromStoreOrEvents(e){try{const t=getAnyExtension(),n=e;if(n.dataset?.state)return removeElementFromStore({key:n.dataset.state,mode:t?1:2,EventModule:t}),!0;if(t&&"function"==typeof t.removeAllEventsFromStore){return(0,t.removeAllEventsFromStore)(e),!0}}catch{}return!1}function removeElement(e){if(!e)return;removeElementFromStoreOrEvents(e);const t=e.querySelectorAll('[listener="true"]');for(let e=t.length-1;e>=0;e--){const n=t[e];n&&removeElementFromStoreOrEvents(n)}const n=e.querySelectorAll("[data-state]");for(let e=n.length-1;e>=0;e--){const t=n[e];if(t){const e=getAnyExtension();removeElementFromStore({key:t.dataset.state,mode:e?1:2,EventModule:e}),t.removeAttribute("data-state")}}e.remove()}const n="SBCoreDom",r="2.0.1",o=Object.freeze({name:n,version:r,domModuleExtends,createHTMLElement,createSVGElement,addElementToStore,getElementFromStore,removeElementFromStore,createFromStruct,removeElement});export{o as DomModule,addElementToStore,clearStore,createEventElement,createFromStruct,createHTMLElement,createSVGElement,o as default,domModuleExtends,getElementFromStore,n as name,removeElement,removeElementFromStore,setAttr,setAttrNS,setData,validVersionSupport,r as version};
3
3
  /*! https://simplybuilder.github.io */
package/lib/main.esm.mjs CHANGED
@@ -1,4 +1,4 @@
1
- /*! SBCoreDom version 2.0.0 */
1
+ /*! SBCoreDom version 2.0.1 */
2
2
  /**
3
3
  * @module DomStoreModule
4
4
  * @description
@@ -92,7 +92,7 @@ function clearStore() {
92
92
  const internalStore = {
93
93
  app: {
94
94
  name: 'DomModule',
95
- version: "2.0.0",
95
+ version: "2.0.1",
96
96
  },
97
97
  register: {},
98
98
  allow: {
@@ -251,8 +251,6 @@ function setData(data) {
251
251
  /**
252
252
  * Attaches a shadow root to an HTML element with the specified mode.
253
253
  *
254
- * @private
255
- * @ignore
256
254
  * @function attachShadow
257
255
  * @param {HTMLElement} host - The element to attach the shadow root to.
258
256
  * @param {'open'|'closed'} mode - The shadow DOM mode.
@@ -266,8 +264,6 @@ function attachShadow(host, mode) {
266
264
  * String mode creates a shadow root with that mode ('open'/'closed').
267
265
  * Object mode can additionally include `styles` via CSSStyleSheet.
268
266
  *
269
- * @private
270
- * @ignore
271
267
  * @function createShadowFromConfig
272
268
  * @param {HTMLElement} host - The element to attach the shadow root to.
273
269
  * @param {ShadowConfig} shadow - Shadow configuration (string or object with mode/styles).
@@ -315,8 +311,8 @@ function applyAttributes(element, data) {
315
311
  }
316
312
  /**
317
313
  * Creates an HTML element and appends it to a parent.
318
- * Supports attribute assignment, dataset configuration with automatic
319
- * store registration, and optional shadow DOM creation.
314
+ * Supports attribute assignment and dataset configuration with automatic
315
+ * store registration.
320
316
  *
321
317
  * @function createHTMLElement
322
318
  * @param {Object} [options] - Element creation options.
@@ -325,23 +321,17 @@ function applyAttributes(element, data) {
325
321
  * @param {string} options.element.type - HTML tag name (e.g., 'div', 'button').
326
322
  * @param {Array} [options.element.attr] - Array of {name, value} attribute pairs.
327
323
  * @param {Array} [options.element.dataset] - Array of {name, value} dataset pairs.
328
- * @param {ShadowConfig} [options.shadow] - Shadow DOM configuration.
329
- * @returns {HTMLElement|ShadowRoot|undefined} The created element, shadow root, or undefined on error.
324
+ * @returns {HTMLElement|undefined} The created element, or undefined on error.
330
325
  */
331
326
  function createHTMLElement(data = {}) {
332
327
  try {
333
- const { parent, element: elementData, shadow } = data;
328
+ const { parent, element: elementData } = data;
334
329
  const element = document.createElement(elementData.type);
335
330
  applyAttributes(element, elementData);
336
331
  const targetParent = parent ?? document.body;
337
332
  if (targetParent instanceof HTMLElement || targetParent instanceof SVGElement || targetParent instanceof ShadowRoot) {
338
333
  targetParent.appendChild(element);
339
334
  }
340
- if (shadow && element.dataset?.state) {
341
- const shadowRoot = createShadowFromConfig(element, shadow);
342
- if (shadowRoot)
343
- return shadowRoot;
344
- }
345
335
  return element;
346
336
  }
347
337
  catch (err) {
@@ -461,18 +451,19 @@ function createFromStruct(data) {
461
451
  attr: attrArray,
462
452
  dataset: datasetArray,
463
453
  },
464
- shadow: struct.shadow,
465
454
  };
466
- const result = createHTMLElement(htmlData);
467
- if (result instanceof ShadowRoot) {
468
- element = result.host;
469
- }
470
- else {
471
- element = result;
472
- }
455
+ element = createHTMLElement(htmlData);
473
456
  }
474
457
  if (!element)
475
458
  return false;
459
+ let shadowRoot;
460
+ const state = element.dataset.state;
461
+ if (struct.shadow && state) {
462
+ const storedEl = getElementFromStore(state);
463
+ if (storedEl) {
464
+ shadowRoot = createShadowFromConfig(storedEl, struct.shadow);
465
+ }
466
+ }
476
467
  if (struct.text !== undefined) {
477
468
  element.textContent = struct.text;
478
469
  }
@@ -481,8 +472,9 @@ function createFromStruct(data) {
481
472
  }
482
473
  createEventElement({ struct, element });
483
474
  if (struct.children && struct.children.length > 0) {
475
+ const parentNode = shadowRoot ?? element;
484
476
  for (const child of struct.children) {
485
- createFromStruct({ struct: child, parent: element });
477
+ createFromStruct({ struct: child, parent: parentNode });
486
478
  }
487
479
  }
488
480
  return element;
@@ -561,7 +553,7 @@ function removeElement(element) {
561
553
  * integration with @simplybuilder/core-event.
562
554
  */
563
555
  const name = "SBCoreDom";
564
- const version = "2.0.0";
556
+ const version = "2.0.1";
565
557
  /**
566
558
  * Frozen singleton combining all DOM manipulation capabilities.
567
559
  * Pass `domModuleExtends()` to register an EventModule for declarative
package/lib/types.d.ts CHANGED
@@ -36,7 +36,6 @@ export interface CreateHTMLElementOptions {
36
36
  value: string;
37
37
  }>;
38
38
  };
39
- shadow?: ShadowConfig;
40
39
  }
41
40
  export interface CreateSVGElementOptions {
42
41
  parent?: HTMLElement | SVGElement;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simplybuilder/core-dom",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "main": "lib/main.commonjs.min.cjs",
5
5
  "module": "lib/main.esm.min.mjs",
6
6
  "types": "lib/core-dom.d.ts",