Skip to content

Commit 8b045eb

Browse files
committed
Give an active form field the width and style of its CSS border (#431) (#445) (#469)
* Give an active form field the width and style of its CSS border (#431) An active text field, text area, select or button always had a 1pt solid border in /BS, whatever its CSS said. The border width, in points, and style the form tags record now reach /BS and the appearance: solid, dashed, dotted, inset and outset become /S, /D (dashed in the appearance too), /I and /B, and a border of style none has no width and no /BC, which a viewer would draw regardless. A select also takes its CSS border and background colours, which it ignored before. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * Share the active field colour fallback and the no-border rules between the field kinds Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * Say what the active border helpers do more plainly Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * Refresh the active form snapshots for the CSS border width and the select's border colour form-fields-active and form-fields-pdfa write the styled field's 0.5mm border in /BS and draw it in its appearance. page-break-avoid-forms and page-break-avoid-references draw their fields' 0.2mm CSS border, thinner than the 1pt default, and their select takes its grey colour too. Rasterised against #432's fixtures only those borders change; against this branch before its rebase onto #441, only the maxlength field differs, which #441 draws whole in a smaller font. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Cherry-picked from c675d3f. PR #445 was merged into fix/432-static-field-css after #442 had already merged that branch into feature/pdfa-2-conformance, so it never reached this branch.
1 parent 91c44a7 commit 8b045eb

6 files changed

Lines changed: 300 additions & 61 deletions

File tree

‎src/Form.php‎

Lines changed: 108 additions & 61 deletions
Large diffs are not rendered by default.
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
<?php
2+
3+
namespace Mpdf;
4+
5+
/**
6+
* An active form field's widget and appearance take its CSS border colour, width and style (#431)
7+
*/
8+
class ActiveFieldBorderTest extends \Yoast\PHPUnitPolyfills\TestCases\TestCase
9+
{
10+
11+
use PageStreams;
12+
13+
const STYLE = 'font-size: 14pt; color: #c00000; background-color: #ffffcc; border: 0.5mm solid #0000ff';
14+
15+
/**
16+
* The issue's text input has a 0.5mm (1.417pt) solid blue border in its /BS, its /MK and its appearance
17+
*/
18+
public function testTheIssuesTextInputTakesItsBorder()
19+
{
20+
$pdf = $this->render('<form><input type="text" name="s" value="Styled" style="' . self::STYLE . '" /></form>', ['useActiveForms' => true]);
21+
22+
$widget = $this->widget($pdf, 's');
23+
$this->assertStringContainsString('/BS << /W 1.417 /S /S >>', $widget);
24+
$this->assertStringContainsString('/MK <</BC [ 0.000 0.000 1.000 ] /BG [ 1.000 1.000 0.800 ] >>', $widget);
25+
$this->assertStringContainsString('/DA (/F2 14 Tf 0.753 0.000 0.000 rg)', $widget);
26+
$this->assertStringContainsString('1.000 1.000 0.800 rg 0 0 84.643 20.803 re f 0.000 0.000 1.000 RG 1.417 w 0.709 0.709 83.226 19.386 re S', $pdf);
27+
}
28+
29+
/**
30+
* Every kind of field with a border and appearance takes the CSS border
31+
*
32+
* @dataProvider fields
33+
*
34+
* @param string $field named f
35+
*/
36+
public function testEachFieldTakesItsBorder($field)
37+
{
38+
$pdf = $this->render('<form>' . $field . '</form>', ['useActiveForms' => true]);
39+
40+
$widget = $this->widget($pdf, 'f');
41+
$this->assertStringContainsString('/BS << /W 1.417 /S /S >>', $widget);
42+
$this->assertMatchesRegularExpression('/\/MK <<\s?\/BC \[ 0\.000 0\.000 1\.000 \] \/BG \[ 1\.000 1\.000 0\.800 \]/', $widget);
43+
$this->assertMatchesRegularExpression('/1\.000 1\.000 0\.800 rg 0 0 [\d.]+ [\d.]+ re f 0\.000 0\.000 1\.000 RG 1\.417 w 0\.709 0\.709 /', $pdf);
44+
}
45+
46+
/**
47+
* One field of each kind, styled as the issue's text input
48+
*
49+
* @return string[][]
50+
*/
51+
public function fields()
52+
{
53+
$style = ' style="' . self::STYLE . '"';
54+
55+
return [
56+
'textarea' => ['<textarea name="f"' . $style . '>Styled</textarea>'],
57+
'combo box' => ['<select name="f"' . $style . '><option>Styled</option></select>'],
58+
'list box' => ['<select name="f" size="3"' . $style . '><option>Styled</option></select>'],
59+
'submit button' => ['<input type="submit" name="f" value="Styled"' . $style . ' />'],
60+
'reset button' => ['<input type="reset" name="f" value="Styled"' . $style . ' />'],
61+
'button' => ['<input type="button" name="f" value="Styled"' . $style . ' />'],
62+
];
63+
}
64+
65+
/**
66+
* A field without CSS keeps mPDF's 1pt solid border in the default colour
67+
*/
68+
public function testAFieldWithoutStyleKeepsTheDefaults()
69+
{
70+
$pdf = $this->render('<form><input type="text" name="s" value="Plain" /></form>', ['useActiveForms' => true]);
71+
72+
$widget = $this->widget($pdf, 's');
73+
$this->assertStringContainsString('/BS << /W 1 /S /S >>', $widget);
74+
$this->assertStringContainsString('/MK <</BC [ 0.6 0.6 0.72 ] /BG [ 0.975 0.975 0.975 ] >>', $widget);
75+
$this->assertStringContainsString('0.600 0.600 0.722 RG 1.000 w 0.500 0.500 ', $pdf);
76+
}
77+
78+
/**
79+
* Each CSS border style is written as the /BS style closest to it, and a dashed one is dashed in the appearance
80+
*
81+
* @dataProvider styles
82+
*
83+
* @param string $css
84+
* @param string $bs the /S entry expected
85+
* @param string $stroke how the appearance strokes the border
86+
*/
87+
public function testTheBorderStyleIsKept($css, $bs, $stroke)
88+
{
89+
$pdf = $this->render('<form><input type="text" name="s" value="x" style="border: 1pt ' . $css . ' #0000ff" /></form>', ['useActiveForms' => true]);
90+
91+
$this->assertStringContainsString('/BS << /W 1.000 /S ' . $bs . ' >>', $this->widget($pdf, 's'));
92+
$this->assertStringContainsString($stroke, $pdf);
93+
}
94+
95+
/**
96+
* CSS border styles and the /BS styles they become
97+
*
98+
* @return string[][]
99+
*/
100+
public function styles()
101+
{
102+
$solid = '0.000 0.000 1.000 RG 1.000 w 0.500 0.500 ';
103+
104+
return [
105+
'solid' => ['solid', '/S', $solid],
106+
'dashed' => ['dashed', '/D /D [3]', '0.000 0.000 1.000 RG 1.000 w [3] 0 d 0.500 0.500 '],
107+
'dotted' => ['dotted', '/D /D [1]', '0.000 0.000 1.000 RG 1.000 w [1] 0 d 0.500 0.500 '],
108+
'inset' => ['inset', '/I', $solid],
109+
'outset' => ['outset', '/B', $solid],
110+
'double' => ['double', '/S', $solid],
111+
];
112+
}
113+
114+
/**
115+
* A border of style none has no width and no border colour, and its appearance draws no border
116+
*
117+
* @dataProvider noBorders
118+
*
119+
* @param string $style
120+
*/
121+
public function testAFieldWithoutABorderDrawsNone($style)
122+
{
123+
$pdf = $this->render('<form><input type="text" name="s" value="x" style="' . $style . '" /></form>', ['useActiveForms' => true]);
124+
125+
$widget = $this->widget($pdf, 's');
126+
$this->assertStringContainsString('/BS << /W 0 /S /S >>', $widget);
127+
$this->assertStringNotContainsString('/BC', $widget);
128+
$this->assertStringNotContainsString(' re S', $pdf);
129+
}
130+
131+
/**
132+
* Ways CSS takes a border away
133+
*
134+
* @return string[][]
135+
*/
136+
public function noBorders()
137+
{
138+
return [
139+
'none' => ['border: none'],
140+
'hidden' => ['border-style: hidden'],
141+
];
142+
}
143+
144+
/**
145+
* A border colour or width given on its own keeps the rest of the default border
146+
*/
147+
public function testABorderLonghandKeepsTheRestOfTheDefault()
148+
{
149+
$pdf = $this->render('<form><input type="text" name="c" value="x" style="border-color: #0000ff" />'
150+
. '<input type="text" name="w" value="x" style="border-width: 2pt" /></form>', ['useActiveForms' => true]);
151+
152+
$this->assertStringContainsString('/BS << /W 1 /S /S >>', $this->widget($pdf, 'c'));
153+
$this->assertStringContainsString('/BC [ 0.000 0.000 1.000 ]', $this->widget($pdf, 'c'));
154+
$this->assertStringContainsString('/BS << /W 2.000 /S /S >>', $this->widget($pdf, 'w'));
155+
$this->assertStringContainsString('/BC [ 0.6 0.6 0.72 ]', $this->widget($pdf, 'w'));
156+
}
157+
158+
/**
159+
* A PDF/A document restricted to CMYK writes the border colour in CMYK, in both /MK and the appearance
160+
*/
161+
public function testTheBorderColourFollowsTheOutputIntent()
162+
{
163+
$pdf = $this->render(
164+
'<form><input type="text" name="s" value="Styled" style="' . self::STYLE . '" /></form>',
165+
['mode' => 'utf-8', 'useActiveForms' => true, 'PDFA' => true, 'PDFAauto' => true, 'restrictColorSpace' => 3]
166+
);
167+
168+
$this->assertStringContainsString('/BC [ 1.000 1.000 0.000 0.000 ]', $this->widget($pdf, 's'));
169+
$this->assertStringContainsString('1.000 1.000 0.000 0.000 K 1.417 w', $pdf);
170+
}
171+
172+
/**
173+
* The widget annotation of the field with a name
174+
*
175+
* @param string $pdf
176+
* @param string $name
177+
*
178+
* @return string
179+
*/
180+
private function widget($pdf, $name)
181+
{
182+
foreach ($this->annotationRefs($pdf)[0] as $ref) {
183+
$annotation = $this->object($pdf, $ref);
184+
if (strpos($annotation, '/T (' . $name . ')') !== false) {
185+
return $annotation;
186+
}
187+
}
188+
189+
$this->fail('The field ' . $name . ' should have a widget');
190+
}
191+
192+
}
4 Bytes
Binary file not shown.
4 Bytes
Binary file not shown.
58 Bytes
Binary file not shown.
8 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)