Skip to content

Commit

Permalink
fix: skip bad data uri images on parsing
Browse files Browse the repository at this point in the history
fix #36
  • Loading branch information
vzhd1701 committed Jun 14, 2022
1 parent 0ccf879 commit 2b6060a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 5 additions & 1 deletion enex2notion/note_parser/elements/media.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ def parse_img(element: Tag):
url=src,
)

img_resource = _parse_img_resource(src)
try:
img_resource = _parse_img_resource(src)
except ValueError:
logger.warning(f"Failed to parse image: '{src}'")
return None

# Make SVG small by default to avoid them spreading too much
if "svg" in img_resource.mime and not any((w, h)):
Expand Down
15 changes: 15 additions & 0 deletions tests/test_note_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,21 @@ def test_embedded_inline_svg_no_size(parse_html, smallest_svg):
)


def test_embedded_inline_bad(parse_html, smallest_svg, caplog):
test_note = parse_html(
f"<img "
'src="data:image/svg+xml;utf8,'
"<svg xmlns='http://www.w3.org/2000/svg' width='416' height='377'>"
'</svg/>" />'
)

with caplog.at_level(logging.WARNING, logger="enex2notion"):
result_block = parse_note_blocks(test_note)

assert result_block == []
assert "Failed to parse image" in caplog.text


def test_embedded_inline_img_url(parse_html):
test_note = parse_html('<img src="https://google.com/image.jpg" />')

Expand Down

0 comments on commit 2b6060a

Please sign in to comment.