fix missing images in paras
This commit is contained in:
@@ -7,7 +7,7 @@ including styled content within paragraphs and block-level elements.
|
||||
|
||||
import unittest
|
||||
from pyWebLayout.io.readers.html_extraction import parse_html_string
|
||||
from pyWebLayout.abstract.block import Paragraph, Heading, HeadingLevel, Quote, CodeBlock, HList, ListStyle, Table
|
||||
from pyWebLayout.abstract.block import Paragraph, Heading, HeadingLevel, Quote, CodeBlock, HList, ListStyle, Table, Image
|
||||
from pyWebLayout.abstract.document import Document
|
||||
from pyWebLayout.style import Font, FontWeight, FontStyle, TextDecoration
|
||||
|
||||
@@ -585,5 +585,103 @@ class TestHTMLFontRegistryIntegration(unittest.TestCase):
|
||||
"Should create separate styles for style combinations")
|
||||
|
||||
|
||||
class TestHTMLImagesInParagraphs(unittest.TestCase):
|
||||
"""Test cases for handling images inside paragraph tags."""
|
||||
|
||||
def setUp(self):
|
||||
"""Set up test fixtures."""
|
||||
self.base_font = Font(font_size=14)
|
||||
|
||||
def test_image_only_paragraph(self):
|
||||
"""Test paragraph containing only an image (common in EPUBs)."""
|
||||
html = '<p><img src="cover.jpg" alt="Book Cover"/></p>'
|
||||
blocks = parse_html_string(html, base_font=self.base_font)
|
||||
|
||||
# Should parse as an Image block, not a Paragraph
|
||||
self.assertGreater(len(blocks), 0, "Should parse at least one block")
|
||||
|
||||
# Check that we have an Image block
|
||||
image_blocks = [b for b in blocks if isinstance(b, Image)]
|
||||
self.assertGreater(len(image_blocks), 0, "Should have at least one Image block")
|
||||
|
||||
# Verify image properties
|
||||
img = image_blocks[0]
|
||||
self.assertEqual(img.source, "cover.jpg")
|
||||
self.assertEqual(img.alt_text, "Book Cover")
|
||||
|
||||
def test_paragraph_with_multiple_images(self):
|
||||
"""Test paragraph with multiple images."""
|
||||
html = '<p><img src="img1.jpg" alt="First"/><img src="img2.jpg" alt="Second"/></p>'
|
||||
blocks = parse_html_string(html, base_font=self.base_font)
|
||||
|
||||
# Should have multiple Image blocks
|
||||
image_blocks = [b for b in blocks if isinstance(b, Image)]
|
||||
self.assertEqual(len(image_blocks), 2, "Should have two Image blocks")
|
||||
|
||||
# Verify both images were parsed
|
||||
sources = [img.source for img in image_blocks]
|
||||
self.assertIn("img1.jpg", sources)
|
||||
self.assertIn("img2.jpg", sources)
|
||||
|
||||
def test_paragraph_with_text_and_image(self):
|
||||
"""Test paragraph with mixed text and image content."""
|
||||
html = '<p>Some text before <img src="inline.jpg" alt="Inline"/> and after</p>'
|
||||
blocks = parse_html_string(html, base_font=self.base_font)
|
||||
|
||||
# Should have both paragraph and image blocks
|
||||
paragraphs = [b for b in blocks if isinstance(b, Paragraph)]
|
||||
images = [b for b in blocks if isinstance(b, Image)]
|
||||
|
||||
self.assertGreater(len(paragraphs), 0, "Should have a Paragraph block for text")
|
||||
self.assertGreater(len(images), 0, "Should have an Image block")
|
||||
|
||||
# Verify image was parsed
|
||||
self.assertEqual(images[0].source, "inline.jpg")
|
||||
|
||||
# Verify text was extracted (should have words like "Some", "text", etc.)
|
||||
if paragraphs:
|
||||
words = list(paragraphs[0].words_iter())
|
||||
self.assertGreater(len(words), 0, "Paragraph should have words")
|
||||
|
||||
def test_regular_paragraph_still_works(self):
|
||||
"""Test that regular paragraphs without images still work correctly."""
|
||||
html = '<p>Just regular text without any images.</p>'
|
||||
blocks = parse_html_string(html, base_font=self.base_font)
|
||||
|
||||
# Should be exactly one Paragraph block
|
||||
self.assertEqual(len(blocks), 1, "Should have exactly one block")
|
||||
self.assertIsInstance(blocks[0], Paragraph, "Should be a Paragraph block")
|
||||
|
||||
# Should not have any Image blocks
|
||||
image_blocks = [b for b in blocks if isinstance(b, Image)]
|
||||
self.assertEqual(len(image_blocks), 0, "Should have no Image blocks")
|
||||
|
||||
def test_image_with_width_and_height(self):
|
||||
"""Test image parsing with width and height attributes."""
|
||||
html = '<p><img src="sized.jpg" alt="Sized Image" width="400" height="300"/></p>'
|
||||
blocks = parse_html_string(html, base_font=self.base_font)
|
||||
|
||||
# Should have an Image block
|
||||
image_blocks = [b for b in blocks if isinstance(b, Image)]
|
||||
self.assertEqual(len(image_blocks), 1, "Should have one Image block")
|
||||
|
||||
# Verify dimensions were parsed
|
||||
img = image_blocks[0]
|
||||
self.assertEqual(img.width, 400)
|
||||
self.assertEqual(img.height, 300)
|
||||
|
||||
def test_nested_paragraph_with_image_in_span(self):
|
||||
"""Test image inside nested inline elements."""
|
||||
html = '<p><span><img src="nested.jpg" alt="Nested"/></span></p>'
|
||||
blocks = parse_html_string(html, base_font=self.base_font)
|
||||
|
||||
# Should still extract the image
|
||||
image_blocks = [b for b in blocks if isinstance(b, Image)]
|
||||
self.assertGreater(len(image_blocks), 0, "Should find image even when nested")
|
||||
|
||||
# Verify image was parsed correctly
|
||||
self.assertEqual(image_blocks[0].source, "nested.jpg")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user