Skip to content

Commit

Permalink
img: Support null "width" and "height" attributes, rather than assert.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmv committed Jan 16, 2024
1 parent 3421b62 commit 32f1c60
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
6 changes: 2 additions & 4 deletions html2text/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,11 +548,9 @@ def link_url(self: HTML2Text, link: str, title: str = "") -> None:
self.images_with_size and ("width" in attrs or "height" in attrs)
):
self.o("<img src='" + attrs["src"] + "' ")
if "width" in attrs:
assert attrs["width"] is not None
if "width" in attrs and attrs["width"] is not None:
self.o("width='" + attrs["width"] + "' ")
if "height" in attrs:
assert attrs["height"] is not None
if "height" in attrs and attrs["height"] is not None:
self.o("height='" + attrs["height"] + "' ")
if alt:
self.o("alt='" + alt + "' ")
Expand Down
4 changes: 4 additions & 0 deletions test/images_with_size.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@
<img src='image_with_width_and_height.jpg' width='300' height='300' id='ignored-id' />
<img src='image_with_width_and_height.jpg' id='ignored-id' />
<img id='ignored-id' />

<img src='image_with_no_width_value.jpg' width height='123' id='ignored-id' />
<img src='image_with_no_height_value.jpg' width='123' height id='ignored-id' />
<img src='image_with_no_dimention_values.jpg' width height id='ignored-id' />
5 changes: 4 additions & 1 deletion test/images_with_size.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ src='image_with_width.jpg' width='300' alt='An image with a width attr' />
<img src='image_with_width.jpg' height='300' alt='An image with a height attr'
/> <img src='image_with_width_and_height.jpg' width='300' height='300' alt='An
image with width and height' /> <img src='image_with_width_and_height.jpg'
width='300' height='300' /> ![](image_with_width_and_height.jpg)
width='300' height='300' /> ![](image_with_width_and_height.jpg) <img
src='image_with_no_width_value.jpg' height='123' /> <img
src='image_with_no_height_value.jpg' width='123' /> <img
src='image_with_no_dimention_values.jpg' />

0 comments on commit 32f1c60

Please sign in to comment.