[2.0] Improve extension registration logic (#2505)

* add getSelectors to extension contract and extensionEnabled() to internal API

* remove extensionEnabled method (it's useless)
This commit is contained in:
Denis Palashevskii 2024-04-25 21:39:59 +04:00 committed by GitHub
parent f0bd28b438
commit 45d45c30af
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 1 deletions

1
src/htmx.d.ts vendored
View File

@ -53,6 +53,7 @@ declare namespace htmx {
const triggerSpecsCache: any | null; const triggerSpecsCache: any | null;
const disableInheritance: boolean; const disableInheritance: boolean;
const responseHandling: HtmxResponseHandlingConfig[]; const responseHandling: HtmxResponseHandlingConfig[];
const allowNestedOobSwaps: boolean;
} }
const parseInterval: (str: string) => number; const parseInterval: (str: string) => number;
const _: (str: string) => any; const _: (str: string) => any;

View File

@ -2635,8 +2635,21 @@ var htmx = (function() {
function findElementsToProcess(elt) { function findElementsToProcess(elt) {
if (elt.querySelectorAll) { if (elt.querySelectorAll) {
const boostedSelector = ', [hx-boost] a, [data-hx-boost] a, a[hx-boost], a[data-hx-boost]' 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']," + 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 return results
} else { } else {
return [] return []
@ -4754,6 +4767,7 @@ var htmx = (function() {
function extensionBase() { function extensionBase() {
return { return {
init: function(api) { return null }, init: function(api) { return null },
getSelectors: function() { return null },
onEvent: function(name, evt) { return true }, onEvent: function(name, evt) { return true },
transformResponse: function(text, xhr, elt) { return text }, transformResponse: function(text, xhr, elt) { return text },
isInlineSwap: function(swapStyle) { return false }, isInlineSwap: function(swapStyle) { return false },