Skip to content

Commit

Permalink
Added support for quoted external CSS URLs in privacy plugin (#7651)
Browse files Browse the repository at this point in the history
  • Loading branch information
nejch authored Oct 30, 2024
1 parent 7dc96f1 commit 4918a10
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
7 changes: 4 additions & 3 deletions material/plugins/privacy/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ def on_config(self, config):
# 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*([\"']?)(?P<url>http?[^)'\"]+)\1\s*\)",
".js": r"[\"'](?P<url>http[^\"']+\.(?:css|js(?:on)?))[\"']",
**self.config.assets_expr_map
}

Expand Down Expand Up @@ -271,7 +271,8 @@ def _parse_media(self, initiator: File) -> list[URL]:
# 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.,
Expand Down
7 changes: 4 additions & 3 deletions src/plugins/privacy/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ def on_config(self, config):
# 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*([\"']?)(?P<url>http?[^)'\"]+)\1\s*\)",
".js": r"[\"'](?P<url>http[^\"']+\.(?:css|js(?:on)?))[\"']",
**self.config.assets_expr_map
}

Expand Down Expand Up @@ -271,7 +271,8 @@ def _parse_media(self, initiator: File) -> list[URL]:
# 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.,
Expand Down

0 comments on commit 4918a10

Please sign in to comment.