From 4918a10d8fe45e27831799e123b7e08571af2e2f Mon Sep 17 00:00:00 2001 From: Nejc Habjan Date: Wed, 30 Oct 2024 15:22:06 +0100 Subject: [PATCH] Added support for quoted external CSS URLs in privacy plugin (#7651) --- material/plugins/privacy/plugin.py | 7 ++++--- src/plugins/privacy/plugin.py | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/material/plugins/privacy/plugin.py b/material/plugins/privacy/plugin.py index 3ae8a5bb6..c760d9b6f 100644 --- a/material/plugins/privacy/plugin.py +++ b/material/plugins/privacy/plugin.py @@ -64,8 +64,8 @@ class PrivacyPlugin(BasePlugin[PrivacyConfig]): # Initialize collections of external assets self.assets = Files([]) self.assets_expr_map = { - ".css": r"url\((\s*http?[^)]+)\)", - ".js": r"[\"'](http[^\"']+\.(?:css|js(?:on)?))[\"']", + ".css": r"url\(\s*([\"']?)(?Phttp?[^)'\"]+)\1\s*\)", + ".js": r"[\"'](?Phttp[^\"']+\.(?:css|js(?:on)?))[\"']", **self.config.assets_expr_map } @@ -271,7 +271,8 @@ class PrivacyPlugin(BasePlugin[PrivacyConfig]): # Find and extract all external asset URLs expr = re.compile(self.assets_expr_map[extension], flags = re.I | re.M) with open(initiator.abs_src_path, encoding = "utf-8-sig") as f: - return [urlparse(url) for url in re.findall(expr, f.read())] + results = re.finditer(expr, f.read()) + return [urlparse(result.group("url")) for result in results] # Parse template or page HTML and find all external links that need to be # replaced. Many of the assets should already be downloaded earlier, i.e., diff --git a/src/plugins/privacy/plugin.py b/src/plugins/privacy/plugin.py index 3ae8a5bb6..c760d9b6f 100644 --- a/src/plugins/privacy/plugin.py +++ b/src/plugins/privacy/plugin.py @@ -64,8 +64,8 @@ class PrivacyPlugin(BasePlugin[PrivacyConfig]): # Initialize collections of external assets self.assets = Files([]) self.assets_expr_map = { - ".css": r"url\((\s*http?[^)]+)\)", - ".js": r"[\"'](http[^\"']+\.(?:css|js(?:on)?))[\"']", + ".css": r"url\(\s*([\"']?)(?Phttp?[^)'\"]+)\1\s*\)", + ".js": r"[\"'](?Phttp[^\"']+\.(?:css|js(?:on)?))[\"']", **self.config.assets_expr_map } @@ -271,7 +271,8 @@ class PrivacyPlugin(BasePlugin[PrivacyConfig]): # Find and extract all external asset URLs expr = re.compile(self.assets_expr_map[extension], flags = re.I | re.M) with open(initiator.abs_src_path, encoding = "utf-8-sig") as f: - return [urlparse(url) for url in re.findall(expr, f.read())] + results = re.finditer(expr, f.read()) + return [urlparse(result.group("url")) for result in results] # Parse template or page HTML and find all external links that need to be # replaced. Many of the assets should already be downloaded earlier, i.e.,