|
| 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 | +} |
0 commit comments