Skip to content

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

clipper: serialize styles using xml serializer

Abdullah Attaa0f5880e2c91
diff --git a/packages/clipper/src/domtoimage.ts b/packages/clipper/src/domtoimage.ts
index a1ba8582a..185af3100 100644
--- a/packages/clipper/src/domtoimage.ts
+++ b/packages/clipper/src/domtoimage.ts
@@ -20,14 +20,7 @@ import { createImage, FetchOptions } from "./fetch.js";
import { resolveAll } from "./fontfaces.js";
import { inlineAllImages } from "./images.js";
import { Options } from "./types.js";
-import {
- canvasToBlob,
- delay,
- escapeXhtml,
- height,
- width,
- isSVGElement
-} from "./utils.js";
+import { canvasToBlob, delay, height, width, isSVGElement } from "./utils.js";
import { cloneNode } from "./clone.js";
// Default impl options
@@ -147,21 +140,22 @@ function makeSvgDataUri(
const xhtml = new XMLSerializer().serializeToString(body);
+ const xstyles = Array.from(head.getElementsByTagName("style"))
+ .map((s) => new XMLSerializer().serializeToString(s))
+ .join("\n");
+
const foreignObject =
'<foreignObject x="0" y="0" width="100%" height="100%">' +
xhtml +
"</foreignObject>";
- const styles = Array.from(head.getElementsByTagName("style"))
- .map((e) => e.outerHTML.replace(/\\"/gm, "'"))
- .join("\n");
const svgStr =
'<svg xmlns="http://www.w3.org/2000/svg" width="' +
width +
'" height="' +
height +
'">' +
- styles +
+ xstyles +
foreignObject +
"</svg>";
diff --git a/packages/clipper/src/index.ts b/packages/clipper/src/index.ts
index 1559e135c..7f51ac80f 100644
--- a/packages/clipper/src/index.ts
+++ b/packages/clipper/src/index.ts
@@ -50,8 +50,7 @@ async function clipPage(
): Promise<string | null> {
const { body, head } = await getPage(document, config, onlyVisible);
if (!body || !head) return null;
- const result = toDocument(head, body, toAttributes(document.documentElement))
- .documentElement.outerHTML;
+ const result = toDocument(head, body).documentElement.outerHTML;
return `<!doctype html>\n${result}`;
}
@@ -434,19 +433,8 @@ function getElementViewportInfo(el: HTMLElement) {
return result;
}
-function toDocument(
- head: HTMLElement,
- body: HTMLElement,
- rootAttributes?: Record<string, string>
-) {
+function toDocument(head: HTMLElement, body: HTMLElement) {
const doc = document.implementation.createHTMLDocument();
-
- if (rootAttributes) {
- for (const [name, value] of Object.entries(rootAttributes)) {
- doc.documentElement.setAttribute(name, value);
- }
- }
-
doc.documentElement.replaceChildren(head, body);
return doc;
}
@@ -499,6 +487,12 @@ async function getPage(
await addStylesToHead(head, fetchOptions);
}
+ for (const [name, value] of Object.entries(
+ toAttributes(document.documentElement)
+ )) {
+ body.setAttribute(name, value);
+ }
+
return {
body,
head