Skip to content

Commit

Permalink
Fix #106 - Fix document.body not including content from parsed <body>
Browse files Browse the repository at this point in the history
Fix #106

Before:
```js
new DOMParser().parseFromString(`<html><body><div>asdf</div></body></html>`, "text/html").body.outerHTML
// '<body></body>'
```

After:
```js
new DOMParser().parseFromString(`<html><body><div>asdf</div></body></html>`, "text/html").body.outerHTML
// '<body><div>asdf</div></body>'
```
  • Loading branch information
MadLittleMods committed Feb 9, 2022
1 parent b9774d6 commit 8edc4e3
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
7 changes: 6 additions & 1 deletion cjs/shared/parse-from-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ const parseFromString = (document, isHTML, markupLanguage) => {
onopentag(name, attributes) {
let create = true;
if (isHTML) {
if (ownerSVGElement) {
if (name === 'body') {
// Accessing the document.body property will create the <body>
node = document.body;
create = false;
}
else if (ownerSVGElement) {
node = append(node, document.createElementNS(SVG_NAMESPACE, name), active);
node.ownerSVGElement = ownerSVGElement;
create = false;
Expand Down
7 changes: 6 additions & 1 deletion esm/shared/parse-from-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ export const parseFromString = (document, isHTML, markupLanguage) => {
onopentag(name, attributes) {
let create = true;
if (isHTML) {
if (ownerSVGElement) {
if (name === 'body') {
// Accessing the document.body property will create the <body>
node = document.body;
create = false;
}
else if (ownerSVGElement) {
node = append(node, document.createElementNS(SVG_NAMESPACE, name), active);
node.ownerSVGElement = ownerSVGElement;
create = false;
Expand Down
5 changes: 5 additions & 0 deletions test/html/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ assert(document.all[1], document.querySelector('head'));
assert(document.all[2], document.querySelector('title'));
assert(document.all[3], document.querySelector('body'));

document = (new DOMParser).parseFromString('<!DOCTYPE html><html><body><div>foo</div></body></html>', 'text/html');
assert(document.body.tagName, 'BODY');
assert(document.body, document.querySelector('body'));
assert(document.body.textContent, 'foo');

// global listener
let triggered = false;
window.addEventListener('test', function once() {
Expand Down
7 changes: 6 additions & 1 deletion worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -8742,7 +8742,12 @@ const parseFromString = (document, isHTML, markupLanguage) => {
onopentag(name, attributes) {
let create = true;
if (isHTML) {
if (ownerSVGElement) {
if (name === 'body') {
// Accessing the document.body property will create the <body>
node = document.body;
create = false;
}
else if (ownerSVGElement) {
node = append$1(node, document.createElementNS(SVG_NAMESPACE, name), active);
node.ownerSVGElement = ownerSVGElement;
create = false;
Expand Down

0 comments on commit 8edc4e3

Please sign in to comment.