Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions src/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -1333,6 +1333,25 @@ private function unnamedButtonName($kind)
return $kind . '_' . ($this->formCount + 1);
}

/**
* A push button's /MK captions: normal, rollover and down. A caption set by SetFormButtonText() is written even
* when empty, as a browser leaves a button with an empty value blank. A button given none, such as an image
* button, is captioned with its field name.
*
* @param array $form
*
* @return string
*/
private function buttonCaptions($form)
{
$captions = '';
foreach (['CA', 'RC', 'AC'] as $key) {
$captions .= '/' . $key . ' ' . $this->writer->string($form[$key] === null ? $form['T'] : $form[$key]) . ' ';
}

return $captions;
}

function SetFormButtonText($ca, $rc = '', $ac = '')
{
if ($this->mpdf->onlyCoreFonts) {
Expand Down Expand Up @@ -1375,8 +1394,9 @@ function SetFormButton($bb, $hh, $name, $value, $type, $title = '', $flags = [],
$border += ['W' => $this->form_button_border_width, 'S' => $this->form_button_border_style];
$appearance = null;
if ($type !== 'radio' && $type !== 'checkbox') {
// A button showing an icon draws no caption to fit
$appearance = $this->appearanceText($bb, $hh, $border['W'], [$value === '' ? $name : $value], '1', 'line', [], !isset($this->form_button_icon[$this->formCount]));
// Matches the caption buttonCaptions() writes. A button showing an icon draws no caption to fit
$caption = $value === '' && $this->form_button_text === null ? $name : $value;
$appearance = $this->appearanceText($bb, $hh, $border['W'], [$caption], '1', 'line', [], !isset($this->form_button_icon[$this->formCount]));
}
if (!$this->mpdf->onlyCoreFonts) {
if (isset($this->mpdf->CurrentFont['subset'])) {
Expand Down Expand Up @@ -2196,9 +2216,7 @@ function _putform_bt($form, $hPt)
}

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

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

$temp .= $form['CA'] ? '/CA ' . $this->writer->string($form['CA']) . ' ' : '/CA ' . $this->writer->string($form['T']) . ' ';
$temp .= $form['RC'] ? '/RC ' . $this->writer->string($form['RC']) . ' ' : '/RC ' . $this->writer->string($form['T']) . ' ';
$temp .= $form['AC'] ? '/AC ' . $this->writer->string($form['AC']) . ' ' : '/AC ' . $this->writer->string($form['T']) . ' ';
$temp .= $this->buttonCaptions($form);
$this->writer->write("/BS << $bstemp >>");
$this->writer->write("/MK << $temp >>");
$this->writer->write('/DA ' . $this->writer->string('/F' . $this->mpdf->fonts[$form['style']['font']]['i'] . ' ' . $form['style']['fontsize'] . ' Tf ' . $form['style']['fontcolor']));
Expand Down Expand Up @@ -2255,9 +2271,7 @@ function _putform_bt($form, $hPt)
}
$put_icon = 1;
}
$temp .= $form['CA'] ? '/CA ' . $this->writer->string($form['CA']) . ' ' : '/CA ' . $this->writer->string($form['T']) . ' ';
$temp .= $form['RC'] ? '/RC ' . $this->writer->string($form['RC']) . ' ' : '/RC ' . $this->writer->string($form['T']) . ' ';
$temp .= $form['AC'] ? '/AC ' . $this->writer->string($form['AC']) . ' ' : '/AC ' . $this->writer->string($form['T']) . ' ';
$temp .= $this->buttonCaptions($form);
$this->writer->write("/BS << $bstemp >>");
$this->writer->write("/MK << $temp >>");
$this->writer->write('/DA ' . $this->writer->string('/F' . $this->mpdf->fonts[$form['style']['font']]['i'] . ' ' . $form['style']['fontsize'] . ' Tf ' . $form['style']['fontcolor']));
Expand Down
123 changes: 123 additions & 0 deletions tests/Mpdf/ButtonCaptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php

namespace Mpdf;

/**
* A push button shows its value as its caption whether forms are active or not, as a browser does: an empty value
* leaves it blank, and no value at all gives it the browser's default caption. Its field name is never shown.
*/
class ButtonCaptionTest extends \Yoast\PHPUnitPolyfills\TestCases\TestCase
{

use PageStreams;

/**
* Each kind of push button with an empty value, with no value, and with a value, and the caption it shows
*
* @return string[][]
*/
public function buttons()
{
return [
'submit with an empty value' => ['<input type="submit" name="save_and_continue" value="" />', ''],
'reset with an empty value' => ['<input type="reset" name="clear_form" value="" />', ''],
'button with an empty value' => ['<input type="button" name="run_script" value="" onclick="app.alert(1)" />', ''],
'unnamed submit with an empty value' => ['<input type="submit" value="" />', ''],
'submit with no value' => ['<input type="submit" name="save_and_continue" />', 'Submit'],
'reset with no value' => ['<input type="reset" name="clear_form" />', 'Reset'],
'button with no value' => ['<input type="button" name="run_script" onclick="app.alert(1)" />', 'Button'],
'submit with a value' => ['<input type="submit" name="save_and_continue" value="Save" />', 'Save'],
'reset with a value' => ['<input type="reset" name="clear_form" value="Clear" />', 'Clear'],
'button with a value' => ['<input type="button" name="run_script" value="Run" onclick="app.alert(1)" />', 'Run'],
];
}

/**
* An active button's normal, rollover and down captions are all its caption
*
* @dataProvider buttons
*
* @param string $button
* @param string $caption
*/
public function testActiveButtonIsCaptionedWithItsValue($button, $caption)
{
$widget = $this->widget($this->render('<form>' . $button . '</form>', ['mode' => 'c', 'useActiveForms' => true]));

$this->assertStringContainsString('/CA (' . $caption . ') /RC (' . $caption . ') /AC (' . $caption . ')', $widget);
}

/**
* An active button's appearance draws the caption a viewer would draw from /CA
*
* @dataProvider buttons
*
* @param string $button
* @param string $caption
*/
public function testActiveButtonAppearanceDrawsItsCaption($button, $caption)
{
$pdf = $this->render('<form>' . $button . '</form>', ['mode' => 'c', 'useActiveForms' => true]);

$this->assertSame(1, preg_match('/\/AP << \/N << \/Push (\d+) 0 R/', $this->widget($pdf), $appearance));
$this->assertSame($caption, $this->drawnText($this->object($pdf, $appearance[1])));
}

/**
* A button drawn on the page when forms are not active shows the same caption
*
* @dataProvider buttons
*
* @param string $button
* @param string $caption
*/
public function testStaticButtonDrawsItsCaption($button, $caption)
{
$contents = $this->pageContents($this->render('<form>' . $button . '</form>', ['mode' => 'c']));

$this->assertSame($caption, trim($this->drawnText($contents[0])));
}

/**
* With an embedded font an empty caption is an empty UTF-16 string, and the appearance draws no text
*/
public function testEmptyCaptionInAnEmbeddedFont()
{
$pdf = $this->render('<form><input type="submit" name="save_and_continue" value="" /></form>', ['mode' => 'utf-8', 'useActiveForms' => true]);
$widget = $this->widget($pdf);

$this->assertStringContainsString("/CA (\xFE\xFF) ", $widget);
$this->assertSame(1, preg_match('/\/AP << \/N << \/Push (\d+) 0 R/', $widget, $appearance));
$this->assertSame('', $this->drawnText($this->object($pdf, $appearance[1])));
}

/**
* An image button shows its icon and no caption, and keeps its field name as /CA
*/
public function testImageButtonKeepsItsName()
{
$widget = $this->widget($this->render(
'<form><input type="image" name="go" src="' . $this->pngImage() . '" onclick="app.alert(1)" /></form>',
['mode' => 'c', 'useActiveForms' => true]
));

$this->assertStringContainsString('/TP 1 ', $widget);
$this->assertStringContainsString('/CA (go) /RC (go) /AC (go)', $widget);
}

/**
* The only widget on the first page
*
* @param string $pdf
*
* @return string
*/
private function widget($pdf)
{
$refs = $this->annotationRefs($pdf);
$this->assertCount(1, $refs[0]);

return $this->object($pdf, $refs[0][0]);
}

}
60 changes: 60 additions & 0 deletions tests/Snapshots/EmptyButtonCaptionSnapshotTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Snapshots;

/**
* Active submit, reset and script buttons with an empty value, no value, a value and a value of "0". A button with an
* empty value is drawn blank, as a browser draws it, rather than captioned with its field name (#459).
*
* @group snapshot
*/
class EmptyButtonCaptionSnapshotTest extends Snapshot
{

/**
* @return string
*/
public function getId()
{
return 'empty-button-caption';
}

/**
* A table with a row per button type and a column per value
*/
public function generatePdf()
{
$values = [
'value=""' => ' value=""',
'No value' => '',
'value="Send"' => ' value="Send"',
'value="0"' => ' value="0"',
];

$html = '<style>td, th { border: 0.2mm solid #808080; padding: 2mm; }</style>'
. '<h2>Active push buttons</h2>'
. '<p>A button with an empty value has no caption. Its field name is never shown.</p>'
. '<form><table><tr><th></th>';

foreach (array_keys($values) as $label) {
$html .= '<th>' . htmlspecialchars($label) . '</th>';
}
$html .= '</tr>';

foreach (['submit', 'reset', 'button'] as $type) {
$html .= '<tr><th>' . $type . '</th>';
$i = 0;
foreach ($values as $attribute) {
$html .= '<td><input type="' . $type . '" name="' . $type . '_name_' . $i++ . '"' . $attribute . ' /></td>';
}
$html .= '</tr>';
}

$html .= '<tr><th>unnamed submit</th><td><input type="submit" value="" /></td><td><input type="submit" /></td>'
. '<td></td><td></td></tr></table></form>';

$this->mpdf = $this->createMpdf(['useActiveForms' => true]);
$this->mpdf->WriteHTML($html);
}

}
Binary file added tests/data/snapshots/empty-button-caption.pdf
Binary file not shown.
Loading