Skip to content

Commit 3a011e0

Browse files
jakejackson1claude
andauthored
Leave an active button with an empty value blank instead of showing its field name (#459) (#489)
* Leave an active button with an empty value blank instead of showing its field name A submit, reset or script button with value="" wrote its field name as its /MK /CA caption, and its appearance drew the name as well, while the same button drawn without active forms, and in a browser, has no caption. The caption is now written as given, even when empty. A button whose caption was never set, such as an image button, still falls back to its field name. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * Add a snapshot of active push buttons with an empty value Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 2f557bc commit 3a011e0

4 files changed

Lines changed: 208 additions & 11 deletions

File tree

‎src/Form.php‎

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,6 +1333,25 @@ private function unnamedButtonName($kind)
13331333
return $kind . '_' . ($this->formCount + 1);
13341334
}
13351335

1336+
/**
1337+
* A push button's /MK captions: normal, rollover and down. A caption set by SetFormButtonText() is written even
1338+
* when empty, as a browser leaves a button with an empty value blank. A button given none, such as an image
1339+
* button, is captioned with its field name.
1340+
*
1341+
* @param array $form
1342+
*
1343+
* @return string
1344+
*/
1345+
private function buttonCaptions($form)
1346+
{
1347+
$captions = '';
1348+
foreach (['CA', 'RC', 'AC'] as $key) {
1349+
$captions .= '/' . $key . ' ' . $this->writer->string($form[$key] === null ? $form['T'] : $form[$key]) . ' ';
1350+
}
1351+
1352+
return $captions;
1353+
}
1354+
13361355
function SetFormButtonText($ca, $rc = '', $ac = '')
13371356
{
13381357
if ($this->mpdf->onlyCoreFonts) {
@@ -1375,8 +1394,9 @@ function SetFormButton($bb, $hh, $name, $value, $type, $title = '', $flags = [],
13751394
$border += ['W' => $this->form_button_border_width, 'S' => $this->form_button_border_style];
13761395
$appearance = null;
13771396
if ($type !== 'radio' && $type !== 'checkbox') {
1378-
// A button showing an icon draws no caption to fit
1379-
$appearance = $this->appearanceText($bb, $hh, $border['W'], [$value === '' ? $name : $value], '1', 'line', [], !isset($this->form_button_icon[$this->formCount]));
1397+
// Matches the caption buttonCaptions() writes. A button showing an icon draws no caption to fit
1398+
$caption = $value === '' && $this->form_button_text === null ? $name : $value;
1399+
$appearance = $this->appearanceText($bb, $hh, $border['W'], [$caption], '1', 'line', [], !isset($this->form_button_icon[$this->formCount]));
13801400
}
13811401
if (!$this->mpdf->onlyCoreFonts) {
13821402
if (isset($this->mpdf->CurrentFont['subset'])) {
@@ -2196,9 +2216,7 @@ function _putform_bt($form, $hPt)
21962216
}
21972217

21982218
if ($form['subtype'] === 'reset') {
2199-
$temp .= $form['CA'] ? '/CA ' . $this->writer->string($form['CA']) . ' ' : '/CA ' . $this->writer->string($form['T']) . ' ';
2200-
$temp .= $form['RC'] ? '/RC ' . $this->writer->string($form['RC']) . ' ' : '/RC ' . $this->writer->string($form['T']) . ' ';
2201-
$temp .= $form['AC'] ? '/AC ' . $this->writer->string($form['AC']) . ' ' : '/AC ' . $this->writer->string($form['T']) . ' ';
2219+
$temp .= $this->buttonCaptions($form);
22022220
$this->writer->write("/BS << $bstemp >>");
22032221
$this->writer->write('/MK << ' . $temp . ' >>');
22042222
$this->writer->write('/DA ' . $this->writer->string('/F' . $this->mpdf->fonts[$form['style']['font']]['i'] . ' ' . $form['style']['fontsize'] . ' Tf ' . $form['style']['fontcolor']));
@@ -2211,9 +2229,7 @@ function _putform_bt($form, $hPt)
22112229

22122230
if ($form['subtype'] === 'submit') {
22132231

2214-
$temp .= $form['CA'] ? '/CA ' . $this->writer->string($form['CA']) . ' ' : '/CA ' . $this->writer->string($form['T']) . ' ';
2215-
$temp .= $form['RC'] ? '/RC ' . $this->writer->string($form['RC']) . ' ' : '/RC ' . $this->writer->string($form['T']) . ' ';
2216-
$temp .= $form['AC'] ? '/AC ' . $this->writer->string($form['AC']) . ' ' : '/AC ' . $this->writer->string($form['T']) . ' ';
2232+
$temp .= $this->buttonCaptions($form);
22172233
$this->writer->write("/BS << $bstemp >>");
22182234
$this->writer->write("/MK << $temp >>");
22192235
$this->writer->write('/DA ' . $this->writer->string('/F' . $this->mpdf->fonts[$form['style']['font']]['i'] . ' ' . $form['style']['fontsize'] . ' Tf ' . $form['style']['fontcolor']));
@@ -2255,9 +2271,7 @@ function _putform_bt($form, $hPt)
22552271
}
22562272
$put_icon = 1;
22572273
}
2258-
$temp .= $form['CA'] ? '/CA ' . $this->writer->string($form['CA']) . ' ' : '/CA ' . $this->writer->string($form['T']) . ' ';
2259-
$temp .= $form['RC'] ? '/RC ' . $this->writer->string($form['RC']) . ' ' : '/RC ' . $this->writer->string($form['T']) . ' ';
2260-
$temp .= $form['AC'] ? '/AC ' . $this->writer->string($form['AC']) . ' ' : '/AC ' . $this->writer->string($form['T']) . ' ';
2274+
$temp .= $this->buttonCaptions($form);
22612275
$this->writer->write("/BS << $bstemp >>");
22622276
$this->writer->write("/MK << $temp >>");
22632277
$this->writer->write('/DA ' . $this->writer->string('/F' . $this->mpdf->fonts[$form['style']['font']]['i'] . ' ' . $form['style']['fontsize'] . ' Tf ' . $form['style']['fontcolor']));

‎tests/Mpdf/ButtonCaptionTest.php‎

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
3+
namespace Mpdf;
4+
5+
/**
6+
* A push button shows its value as its caption whether forms are active or not, as a browser does: an empty value
7+
* leaves it blank, and no value at all gives it the browser's default caption. Its field name is never shown.
8+
*/
9+
class ButtonCaptionTest extends \Yoast\PHPUnitPolyfills\TestCases\TestCase
10+
{
11+
12+
use PageStreams;
13+
14+
/**
15+
* Each kind of push button with an empty value, with no value, and with a value, and the caption it shows
16+
*
17+
* @return string[][]
18+
*/
19+
public function buttons()
20+
{
21+
return [
22+
'submit with an empty value' => ['<input type="submit" name="save_and_continue" value="" />', ''],
23+
'reset with an empty value' => ['<input type="reset" name="clear_form" value="" />', ''],
24+
'button with an empty value' => ['<input type="button" name="run_script" value="" onclick="app.alert(1)" />', ''],
25+
'unnamed submit with an empty value' => ['<input type="submit" value="" />', ''],
26+
'submit with no value' => ['<input type="submit" name="save_and_continue" />', 'Submit'],
27+
'reset with no value' => ['<input type="reset" name="clear_form" />', 'Reset'],
28+
'button with no value' => ['<input type="button" name="run_script" onclick="app.alert(1)" />', 'Button'],
29+
'submit with a value' => ['<input type="submit" name="save_and_continue" value="Save" />', 'Save'],
30+
'reset with a value' => ['<input type="reset" name="clear_form" value="Clear" />', 'Clear'],
31+
'button with a value' => ['<input type="button" name="run_script" value="Run" onclick="app.alert(1)" />', 'Run'],
32+
];
33+
}
34+
35+
/**
36+
* An active button's normal, rollover and down captions are all its caption
37+
*
38+
* @dataProvider buttons
39+
*
40+
* @param string $button
41+
* @param string $caption
42+
*/
43+
public function testActiveButtonIsCaptionedWithItsValue($button, $caption)
44+
{
45+
$widget = $this->widget($this->render('<form>' . $button . '</form>', ['mode' => 'c', 'useActiveForms' => true]));
46+
47+
$this->assertStringContainsString('/CA (' . $caption . ') /RC (' . $caption . ') /AC (' . $caption . ')', $widget);
48+
}
49+
50+
/**
51+
* An active button's appearance draws the caption a viewer would draw from /CA
52+
*
53+
* @dataProvider buttons
54+
*
55+
* @param string $button
56+
* @param string $caption
57+
*/
58+
public function testActiveButtonAppearanceDrawsItsCaption($button, $caption)
59+
{
60+
$pdf = $this->render('<form>' . $button . '</form>', ['mode' => 'c', 'useActiveForms' => true]);
61+
62+
$this->assertSame(1, preg_match('/\/AP << \/N << \/Push (\d+) 0 R/', $this->widget($pdf), $appearance));
63+
$this->assertSame($caption, $this->drawnText($this->object($pdf, $appearance[1])));
64+
}
65+
66+
/**
67+
* A button drawn on the page when forms are not active shows the same caption
68+
*
69+
* @dataProvider buttons
70+
*
71+
* @param string $button
72+
* @param string $caption
73+
*/
74+
public function testStaticButtonDrawsItsCaption($button, $caption)
75+
{
76+
$contents = $this->pageContents($this->render('<form>' . $button . '</form>', ['mode' => 'c']));
77+
78+
$this->assertSame($caption, trim($this->drawnText($contents[0])));
79+
}
80+
81+
/**
82+
* With an embedded font an empty caption is an empty UTF-16 string, and the appearance draws no text
83+
*/
84+
public function testEmptyCaptionInAnEmbeddedFont()
85+
{
86+
$pdf = $this->render('<form><input type="submit" name="save_and_continue" value="" /></form>', ['mode' => 'utf-8', 'useActiveForms' => true]);
87+
$widget = $this->widget($pdf);
88+
89+
$this->assertStringContainsString("/CA (\xFE\xFF) ", $widget);
90+
$this->assertSame(1, preg_match('/\/AP << \/N << \/Push (\d+) 0 R/', $widget, $appearance));
91+
$this->assertSame('', $this->drawnText($this->object($pdf, $appearance[1])));
92+
}
93+
94+
/**
95+
* An image button shows its icon and no caption, and keeps its field name as /CA
96+
*/
97+
public function testImageButtonKeepsItsName()
98+
{
99+
$widget = $this->widget($this->render(
100+
'<form><input type="image" name="go" src="' . $this->pngImage() . '" onclick="app.alert(1)" /></form>',
101+
['mode' => 'c', 'useActiveForms' => true]
102+
));
103+
104+
$this->assertStringContainsString('/TP 1 ', $widget);
105+
$this->assertStringContainsString('/CA (go) /RC (go) /AC (go)', $widget);
106+
}
107+
108+
/**
109+
* The only widget on the first page
110+
*
111+
* @param string $pdf
112+
*
113+
* @return string
114+
*/
115+
private function widget($pdf)
116+
{
117+
$refs = $this->annotationRefs($pdf);
118+
$this->assertCount(1, $refs[0]);
119+
120+
return $this->object($pdf, $refs[0][0]);
121+
}
122+
123+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace Snapshots;
4+
5+
/**
6+
* Active submit, reset and script buttons with an empty value, no value, a value and a value of "0". A button with an
7+
* empty value is drawn blank, as a browser draws it, rather than captioned with its field name (#459).
8+
*
9+
* @group snapshot
10+
*/
11+
class EmptyButtonCaptionSnapshotTest extends Snapshot
12+
{
13+
14+
/**
15+
* @return string
16+
*/
17+
public function getId()
18+
{
19+
return 'empty-button-caption';
20+
}
21+
22+
/**
23+
* A table with a row per button type and a column per value
24+
*/
25+
public function generatePdf()
26+
{
27+
$values = [
28+
'value=""' => ' value=""',
29+
'No value' => '',
30+
'value="Send"' => ' value="Send"',
31+
'value="0"' => ' value="0"',
32+
];
33+
34+
$html = '<style>td, th { border: 0.2mm solid #808080; padding: 2mm; }</style>'
35+
. '<h2>Active push buttons</h2>'
36+
. '<p>A button with an empty value has no caption. Its field name is never shown.</p>'
37+
. '<form><table><tr><th></th>';
38+
39+
foreach (array_keys($values) as $label) {
40+
$html .= '<th>' . htmlspecialchars($label) . '</th>';
41+
}
42+
$html .= '</tr>';
43+
44+
foreach (['submit', 'reset', 'button'] as $type) {
45+
$html .= '<tr><th>' . $type . '</th>';
46+
$i = 0;
47+
foreach ($values as $attribute) {
48+
$html .= '<td><input type="' . $type . '" name="' . $type . '_name_' . $i++ . '"' . $attribute . ' /></td>';
49+
}
50+
$html .= '</tr>';
51+
}
52+
53+
$html .= '<tr><th>unnamed submit</th><td><input type="submit" value="" /></td><td><input type="submit" /></td>'
54+
. '<td></td><td></td></tr></table></form>';
55+
56+
$this->mpdf = $this->createMpdf(['useActiveForms' => true]);
57+
$this->mpdf->WriteHTML($html);
58+
}
59+
60+
}
57.8 KB
Binary file not shown.

0 commit comments

Comments
 (0)