Просмотр исходного кода

fixed issue if no <head> is present in the DOM

Gildas 7 лет назад
Родитель
Сommit
81ce7b9fe0
2 измененных файлов с 9 добавлено и 2 удалено
  1. 6 2
      lib/single-file/util/doc-helper.js
  2. 3 0
      lib/single-file/util/doc-util.js

+ 6 - 2
lib/single-file/util/doc-helper.js

@@ -58,7 +58,9 @@ this.docHelper = this.docHelper || (() => {
 			disabledNoscriptElement.hidden = true;
 			element.parentElement.replaceChild(disabledNoscriptElement, element);
 		});
-		doc.head.querySelectorAll("*:not(base):not(link):not(meta):not(noscript):not(script):not(style):not(template):not(title)").forEach(element => element.hidden = true);
+		if (doc.head) {
+			doc.head.querySelectorAll("*:not(base):not(link):not(meta):not(noscript):not(script):not(style):not(template):not(title)").forEach(element => element.hidden = true);
+		}
 		let canvasData, imageData, usedFonts, shadowRootContents;
 		if (win) {
 			canvasData = getCanvasData(doc, win);
@@ -203,7 +205,9 @@ this.docHelper = this.docHelper || (() => {
 			Array.from(element.childNodes).forEach(node => noscriptElement.appendChild(node));
 			element.parentElement.replaceChild(noscriptElement, element);
 		});
-		doc.head.querySelectorAll("*:not(base):not(link):not(meta):not(noscript):not(script):not(style):not(template):not(title)").forEach(element => element.removeAttribute("hidden"));
+		if (doc.head) {
+			doc.head.querySelectorAll("*:not(base):not(link):not(meta):not(noscript):not(script):not(style):not(template):not(title)").forEach(element => element.removeAttribute("hidden"));
+		}
 		if (options.removeHiddenElements) {
 			doc.querySelectorAll("[" + REMOVED_CONTENT_ATTRIBUTE_NAME + "]").forEach(element => element.removeAttribute(REMOVED_CONTENT_ATTRIBUTE_NAME));
 		}

+ 3 - 0
lib/single-file/util/doc-util.js

@@ -58,6 +58,9 @@ this.docUtil = this.docUtil || (() => {
 				},
 				parseDocContent(content, baseURI) {
 					const doc = domUtil.parseDocContent(content);
+					if (!doc.head) {
+						doc.documentElement.insertBefore(doc.createElement("HEAD"), doc.body);
+					}
 					let baseElement = doc.querySelector("base");
 					if (!baseElement || !baseElement.getAttribute("href")) {
 						if (baseElement) {