From 616be6e58b6bb7d849411e6c392fac511006a37f Mon Sep 17 00:00:00 2001 From: Christian Tanul Date: Thu, 18 Jun 2026 00:23:32 +0300 Subject: [PATCH] Rename elt to root in process() and __cleanup() --- dist/htmx.d.ts | 2 +- src/htmx.js | 53 +++++++++++++++++++++++++++++--------------------- 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/dist/htmx.d.ts b/dist/htmx.d.ts index adcac3260..95199f9c1 100644 --- a/dist/htmx.d.ts +++ b/dist/htmx.d.ts @@ -76,7 +76,7 @@ export interface Htmx { on(event: string, handler: (evt: Event) => void): void; on(target: string | Element, event: string, handler: (evt: Event) => void): void; onLoad(callback: (elt: Element) => void): void; - process(elt: Element): void; + process(root: Element | ShadowRoot | DocumentFragment): void; registerExtension(name: string, ext: any): void; trigger(elt: Element | string, event: string, detail?: any, bubbles?: boolean): boolean; timeout(ms: number): Promise; diff --git a/src/htmx.js b/src/htmx.js index 5fedc7d59..aa28b3407 100644 --- a/src/htmx.js +++ b/src/htmx.js @@ -869,16 +869,21 @@ var htmx = (() => { return func.call(thisArg, ...values); } - process(elt) { - if (!elt) return; - if (!(elt instanceof Element)) { - for (let child of elt.children || []) this.process(child); + /** + * Initialize htmx attributes on root and all its descendants. + * @see https://four.htmx.org/reference/methods/htmx-process + * @param {Element | ShadowRoot | DocumentFragment} root + */ + process(root) { + if (!root) return; + if (!(root instanceof Element)) { // ShadowRoot, DocumentFragment + for (let elt of root.children || []) this.process(elt); return; } - if (this.__ignore(elt)) return; - if (!this.__trigger(elt, "htmx:before:process")) return - let hxOnNodes = [elt]; - let iter = this.#hxOnQuery.evaluate(elt) + if (this.__ignore(root)) return; + if (!this.__trigger(root, "htmx:before:process")) return + let hxOnNodes = [root]; + let iter = this.#hxOnQuery.evaluate(root) let node = null while (node = iter.iterateNext()) hxOnNodes.push(node) for (let hxOnNode of hxOnNodes) { @@ -886,13 +891,13 @@ var htmx = (() => { this.__handleHxOnAttributes(hxOnNode); } } - for (let child of this.__queryEltAndDescendants(elt, this.#actionSelector)) { - this.__initializeElement(child); + for (let elt of this.__queryEltAndDescendants(root, this.#actionSelector)) { + this.__initializeElement(elt); } - for (let child of this.__queryEltAndDescendants(elt, this.#boostSelector)) { - this.__maybeBoost(child); + for (let elt of this.__queryEltAndDescendants(root, this.#boostSelector)) { + this.__maybeBoost(elt); } - this.__trigger(elt, "htmx:after:process"); + this.__trigger(root, "htmx:after:process"); } __maybeBoost(elt) { @@ -936,23 +941,27 @@ var htmx = (() => { return !elt._htmx?.initialized && !this.__ignore(elt); } - __cleanup(elt) { - if (elt._htmx) { - this.__trigger(elt, "htmx:before:cleanup") - for (let spec of elt._htmx.triggerSpecs || []) { + /** + * Remove listeners, timers, and observers from root and all powered descendants. + * @param {Element} root + */ + __cleanup(root) { + if (root._htmx) { + this.__trigger(root, "htmx:before:cleanup") + for (let spec of root._htmx.triggerSpecs || []) { if (spec.interval) clearInterval(spec.interval); if (spec.timeout) clearTimeout(spec.timeout); if (spec.throttleTimeout) clearTimeout(spec.throttleTimeout); spec.observer?.disconnect() } - for (let listenerInfo of elt._htmx.listeners || []) { + for (let listenerInfo of root._htmx.listeners || []) { listenerInfo.fromElt.removeEventListener(listenerInfo.eventName, listenerInfo.handler, listenerInfo); } - this.__trigger(elt, "htmx:after:cleanup") + this.__trigger(root, "htmx:after:cleanup") } - if (elt.firstChild) { - for (let child of elt.querySelectorAll('[data-htmx-powered]')) { - this.__cleanup(child); + if (root.firstChild) { + for (let elt of root.querySelectorAll('[data-htmx-powered]')) { + this.__cleanup(elt); } } }