Skip to content

Commit

Permalink
Improved resilience of privacy plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
squidfunk committed Mar 31, 2024
1 parent 6c9ba87 commit 29cf44b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
16 changes: 13 additions & 3 deletions material/plugins/privacy/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from hashlib import sha1
from mkdocs.config.config_options import ExtraScriptValue
from mkdocs.config.defaults import MkDocsConfig
from mkdocs.exceptions import PluginError
from mkdocs.plugins import BasePlugin, event_priority
from mkdocs.structure.files import File, Files
from mkdocs.utils import is_error_template
Expand Down Expand Up @@ -241,9 +242,18 @@ def _parse_fragment(self, fragment: str):
parser.feed(fragment)
parser.close()

# Return element
assert isinstance(parser.result, Element)
return parser.result
# Check parse result and return element
if isinstance(parser.result, Element):
return parser.result

# Otherwise, raise a plugin error - if the author accidentally used
# invalid HTML inside of the tag, e.g., forget a opening or closing
# quote, we need to catch this here, as we're using pretty basic
# regular expression based extraction
raise PluginError(
f"Could not parse due to possible syntax error in HTML: \n\n"
+ fragment
)

# Parse and extract all external assets from a media file using a preset
# regular expression, and return all URLs found.
Expand Down
16 changes: 13 additions & 3 deletions src/plugins/privacy/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from hashlib import sha1
from mkdocs.config.config_options import ExtraScriptValue
from mkdocs.config.defaults import MkDocsConfig
from mkdocs.exceptions import PluginError
from mkdocs.plugins import BasePlugin, event_priority
from mkdocs.structure.files import File, Files
from mkdocs.utils import is_error_template
Expand Down Expand Up @@ -241,9 +242,18 @@ def _parse_fragment(self, fragment: str):
parser.feed(fragment)
parser.close()

# Return element
assert isinstance(parser.result, Element)
return parser.result
# Check parse result and return element
if isinstance(parser.result, Element):
return parser.result

# Otherwise, raise a plugin error - if the author accidentally used
# invalid HTML inside of the tag, e.g., forget a opening or closing
# quote, we need to catch this here, as we're using pretty basic
# regular expression based extraction
raise PluginError(
f"Could not parse due to possible syntax error in HTML: \n\n"
+ fragment
)

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

0 comments on commit 29cf44b

Please sign in to comment.