Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

clipper: fix image inlining in stylesheets

Abdullah Attab5be9b3abfbf
diff --git a/packages/clipper/src/clone.ts b/packages/clipper/src/clone.ts
index c7aa017d0..071979dee 100644
--- a/packages/clipper/src/clone.ts
+++ b/packages/clipper/src/clone.ts
@@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-const INVALID_ELEMENTS = ["script"].map((a) => a.toLowerCase());
+const INVALID_ELEMENTS = ["script"];
type CloneNodeOptions = {
images?: boolean;
@@ -25,52 +25,20 @@ type CloneNodeOptions = {
};
export function cloneNode(node: HTMLElement, options: CloneNodeOptions) {
- const clone = node.cloneNode(true) as HTMLElement;
- processNode(clone, options);
- return clone;
-}
-
-function processNode(node: HTMLElement, options: CloneNodeOptions) {
- try {
- const tagNames = [
- "img",
- "button",
- "form",
- "select",
- "input",
- "textarea"
- ].concat(INVALID_ELEMENTS);
- const elements = node.querySelectorAll(tagNames.join(","));
-
- for (const element of elements) {
- if (!options.images && element instanceof HTMLImageElement) {
- element.remove();
- continue;
- }
-
- if (
- !options.styles &&
- (element instanceof HTMLButtonElement ||
- element instanceof HTMLFormElement ||
- element instanceof HTMLSelectElement ||
- element instanceof HTMLInputElement ||
- element instanceof HTMLTextAreaElement)
- ) {
- element.remove();
- continue;
- }
+ node = node.cloneNode(true) as HTMLElement;
+ if (!options.images) {
+ const images = node.getElementsByTagName("img");
+ for (const image of images) image.remove();
+ }
- if (isInvalidElement(element as HTMLElement)) {
- element.remove();
- }
- }
- } catch (e) {
- console.error("Failed to process node", e);
- return null;
+ if (!options.styles) {
+ const elements = node.querySelectorAll(
+ `button, form, select, input, textarea`
+ );
+ for (const element of elements) element.remove();
}
-}
-function isInvalidElement(element: HTMLElement) {
- if (!element || !element.tagName) return false;
- return INVALID_ELEMENTS.includes(element.tagName.toLowerCase());
+ const invalidElements = node.querySelectorAll(INVALID_ELEMENTS.join(","));
+ for (const element of invalidElements) element.remove();
+ return node;
}
diff --git a/packages/clipper/src/domtoimage.ts b/packages/clipper/src/domtoimage.ts
index 370bf63a8..5c35c435c 100644
--- a/packages/clipper/src/domtoimage.ts
+++ b/packages/clipper/src/domtoimage.ts
@@ -40,8 +40,6 @@ async function getInlinedNode(node: HTMLElement, options: Options) {
const { fonts, images, stylesheets, inlineImages } =
options.inlineOptions || {};
- if (stylesheets) await inlineStylesheets(options.fetchOptions);
-
let clone = cloneNode(node, {
images,
styles: options.styles
@@ -49,6 +47,8 @@ async function getInlinedNode(node: HTMLElement, options: Options) {
if (!clone || clone instanceof Text) return;
+ if (stylesheets) await inlineStylesheets(options.fetchOptions);
+
if (fonts) clone = await embedFonts(clone, options.fetchOptions);
if (inlineImages) await inlineAllImages(clone, options.fetchOptions);
diff --git a/packages/clipper/src/images.ts b/packages/clipper/src/images.ts
index 0a908b44e..b7579569f 100644
--- a/packages/clipper/src/images.ts
+++ b/packages/clipper/src/images.ts
@@ -17,7 +17,6 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { FetchOptions, fetchResource } from "./fetch.js";
-import { inlineAll } from "./inliner.js";
import { isDataUrl } from "./utils.js";
async function inlineAllImages(root: HTMLElement, options?: FetchOptions) {
diff --git a/packages/clipper/src/index.ts b/packages/clipper/src/index.ts
index 23a3a7687..e192ed30b 100644
--- a/packages/clipper/src/index.ts
+++ b/packages/clipper/src/index.ts
@@ -491,7 +491,7 @@ async function getPage(
head.appendChild(title);
if (config?.styles) {
- await addStylesToHead(head, fetchOptions);
+ addStylesToHead(head, fetchOptions);
}
return {
diff --git a/packages/clipper/src/inliner.ts b/packages/clipper/src/inliner.ts
index b86b420f8..2c1c65f8e 100644
--- a/packages/clipper/src/inliner.ts
+++ b/packages/clipper/src/inliner.ts
@@ -16,7 +16,7 @@ GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-import { FetchOptions, fetchResource } from "./fetch.js";
+import { constructUrl, FetchOptions, fetchResource } from "./fetch.js";
import { isDataUrl, resolveUrl, escape } from "./utils.js";
const URL_REGEX = /url\(['"]?([^'"]+?)['"]?\)/g;
@@ -40,32 +40,37 @@ async function inline(
string: string,
url: string,
options?: FetchOptions,
- baseUrl?: string
+ baseUrl?: string,
+ onlyResolve?: boolean
) {
- url = baseUrl ? resolveUrl(url, baseUrl) : url;
- const dataUrl = await fetchResource(url, options);
- // const dataUrl = dataAsUrl(data, mimeType(url));
- return string.replace(urlAsRegex(url), "$1" + dataUrl + "$3");
-}
-
-function urlAsRegex(urlValue: string) {
- return new RegExp("(url\\(['\"]?)(" + escape(urlValue) + ")(['\"]?\\))", "g");
+ const resolvedUrl = baseUrl ? resolveUrl(url, baseUrl) : url;
+ const dataUrl = onlyResolve
+ ? constructUrl(resolvedUrl, options)
+ : await fetchResource(resolvedUrl, options).catch(() =>
+ constructUrl(resolvedUrl, options)
+ );
+ return string.replace(url, dataUrl || resolvedUrl);
}
async function inlineAll(
string: string,
options?: FetchOptions,
- baseUrl?: string
+ baseUrl?: string,
+ onlyResolve?: boolean
) {
if (!shouldProcess(string)) return string;
const urls = readUrls(string);
- let prefix = string;
for (const url of urls) {
- prefix = await inline(prefix, url, options, baseUrl);
+ string = await inline(string, url, options, baseUrl, onlyResolve).catch(
+ (e) => {
+ console.error(e);
+ return string;
+ }
+ );
}
- return prefix;
+ return string;
}
export { shouldProcess, inlineAll, readUrls };
diff --git a/packages/clipper/src/styles.ts b/packages/clipper/src/styles.ts
index 52b71a176..1777a641d 100644
--- a/packages/clipper/src/styles.ts
+++ b/packages/clipper/src/styles.ts
@@ -20,13 +20,6 @@ import { constructUrl, FetchOptions } from "./fetch.js";
import { inlineAll } from "./inliner.js";
export async function inlineStylesheets(options?: FetchOptions) {
- for (const sheet of document.styleSheets) {
- if (await skipStyleSheet(sheet, options)) continue;
- }
- await resolveImports(options);
-}
-
-async function resolveImports(options?: FetchOptions) {
for (const sheet of document.styleSheets) {
const rulesToDelete = [];
if (await skipStyleSheet(sheet, options)) continue;
@@ -38,14 +31,15 @@ async function resolveImports(options?: FetchOptions) {
if (rule.type === CSSRule.IMPORT_RULE) {
const href = (rule as CSSImportRule).href;
const result = await downloadStylesheet(href, options);
- if (result) {
- if (sheet.ownerNode) sheet.ownerNode.before(result);
- else document.head.appendChild(result);
- rulesToDelete.push(i);
- }
+ if (!result) continue;
+ if (sheet.ownerNode) sheet.ownerNode.before(result);
+ else document.head.appendChild(result);
+ rulesToDelete.push(i);
}
}
+ await inlineBackgroundImages(sheet, options);
+
for (const ruleIndex of rulesToDelete) sheet.deleteRule(ruleIndex);
}
}
@@ -68,7 +62,11 @@ async function skipStyleSheet(sheet: CSSStyleSheet, options?: FetchOptions) {
sheet.cssRules.length;
} catch (_e) {
const node = sheet.ownerNode;
- if (sheet.href && node instanceof HTMLLinkElement) {
+ if (
+ sheet.href &&
+ node instanceof HTMLLinkElement &&
+ !isStylesheetForPrint(node)
+ ) {
const styleNode = await downloadStylesheet(node.href, options);
if (styleNode) node.replaceWith(styleNode);
}
@@ -78,48 +76,20 @@ async function skipStyleSheet(sheet: CSSStyleSheet, options?: FetchOptions) {
return isStylesheetForPrint(sheet);
}
-function isStylesheetForPrint(sheet: CSSStyleSheet) {
- return sheet.media.mediaText
+function isStylesheetForPrint(sheet: CSSStyleSheet | HTMLLinkElement) {
+ const mediaText =
+ typeof sheet.media === "string" ? sheet.media : sheet.media.mediaText;
+ return mediaText
.split(",")
.map((t) => t.trim())
.includes("print");
}
-export async function addStylesToHead(
- head: HTMLHeadElement,
- options?: FetchOptions
-) {
+export function addStylesToHead(head: HTMLHeadElement, options?: FetchOptions) {
for (const sheet of document.styleSheets) {
if (isStylesheetForPrint(sheet)) continue;
- const node = sheet.ownerNode;
- const href =
- sheet.href && node instanceof HTMLLinkElement
- ? node.href
- : node instanceof HTMLStyleElement
- ? node.getAttribute("href")
- : null;
- if (href) {
- const downloadedStyleNode = await downloadStylesheet(href, options);
- if (downloadedStyleNode) {
- const cssStyleSheet = new CSSStyleSheet();
- cssStyleSheet.replace(downloadedStyleNode.innerHTML);
- await inlineBackgroundImages(cssStyleSheet, options);
- const styleNode = rulesToStyleNode(cssStyleSheet.cssRules);
- head.appendChild(styleNode);
- }
- continue;
- }
-
- if (sheet.cssRules.length) {
- await inlineBackgroundImages(sheet, options);
- const styleNode = rulesToStyleNode(sheet.cssRules);
- head.appendChild(styleNode);
- continue;
- }
-
- if (node instanceof HTMLStyleElement) {
- head.appendChild(node.cloneNode(true));
- }
+ const styleNode = rulesToStyleNode(sheet.cssRules);
+ head.appendChild(styleNode);
}
}
@@ -133,38 +103,52 @@ function rulesToStyleNode(cssRules: CSSRuleList) {
}
async function inlineBackgroundImages(
- stylesheet: CSSStyleSheet,
+ sheet: CSSStyleSheet,
options?: FetchOptions
) {
- for (let i = 0; i < stylesheet.cssRules.length; ++i) {
- const rule = stylesheet.cssRules.item(i);
- if (!rule) continue;
-
- const styleRules: CSSStyleRule[] = [];
-
+ const promises: Promise<void>[] = [];
+ for (const rule of sheet.cssRules) {
if (rule.type === CSSRule.STYLE_RULE) {
- styleRules.push(rule as CSSStyleRule);
+ promises.push(processStyleRule(sheet, rule as CSSStyleRule, options));
} else if (rule.type === CSSRule.MEDIA_RULE) {
- const mediaRule: CSSMediaRule = rule as CSSMediaRule;
- for (let i = 0; i < mediaRule.cssRules.length; ++i) {
- if (!window.matchMedia(mediaRule.media.mediaText).matches) {
- continue;
+ const mediaRule = rule as CSSMediaRule;
+ const mediaMatches = window.matchMedia(mediaRule.media.mediaText).matches;
+ for (const innerRule of mediaRule.cssRules) {
+ if (innerRule && innerRule.type === CSSRule.STYLE_RULE) {
+ promises.push(
+ processStyleRule(
+ sheet,
+ innerRule as CSSStyleRule,
+ options,
+ mediaMatches
+ )
+ );
}
- const innerRule = mediaRule.cssRules.item(i);
+ }
+ } else if (rule.type === CSSRule.SUPPORTS_RULE) {
+ const supportsRule = rule as CSSSupportsRule;
+ for (const innerRule of supportsRule.cssRules) {
if (innerRule && innerRule.type === CSSRule.STYLE_RULE) {
- styleRules.push(innerRule as CSSStyleRule);
+ promises.push(
+ processStyleRule(sheet, innerRule as CSSStyleRule, options, false)
+ );
}
}
}
+ }
+ await Promise.allSettled(promises);
+}
- for (const styleRule of styleRules) {
- const backgroundImage =
- styleRule.style.getPropertyValue("background-image");
- if (!backgroundImage || !backgroundImage.startsWith("url(")) continue;
-
- const inlined = await inlineAll(backgroundImage, options);
-
- styleRule.style.setProperty("background-image", inlined);
- }
+async function processStyleRule(
+ sheet: CSSStyleSheet,
+ rule: CSSStyleRule,
+ options?: FetchOptions,
+ inline = true
+) {
+ const baseUrl = sheet.href || document.location.origin;
+ for (const property of rule.style) {
+ const oldValue = rule.style.getPropertyValue(property);
+ const resolved = await inlineAll(oldValue, options, baseUrl, !inline);
+ rule.style.setProperty(property, resolved);
}
}