Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cjs/html/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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') {
Expand Down
5 changes: 5 additions & 0 deletions esm/html/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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') {
Expand Down
7 changes: 7 additions & 0 deletions test/html/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,10 @@ const issue187 = (new DOMParser).parseFromString(`<!DOCTYPE html><html><head></h
issue187.body.innerHTML = '<span></span>';
let span = issue187.body.firstElementChild;
assert(span.ownerDocument.body, issue187.body);

const issue283 = parseHTML('<body><div class="whatever"></div></body>');
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('<head><title>t</title></head>');
assert(issue283Head.document.querySelector('head'), issue283Head.document.head, 'document.head is the real head element');