diff --git a/cjs/html/document.js b/cjs/html/document.js index 85f1a4f8..1f57fd82 100644 --- a/cjs/html/document.js +++ b/cjs/html/document.js @@ -52,6 +52,8 @@ class HTMLDocument extends Document { */ get head() { const {documentElement} = this; + if (documentElement && documentElement.tagName === 'HEAD') + return documentElement; let {firstElementChild} = documentElement; if (!firstElementChild || firstElementChild.tagName !== 'HEAD') { firstElementChild = this.createElement('head'); @@ -64,6 +66,9 @@ class HTMLDocument extends Document { * @type HTMLBodyElement */ get body() { + const {documentElement} = this; + if (documentElement && documentElement.tagName === 'BODY') + return documentElement; const {head} = this; let {nextElementSibling} = head; if (!nextElementSibling || nextElementSibling.tagName !== 'BODY') { diff --git a/esm/html/document.js b/esm/html/document.js index 47e23bca..5f8bc348 100644 --- a/esm/html/document.js +++ b/esm/html/document.js @@ -51,6 +51,8 @@ export class HTMLDocument extends Document { */ get head() { const {documentElement} = this; + if (documentElement && documentElement.tagName === 'HEAD') + return documentElement; let {firstElementChild} = documentElement; if (!firstElementChild || firstElementChild.tagName !== 'HEAD') { firstElementChild = this.createElement('head'); @@ -63,6 +65,9 @@ export class HTMLDocument extends Document { * @type HTMLBodyElement */ get body() { + const {documentElement} = this; + if (documentElement && documentElement.tagName === 'BODY') + return documentElement; const {head} = this; let {nextElementSibling} = head; if (!nextElementSibling || nextElementSibling.tagName !== 'BODY') { diff --git a/test/html/document.js b/test/html/document.js index b5e15062..eaab23e3 100644 --- a/test/html/document.js +++ b/test/html/document.js @@ -98,3 +98,10 @@ const issue187 = (new DOMParser).parseFromString(`
'); +assert(issue283.document.querySelectorAll('body').length, 1, 'only one body element exists'); +assert(issue283.document.querySelector('body'), issue283.document.body, 'document.body is the real body element'); + +const issue283Head = parseHTML('t'); +assert(issue283Head.document.querySelector('head'), issue283Head.document.head, 'document.head is the real head element');