private injectHtml()

in src/Popout.tsx [94:161]


    private injectHtml(id: string, child: Window) {
        let container: HTMLDivElement;

        if (this.props.html) {
            child.document.write(this.props.html);
            const head = child.document.head;

            let cssText = '';
            let rules = null;

            for (let i = window.document.styleSheets.length - 1; i >= 0; i--) {
                let styleSheet = window.document.styleSheets[i] as CSSStyleSheet;
                try {
                    rules = styleSheet.cssRules;
                } catch {
                    // We're primarily looking for a security exception here.
                    // See https://bugs.chromium.org/p/chromium/issues/detail?id=775525
                    // Try to just embed the style element instead.
                    let styleElement = child.document.createElement('link');
                    styleElement.type = styleSheet.type;
                    styleElement.rel = 'stylesheet';
                    styleElement.href = styleSheet.href;
                    head.appendChild(styleElement);
                } finally {
                    if (rules) {
                        for (let j = 0; j < rules.length; j++) {
                            try {
                                cssText += rules[j].cssText;
                            } catch {
                                // IE11 will throw a security exception sometimes when accessing cssText.
                                // There's no good way to detect this, so we capture the exception instead.
                            }
                        }
                    }
                }

                rules = null;
            }

            const style = child.document.createElement('style');
            style.innerHTML = cssText;

            head.appendChild(style);
            container = child.document.createElement('div');
            container.id = id;
            child.document.body.appendChild(container);
        } else {
            let childHtml = '<!DOCTYPE html><html><head>';
            for (let i = window.document.styleSheets.length - 1; i >= 0; i--) {
                let styleSheet = window.document.styleSheets[i] as CSSStyleSheet;
                try {
                    const cssText = styleSheet.cssText;
                    childHtml += `<style>${cssText}</style>`;
                } catch {
                    // IE11 will throw a security exception sometimes when accessing cssText.
                    // There's no good way to detect this, so we capture the exception instead.
                }
            }
            childHtml += `</head><body><div id="${id}"></div></body></html>`;
            child.document.write(childHtml);
            container = child.document.getElementById(id)! as HTMLDivElement;
        }

        // Create a document with the styles of the parent window first
        this.setupStyleElement(child);

        return container;
    }