restore old semantics w/ body returning its children

This commit is contained in:
Carson Gross 2023-12-27 19:10:35 -07:00
parent f685b57332
commit d2d22c2903
2 changed files with 13 additions and 6 deletions

View File

@ -273,6 +273,12 @@ var htmx = (function() {
return parser.parseFromString(resp, 'text/html')
}
function takeChildren(elt, fragment) {
while (elt.childNodes.length > 0) {
fragment.append(elt.childNodes[0]);
}
}
/**
* @param {string} response HTML
* @returns {DocumentFragment} a document fragment representing the response HTML, including
@ -287,14 +293,15 @@ var htmx = (function() {
// if it is a full document, parse it and return the body
const fragment = new DocumentFragment();
let doc = parseHTML(response);
fragment.append(doc.body);
takeChildren(doc.body, fragment)
forEach(doc.body.children, (elt) => fragment.append(elt));
fragment.head = doc.head;
return fragment;
} else if (startTag === 'body') {
// body w/ a potential head, parse head & body w/o wrapping in template
const fragment = new DocumentFragment();
let doc = parseHTML(head + responseWithNoHead);
fragment.append(doc.body);
takeChildren(doc.body, fragment)
fragment.head = doc.head;
return fragment;
} else {

View File

@ -10,8 +10,8 @@ describe('Core htmx internals Tests', function() {
})
it('makeFragment works with janky stuff', function() {
htmx._('makeFragment')('<html></html>').firstElementChild.tagName.should.equal('BODY')
htmx._('makeFragment')('<html><body></body></html>').firstElementChild.tagName.should.equal('BODY')
htmx._('makeFragment')('<html></html>').children.length.should.equal(0)
htmx._('makeFragment')('<html><body></body></html>').children.length.should.equal(0)
// NB - the tag name should be the *parent* element hosting the HTML since we use the fragment children
// for the swap
@ -22,8 +22,8 @@ describe('Core htmx internals Tests', function() {
})
it('makeFragment works with template wrapping', function() {
htmx._('makeFragment')('<html></html>').children.length.should.equal(1)
htmx._('makeFragment')('<html><body></body></html>').children.length.should.equal(1)
htmx._('makeFragment')('<html></html>').children.length.should.equal(0)
htmx._('makeFragment')('<html><body></body></html>').children.length.should.equal(0)
var fragment = htmx._('makeFragment')('<td></td>')
fragment.firstElementChild.tagName.should.equal('TD')