Skip to content

Commit afda275

Browse files
jakejackson1claude
andauthored
Write a select without a size as a combo box (#396) (#419)
* Write a select without a size as a combo box (#396) An active select got the combo box flag only when it carried a size below two, but Tag\Select keeps the size only when it is above one, so every select was written as a list box. HTML draws a select as a drop-down unless it is multiple or given two or more rows, and the flags now follow that rule. A bare multiple attribute was read as an empty string and so ignored; its presence now makes the select multi-select. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * Refresh the page-break-avoid-forms fixture for its selects now being combo boxes Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * Merge pull request #429 from GravityPDF/fix/select-bare-disabled Disable a select given a bare disabled attribute --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 41fd884 commit afda275

6 files changed

Lines changed: 158 additions & 9 deletions

File tree

‎src/Form.php‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ function print_ob_text($objattr, $w, $h, $texto, $rtlalign, $k, $blockdir)
175175

176176
if (!empty($objattr['disabled'])) {
177177
$flags[] = self::FLAG_NO_EXPORT;
178-
$objattr['color'] = [3, 128, 128, 128]; // gray out disabled
178+
$objattr['color'] = $this->colorConverter->convert(128, $this->mpdf->PDFAXwarnings); // gray out disabled
179179
}
180180

181181
if (!empty($objattr['required'])) {
@@ -279,7 +279,7 @@ function print_ob_textarea($objattr, $w, $h, $texto, $rtlalign, $k, $blockdir)
279279

280280
if (!empty($objattr['disabled'])) {
281281
$flags[] = self::FLAG_NO_EXPORT;
282-
$objattr['color'] = [3, 128, 128, 128]; // gray out disabled
282+
$objattr['color'] = $this->colorConverter->convert(128, $this->mpdf->PDFAXwarnings); // gray out disabled
283283
}
284284

285285
if (!empty($objattr['required'])) {
@@ -380,23 +380,26 @@ function print_ob_select($objattr, $w, $h, $texto, $rtlalign, $k, $blockdir)
380380
if (!empty($objattr['disabled'])) {
381381
$flags[] = self::FLAG_READONLY;
382382
$flags[] = self::FLAG_NO_EXPORT;
383-
$objattr['color'] = [3, 128, 128, 128]; // gray out disabled
383+
$objattr['color'] = $this->colorConverter->convert(128, $this->mpdf->PDFAXwarnings); // gray out disabled
384384
}
385385
if (!empty($objattr['required'])) {
386386
$flags[] = self::FLAG_REQUIRED;
387387
}
388-
if (!empty($objattr['multiple']) && isset($objattr['size']) && $objattr['size'] > 1) {
388+
// As in HTML, a select is a drop-down unless it is multiple or given a size of two or more rows
389+
$multiple = !empty($objattr['multiple']);
390+
$combo = !$multiple && (!isset($objattr['size']) || $objattr['size'] < 2);
391+
if ($multiple) {
389392
$flags[] = self::FLAG_MULTISELECT;
390393
}
391-
if (isset($objattr['size']) && $objattr['size'] < 2) {
394+
if ($combo) {
392395
$flags[] = self::FLAG_COMBOBOX;
393396
if (!empty($objattr['editable'])) {
394397
$flags[] = self::FLAG_EDITABLE;
395398
}
396399
}
397400

398401
// only allow spellcheck if combo and editable
399-
if ((!isset($objattr['spellcheck']) || !$objattr['spellcheck']) || (isset($objattr['size']) && $objattr['size'] > 1) || (!isset($objattr['editable']) || !$objattr['editable'])) {
402+
if (!$combo || empty($objattr['spellcheck']) || empty($objattr['editable'])) {
400403
$flags[] = self::FLAG_NO_SPELLCHECK;
401404
}
402405

@@ -514,7 +517,7 @@ function print_ob_button($objattr, $w, $h, $texto, $rtlalign, $k, $blockdir)
514517
if (!empty($objattr['disabled'])) {
515518
$flags[] = self::FLAG_READONLY;
516519
$flags[] = self::FLAG_NO_EXPORT;
517-
$objattr['color'] = [3, 128, 128, 128];
520+
$objattr['color'] = $this->colorConverter->convert(128, $this->mpdf->PDFAXwarnings);
518521
}
519522

520523
$this->mpdf->SetTColor(isset($objattr['color']) ? $objattr['color'] : $this->colorConverter->convert(0, $this->mpdf->PDFAXwarnings));

‎src/Tag/Select.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ public function close(&$ahtml, &$ihtml)
104104
$objattr['items'] = $this->mpdf->selectoption['ITEMS'];
105105
}
106106
if (isset($this->mpdf->selectoption['MULTIPLE'])) {
107-
$objattr['multiple'] = $this->mpdf->selectoption['MULTIPLE'];
107+
$objattr['multiple'] = true;
108108
}
109109
if (isset($this->mpdf->selectoption['DISABLED'])) {
110-
$objattr['disabled'] = $this->mpdf->selectoption['DISABLED'];
110+
$objattr['disabled'] = true;
111111
}
112112
if (isset($this->mpdf->selectoption['TITLE'])) {
113113
$objattr['title'] = $this->mpdf->selectoption['TITLE'];
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Mpdf;
4+
5+
/**
6+
* An active field that is disabled writes its text in mid grey
7+
*/
8+
class DisabledFieldColourTest extends \Yoast\PHPUnitPolyfills\TestCases\TestCase
9+
{
10+
use PageStreams;
11+
12+
/**
13+
* A disabled field of each kind that is greyed out
14+
*
15+
* @return string[][]
16+
*/
17+
public function disabledFields()
18+
{
19+
return [
20+
'text input' => ['<input type="text" name="field" value="A" disabled>'],
21+
'textarea' => ['<textarea name="field" disabled>A</textarea>'],
22+
'select' => ['<select name="field" disabled><option>A</option></select>'],
23+
'button' => ['<input type="button" name="field" value="A" disabled>'],
24+
];
25+
}
26+
27+
/**
28+
* @dataProvider disabledFields
29+
*
30+
* @param string $field
31+
*/
32+
public function testTheFieldTextIsGrey($field)
33+
{
34+
$pdf = $this->render('<form>' . $field . '</form>', ['useActiveForms' => true]);
35+
36+
$this->assertSame(1, preg_match('/\/T \(field\).*?\/DA \(([^)]*)\)/s', $pdf, $match), 'The field should carry a default appearance');
37+
$this->assertStringEndsWith('0.502 g', $match[1]);
38+
}
39+
}

‎tests/Mpdf/SelectDisabledTest.php‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Mpdf;
4+
5+
/**
6+
* A bare disabled attribute greys out a static select, just as disabled="disabled" does
7+
*/
8+
class SelectDisabledTest extends \Yoast\PHPUnitPolyfills\TestCases\TestCase
9+
{
10+
use PageStreams;
11+
12+
/**
13+
* The two ways of writing the attribute
14+
*
15+
* @return string[][]
16+
*/
17+
public function disabledAttributes()
18+
{
19+
return [
20+
'bare' => ['disabled'],
21+
'with a value' => ['disabled="disabled"'],
22+
];
23+
}
24+
25+
/**
26+
* @dataProvider disabledAttributes
27+
*
28+
* @param string $attribute
29+
*/
30+
public function testAStaticSelectIsGreyedOut($attribute)
31+
{
32+
$pages = $this->pages($this->render('<form><select name="choice" ' . $attribute . '><option>A</option></select></form>'));
33+
34+
$this->assertStringContainsString('0.882 g', $pages[0], 'The box should be filled light grey');
35+
$this->assertStringContainsString('0.498 g', $pages[0], 'The text should be grey');
36+
}
37+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace Mpdf;
4+
5+
/**
6+
* An active select is a combo box when HTML would draw it as a drop-down: neither multiple nor given a size of two
7+
* or more rows. A select with no size attribute has a size of one (GravityPDF/mpdf#396)
8+
*/
9+
class SelectFieldFlagsTest extends \Yoast\PHPUnitPolyfills\TestCases\TestCase
10+
{
11+
use PageStreams;
12+
13+
/**
14+
* The attributes of a select and the /Ff bits its field should carry
15+
*
16+
* @return mixed[]
17+
*/
18+
public function selects()
19+
{
20+
return [
21+
'no size' => ['', [Form::FLAG_COMBOBOX, Form::FLAG_NO_SPELLCHECK]],
22+
'a size of one' => ['size="1"', [Form::FLAG_COMBOBOX, Form::FLAG_NO_SPELLCHECK]],
23+
'a size of four' => ['size="4"', [Form::FLAG_NO_SPELLCHECK]],
24+
'multiple with no size' => ['multiple', [Form::FLAG_MULTISELECT, Form::FLAG_NO_SPELLCHECK]],
25+
'multiple with a size of three' => ['multiple size="3"', [Form::FLAG_MULTISELECT, Form::FLAG_NO_SPELLCHECK]],
26+
'editable' => ['editable', [Form::FLAG_COMBOBOX, Form::FLAG_EDITABLE, Form::FLAG_NO_SPELLCHECK]],
27+
'editable and spellchecked' => ['editable spellcheck="true"', [Form::FLAG_COMBOBOX, Form::FLAG_EDITABLE]],
28+
'spellchecked but not editable' => ['spellcheck="true"', [Form::FLAG_COMBOBOX, Form::FLAG_NO_SPELLCHECK]],
29+
'editable and spellchecked with a size of four' => ['editable spellcheck="true" size="4"', [Form::FLAG_NO_SPELLCHECK]],
30+
'disabled' => ['disabled', [Form::FLAG_READONLY, Form::FLAG_NO_EXPORT, Form::FLAG_COMBOBOX, Form::FLAG_NO_SPELLCHECK]],
31+
'disabled="disabled"' => ['disabled="disabled"', [Form::FLAG_READONLY, Form::FLAG_NO_EXPORT, Form::FLAG_COMBOBOX, Form::FLAG_NO_SPELLCHECK]],
32+
];
33+
}
34+
35+
/**
36+
* @dataProvider selects
37+
*
38+
* @param string $attributes
39+
* @param int[] $bits
40+
*/
41+
public function testTheFieldFlagsFollowTheSelectsAttributes($attributes, $bits)
42+
{
43+
$pdf = $this->render(
44+
'<form><select name="choice" ' . $attributes . '><option>A</option><option selected>B</option></select></form>',
45+
['useActiveForms' => true]
46+
);
47+
48+
$this->assertSame(1, preg_match('/\/FT \/Ch\b.*?\/Ff (\d+)/s', $pdf, $match), 'The select should be written as a choice field');
49+
$this->assertSame($bits, $this->bits((int) $match[1]));
50+
}
51+
52+
/**
53+
* The 1-based positions of the bits set in $flags, lowest first
54+
*
55+
* @param int $flags
56+
*
57+
* @return int[]
58+
*/
59+
private function bits($flags)
60+
{
61+
$bits = [];
62+
for ($bit = 1; $flags; $bit++, $flags >>= 1) {
63+
if ($flags & 1) {
64+
$bits[] = $bit;
65+
}
66+
}
67+
68+
return $bits;
69+
}
70+
}
0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)