Skip to content

Commit 29cf44b

Browse files
committed
Improved resilience of privacy plugin
1 parent 6c9ba87 commit 29cf44b

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

Diff for: material/plugins/privacy/plugin.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from hashlib import sha1
3434
from mkdocs.config.config_options import ExtraScriptValue
3535
from mkdocs.config.defaults import MkDocsConfig
36+
from mkdocs.exceptions import PluginError
3637
from mkdocs.plugins import BasePlugin, event_priority
3738
from mkdocs.structure.files import File, Files
3839
from mkdocs.utils import is_error_template
@@ -241,9 +242,18 @@ def _parse_fragment(self, fragment: str):
241242
parser.feed(fragment)
242243
parser.close()
243244

244-
# Return element
245-
assert isinstance(parser.result, Element)
246-
return parser.result
245+
# Check parse result and return element
246+
if isinstance(parser.result, Element):
247+
return parser.result
248+
249+
# Otherwise, raise a plugin error - if the author accidentally used
250+
# invalid HTML inside of the tag, e.g., forget a opening or closing
251+
# quote, we need to catch this here, as we're using pretty basic
252+
# regular expression based extraction
253+
raise PluginError(
254+
f"Could not parse due to possible syntax error in HTML: \n\n"
255+
+ fragment
256+
)
247257

248258
# Parse and extract all external assets from a media file using a preset
249259
# regular expression, and return all URLs found.

Diff for: src/plugins/privacy/plugin.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from hashlib import sha1
3434
from mkdocs.config.config_options import ExtraScriptValue
3535
from mkdocs.config.defaults import MkDocsConfig
36+
from mkdocs.exceptions import PluginError
3637
from mkdocs.plugins import BasePlugin, event_priority
3738
from mkdocs.structure.files import File, Files
3839
from mkdocs.utils import is_error_template
@@ -241,9 +242,18 @@ def _parse_fragment(self, fragment: str):
241242
parser.feed(fragment)
242243
parser.close()
243244

244-
# Return element
245-
assert isinstance(parser.result, Element)
246-
return parser.result
245+
# Check parse result and return element
246+
if isinstance(parser.result, Element):
247+
return parser.result
248+
249+
# Otherwise, raise a plugin error - if the author accidentally used
250+
# invalid HTML inside of the tag, e.g., forget a opening or closing
251+
# quote, we need to catch this here, as we're using pretty basic
252+
# regular expression based extraction
253+
raise PluginError(
254+
f"Could not parse due to possible syntax error in HTML: \n\n"
255+
+ fragment
256+
)
247257

248258
# Parse and extract all external assets from a media file using a preset
249259
# regular expression, and return all URLs found.

0 commit comments

Comments
 (0)