Skip to content

Commit

Permalink
Handle quoted external CSS URLs in privacy plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
nejch committed Oct 30, 2024
1 parent 1357cd2 commit c4105b0
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\(([\"']?)(?P<url>\s*http?[^)'\"]+)\1\)",
".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\(([\"']?)(?P<url>\s*http?[^)'\"]+)\1\)",
".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 c4105b0

Please sign in to comment.