Skip to content

Commit 0942d26

Browse files
jakejackson1claude
andcommitted
Lay out a multiple select with no size four rows tall
HTML draws a multiple select without a size as a list box four rows tall, but Tag\Select kept a size only when it was above one, so the box was laid out one row tall in both static and active forms. A multiple select whose size is missing or below one now takes a size of four; an explicit size is kept as given. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent f9d3db7 commit 0942d26

2 files changed

Lines changed: 117 additions & 2 deletions

File tree

‎src/Tag/Select.php‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,12 @@ public function open($attr, &$ahtml, &$ihtml)
4545
if (isset($attr['MULTIPLE'])) {
4646
$this->mpdf->selectoption['MULTIPLE'] = $attr['MULTIPLE'];
4747
}
48-
if (isset($attr['SIZE']) && $attr['SIZE'] > 1) {
49-
$this->mpdf->selectoption['SIZE'] = $attr['SIZE'];
48+
$size = isset($attr['SIZE']) ? (int) $attr['SIZE'] : 0;
49+
if ($size < 1 && isset($attr['MULTIPLE'])) {
50+
$size = 4; // as in HTML, a multiple select with no size is a list box four rows tall
51+
}
52+
if ($size > 1) {
53+
$this->mpdf->selectoption['SIZE'] = $size;
5054
}
5155
if ($this->mpdf->useActiveForms) {
5256
if (isset($attr['NAME'])) {

‎tests/Mpdf/SelectRowsTest.php‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
3+
namespace Mpdf;
4+
5+
/**
6+
* A select is laid out as many rows tall as HTML would draw it: a multiple select with no size is a list box four
7+
* rows tall, and an explicit size is kept (GravityPDF/mpdf#425)
8+
*/
9+
class SelectRowsTest extends \Yoast\PHPUnitPolyfills\TestCases\TestCase
10+
{
11+
use PageStreams;
12+
13+
/**
14+
* A select's attributes, the attributes of a select it should be as tall as, and whether forms are active
15+
*
16+
* @return mixed[]
17+
*/
18+
public function selects()
19+
{
20+
$selects = [
21+
'multiple with no size' => ['multiple', 'size="4"'],
22+
'multiple="multiple" with no size' => ['multiple="multiple"', 'size="4"'],
23+
'multiple with a size of zero' => ['multiple size="0"', 'size="4"'],
24+
'multiple with a size of one' => ['multiple size="1"', ''],
25+
'multiple with a size of two' => ['multiple size="2"', 'size="2"'],
26+
'multiple with a size of six' => ['multiple size="6"', 'size="6"'],
27+
'no size' => ['', 'size="1"'],
28+
];
29+
30+
$cases = [];
31+
foreach ($selects as $name => $select) {
32+
$cases[$name . ', static'] = [$select[0], $select[1], false];
33+
$cases[$name . ', active'] = [$select[0], $select[1], true];
34+
}
35+
36+
return $cases;
37+
}
38+
39+
/**
40+
* @dataProvider selects
41+
*
42+
* @param string $attributes
43+
* @param string $sameAs
44+
* @param bool $active
45+
*/
46+
public function testASelectIsAsTallAsTheRowsHtmlGivesIt($attributes, $sameAs, $active)
47+
{
48+
$this->assertEqualsWithDelta($this->height($sameAs, $active), $this->height($attributes, $active), 0.001);
49+
}
50+
51+
/**
52+
* A multiple select with no size is taller than a drop-down in both modes
53+
*/
54+
public function testAMultipleSelectWithNoSizeIsTallerThanADropDown()
55+
{
56+
$this->assertGreaterThan($this->height('', false) + 1, $this->height('multiple', false));
57+
$this->assertGreaterThan($this->height('', true) + 1, $this->height('multiple', true));
58+
}
59+
60+
/**
61+
* An active multiple select with no size gets a widget four rows tall
62+
*/
63+
public function testAnActiveMultipleSelectWithNoSizeHasAWidgetFourRowsTall()
64+
{
65+
$this->assertEqualsWithDelta($this->widgetHeight('size="4"'), $this->widgetHeight('multiple'), 0.001);
66+
}
67+
68+
/**
69+
* How far a line holding only the select advances the page, in mm
70+
*
71+
* @param string $attributes
72+
* @param bool $active
73+
*
74+
* @return float
75+
*/
76+
private function height($attributes, $active)
77+
{
78+
$mpdf = $this->mpdf(['useActiveForms' => $active]);
79+
$top = $mpdf->y;
80+
$mpdf->WriteHTML($this->select($attributes));
81+
82+
return $mpdf->y - $top;
83+
}
84+
85+
/**
86+
* The height of the active select's widget /Rect, in points
87+
*
88+
* @param string $attributes
89+
*
90+
* @return float
91+
*/
92+
private function widgetHeight($attributes)
93+
{
94+
$pdf = $this->render($this->select($attributes), ['useActiveForms' => true]);
95+
$this->assertSame(1, preg_match('/\/Rect \[\s*[\d.]+ ([\d.]+) [\d.]+ ([\d.]+)\s*\][^>]*?\/FT \/Ch\b/s', $pdf, $rect), 'The select should be written as a choice field');
96+
97+
return $rect[2] - $rect[1];
98+
}
99+
100+
/**
101+
* A form holding one select with two options
102+
*
103+
* @param string $attributes
104+
*
105+
* @return string
106+
*/
107+
private function select($attributes)
108+
{
109+
return '<form><select name="choice" ' . $attributes . '><option>A</option><option>B</option></select></form>';
110+
}
111+
}

0 commit comments

Comments
 (0)