Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

9.8 KB
Raw Blame
diff --git a/es/clone-node.js b/es/clone-node.js
index 114cbf214f65cda237dbba67458c2b68fadf421e..8aaec57528ffc1f912c6f62ab4fbbf717cb104b2 100644
--- a/es/clone-node.js
+++ b/es/clone-node.js
@@ -59,9 +59,15 @@ async function cloneChildren(nativeNode, clonedNode, options) {
     if (isSlotElement(nativeNode) && nativeNode.assignedNodes) {
         children = toArray(nativeNode.assignedNodes());
     }
-    else if (isInstanceOfElement(nativeNode, HTMLIFrameElement) &&
-        ((_a = nativeNode.contentDocument) === null || _a === void 0 ? void 0 : _a.body)) {
-        children = toArray(nativeNode.contentDocument.body.childNodes);
+    else if (isInstanceOfElement(nativeNode, HTMLIFrameElement)) {
+        try {
+            if ((_a = nativeNode.contentDocument) === null || _a === void 0 ? void 0 : _a.body) {
+                children = toArray(nativeNode.contentDocument.body.childNodes);
+            }
+        }
+        catch (_e) {
+            // Cross-origin iframes cannot be accessed; fall through to clone as-is
+        }
     }
     else {
         children = toArray(((_b = nativeNode.shadowRoot) !== null && _b !== void 0 ? _b : nativeNode).childNodes);
diff --git a/es/embed-webfonts.js b/es/embed-webfonts.js
index f6b0d96829c8a3301c142cacd07079e85b1f64fd..c0eba90427585cefadb3baab8b05e58b8115e34c 100644
--- a/es/embed-webfonts.js
+++ b/es/embed-webfonts.js
@@ -153,15 +153,17 @@ async function parseWebFontRules(node, options) {
     return getWebFontRules(cssRules);
 }
 function normalizeFontFamily(font) {
-    return font.trim().replace(/["']/g, '');
+    return (font || '').trim().replace(/["']/g, '');
 }
 function getUsedFonts(node) {
     const fonts = new Set();
     function traverse(node) {
         const fontFamily = node.style.fontFamily || getComputedStyle(node).fontFamily;
-        fontFamily.split(',').forEach((font) => {
-            fonts.add(normalizeFontFamily(font));
-        });
+        if (fontFamily) {
+            fontFamily.split(',').forEach((font) => {
+                fonts.add(normalizeFontFamily(font));
+            });
+        }
         Array.from(node.children).forEach((child) => {
             if (child instanceof HTMLElement) {
                 traverse(child);
@@ -190,9 +192,12 @@ export async function embedWebFonts(clonedNode, options) {
         : options.skipFonts
             ? null
             : await getWebFontCSS(clonedNode, options);
-    if (cssText) {
+    const finalCssText = options.extraStyleContent != null
+        ? (cssText || '').concat(options.extraStyleContent)
+        : cssText
+    if (finalCssText) {
         const styleNode = document.createElement('style');
-        const sytleContent = document.createTextNode(cssText);
+        const sytleContent = document.createTextNode(finalCssText);
         styleNode.appendChild(sytleContent);
         if (clonedNode.firstChild) {
             clonedNode.insertBefore(styleNode, clonedNode.firstChild);
diff --git a/es/types.d.ts b/es/types.d.ts
index a832baf45e95d7b3bb076ecacd46e73493f3e9ef..b487b064337bf75cb74dd530b54b7c6a1db40da4 100644
--- a/es/types.d.ts
+++ b/es/types.d.ts
@@ -75,6 +75,11 @@ export interface Options {
      * create embed CSS for use across multiple calls to library functions.
      */
     fontEmbedCSS?: string;
+    /**
+     * A CSS string to specify additional custom style content. It will be
+     * added to the style-tag.
+     */
+    extraStyleContent?: string;
     /**
      * A boolean to turn off auto scaling for truly massive images..
      */
diff --git a/lib/clone-node.js b/lib/clone-node.js
index 214f4d30a50baae7f007d2399eeac298366538c7..74d67830cf549c05c533904123a9482c2cf5314f 100644
--- a/lib/clone-node.js
+++ b/lib/clone-node.js
@@ -134,9 +134,15 @@ function cloneChildren(nativeNode, clonedNode, options) {
                     if (isSlotElement(nativeNode) && nativeNode.assignedNodes) {
                         children = (0, util_1.toArray)(nativeNode.assignedNodes());
                     }
-                    else if ((0, util_1.isInstanceOfElement)(nativeNode, HTMLIFrameElement) &&
-                        ((_a = nativeNode.contentDocument) === null || _a === void 0 ? void 0 : _a.body)) {
-                        children = (0, util_1.toArray)(nativeNode.contentDocument.body.childNodes);
+                    else if ((0, util_1.isInstanceOfElement)(nativeNode, HTMLIFrameElement)) {
+                        try {
+                            if ((_a = nativeNode.contentDocument) === null || _a === void 0 ? void 0 : _a.body) {
+                                children = (0, util_1.toArray)(nativeNode.contentDocument.body.childNodes);
+                            }
+                        }
+                        catch (_e) {
+                            // Cross-origin iframes cannot be accessed; fall through to clone as-is
+                        }
                     }
                     else {
                         children = (0, util_1.toArray)(((_b = nativeNode.shadowRoot) !== null && _b !== void 0 ? _b : nativeNode).childNodes);
diff --git a/lib/embed-webfonts.js b/lib/embed-webfonts.js
index c91538bd9c3afa63326a91267fb7b5b9c999cccd..ca5fdedfd7904b6d99ad201d739300d4a50f451c 100644
--- a/lib/embed-webfonts.js
+++ b/lib/embed-webfonts.js
@@ -300,9 +300,12 @@ function embedWebFonts(clonedNode, options) {
                     _c.label = 5;
                 case 5:
                     cssText = _a;
-                    if (cssText) {
+                    const finalCssText = options.extraStyleContent != null
+                        ? (cssText || '').concat(options.extraStyleContent)
+                        : cssText
+                    if (finalCssText) {
                         styleNode = document.createElement('style');
-                        sytleContent = document.createTextNode(cssText);
+                        sytleContent = document.createTextNode(finalCssText);
                         styleNode.appendChild(sytleContent);
                         if (clonedNode.firstChild) {
                             clonedNode.insertBefore(styleNode, clonedNode.firstChild);
diff --git a/lib/types.d.ts b/lib/types.d.ts
index a832baf45e95d7b3bb076ecacd46e73493f3e9ef..b487b064337bf75cb74dd530b54b7c6a1db40da4 100644
--- a/lib/types.d.ts
+++ b/lib/types.d.ts
@@ -75,6 +75,11 @@ export interface Options {
      * create embed CSS for use across multiple calls to library functions.
      */
     fontEmbedCSS?: string;
+    /**
+     * A CSS string to specify additional custom style content. It will be
+     * added to the style-tag.
+     */
+    extraStyleContent?: string;
     /**
      * A boolean to turn off auto scaling for truly massive images..
      */
diff --git a/src/clone-node.ts b/src/clone-node.ts
index 5dfcd117a1dd3d3d5cfb7c98ead3eb828085337a..e0cd29915bd053ad31c1460ead1cb3556a23a79e 100644
--- a/src/clone-node.ts
+++ b/src/clone-node.ts
@@ -89,10 +89,15 @@ async function cloneChildren<T extends HTMLElement>(
   if (isSlotElement(nativeNode) && nativeNode.assignedNodes) {
     children = toArray<T>(nativeNode.assignedNodes())
   } else if (
-    isInstanceOfElement(nativeNode, HTMLIFrameElement) &&
-    nativeNode.contentDocument?.body
+    isInstanceOfElement(nativeNode, HTMLIFrameElement)
   ) {
-    children = toArray<T>(nativeNode.contentDocument.body.childNodes)
+    try {
+      if (nativeNode.contentDocument?.body) {
+        children = toArray<T>(nativeNode.contentDocument.body.childNodes)
+      }
+    } catch {
+      // Cross-origin iframes cannot be accessed; fall through to clone as-is
+    }
   } else {
     children = toArray<T>((nativeNode.shadowRoot ?? nativeNode).childNodes)
   }
diff --git a/src/embed-webfonts.ts b/src/embed-webfonts.ts
index a84a699d37aeaaea19b9b31e0e98710f1e30da3c..83e33eade74c61a5f2ff56952535292c158f9cbb 100644
--- a/src/embed-webfonts.ts
+++ b/src/embed-webfonts.ts
@@ -203,7 +203,7 @@ async function parseWebFontRules<T extends HTMLElement>(
 }
 
 function normalizeFontFamily(font: string) {
-  return font.trim().replace(/["']/g, '')
+  return (font || '').trim().replace(/["']/g, '')
 }
 
 function getUsedFonts(node: HTMLElement) {
@@ -211,9 +211,11 @@ function getUsedFonts(node: HTMLElement) {
   function traverse(node: HTMLElement) {
     const fontFamily =
       node.style.fontFamily || getComputedStyle(node).fontFamily
-    fontFamily.split(',').forEach((font) => {
-      fonts.add(normalizeFontFamily(font))
-    })
+    if (fontFamily) {
+      fontFamily.split(',').forEach((font) => {
+        fonts.add(normalizeFontFamily(font))
+      })
+    }
 
     Array.from(node.children).forEach((child) => {
       if (child instanceof HTMLElement) {
@@ -258,9 +260,13 @@ export async function embedWebFonts<T extends HTMLElement>(
       ? null
       : await getWebFontCSS(clonedNode, options)
 
-  if (cssText) {
+  const finalCssText = options.extraStyleContent != null
+    ? (cssText || '').concat(options.extraStyleContent)
+    : cssText
+
+  if (finalCssText) {
     const styleNode = document.createElement('style')
-    const sytleContent = document.createTextNode(cssText)
+    const sytleContent = document.createTextNode(finalCssText)
 
     styleNode.appendChild(sytleContent)
 
diff --git a/src/types.ts b/src/types.ts
index 6023c3c2def1cb3ce83112c4c902997617582f59..ef40283ab6e47c98c7f8ea477c3ae29671682d98 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -76,12 +76,18 @@ export interface Options {
     | 'embedded-opentype'
     | 'svg'
     | string
+
   /**
    * A CSS string to specify for font embeds. If specified only this CSS will
    * be present in the resulting image. Use with `getFontEmbedCSS()` to
    * create embed CSS for use across multiple calls to library functions.
    */
   fontEmbedCSS?: string
+  /**
+   * A CSS string to specify additional custom style content. It will be
+   * added to the style-tag.
+   */
+  extraStyleContent?: string
   /**
    * A boolean to turn off auto scaling for truly massive images..
    */