From 5df240f76764d217f04ae032ca8943cbaa48ea58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Thu, 29 May 2025 16:47:35 -0300 Subject: [PATCH] Extract HTML from InsertEdit::getHtmlInput() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: MaurĂ­cio Meneghini Fauth --- phpstan-baseline.neon | 2 +- psalm-baseline.xml | 4 - .../templates/table/insert/column_row.twig | 5 +- .../value_column_for_other_datatype.twig | 21 +++- src/InsertEdit.php | 110 +++++++----------- .../Table/ChangeControllerTest.php | 44 ++++--- tests/unit/InsertEditTest.php | 83 +++---------- 7 files changed, 98 insertions(+), 171 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 4b165f475b..01438ddeef 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -18345,7 +18345,7 @@ parameters: Use dependency injection instead\.$# ''' identifier: staticMethod.deprecated - count: 33 + count: 32 path: tests/unit/InsertEditTest.php - diff --git a/psalm-baseline.xml b/psalm-baseline.xml index dea05a2c17..b02e1c8d5b 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -10948,7 +10948,6 @@ - @@ -10984,9 +10983,6 @@ - - - diff --git a/resources/templates/table/insert/column_row.twig b/resources/templates/table/insert/column_row.twig index df28067218..3fd23a07e1 100644 --- a/resources/templates/table/insert/column_row.twig +++ b/resources/templates/table/insert/column_row.twig @@ -105,7 +105,10 @@ {% else %} {{ backup_field|raw }} - {{ input_field_html|raw }} + {% endif %} {% if is_upload and column.isBlob %}
diff --git a/resources/templates/table/insert/value_column_for_other_datatype.twig b/resources/templates/table/insert/value_column_for_other_datatype.twig index f25a316fad..5c29dcf2dc 100644 --- a/resources/templates/table/insert/value_column_for_other_datatype.twig +++ b/resources/templates/table/insert/value_column_for_other_datatype.twig @@ -1,15 +1,24 @@ {{ backup_field|raw }} {% if is_textarea %} - {{- html_field|raw -}} + {{- textarea_html|raw -}} {% else %} - {{- html_field|raw -}} + + {%- if extra matches '/(VIRTUAL|PERSISTENT|GENERATED)/' and 'DEFAULT_GENERATED' not in extra -%} - + {%- endif -%} {%- if extra == 'auto_increment' -%} - + {%- endif -%} - {%- if trueType == 'bit' or trueType == 'uuid' or trueType == 'timestamp' or trueType == 'datetime' or trueType == 'date' -%} - + {%- if true_type == 'bit' or true_type == 'uuid' or true_type == 'timestamp' or true_type == 'datetime' or true_type == 'date' -%} + {%- endif -%} {% endif %} diff --git a/src/InsertEdit.php b/src/InsertEdit.php index a126f92462..ecfde0968f 100644 --- a/src/InsertEdit.php +++ b/src/InsertEdit.php @@ -382,60 +382,28 @@ class InsertEdit . ''; } - /** - * Get HTML input type - * - * @param InsertEditColumn $column description of column in given table - * @param string $columnNameAppendix the name attribute - * @param string $specialChars special characters - * @param int $fieldsize html field size - * @param string $onChangeClause onchange clause for fields - * @param string $dataType the html5 data-* attribute type - * - * @return string an html snippet - */ - private function getHtmlInput( - InsertEditColumn $column, - string $columnNameAppendix, - string $specialChars, - int $fieldsize, - string $onChangeClause, - string $dataType, - ): string { - $theClass = 'textfield'; - if ($column->trueType === 'date') { - $theClass .= ' datefield'; - } elseif ($column->trueType === 'time') { - $theClass .= ' timefield'; - } elseif ($column->trueType === 'datetime' || $column->trueType === 'timestamp') { - $theClass .= ' datetimefield'; - } - - $inputMinMax = ''; + /** @return object{isInteger: bool, minValue: string, maxValue: string} */ + private function getIntegerRange(InsertEditColumn $column): object + { + $minValue = ''; + $maxValue = ''; $isInteger = in_array($column->trueType, $this->dbi->types->getIntegerTypes(), true); if ($isInteger) { - $extractedColumnspec = Util::extractColumnSpec($column->type); - $isUnsigned = $extractedColumnspec['unsigned']; + $extractedColumnSpec = Util::extractColumnSpec($column->type); + $isUnsigned = $extractedColumnSpec['unsigned']; $minMaxValues = $this->dbi->types->getIntegerRange($column->trueType, ! $isUnsigned); - $inputMinMax = 'min="' . $minMaxValues[0] . '" ' - . 'max="' . $minMaxValues[1] . '"'; - $dataType = 'INT'; + $minValue = $minMaxValues[0]; + $maxValue = $minMaxValues[1]; } - // do not use the 'date' or 'time' types here; they have no effect on some - // browsers and create side effects (see bug #4218) - return 'isChar - ? ' data-maxlength="' . $fieldsize . '"' - : '') - . ($inputMinMax !== '' ? ' ' . $inputMinMax : '') - . ' data-type="' . $dataType . '"' - . ' class="' . $theClass . '" onchange="' . htmlspecialchars($onChangeClause, ENT_COMPAT) . '"' - . ' tabindex="' . $this->fieldIndex . '"' - . ($isInteger ? ' inputmode="numeric"' : '') - . ' id="field_' . $this->fieldIndex . '_3">'; + return new class ($isInteger, $minValue, $maxValue) { + public function __construct( + public readonly bool $isInteger, + public readonly string $minValue, + public readonly string $maxValue, + ) { + } + }; } /** @@ -525,11 +493,13 @@ class InsertEdit $dataType = $this->dbi->types->getTypeClass($column->trueType); $fieldsize = $this->getColumnSize($column, $specInBrackets); + $input = []; + $textareaHtml = ''; $isTextareaRequired = $column->isChar && ($this->config->settings['CharEditing'] === 'textarea' || str_contains($data, "\n")); if ($isTextareaRequired) { $this->config->settings['CharEditing'] = $defaultCharEditing; - $htmlField = $this->getTextarea( + $textareaHtml = $this->getTextarea( $column, $backupField, $columnNameAppendix, @@ -538,23 +508,28 @@ class InsertEdit $dataType, ); } else { - $htmlField = $this->getHtmlInput( - $column, - $columnNameAppendix, - $specialChars, - $fieldsize, - $onChangeClause, - $dataType->value, - ); + $integerRange = $this->getIntegerRange($column); + $input = [ + 'value' => $specialChars, + 'size' => $fieldsize, + 'is_char' => $column->isChar, + 'is_integer' => $integerRange->isInteger, + 'min' => $integerRange->minValue, + 'max' => $integerRange->maxValue, + 'data_type' => $dataType->value, + 'on_change_clause' => $onChangeClause, + 'field_index' => $this->fieldIndex, + ]; } return $this->template->render('table/insert/value_column_for_other_datatype', [ - 'html_field' => $htmlField, + 'input' => $input, + 'textarea_html' => $textareaHtml, 'backup_field' => $backupField, 'is_textarea' => $isTextareaRequired, - 'columnNameAppendix' => $columnNameAppendix, + 'column_name_appendix' => $columnNameAppendix, 'extra' => $column->extra, - 'trueType' => $column->trueType, + 'true_type' => $column->trueType, ]); } @@ -1655,7 +1630,7 @@ class InsertEdit $blobValueUnit = ''; $maxUploadSize = 0; $selectOptionForUpload = ''; - $inputFieldHtml = ''; + $hexInputSize = 0; if ($transformedHtml === '') { if ($foreignData->dispRow !== null) { $foreignDropdown = $this->relation->foreignDropdown( @@ -1714,14 +1689,7 @@ class InsertEdit ! $isColumnProtectedBlob && ! ($column->isBlob || ($column->length > $this->config->settings['LimitChars'])) ) { - $inputFieldHtml = $this->getHtmlInput( - $column, - $columnNameAppendix, - $specialChars, - min(max($column->length * 2, 4), $this->config->settings['LimitChars']), - $onChangeClause, - 'HEX', - ); + $hexInputSize = min(max($column->length * 2, 4), $this->config->settings['LimitChars']); } } else { $columnValue = $this->getValueColumnForOtherDatatypes( @@ -1777,7 +1745,7 @@ class InsertEdit 'max_upload_size' => $maxUploadSize, 'select_option_for_upload' => $selectOptionForUpload, 'limit_chars' => $this->config->settings['LimitChars'], - 'input_field_html' => $inputFieldHtml, + 'hex_input_size' => $hexInputSize, 'field_title' => $this->getColumnTitle($column->field, $commentsMap), ]); } diff --git a/tests/unit/Controllers/Table/ChangeControllerTest.php b/tests/unit/Controllers/Table/ChangeControllerTest.php index 6c3d1303f8..696cf676cf 100644 --- a/tests/unit/Controllers/Table/ChangeControllerTest.php +++ b/tests/unit/Controllers/Table/ChangeControllerTest.php @@ -71,33 +71,41 @@ final class ChangeControllerTest extends AbstractTestCase $actual = $response->getHTMLResult(); self::assertStringContainsString($pageSettings->getHTML(), $actual); + // phpcs:disable Generic.Files.LineLength.TooLong self::assertStringContainsString( - '', + <<<'HTML' + + HTML, $actual, ); self::assertStringContainsString( - '', + <<<'HTML' + + HTML, $actual, ); self::assertStringContainsString( - '', + <<<'HTML' + + HTML, $actual, ); + // phpcs:enable self::assertStringContainsString( '', - $result, - ); - - // case 2 datetime - $column = new InsertEditColumn('f', 'datetime', false, 'PRI', null, '', -1, false, false, false, false); - $result = $this->callFunction( - $this->insertEdit, - InsertEdit::class, - 'getHtmlInput', - [$column, 'a', 'b', 30, 'c', 'DATE'], - ); - self::assertSame( - '', - $result, - ); - - // case 3 timestamp - $column = new InsertEditColumn('f', 'timestamp', false, 'PRI', null, '', -1, false, false, false, false); - $result = $this->callFunction( - $this->insertEdit, - InsertEdit::class, - 'getHtmlInput', - [$column, 'a', 'b', 30, 'c', 'DATE'], - ); - self::assertSame( - '', - $result, - ); - - // case 4 int - $column = new InsertEditColumn('f', 'int(11)', false, 'PRI', null, '', -1, false, false, false, false); - $result = $this->callFunction( - $this->insertEdit, - InsertEdit::class, - 'getHtmlInput', - [$column, 'a', 'b', 11, 'c', 'INT'], - ); - self::assertSame( - '', - $result, - ); - } - /** * Test for getMaxUploadSize */ @@ -754,14 +690,20 @@ class InsertEditTest extends AbstractTestCase ], ); + // phpcs:disable Generic.Files.LineLength.TooLong self::assertSame( - "a\n" - . '' - . '', + <<<'HTML' + a + + HTML, $result, ); + // phpcs:enable // case 3: (else -> datetime) $column = new InsertEditColumn( @@ -2515,7 +2457,8 @@ class InsertEditTest extends AbstractTestCase self::assertStringContainsString('', $actual); self::assertStringContainsString('datetime', $actual); self::assertStringContainsString( - '