Skip to content

Commit

Permalink
[2.0] Improve extension registration logic (#2505)
Browse files Browse the repository at this point in the history
* add getSelectors to extension contract and extensionEnabled() to internal API

* remove extensionEnabled method (it's useless)
  • Loading branch information
Renerick committed Apr 25, 2024
1 parent f0bd28b commit 45d45c3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/htmx.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ declare namespace htmx {
const triggerSpecsCache: any | null;
const disableInheritance: boolean;
const responseHandling: HtmxResponseHandlingConfig[];
const allowNestedOobSwaps: boolean;
}
const parseInterval: (str: string) => number;
const _: (str: string) => any;
Expand Down
16 changes: 15 additions & 1 deletion src/htmx.js
Original file line number Diff line number Diff line change
Expand Up @@ -2635,8 +2635,21 @@ var htmx = (function() {
function findElementsToProcess(elt) {
if (elt.querySelectorAll) {
const boostedSelector = ', [hx-boost] a, [data-hx-boost] a, a[hx-boost], a[data-hx-boost]'

const extensionSelectors = []
for (const e in extensions) {
const extension = extensions[e]
if (extension.getSelectors) {
var selectors = extension.getSelectors()
if (selectors) {
extensionSelectors.push(selectors)
}
}
}

const results = elt.querySelectorAll(VERB_SELECTOR + boostedSelector + ", form, [type='submit']," +
' [hx-ext], [data-hx-ext], [hx-trigger], [data-hx-trigger]')
' [hx-ext], [data-hx-ext], [hx-trigger], [data-hx-trigger]' + extensionSelectors.flat().map(s => ', ' + s).join(''))

return results
} else {
return []
Expand Down Expand Up @@ -4754,6 +4767,7 @@ var htmx = (function() {
function extensionBase() {
return {
init: function(api) { return null },
getSelectors: function() { return null },
onEvent: function(name, evt) { return true },
transformResponse: function(text, xhr, elt) { return text },
isInlineSwap: function(swapStyle) { return false },
Expand Down

0 comments on commit 45d45c3

Please sign in to comment.