Skip to content

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

clipper: inline background-images style for stylesheets appended to head Signed-off-by: 01zulfi <[email protected]>

01zulfi911105c40970
diff --git a/packages/clipper/src/images.ts b/packages/clipper/src/images.ts
index 3d76b6303..0a908b44e 100644
--- a/packages/clipper/src/images.ts
+++ b/packages/clipper/src/images.ts
@@ -28,14 +28,6 @@ async function inlineAllImages(root: HTMLElement, options?: FetchOptions) {
promises.push(inlineImage(image, options));
}
- const backgroundImageNodes = root.querySelectorAll(
- `[style*="background-image:"],[style*="background:"]`
- );
- for (let i = 0; i < backgroundImageNodes.length; ++i) {
- const image = backgroundImageNodes[i];
- promises.push(inlineBackground(image as HTMLElement, options));
- }
-
await Promise.all(promises).catch((e) => console.error(e));
}
export { inlineAllImages };
@@ -66,14 +58,3 @@ async function inlineImage(element: HTMLImageElement, options?: FetchOptions) {
element.removeAttribute("srcset");
});
}
-
-async function inlineBackground(
- backgroundNode: HTMLElement,
- options?: FetchOptions
-) {
- const background = backgroundNode.style.getPropertyValue("background-image");
- if (!background) return backgroundNode;
- const inlined = await inlineAll(background, options);
- backgroundNode.style.setProperty("background-image", inlined);
- return backgroundNode;
-}
diff --git a/packages/clipper/src/styles.ts b/packages/clipper/src/styles.ts
index fb351fa8b..52b71a176 100644
--- a/packages/clipper/src/styles.ts
+++ b/packages/clipper/src/styles.ts
@@ -17,6 +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/>.
*/
import { constructUrl, FetchOptions } from "./fetch.js";
+import { inlineAll } from "./inliner.js";
export async function inlineStylesheets(options?: FetchOptions) {
for (const sheet of document.styleSheets) {
@@ -98,14 +99,19 @@ export async function addStylesToHead(
? node.getAttribute("href")
: null;
if (href) {
- const styleNode = await downloadStylesheet(href, options);
- if (styleNode) {
+ 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;
@@ -125,3 +131,40 @@ function rulesToStyleNode(cssRules: CSSRuleList) {
style.innerHTML = cssText;
return style;
}
+
+async function inlineBackgroundImages(
+ stylesheet: CSSStyleSheet,
+ options?: FetchOptions
+) {
+ for (let i = 0; i < stylesheet.cssRules.length; ++i) {
+ const rule = stylesheet.cssRules.item(i);
+ if (!rule) continue;
+
+ const styleRules: CSSStyleRule[] = [];
+
+ if (rule.type === CSSRule.STYLE_RULE) {
+ styleRules.push(rule as CSSStyleRule);
+ } 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 innerRule = mediaRule.cssRules.item(i);
+ if (innerRule && innerRule.type === CSSRule.STYLE_RULE) {
+ styleRules.push(innerRule as CSSStyleRule);
+ }
+ }
+ }
+
+ 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);
+ }
+ }
+}