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
38 changes: 27 additions & 11 deletions src/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ function print_ob_text($objattr, $w, $h, $texto, $rtlalign, $k, $blockdir)
$this->mpdf->FontSizePt = 0.0;
}

$this->SetFormText($w, $h, $objattr['fieldname'], $val, $val, $objattr['title'], $flags, $fieldalign, false, (isset($objattr['maxlength']) ? $objattr['maxlength'] : false), $js, (isset($objattr['background-col']) ? $objattr['background-col'] : false), (isset($objattr['border-col']) ? $objattr['border-col'] : false));
$this->SetFormText($w, $h, (isset($objattr['fieldname']) ? $objattr['fieldname'] : ''), $val, $val, $objattr['title'], $flags, $fieldalign, false, (isset($objattr['maxlength']) ? $objattr['maxlength'] : false), $js, (isset($objattr['background-col']) ? $objattr['background-col'] : false), (isset($objattr['border-col']) ? $objattr['border-col'] : false));

} else {

Expand Down Expand Up @@ -333,7 +333,7 @@ function print_ob_textarea($objattr, $w, $h, $texto, $rtlalign, $k, $blockdir)
$this->mpdf->FontSizePt = 0.0;
}

$this->SetFormText($w, $h, $objattr['fieldname'], $texto, $texto, (isset($objattr['title']) ? $objattr['title'] : ''), $flags, $fieldalign, false, -1, $js, (isset($objattr['background-col']) ? $objattr['background-col'] : false), (isset($objattr['border-col']) ? $objattr['border-col'] : false));
$this->SetFormText($w, $h, (isset($objattr['fieldname']) ? $objattr['fieldname'] : ''), $texto, $texto, (isset($objattr['title']) ? $objattr['title'] : ''), $flags, $fieldalign, false, -1, $js, (isset($objattr['background-col']) ? $objattr['background-col'] : false), (isset($objattr['border-col']) ? $objattr['border-col'] : false));
$this->mpdf->SetTColor($this->colorConverter->convert(0, $this->mpdf->PDFAXwarnings));

} else {
Expand Down Expand Up @@ -438,7 +438,7 @@ function print_ob_select($objattr, $w, $h, $texto, $rtlalign, $k, $blockdir)
$this->mpdf->SetTColor($this->colorConverter->convert(0, $this->mpdf->PDFAXwarnings));
}

$this->SetFormChoice($w, $h, $objattr['fieldname'], $flags, $data, $rtlalign, $js);
$this->SetFormChoice($w, $h, (isset($objattr['fieldname']) ? $objattr['fieldname'] : ''), $flags, $data, $rtlalign, $js);
$this->mpdf->SetTColor($this->colorConverter->convert(0, $this->mpdf->PDFAXwarnings));

} else {
Expand Down Expand Up @@ -502,7 +502,7 @@ function print_ob_imageinput($objattr, $w, $h, $texto, $rtlalign, $k, $blockdir,
} else {
$js = '';
}
$this->SetJSButton($w, $h, $objattr['fieldname'], (isset($objattr['value']) ? $objattr['value'] : ''), $js, $objattr['ID'], $objattr['title'], $flags, (isset($objattr['Indexed']) ? $objattr['Indexed'] : false));
$this->SetJSButton($w, $h, (isset($objattr['fieldname']) ? $objattr['fieldname'] : ''), (isset($objattr['value']) ? $objattr['value'] : ''), $js, $objattr['ID'], $objattr['title'], $flags, (isset($objattr['Indexed']) ? $objattr['Indexed'] : false));
} else {
$this->mpdf->y = $objattr['INNER-Y'];
$this->writer->write(sprintf('q %.3F 0 0 %.3F %.3F %.3F cm /I%d Do Q', $objattr['INNER-WIDTH'] * Mpdf::SCALE, $objattr['INNER-HEIGHT'] * Mpdf::SCALE, $objattr['INNER-X'] * Mpdf::SCALE, ($this->mpdf->h - ($objattr['INNER-Y'] + $objattr['INNER-HEIGHT'] )) * Mpdf::SCALE, $objattr['ID']));
Expand All @@ -529,21 +529,21 @@ function print_ob_button($objattr, $w, $h, $texto, $rtlalign, $k, $blockdir)

if ($objattr['subtype'] === 'RESET') {
$this->SetFormButtonText($objattr['value']);
$this->SetFormReset($w, $h, $objattr['fieldname'], $objattr['value'], $objattr['title'], $flags, (isset($objattr['background-col']) ? $objattr['background-col'] : false), (isset($objattr['border-col']) ? $objattr['border-col'] : false), (isset($objattr['noprint']) ? $objattr['noprint'] : false));
$this->SetFormReset($w, $h, (isset($objattr['fieldname']) ? $objattr['fieldname'] : ''), $objattr['value'], $objattr['title'], $flags, (isset($objattr['background-col']) ? $objattr['background-col'] : false), (isset($objattr['border-col']) ? $objattr['border-col'] : false), (isset($objattr['noprint']) ? $objattr['noprint'] : false));
} elseif ($objattr['subtype'] === 'SUBMIT') {
$url = $this->formAction;
$type = $this->formExportType;
$method = $this->formMethod;
$this->SetFormButtonText($objattr['value']);
$this->SetFormSubmit($w, $h, $objattr['fieldname'], $objattr['value'], $url, $objattr['title'], $type, $method, $flags, (isset($objattr['background-col']) ? $objattr['background-col'] : false), (isset($objattr['border-col']) ? $objattr['border-col'] : false), (isset($objattr['noprint']) ? $objattr['noprint'] : false));
$this->SetFormSubmit($w, $h, (isset($objattr['fieldname']) ? $objattr['fieldname'] : ''), $objattr['value'], $url, $objattr['title'], $type, $method, $flags, (isset($objattr['background-col']) ? $objattr['background-col'] : false), (isset($objattr['border-col']) ? $objattr['border-col'] : false), (isset($objattr['noprint']) ? $objattr['noprint'] : false));
} elseif ($objattr['subtype'] === 'BUTTON') {
$this->SetFormButtonText($objattr['value']);
if (isset($objattr['onClick']) && $objattr['onClick']) {
$js = $objattr['onClick'];
} else {
$js = '';
}
$this->SetJSButton($w, $h, $objattr['fieldname'], $objattr['value'], $js, 0, $objattr['title'], $flags, false, (isset($objattr['background-col']) ? $objattr['background-col'] : false), (isset($objattr['border-col']) ? $objattr['border-col'] : false), (isset($objattr['noprint']) ? $objattr['noprint'] : false));
$this->SetJSButton($w, $h, (isset($objattr['fieldname']) ? $objattr['fieldname'] : ''), $objattr['value'], $js, 0, $objattr['title'], $flags, false, (isset($objattr['background-col']) ? $objattr['background-col'] : false), (isset($objattr['border-col']) ? $objattr['border-col'] : false), (isset($objattr['noprint']) ? $objattr['noprint'] : false));
}
}

Expand Down Expand Up @@ -598,7 +598,7 @@ function print_ob_checkbox($objattr, $w, $h, $texto, $rtlalign, $k, $blockdir, $
if (!empty($objattr['checked'])) {
$checked = true;
}
$this->SetCheckBox($w, $h, $objattr['fieldname'], $objattr['value'], $objattr['title'], $checked, $flags, (isset($objattr['disabled']) ? $objattr['disabled'] : false));
$this->SetCheckBox($w, $h, (isset($objattr['fieldname']) ? $objattr['fieldname'] : ''), $objattr['value'], $objattr['title'], $checked, $flags, (isset($objattr['disabled']) ? $objattr['disabled'] : false));
} else {
$iw = $w * 0.7;
$ih = $h * 0.7;
Expand Down Expand Up @@ -641,7 +641,7 @@ function print_ob_radio($objattr, $w, $h, $texto, $rtlalign, $k, $blockdir, $x,
if (!empty($objattr['checked'])) {
$checked = true;
}
$this->SetRadio($w, $h, $objattr['fieldname'], $objattr['value'], (isset($objattr['title']) ? $objattr['title'] : ''), $checked, $flags, (isset($objattr['disabled']) ? $objattr['disabled'] : false));
$this->SetRadio($w, $h, (isset($objattr['fieldname']) ? $objattr['fieldname'] : ''), $objattr['value'], (isset($objattr['title']) ? $objattr['title'] : ''), $checked, $flags, (isset($objattr['disabled']) ? $objattr['disabled'] : false));
} else {
$this->mpdf->SetLineWidth(0.2 / $k);
$radius = $this->mpdf->FontSize * 0.35;
Expand Down Expand Up @@ -1092,14 +1092,17 @@ function SetRadio($w, $h, $name, $value, $title = '', $checked = false, $flags =
function SetFormReset($w, $h, $name, $value = 'Reset', $title = '', $flags = [], $background_col = false, $border_col = false, $noprint = false)
{
if (!$name) {
$name = 'Reset';
$name = $this->unnamedButtonName('Reset');
}
$this->SetFormButton($w, $h, $name, $value, 'reset', $title, $flags, false, false, $background_col, $border_col, $noprint);
$this->mpdf->x += $w;
}

function SetJSButton($w, $h, $name, $value, $js, $image_id = 0, $title = '', $flags = [], $indexed = false, $background_col = false, $border_col = false, $noprint = false)
{
if (!$name) {
$name = $this->unnamedButtonName('Button');
}
$this->SetFormButton($w, $h, $name, $value, 'js_button', $title, $flags, false, false, $background_col, $border_col, $noprint);
// pos => 1 = no caption, icon only; 0 = caption only
if ($image_id) {
Expand All @@ -1118,7 +1121,7 @@ function SetJSButton($w, $h, $name, $value, $js, $image_id = 0, $title = '', $fl
function SetFormSubmit($w, $h, $name, $value = 'Submit', $url = '', $title = '', $typ = 'html', $method = 'POST', $flags = [], $background_col = false, $border_col = false, $noprint = false)
{
if (!$name) {
$name = 'Submit';
$name = $this->unnamedButtonName('Submit');
}

$this->SetFormButton($w, $h, $name, $value, 'submit', $title, $flags, false, false, $background_col, $border_col, $noprint);
Expand All @@ -1131,6 +1134,19 @@ function SetFormSubmit($w, $h, $name, $value = 'Submit', $url = '', $title = '',
$this->mpdf->x += $w;
}

/**
* A name for a button that has none. Buttons sharing a name are one field to a viewer and share the entry
* that holds an action or icon, so it carries the number SetFormButton() is about to give the field.
*
* @param string $kind
*
* @return string
*/
private function unnamedButtonName($kind)
{
return $kind . '_' . ($this->formCount + 1);
}

function SetFormButtonText($ca, $rc = '', $ac = '')
{
if ($this->mpdf->onlyCoreFonts) {
Expand Down
2 changes: 1 addition & 1 deletion src/Tag/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function open($attr, &$ahtml, &$ihtml)
case 'HIDDEN':
$this->mpdf->ignorefollowingspaces = true; //Eliminate exceeding left-side spaces
if ($this->mpdf->useActiveForms) {
$this->form->SetFormText(0, 0, $objattr['fieldname'], $objattr['value'], $objattr['value'], '', 0, '', true);
$this->form->SetFormText(0, 0, (isset($objattr['fieldname']) ? $objattr['fieldname'] : ''), $objattr['value'], $objattr['value'], '', 0, '', true);
}
if ($this->mpdf->InlineProperties[$tag]) {
$this->mpdf->restoreInlineProperties($this->mpdf->InlineProperties[$tag]);
Expand Down
102 changes: 102 additions & 0 deletions tests/Mpdf/UnnamedButtonTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace Mpdf;

/**
* A button needs no name in HTML: a submit button without one triggers the form without being sent with it
*/
class UnnamedButtonTest extends \Yoast\PHPUnitPolyfills\TestCases\TestCase
{
use PageStreams;

/**
* Each kind of button without a name, and the word its generated name starts with
*
* @return string[][]
*/
public function unnamedButtons()
{
return [
'submit' => ['<input type="submit" value="Go">', 'Submit'],
'reset' => ['<input type="reset" value="Clear">', 'Reset'],
'button' => ['<input type="button" value="Run" onclick="app.alert(1)">', 'Button'],
'image' => ['<input type="image" src="' . $this->pngImage() . '" onclick="app.alert(1)">', 'Button'],
];
}

/**
* Each unnamed button is its own field, written without a warning (PHPUnit fails the test on one). Two
* buttons with the same name are one field to a viewer, and share the entry that holds an action or icon
*
* @dataProvider unnamedButtons
*
* @param string $button
* @param string $name
*/
public function testTwoUnnamedButtonsAreTwoFields($button, $name)
{
$pdf = $this->render('<form>' . $button . $button . '</form>', ['useActiveForms' => true]);

$this->assertSame([$name . '_1', $name . '_2'], $this->fieldNames($pdf));
}

/**
* A button's action is looked up by its name, so under a shared name the second action would replace the first
*/
public function testEachUnnamedButtonKeepsItsOwnAction()
{
$pdf = $this->render(
'<form><input type="button" value="A" onclick="app.alert(\'first\')"><input type="button" value="B" onclick="app.alert(\'second\')"></form>',
['useActiveForms' => true]
);

$this->assertSame(1, substr_count($pdf, "/JS (app.alert\\('first'\\))"));
$this->assertSame(1, substr_count($pdf, "/JS (app.alert\\('second'\\))"));
}

/**
* Each kind of field that cannot be written without a name
*
* @return string[][]
*/
public function unnamedFields()
{
return [
'text input' => ['<input type="text" value="A">'],
'hidden input' => ['<input type="hidden" value="A">'],
'textarea' => ['<textarea>A</textarea>'],
'select' => ['<select><option>A</option></select>'],
'checkbox' => ['<input type="checkbox" value="A">'],
'radio' => ['<input type="radio" value="A">'],
];
}

/**
* The field is refused with mPDF's own message rather than a warning about the missing name
*
* @dataProvider unnamedFields
*
* @param string $field
*/
public function testAnUnnamedFieldIsRefused($field)
{
$this->expectException('Mpdf\MpdfException');
$this->expectExceptionMessage('must have a name attribute');

$this->render('<form>' . $field . '</form>', ['useActiveForms' => true]);
}

/**
* The /T of each field, in the order they are written
*
* @param string $pdf
*
* @return string[]
*/
private function fieldNames($pdf)
{
preg_match_all('/\/FT \/\w+ .*?\/T \(([^)]*)\)/s', $pdf, $matches);

return $matches[1];
}
}
Loading