Skip to content

Commit 47cdb77

Browse files
committed
Added --escape-with-double-encode command option
1 parent 5f3edb5 commit 47cdb77

File tree

7 files changed

+139
-69
lines changed

7 files changed

+139
-69
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
Changelog
22
=========
33

4+
1.1.0 (2024-06-12)
5+
------------------
6+
7+
- Added `--escape-with-double-encode` command option
8+
9+
410
1.0.1 (2024-04-26)
511
------------------
612

README.md

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,24 @@ composer require axn/laravelcollective-form-to-raw-html --dev
2222
Usage
2323
-----
2424

25-
Simply launch this command:
25+
Simply run this command:
2626

27-
```php
28-
laravelcollective-form-to-raw-html:run
27+
```sh
28+
php artisan laravelcollective-form-to-raw-html:run
2929
```
3030

3131
By default, the command scans all files in `resources/views/`.
3232

3333
You can precise an other directory:
3434

35-
```php
36-
laravelcollective-form-to-raw-html:run resources/views/admin/users
35+
```sh
36+
php artisan laravelcollective-form-to-raw-html:run resources/views/admin/users
3737
```
3838

3939
Or a single file:
4040

41-
```php
42-
laravelcollective-form-to-raw-html:run resources/views/admin/users/create.blade.php
41+
```sh
42+
php artisan laravelcollective-form-to-raw-html:run resources/views/admin/users/create.blade.php
4343
```
4444

4545
**NOTE:** The target path is always relative to the project root.
@@ -106,6 +106,22 @@ The HTML result will be:
106106
<strong>Name & firstname</strong>
107107
```
108108

109+
### Escaped with double-encode
110+
111+
If you do not want to use this feature you can pass the `--escape-with-double-encode` option to the command.
112+
113+
So instead of escaping this way:
114+
115+
```blade
116+
{!! e($value, false) !!}
117+
```
118+
119+
The values ​​will be escaped like this:
120+
121+
```blade
122+
{{ $value }}
123+
```
124+
109125
## Automatically retrieve field value
110126

111127
LaravelCollective has a complex method to automatically retrieve the value of the field: it searches in "old" values

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
"require": {
1616
"php": "^8.2",
1717
"laravel/framework": "^10.0 || ^11.0"
18+
},
19+
"require-dev": {
20+
"laravel/pint": "^1.16.0"
1821
},
1922
"autoload": {
2023
"psr-4": {

pint.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"preset": "laravel",
3+
"rules": {
4+
"line_ending": false,
5+
"native_function_invocation": {
6+
"include": ["@compiler_optimized"],
7+
"scope": "namespaced",
8+
"strict": true
9+
},
10+
"new_with_braces": {
11+
"anonymous_class": false,
12+
"named_class": true
13+
},
14+
"blank_line_before_statement": {
15+
"statements": [
16+
"case",
17+
"continue",
18+
"declare",
19+
"default",
20+
"do",
21+
"exit",
22+
"for",
23+
"foreach",
24+
"goto",
25+
"if",
26+
"include",
27+
"include_once",
28+
"phpdoc",
29+
"require",
30+
"require_once",
31+
"return",
32+
"switch",
33+
"throw",
34+
"try",
35+
"while",
36+
"yield",
37+
"yield_from"
38+
]
39+
}
40+
}
41+
}

src/Console/RunCommand.php

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,61 @@
11
<?php
2-
3-
namespace Axn\LaravelCollectiveFormToRawHtml\Console;
4-
5-
use Axn\LaravelCollectiveFormToRawHtml\Converter;
6-
use Illuminate\Console\Command;
7-
use Symfony\Component\Finder\Finder;
8-
9-
class RunCommand extends Command
2+
3+
namespace Axn\LaravelCollectiveFormToRawHtml\Console;
4+
5+
use Axn\LaravelCollectiveFormToRawHtml\Converter;
6+
use Illuminate\Console\Command;
7+
use Symfony\Component\Finder\Finder;
8+
9+
class RunCommand extends Command
1010
{
1111
protected $signature = 'laravelcollective-form-to-raw-html:run
12-
{target=resources/views : Target path to scan (directory or single file relative to the project root)}';
13-
14-
protected $description = 'Replaces LaravelCollective `Form::` syntax by raw HTML';
15-
16-
public function handle(): int
12+
{target=resources/views : Target path to scan (directory or single file relative to the project root)}
13+
{--escape-with-double-encode}';
14+
15+
protected $description = 'Replaces LaravelCollective `Form::` syntax by raw HTML';
16+
17+
public function handle(): int
1718
{
18-
$target = $this->argument('target');
19-
20-
$path = base_path($target);
21-
19+
$target = $this->argument('target');
20+
21+
$path = base_path($target);
22+
2223
if (is_file($path)) {
2324
$finder = Finder::create()
2425
->files()
25-
->in(dirname($path))
26+
->in(\dirname($path))
2627
->depth(0)
27-
->name(basename($path));
28-
29-
$this->comment('Replacing `Form::` syntax by raw HTML in file `'.$path.'`...');
30-
28+
->name(basename($path));
29+
30+
$this->comment('Replacing `Form::` syntax by raw HTML in file `'.$path.'`...');
31+
3132
} elseif (is_dir($path)) {
3233
$finder = Finder::create()
3334
->files()
34-
->in($path);
35-
36-
$this->comment('Replacing `Form::` syntax by raw HTML in all files of directory `'.$path.'`...');
37-
35+
->in($path);
36+
37+
$this->comment('Replacing `Form::` syntax by raw HTML in all files of directory `'.$path.'`...');
38+
3839
} else {
39-
$this->error('Target `'.$path.'` not found.');
40-
40+
$this->error('Target `'.$path.'` not found.');
41+
4142
return 0;
42-
}
43-
44-
$files = iterator_to_array($finder, false);
45-
46-
$nbReplacements = Converter::execute($files);
47-
48-
$this->info('Finished with '.$nbReplacements.' replacement(s) done.');
49-
43+
}
44+
45+
$files = iterator_to_array($finder, false);
46+
47+
Converter::$escapeWithDoubleEncode = $this->option('escape-with-double-encode');
48+
49+
$nbReplacements = Converter::execute($files);
50+
51+
$this->info('Finished with '.$nbReplacements.' replacement(s) done.');
52+
5053
$this->line('Remember to search and review `'.Converter::CHECK_COMMENTS_TAG.'`');
5154
$this->line('Remember to search and review `'.Converter::CHECK_OPTIONS_TAG.'`');
52-
$this->line('Remember to search and replace leaving `Form::`');
53-
54-
$this->comment('See README.md for more info.');
55-
55+
$this->line('Remember to search and replace leaving `Form::`');
56+
57+
$this->comment('See README.md for more info.');
58+
5659
return 0;
5760
}
58-
}
61+
}

src/Converter.php

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ class Converter
1010

1111
public const CHECK_OPTIONS_TAG = '@TODO CHECK OPTIONS';
1212

13+
public static bool $escapeWithDoubleEncode = false;
14+
1315
protected static string $indent = '';
1416

1517
protected static bool $hasComments = false;
1618

1719
/**
18-
* @param array<SplFileInfo> $files
19-
* @return int
20+
* @param array<SplFileInfo> $files
2021
*/
2122
public static function execute(array $files): int
2223
{
@@ -48,7 +49,7 @@ public static function execute(array $files): int
4849
} elseif ($formBuilderMethod === 'close') {
4950
$result = static::buildFormClose();
5051

51-
} elseif (in_array($formBuilderMethod, ['label', 'labelRequired'])) {
52+
} elseif (\in_array($formBuilderMethod, ['label', 'labelRequired'])) {
5253
$result = static::buildLabel(
5354
$formBuilderArgs[0],
5455
$formBuilderArgs[1] ?? '',
@@ -57,7 +58,7 @@ public static function execute(array $files): int
5758
$formBuilderMethod === 'labelRequired'
5859
);
5960

60-
} elseif (in_array($formBuilderMethod, ['input', 'text', 'number', 'date', 'time', 'datetime', 'week', 'month', 'range', 'search', 'email', 'tel', 'url', 'color', 'hidden'])) {
61+
} elseif (\in_array($formBuilderMethod, ['input', 'text', 'number', 'date', 'time', 'datetime', 'week', 'month', 'range', 'search', 'email', 'tel', 'url', 'color', 'hidden'])) {
6162
if ($formBuilderMethod === 'input') {
6263
$formBuilderMethod = trim(array_shift($formBuilderArgs), '\'"');
6364
}
@@ -70,7 +71,7 @@ public static function execute(array $files): int
7071
$formBuilderArgs[2] ?? ''
7172
);
7273

73-
} elseif (in_array($formBuilderMethod, ['checkbox', 'radio'])) {
74+
} elseif (\in_array($formBuilderMethod, ['checkbox', 'radio'])) {
7475
$result = static::buildDefaultInput(
7576
$formBuilderMethod,
7677
$formBuilderArgs[0],
@@ -79,7 +80,7 @@ public static function execute(array $files): int
7980
$formBuilderArgs[3] ?? ''
8081
);
8182

82-
} elseif (in_array($formBuilderMethod, ['file', 'password'])) {
83+
} elseif (\in_array($formBuilderMethod, ['file', 'password'])) {
8384
$result = static::buildNoValueInput(
8485
$formBuilderMethod,
8586
$formBuilderArgs[0],
@@ -107,7 +108,7 @@ public static function execute(array $files): int
107108
$formBuilderArgs[3] ?? ''
108109
);
109110

110-
} elseif (in_array($formBuilderMethod, ['button', 'submit'])) {
111+
} elseif (\in_array($formBuilderMethod, ['button', 'submit'])) {
111112
$result = static::buildButton(
112113
$formBuilderMethod,
113114
$formBuilderArgs[0],
@@ -147,7 +148,7 @@ protected static function buildFormOpen(string $options): string
147148
$method = $extractedOptions['method'];
148149
}
149150

150-
if (in_array($method, ['PUT', 'PATCH', 'DELETE'])) {
151+
if (\in_array($method, ['PUT', 'PATCH', 'DELETE'])) {
151152
$attributes['method'] = 'POST';
152153
} else {
153154
$attributes['method'] = $method;
@@ -164,10 +165,10 @@ protected static function buildFormOpen(string $options): string
164165

165166
$attributes['action'] = 'route('.$route;
166167

167-
if (count($routeArgs) === 1) {
168+
if (\count($routeArgs) === 1) {
168169
$attributes['action'] .= ', '.$routeArgs[0];
169170

170-
} elseif (count($routeArgs) > 1) {
171+
} elseif (\count($routeArgs) > 1) {
171172
$attributes['action'] .= ', ['.implode(', ', $routeArgs).']';
172173
}
173174

@@ -177,7 +178,7 @@ protected static function buildFormOpen(string $options): string
177178
}
178179

179180
if (isset($extractedOptions['files'])) {
180-
if (in_array(strtolower($extractedOptions['files']), ['true', '1'])) {
181+
if (\in_array(strtolower($extractedOptions['files']), ['true', '1'])) {
181182
$attributes['enctype'] = "'multipart/form-data'";
182183

183184
} elseif (! static::isEmpty($extractedOptions['files'])) {
@@ -191,7 +192,7 @@ protected static function buildFormOpen(string $options): string
191192

192193
$input = static::$indent.'<form'.static::buildHtmlTagAttributes($attributes).'>';
193194

194-
if (! in_array($method, ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'])) {
195+
if (! \in_array($method, ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'])) {
195196
$input .= "\n".static::$indent.' @if (strtoupper('.$method.') !== \'GET\')';
196197
$input .= "\n".static::$indent.' @csrf';
197198
$input .= "\n".static::$indent.' @if (strtoupper('.$method.') !== \'POST\')';
@@ -252,7 +253,7 @@ protected static function buildDefaultInput(string $type, string $name, string $
252253
if (! static::isEmpty($name)) {
253254
$attributes['name'] = $name;
254255

255-
if (in_array($type, ['checkbox', 'radio'])) {
256+
if (\in_array($type, ['checkbox', 'radio'])) {
256257
if (! static::useOldHelper($checked) && ! static::isEmpty($value)) {
257258
$checked = (! static::isEmpty($checked) ? '! old() ? '.$checked.' : ' : '')
258259
.'in_array('.$value.', (array) '.static::withOldHelperIfNeeded($name).')';
@@ -385,32 +386,33 @@ protected static function buildHtmlTagAttributes(array $attributes): string
385386
$stringBefore = '';
386387
$stringAfter = '';
387388

388-
if (count($attributes) > 1) {
389+
if (\count($attributes) > 1) {
389390
$stringBefore = ' ';
390391
$stringAfter = "\n".static::$indent;
391392

392393
$builtAttributes .= $stringAfter;
393394

394-
} elseif (count($attributes) === 1) {
395+
} elseif (\count($attributes) === 1) {
395396
$builtAttributes .= ' ';
396397
}
397398

398399
foreach ($attributes as $attrName => $attrValue) {
399400
if ($attrName === static::CHECK_OPTIONS_TAG) {
400401
$builtAttributes .= $stringBefore.'{{-- '.$attrName.': '.$attrValue.' --}}'.$stringAfter;
402+
401403
continue;
402404
}
403405

404406
if (static::isEmpty($attrValue)) {
405407
$attrValue = '';
406408
}
407409

408-
if (in_array($attrName, ['disabled', 'readonly', 'required', 'checked', 'multiple'])) {
410+
if (\in_array($attrName, ['disabled', 'readonly', 'required', 'checked', 'multiple'])) {
409411
if ($attrValue === '') {
410412
continue;
411413
}
412414

413-
if (in_array(strtolower($attrValue), ['true', '1', $attrName])) {
415+
if (\in_array(strtolower($attrValue), ['true', '1', $attrName])) {
414416
$attr = $attrName;
415417
} elseif ($attrName === 'multiple') {
416418
$attr = '@if ('.$attrValue.') multiple @endif';
@@ -421,7 +423,7 @@ protected static function buildHtmlTagAttributes(array $attributes): string
421423
} elseif ($attrName === 'class' && preg_match('/^\s*(\[\s*.*\s*\])\s*$/Us', $attrValue, $matches)) {
422424
$attr = $attrName.'="{!! implode(\' \', '.$matches[1].') !!}"';
423425

424-
} elseif (is_string($attrName)) {
426+
} elseif (\is_string($attrName)) {
425427
$attr = $attrName.'="'.static::withEscapedEchoIfNeeded($attrValue).'"';
426428

427429
} else {
@@ -441,7 +443,7 @@ protected static function canUseNameAsId(string $name): bool
441443

442444
protected static function isEmpty(string $value): bool
443445
{
444-
return empty($value) || in_array(strtolower($value), ["''", '""', 'false', 'null']);
446+
return empty($value) || \in_array(strtolower($value), ["''", '""', 'false', 'null']);
445447
}
446448

447449
/**
@@ -594,7 +596,6 @@ protected static function extractArrayFromString(string $string): array
594596

595597
} elseif (strpos($segment, '=>') !== false) {
596598
throw new ConverterException();
597-
598599
} else {
599600
$array[] = $segment;
600601
}

0 commit comments

Comments
 (0)