Merge remote-tracking branch 'origin/four' into four

This commit is contained in:
Carson Gross
2026-01-20 12:40:53 -07:00
2 changed files with 23 additions and 8 deletions

View File

@@ -1133,18 +1133,18 @@ var htmx = (() => {
let response = text.replace(/<hx-([a-z]+)(\s+|>)/gi, '<template hx type="$1"$2').replace(/<\/hx-[a-z]+>/gi, '</template>');
let title = '';
response = response.replace(/<title[^>]*>[\s\S]*?<\/title>/i, m => (title = this.__parseHTML(m).title, ''));
let responseWithNoHead = response.replace(/<head(\s[^>]*)?>[\s\S]*?<\/head>/i, '');
let startTag = responseWithNoHead.match(/<([a-z][^\/>\x20\t\r\n\f]*)/i)?.[1]?.toLowerCase();
response = response.replace(/<head(\s[^>]*)?>[\s\S]*?<\/head>/i, '');
let startTag = response.match(/<([a-z][^\/>\x20\t\r\n\f]*)/i)?.[1]?.toLowerCase();
let doc, fragment;
if (startTag === 'html') {
if (startTag === 'html' || startTag === 'body') {
doc = this.__parseHTML(response);
fragment = doc.body;
} else if (startTag === 'body') {
doc = this.__parseHTML(responseWithNoHead);
fragment = doc.body;
fragment = document.createDocumentFragment();
while (doc.body.childNodes.length > 0) {
fragment.append(doc.body.childNodes[0]);
}
} else {
doc = this.__parseHTML(`<template>${responseWithNoHead}</template>`);
doc = this.__parseHTML(`<template>${response}</template>`);
fragment = doc.querySelector('template').content;
}
this.__processScripts(fragment);
@@ -1469,6 +1469,7 @@ var htmx = (() => {
this.process(elt);
this.__handleAutoFocus(elt);
}
this.__handleScroll(swapSpec, target);
}

View File

@@ -228,6 +228,20 @@ describe('swap() unit tests', function() {
delete window.testVar;
})
it('executes script when wrapped in html tag', async function () {
window.testVar = 0;
await htmx.swap({"target":"#test-playground", "text":"<html><body><script>window.testVar = 9</script><div>Content</div></body></html>"})
window.testVar.should.equal(9);
delete window.testVar;
})
it('executes script when wrapped in body tag', async function () {
window.testVar = 0;
await htmx.swap({"target":"#test-playground", "text":"<body><script>window.testVar = 10</script><div>Content</div></body>"})
window.testVar.should.equal(10);
delete window.testVar;
})
it('replaces attributes when swapping element with same id', async function () {
createProcessedHTML("<div id='d1' class='old' data-value='1'></div>")
await htmx.swap({"target":"#d1", "text":"<div id='d1' class='new' data-value='2'>Content</div>", "swap":"outerHTML"})