diff --git a/src/htmx.js b/src/htmx.js index 1a796d2c..ca96a4d7 100644 --- a/src/htmx.js +++ b/src/htmx.js @@ -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 { diff --git a/test/core/internals.js b/test/core/internals.js index 7799a520..ed195139 100644 --- a/test/core/internals.js +++ b/test/core/internals.js @@ -10,8 +10,8 @@ describe('Core htmx internals Tests', function() { }) it('makeFragment works with janky stuff', function() { - htmx._('makeFragment')('').firstElementChild.tagName.should.equal('BODY') - htmx._('makeFragment')('').firstElementChild.tagName.should.equal('BODY') + htmx._('makeFragment')('').children.length.should.equal(0) + htmx._('makeFragment')('').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')('').children.length.should.equal(1) - htmx._('makeFragment')('').children.length.should.equal(1) + htmx._('makeFragment')('').children.length.should.equal(0) + htmx._('makeFragment')('').children.length.should.equal(0) var fragment = htmx._('makeFragment')('') fragment.firstElementChild.tagName.should.equal('TD')