nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: Kozea/WeasyPrint
Pull request #1823 (2023-02-26 17:20:24)

Fix vertical positioning for absolute replaced elements
This PR fixes the calculation of vertical positions for absolute replaced elements showcased in this issue #1822 .


Patch:
diff --git a/weasyprint/layout/absolute.py b/weasyprint/layout/absolute.py
index 8bf14ea2..fc63679d 100644
--- a/weasyprint/layout/absolute.py
+++ b/weasyprint/layout/absolute.py
@@ -318,9 +318,9 @@ def absolute_replaced(context, box, cb_x, cb_y, cb_width, cb_height):
             box.margin_bottom = 0
         remaining = cb_height - box.margin_height()
         if box.top == 'auto':
-            box.top = remaining
+            box.top = remaining - box.bottom
         if box.bottom == 'auto':
-            box.bottom = remaining
+            box.bottom = remaining - box.top
     elif 'auto' in (box.margin_top, box.margin_bottom):
         remaining = cb_height - (box.border_height() + box.top + box.bottom)
         if box.margin_top == box.margin_bottom == 'auto':


Test patch:
diff --git a/tests/layout/test_position.py b/tests/layout/test_position.py
index e4126b62..87bd0df7 100644
--- a/tests/layout/test_position.py
+++ b/tests/layout/test_position.py
@@ -327,23 +327,30 @@ def test_absolute_positioning_8():
 def test_absolute_images():
     page, = render_pages('''
       <style>
+        @page { size: 50px; }
         img { display: block; position: absolute }
       </style>
       <div style="margin: 10px">
         <img src=pattern.png />
         <img src=pattern.png style="left: 15px" />
+        <img src=pattern.png style="top: 15px" />
+        <img src=pattern.png style="bottom: 25px" />
       </div>
     ''')
     html, = page.children
     body, = html.children
     div, = body.children
-    img1, img2 = div.children
+    img1, img2, img3, img4 = div.children
     assert div.height == 0
     assert (div.position_x, div.position_y) == (0, 0)
     assert (img1.position_x, img1.position_y) == (10, 10)
     assert (img1.width, img1.height) == (4, 4)
     assert (img2.position_x, img2.position_y) == (15, 10)
     assert (img2.width, img2.height) == (4, 4)
+    assert (img3.position_x, img3.position_y) == (10, 15)
+    assert (img3.width, img3.height) == (4, 4)
+    assert (img4.position_x, img4.position_y) == (10, 21)  # (50 - 4) - 25
+    assert (img4.width, img4.height) == (4, 4)
 
     # TODO: test the various cases in absolute_replaced()
Repository: Kozea/WeasyPrint
Pull request #2022 (2023-12-14 20:53:44)

Filter args with value None before calling html5lib.parse
Related to: https://github.com/Kozea/WeasyPrint/issues/2023


Patch:
diff --git a/weasyprint/__init__.py b/weasyprint/__init__.py
index 47aa905f..52094a37 100644
--- a/weasyprint/__init__.py
+++ b/weasyprint/__init__.py
@@ -162,10 +162,12 @@ class HTML:
             if isinstance(source, str):
                 result = html5lib.parse(source, namespaceHTMLElements=False)
             else:
-                result = html5lib.parse(
-                    source, override_encoding=encoding,
-                    transport_encoding=protocol_encoding,
-                    namespaceHTMLElements=False)
+                kwargs = {'namespaceHTMLElements': False}
+                if protocol_encoding is not None:
+                    kwargs['transport_encoding'] = protocol_encoding
+                if encoding is not None:
+                    kwargs['override_encoding'] = encoding
+                result = html5lib.parse(source, **kwargs)
         self.base_url = _find_base_url(result, base_url)
         self.url_fetcher = url_fetcher
         self.media_type = media_type
@@ -281,12 +283,12 @@ class CSS:
             base_url=base_url, url_fetcher=url_fetcher,
             check_css_mime_type=_check_mime_type)
         with result as (source_type, source, base_url, protocol_encoding):
-            if source_type == 'string' and not isinstance(source, bytes):
+            if source_type == 'file_obj':
+                source = source.read()
+            if isinstance(source, str):
                 # unicode, no encoding
                 stylesheet = tinycss2.parse_stylesheet(source)
             else:
-                if source_type == 'file_obj':
-                    source = source.read()
                 stylesheet, encoding = tinycss2.parse_stylesheet_bytes(
                     source, environment_encoding=encoding,
                     protocol_encoding=protocol_encoding)


Test patch:
diff --git a/tests/test_api.py b/tests/test_api.py
index 16def4b6..cc65249f 100644
--- a/tests/test_api.py
+++ b/tests/test_api.py
@@ -52,7 +52,9 @@ def _test_resource(class_, basename, check, **kwargs):
     check(class_(relative_path, **kwargs))
     kwargs.pop('base_url', None)
     check(class_(string=content, base_url=relative_filename, **kwargs))
-    encoding = kwargs.get('encoding') or 'utf-8'
+    encoding = kwargs.pop('encoding', 'utf-8')
+    with open(absolute_filename, 'r', encoding=encoding) as fd:
+        check(class_(file_obj=fd, **kwargs))
     check(class_(string=content.decode(encoding),  # unicode
                  base_url=relative_filename, **kwargs))
     with pytest.raises(TypeError):
Repository: Kozea/WeasyPrint
Pull request #2135 (2024-04-26 13:59:52)

Allow column-direction flex containers to use percentage-based heights.
Currently, if a `flex-direction: column` flex div is assigned a percentage-based height, that height is interpreted as pixels, not as a percentage of the containing block. This pull request allows percentage-based heights to work properly in this case.


Patch:
diff --git a/weasyprint/layout/flex.py b/weasyprint/layout/flex.py
index 540d55af..330538cf 100644
--- a/weasyprint/layout/flex.py
+++ b/weasyprint/layout/flex.py
@@ -241,7 +241,10 @@ def flex_layout(context, box, bottom_space, skip_stack, containing_block,
         block.block_level_width(box, containing_block)
     else:
         if box.style['height'] != 'auto':
-            box.height = box.style['height'].value
+            if box.style['height'].unit == '%':
+                box.height = box.style['height'].value / 100. * containing_block.height
+            else:
+                box.height = box.style['height'].value
         else:
             box.height = 0
             for i, child in enumerate(children):


Test patch:
diff --git a/tests/layout/test_flex.py b/tests/layout/test_flex.py
index f1a6154e..8ca76144 100644
--- a/tests/layout/test_flex.py
+++ b/tests/layout/test_flex.py
@@ -545,3 +545,21 @@ def test_flex_absolute():
       <div style="display: flex; position: absolute">
         <div>a</div>
       </div>''')
+
+
+@assert_no_logs
+def test_flex_percent_height():
+    page, = render_pages('''
+      <style>
+        .a { height: 10px; width: 10px; }
+        .b { height: 10%; width: 100%; display: flex; flex-direction: column; }
+      </style>
+      <div class="a"">
+        <div class="b"></div>
+      </div>''')
+    html, = page.children
+    body, = html.children
+    a, = body.children
+    b, = a.children
+    assert a.height == 10
+    assert b.height == 1
Repository: Kozea/WeasyPrint
Pull request #2314 (2024-11-26 15:29:20)

Use CSS 'image-rendering' attribute for images in SVGs
Currently the `image-rendering` attribute is not passed to the draw function. 
In the case of `image-rendering: pixelated;` this leads to blurry images inside SVGs if the defined size is larger than the source image.


Patch:
diff --git a/weasyprint/svg/images.py b/weasyprint/svg/images.py
index 4f0e88f4..c4878508 100644
--- a/weasyprint/svg/images.py
+++ b/weasyprint/svg/images.py
@@ -70,5 +70,7 @@ def image(svg, node, font_size):
     svg.stream.push_state()
     svg.stream.transform(a=scale_x, d=scale_y, e=translate_x, f=translate_y)
     image.draw(
-        svg.stream, intrinsic_width, intrinsic_height, image_rendering='auto')
+        svg.stream, intrinsic_width, intrinsic_height,
+        image_rendering=node.attrib.get('image-rendering', 'auto'),
+    )
     svg.stream.pop_state()


Test patch:
diff --git a/tests/draw/svg/test_images.py b/tests/draw/svg/test_images.py
index e16008b4..8002f2f4 100644
--- a/tests/draw/svg/test_images.py
+++ b/tests/draw/svg/test_images.py
@@ -257,6 +257,27 @@ def test_image_image(assert_pixels):
       </svg>
     ''' % path2url(resource_path('pattern.png')))
 
+@assert_no_logs
+def test_image_image_rendering(assert_pixels):
+    assert_pixels('''
+        rrBBBBBB
+        rrBBBBBB
+        BBBBBBBB
+        BBBBBBBB
+        BBBBBBBB
+        BBBBBBBB
+        BBBBBBBB
+        BBBBBBBB
+    ''', '''
+      <style>
+        @page { size: 8px 8px }
+        svg { display: block }
+      </style>
+      <svg width="8px" height="8px" xmlns="http://www.w3.org/2000/svg">
+        <image width="8px" height="8px" xlink:href="%s"
+            style="image-rendering:pixelated"/>
+      </svg>
+    ''' % path2url(resource_path('pattern.png')))
 
 def test_image_image_wrong(assert_pixels):
     assert_pixels('''