Browse Source

always retrieve Element#shadowRoot content

Gildas 6 years ago
parent
commit
d5374ebc38
1 changed files with 30 additions and 11 deletions
  1. 30 11
      lib/single-file/util/doc-helper.js

+ 30 - 11
lib/single-file/util/doc-helper.js

@@ -110,17 +110,9 @@ this.docHelper = this.docHelper || (() => {
 						}
 					});
 				}
-				elementsInfo.forEach((elementInfo, element) => {
-					let elementIndex = 0;
-					if (element.attributes && elementInfo.shadowRoot) {
-						element.setAttribute(SHADOW_ROOT_ATTRIBUTE_NAME, elementIndex);
-						elementIndex++;
-						if (!shadowRootContents) {
-							shadowRootContents = [];
-						}
-						shadowRootContents.push({ content: element.shadowRoot.innerHTML, height: element.clientHeight });
-					}
-				});
+				shadowRootContents = getShadowRootContents(elementsInfo);
+			} else if (doc.body) {
+				shadowRootContents = getShadowRootContents(getShadowRootElementsInfo(win, doc.body));
 			}
 		}
 		retrieveInputValues(doc);
@@ -136,6 +128,22 @@ this.docHelper = this.docHelper || (() => {
 		};
 	}
 
+	function getShadowRootContents(elementsInfo) {
+		let shadowRootContents;
+		elementsInfo.forEach((elementInfo, element) => {
+			let elementIndex = 0;
+			if (element.attributes && elementInfo.shadowRoot) {
+				element.setAttribute(SHADOW_ROOT_ATTRIBUTE_NAME, elementIndex);
+				elementIndex++;
+				if (!shadowRootContents) {
+					shadowRootContents = [];
+				}
+				shadowRootContents.push({ content: element.shadowRoot.innerHTML, height: element.clientHeight });
+			}
+		});
+		return shadowRootContents;
+	}
+
 	function getUsedFonts(styles, loadedFonts) {
 		const usedFonts = new Set();
 		styles.forEach(style => {
@@ -155,6 +163,17 @@ this.docHelper = this.docHelper || (() => {
 		return removeQuotes(fontFamilyName.trim()).toLowerCase();
 	}
 
+	function getShadowRootElementsInfo(win, element, elementsInfo = new Map()) {
+		const elements = Array.from(element.childNodes).filter(node => !win || node instanceof win.Element);
+		elements.forEach(element => {
+			getShadowRootElementsInfo(win, element, elementsInfo);
+			elementsInfo.set(element, {
+				shadowRoot: element.shadowRoot
+			});
+		});
+		return elementsInfo;
+	}
+
 	function getElementsInfo(win, element, elementsInfo = new Map()) {
 		const elements = Array.from(element.childNodes).filter(node => !win || node instanceof win.Element);
 		elements.forEach(element => {