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
2 changes: 1 addition & 1 deletion cjs/dom/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class DOMParser {
if (isHTML && markupLanguage === '...')
markupLanguage = '<!doctype html><html><head></head><body></body></html>';
return markupLanguage ?
parseFromString(document, isHTML, markupLanguage) :
parseFromString(document, isHTML, markupLanguage, true) :
document;
}
}
Expand Down
8 changes: 7 additions & 1 deletion cjs/shared/parse-from-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const attribute = (element, end, attribute, value, active) => {
const isNotParsing = () => notParsing;
exports.isNotParsing = isNotParsing;

const parseFromString = (document, isHTML, markupLanguage) => {
const parseFromString = (document, isHTML, markupLanguage, isDocument = false) => {
const {active, registry} = document[CUSTOM_ELEMENTS];

let node = document;
Expand All @@ -60,6 +60,12 @@ const parseFromString = (document, isHTML, markupLanguage) => {
onopentag(name, attributes) {
let create = true;
if (isHTML) {
// a top level <head> or <body> without <html> gets an implicit <html>,
// so document.head and document.body find the parsed elements
if (isDocument && node === document && (name === 'head' || name === 'body')) {
node = document.firstElementChild ||
append(document, document.createElement('html'), false);
}
if (ownerSVGElement) {
node = append(node, document.createElementNS(SVG_NAMESPACE, name), active);
node.ownerSVGElement = ownerSVGElement;
Expand Down
2 changes: 1 addition & 1 deletion esm/dom/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class DOMParser {
if (isHTML && markupLanguage === '...')
markupLanguage = '<!doctype html><html><head></head><body></body></html>';
return markupLanguage ?
parseFromString(document, isHTML, markupLanguage) :
parseFromString(document, isHTML, markupLanguage, true) :
document;
}
}
8 changes: 7 additions & 1 deletion esm/shared/parse-from-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const attribute = (element, end, attribute, value, active) => {

export const isNotParsing = () => notParsing;

export const parseFromString = (document, isHTML, markupLanguage) => {
export const parseFromString = (document, isHTML, markupLanguage, isDocument = false) => {
const {active, registry} = document[CUSTOM_ELEMENTS];

let node = document;
Expand All @@ -58,6 +58,12 @@ export const parseFromString = (document, isHTML, markupLanguage) => {
onopentag(name, attributes) {
let create = true;
if (isHTML) {
// a top level <head> or <body> without <html> gets an implicit <html>,
// so document.head and document.body find the parsed elements
if (isDocument && node === document && (name === 'head' || name === 'body')) {
node = document.firstElementChild ||
append(document, document.createElement('html'), false);
}
if (ownerSVGElement) {
node = append(node, document.createElementNS(SVG_NAMESPACE, name), active);
node.ownerSVGElement = ownerSVGElement;
Expand Down
6 changes: 6 additions & 0 deletions test/html/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ assert(typeof window.performance.now(), 'number');

assert(parseHTML('<html><body><div>asdf</div></body></html>').document.body.outerHTML, '<body><div>asdf</div></body>');

// issue #283: <body> without <html> still backs document.body
const bodyOnly = parseHTML('<body><div class="whatever"></div></body>').document;
assert(bodyOnly.body, bodyOnly.querySelector('body'));
assert(bodyOnly.documentElement.outerHTML, '<html><head></head><body><div class="whatever"></div></body></html>');
assert(parseHTML('<head><title>t</title></head><body><p></p></body>').document.documentElement.outerHTML, '<html><head><title>t</title></head><body><p></p></body></html>');

const location = {href: 'http://ok'};
const withLocation = parseHTML('<html></html>', {location});
assert(withLocation.document.defaultView.location, location);
Expand Down
2 changes: 1 addition & 1 deletion types/esm/shared/parse-from-string.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export function isNotParsing(): boolean;
export function parseFromString(document: any, isHTML: any, markupLanguage: any): any;
export function parseFromString(document: any, isHTML: any, markupLanguage: any, isDocument?: boolean): any;
10 changes: 8 additions & 2 deletions worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2269,7 +2269,7 @@ const attribute = (element, end, attribute, value, active) => {
attributeChangedCallback$1(element, attribute.name, null, value);
};

const parseFromString = (document, isHTML, markupLanguage) => {
const parseFromString = (document, isHTML, markupLanguage, isDocument = false) => {
const {active, registry} = document[CUSTOM_ELEMENTS];

let node = document;
Expand All @@ -2287,6 +2287,12 @@ const parseFromString = (document, isHTML, markupLanguage) => {
onopentag(name, attributes) {
let create = true;
if (isHTML) {
// a top level <head> or <body> without <html> gets an implicit <html>,
// so document.head and document.body find the parsed elements
if (isDocument && node === document && (name === 'head' || name === 'body')) {
node = document.firstElementChild ||
append$2(document, document.createElement('html'), false);
}
if (ownerSVGElement) {
node = append$2(node, document.createElementNS(SVG_NAMESPACE, name), active);
node.ownerSVGElement = ownerSVGElement;
Expand Down Expand Up @@ -17665,7 +17671,7 @@ class DOMParser {
if (isHTML && markupLanguage === '...')
markupLanguage = '<!doctype html><html><head></head><body></body></html>';
return markupLanguage ?
parseFromString(document, isHTML, markupLanguage) :
parseFromString(document, isHTML, markupLanguage, true) :
document;
}
}
Expand Down