From cbb2b46de279521669e72d9b501116f85aa398f5 Mon Sep 17 00:00:00 2001 From: Johannes Przymusinski Date: Sat, 13 Jul 2024 19:44:08 +0200 Subject: [PATCH] fix: type definitions for HtmxExtension (#2721) --- src/htmx.d.ts | 12 ++++++++++-- src/htmx.js | 9 +++++---- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/htmx.d.ts b/src/htmx.d.ts index 2176ff4b..8037b2c6 100644 --- a/src/htmx.d.ts +++ b/src/htmx.d.ts @@ -15,7 +15,7 @@ declare namespace htmx { const toggleClass: (elt: string | Element, clazz: string) => void; const takeClass: (elt: string | Node, clazz: string) => void; const swap: (target: string | Element, content: string, swapSpec: HtmxSwapSpecification, swapOptions?: SwapOptions) => void; - const defineExtension: (name: string, extension: any) => void; + const defineExtension: (name: string, extension: HtmxExtension) => void; const removeExtension: (name: string) => void; const logAll: () => void; const logNone: () => void; @@ -192,4 +192,12 @@ type HtmxSettleInfo = { elts: Element[]; title?: string; }; -type HtmxExtension = any; +type HtmxExtension = { + init: (api: any) => void; + onEvent: (name: string, event: Event | CustomEvent) => boolean; + transformResponse: (text: string, xhr: XMLHttpRequest, elt: Element) => string; + isInlineSwap: (swapStyle: HtmxSwapStyle) => boolean; + handleSwap: (swapStyle: HtmxSwapStyle, target: Node, fragment: Node, settleInfo: HtmxSettleInfo) => boolean | Node[]; + encodeParameters: (xhr: XMLHttpRequest, parameters: FormData, elt: Node) => any | string | null; + getSelectors: () => string[] | null; +}; diff --git a/src/htmx.js b/src/htmx.js index 834c6779..98c32183 100644 --- a/src/htmx.js +++ b/src/htmx.js @@ -1753,7 +1753,7 @@ var htmx = (function() { try { const newElements = ext.handleSwap(swapStyle, target, fragment, settleInfo) if (newElements) { - if (typeof newElements.length !== 'undefined') { + if (Array.isArray(newElements)) { // if handleSwap returns an array (like) of elements, we handle them for (let j = 0; j < newElements.length; j++) { const child = newElements[j] @@ -5121,12 +5121,13 @@ var htmx = (function() { */ /** + * @see https://github.com/bigskysoftware/htmx-extensions/blob/main/README.md * @typedef {Object} HtmxExtension - * @see https://htmx.org/extensions/#defining * @property {(api: any) => void} init * @property {(name: string, event: Event|CustomEvent) => boolean} onEvent * @property {(text: string, xhr: XMLHttpRequest, elt: Element) => string} transformResponse * @property {(swapStyle: HtmxSwapStyle) => boolean} isInlineSwap - * @property {(swapStyle: HtmxSwapStyle, target: Element, fragment: Node, settleInfo: HtmxSettleInfo) => boolean} handleSwap - * @property {(xhr: XMLHttpRequest, parameters: FormData, elt: Element) => *|string|null} encodeParameters + * @property {(swapStyle: HtmxSwapStyle, target: Node, fragment: Node, settleInfo: HtmxSettleInfo) => boolean|Node[]} handleSwap + * @property {(xhr: XMLHttpRequest, parameters: FormData, elt: Node) => *|string|null} encodeParameters + * @property {() => string[]|null} getSelectors */