@@ -1478,19 +1478,25 @@ var htmx = (function() {
14781478 forEach (
14791479 targets ,
14801480 function ( target ) {
1481+ let fragment
14811482 const oobElementClone = oobElement . cloneNode ( true )
1482- const fragment = getDocument ( ) . createDocumentFragment ( )
1483+ fragment = getDocument ( ) . createDocumentFragment ( )
14831484 fragment . appendChild ( oobElementClone )
14841485 if ( swapSpec . strip === undefined && ! isInlineSwap ( swapSpec . swapStyle , target ) ) {
14851486 swapSpec . strip = true
14861487 }
1488+ if ( swapSpec . strip ) {
1489+ // @ts -ignore if elt is template, content will be valid so use as inner content
1490+ fragment = oobElementClone . content || asParentNode ( oobElementClone ) // if this is not an inline swap, we use the content of the node, not the node itself
1491+ swapSpec . strip = undefined
1492+ }
14871493
1488- const beforeSwapDetails = { shouldSwap : true , target, fragment }
1494+ const beforeSwapDetails = { shouldSwap : true , target, fragment, swapSpec }
14891495 if ( ! triggerEvent ( target , 'htmx:oobBeforeSwap' , beforeSwapDetails ) ) return
14901496
14911497 target = beforeSwapDetails . target // allow re-targeting
14921498 if ( beforeSwapDetails . shouldSwap ) {
1493- swap ( target , fragment , swapSpec , {
1499+ swap ( target , fragment , beforeSwapDetails . swapSpec , {
14941500 contextElement : target ,
14951501 afterSwapCallback : function ( settleInfo ) {
14961502 forEach ( settleInfo . elts , function ( elt ) {
@@ -1846,7 +1852,7 @@ var htmx = (function() {
18461852 }
18471853
18481854 /**
1849- * @param {DocumentFragment } fragment
1855+ * @param {DocumentFragment|ParentNode } fragment
18501856 * @param {HtmxSettleInfo } settleInfo
18511857 * @param {Node|Document } [rootNode]
18521858 */
@@ -1870,7 +1876,7 @@ var htmx = (function() {
18701876 * Implements complete swapping pipeline, including: delay, view transitions, focus and selection preservation,
18711877 * title updates, scroll, OOB swapping, normal swapping and settling
18721878 * @param {string|Element } target
1873- * @param {string|DocumentFragment } content
1879+ * @param {string|ParentNode } content
18741880 * @param {HtmxSwapSpecification } swapSpec
18751881 * @param {SwapOptions } [swapOptions]
18761882 * @param {HtmxSettleInfo } [oobSettleInfo]
@@ -1911,12 +1917,12 @@ var htmx = (function() {
19111917 target . textContent = content
19121918 // Otherwise, make the fragment and process it
19131919 } else {
1920+ /** @type DocumentFragment|ParentNode */
19141921 let fragment = typeof content === 'string' ? makeFragment ( content ) : content
19151922
19161923 // @ts -ignore if fragment is a ParentNode title will be undefined which is fine
19171924 settleInfo . title = swapOptions . title || fragment . title
19181925 if ( swapOptions . historyRequest ) {
1919- // @ts -ignore fragment can be a parentNode Element
19201926 fragment = fragment . querySelector ( '[hx-history-elt],[data-hx-history-elt]' ) || fragment
19211927 }
19221928
@@ -1926,12 +1932,15 @@ var htmx = (function() {
19261932 const oobSelectValues = swapOptions . selectOOB . split ( ',' )
19271933 for ( let i = 0 ; i < oobSelectValues . length ; i ++ ) {
19281934 const oobSelectValue = oobSelectValues [ i ] . split ( ':' )
1929- let id = oobSelectValue . shift ( ) . trim ( )
1930- if ( id . indexOf ( '#' ) === 0 ) {
1931- id = id . substring ( 1 )
1932- }
1935+ const selector = oobSelectValue . shift ( ) . trim ( )
19331936 const oobValue = oobSelectValue . length > 0 ? oobSelectValue . join ( ':' ) : 'true'
1934- const oobElement = fragment . querySelector ( '#' + CSS . escape ( id ) )
1937+ let oobElement
1938+ if ( selector . indexOf ( '#' ) !== 0 ) {
1939+ oobElement = fragment . querySelector ( '#' + CSS . escape ( selector ) ) // check if selector is an id first
1940+ }
1941+ if ( ! oobElement ) {
1942+ oobElement = fragment . querySelector ( selector ) // then support any full selector
1943+ }
19351944 if ( oobElement ) {
19361945 oobSwap ( oobValue , oobElement , settleInfo , rootNode )
19371946 }
@@ -1947,10 +1956,6 @@ var htmx = (function() {
19471956 } )
19481957 }
19491958
1950- if ( swapSpec . strip ) {
1951- // @ts -ignore if element is really a template tag we can safely use its content otherwise use first child
1952- fragment = fragment . firstElementChild ?. content || fragment . firstElementChild // if this is not an inline swap, we use the content of the node, not the node itself
1953- }
19541959 // normal swap
19551960 if ( swapOptions . select ) {
19561961 const newFragment = getDocument ( ) . createDocumentFragment ( )
@@ -1959,6 +1964,9 @@ var htmx = (function() {
19591964 } )
19601965 fragment = newFragment
19611966 }
1967+ if ( swapSpec . strip ) {
1968+ fragment = fragment . firstElementChild
1969+ }
19621970 handlePreservedElements ( fragment )
19631971 swapWithStyle ( swapSpec . swapStyle , swapOptions . contextElement , target , fragment , settleInfo )
19641972 restorePreservedElements ( )
0 commit comments