@@ -25,9 +25,13 @@ public static function execute(array $files): int
2525 foreach ($ files as $ file ) {
2626 $ content = $ file ->getContents ();
2727
28- preg_match_all ('/( *)\{(\{|!!)\s*Form::(\w+)\((.*)\)\s*(\}|!!)\}/Us ' , $ content , $ matches );
28+ preg_match_all ('/([ |\t] *)\{(\{|!!)\s*Form::(\w+)\((.*)\)\s*(\}|!!)\}/Us ' , $ content , $ matches );
2929
3030 foreach (array_keys ($ matches [0 ]) as $ i ) {
31+ if (strpos ($ matches [0 ][$ i ], '{{-- ' ) !== false || strpos ($ matches [0 ][$ i ], '--}} ' ) !== false ) {
32+ continue ;
33+ }
34+
3135 $ result = null ;
3236 static ::$ hasComments = false ;
3337 static ::$ indent = $ matches [1 ][$ i ];
@@ -55,7 +59,7 @@ public static function execute(array $files): int
5559
5660 } elseif (in_array ($ formBuilderMethod , ['input ' , 'text ' , 'number ' , 'date ' , 'time ' , 'datetime ' , 'week ' , 'month ' , 'range ' , 'search ' , 'email ' , 'tel ' , 'url ' , 'color ' , 'hidden ' ])) {
5761 if ($ formBuilderMethod === 'input ' ) {
58- $ formBuilderMethod = trim (array_shift ($ formBuilderArgs ), ' " \' ' );
62+ $ formBuilderMethod = trim (array_shift ($ formBuilderArgs ), '\' " ' );
5963 }
6064
6165 $ result = static ::buildDefaultInput (
@@ -155,7 +159,19 @@ protected static function buildFormOpen(string $options): string
155159 unset($ extractedOptions ['url ' ]);
156160
157161 } elseif (isset ($ extractedOptions ['route ' ])) {
158- $ attributes ['action ' ] = 'route( ' .trim ($ extractedOptions ['route ' ], " \n[] " ).') ' ;
162+ $ routeArgs = static ::extractArgsFromString (trim ($ extractedOptions ['route ' ], " \n\r\t\v\0[] " ));
163+ $ route = array_shift ($ routeArgs );
164+
165+ $ attributes ['action ' ] = 'route( ' .$ route ;
166+
167+ if (count ($ routeArgs ) === 1 ) {
168+ $ attributes ['action ' ] .= ', ' .$ routeArgs [0 ];
169+
170+ } elseif (count ($ routeArgs ) > 1 ) {
171+ $ attributes ['action ' ] .= ', [ ' .implode (', ' , $ routeArgs ).'] ' ;
172+ }
173+
174+ $ attributes ['action ' ] .= ') ' ;
159175
160176 unset($ extractedOptions ['route ' ]);
161177 }
@@ -208,7 +224,7 @@ protected static function buildLabel(string $for, string $value, string $options
208224 $ attributes ['for ' ] = $ for ;
209225
210226 if (static ::isEmpty ($ value )) {
211- $ value = "ucwords(str_replace('_', ' ', " . $ for. " )) " ;
227+ $ value = "ucwords(str_replace('_', ' ', $ for)) " ;
212228 }
213229 }
214230
@@ -420,51 +436,42 @@ protected static function buildHtmlTagAttributes(array $attributes): string
420436
421437 protected static function canUseNameAsId (string $ name ): bool
422438 {
423- if (! static ::isEmpty ($ name )) {
424- $ name = trim ($ name , ' " \'' );
425-
426- if (preg_match ('/^\w+$/ ' , $ name )) {
427- return true ;
428- }
429- }
430-
431- return false ;
439+ return ! static ::isEmpty ($ name ) && preg_match ('/^\w+$/ ' , trim ($ name , '\'" ' ));
432440 }
433441
434442 protected static function isEmpty (string $ value ): bool
435443 {
436444 return empty ($ value ) || in_array (strtolower ($ value ), ["'' " , '"" ' , 'false ' , 'null ' ]);
437445 }
438446
447+ /**
448+ * Examples:
449+ *
450+ * $value = "'Bonjour l\'ami'" + $escape = false|true
451+ * return = "Bonjour l'ami"
452+ *
453+ * $value = "'Bonjour <strong>l\'ami'</strong>" + $escape = false
454+ * return = "Bonjour <strong>l'ami</strong>"
455+ *
456+ * $value = "'Bonjour <strong>l\'ami'</strong>" + $escape = true
457+ * return = "{!! e('Bonjour <strong>l\'ami</strong>', false) !!}"
458+ *
459+ * $value = "'Bonjour l\'ami '.$name" + $escape = false
460+ * return = "{!! 'Bonjour l\'ami '.$name !!}"
461+ *
462+ * $value = "'Bonjour l\'ami '.$name" + $escape = true
463+ * return = "{!! e('Bonjour l\'ami '.$name, false) !!}"
464+ */
439465 protected static function withEchoIfNeeded (string $ value , bool $ escape ): string
440466 {
441- $ value = trim ($ value );
442-
443467 if (static ::isEmpty ($ value ) || preg_match ('/^\w+$/ ' , $ value )) {
444468 return $ value ;
445469 }
446470
447- $ isRegularString = false ;
471+ $ unquotedValue = static :: extractStringBetweenQuotes ( $ value ) ;
448472
449- if (preg_match ('/^ \'(.*) \'$/Us ' , $ value , $ matches )
450- && strpos (str_replace ("\\' " , '' , $ matches [1 ]), "' " ) === false ) {
451-
452- $ value = str_replace ("\\' " , "' " , $ matches [1 ]);
453- $ isRegularString = true ;
454-
455- } elseif (preg_match ('/^"([^\$]*)"$/Us ' , $ value , $ matches )
456- && strpos (str_replace ('\\" ' , '' , $ matches [1 ]), '" ' ) === false ) {
457-
458- $ value = str_replace ('\\" ' , '" ' , $ matches [1 ]);
459- $ isRegularString = true ;
460- }
461-
462- if ($ isRegularString ) {
463- if (! $ escape || $ value === strip_tags ($ value )) {
464- return $ value ;
465- }
466-
467- $ value = "' $ value' " ;
473+ if ($ unquotedValue !== null && (! $ escape || $ unquotedValue === strip_tags ($ unquotedValue ))) {
474+ return $ unquotedValue ;
468475 }
469476
470477 if ($ escape ) {
@@ -490,12 +497,10 @@ protected static function withOldHelperIfNeeded(string $name, string $value = ''
490497 return $ value ;
491498 }
492499
493- if (preg_match ('/^ \'([^ \']*) \'$/Us ' , $ name , $ matches )
494- || preg_match ('/^"([^"\$]*)"$/Us ' , $ name , $ matches )) {
495-
500+ if (static ::extractStringBetweenQuotes ($ name ) !== null ) {
496501 $ key = str_replace (['. ' , '[] ' , '[ ' , '] ' ], ['_ ' , '' , '. ' , '' ], $ name );
497502 } else {
498- $ key = "str_replace(['.', '[]', '[', ']'], ['_', '', '.', ''], " . $ name. " ) " ;
503+ $ key = "str_replace(['.', '[]', '[', ']'], ['_', '', '.', ''], $ name) " ;
499504 }
500505
501506 return 'old( ' .$ key .(! empty ($ value ) ? ', ' .$ value : '' ).') ' ;
@@ -506,6 +511,38 @@ protected static function useOldHelper(string $value): bool
506511 return preg_match ('/[^\w]old\(/ ' , ' ' .$ value );
507512 }
508513
514+ /**
515+ * Examples:
516+ *
517+ * $string = "'test'"
518+ * return = "test"
519+ *
520+ * $string = "'Bonjour l\'ami'"
521+ * return = "Bonjour l'ami"
522+ *
523+ * $string = "'Bonjour l\'ami '.$name"
524+ * return = null
525+ *
526+ * $string = "true"
527+ * return = null
528+ */
529+ protected static function extractStringBetweenQuotes (string $ string ): ?string
530+ {
531+ if (preg_match ('/^ \'(.*) \'$/Us ' , $ string , $ matches )
532+ && strpos (str_replace ("\\' " , '' , $ matches [1 ]), "' " ) === false ) {
533+
534+ return str_replace ("\\' " , "' " , $ matches [1 ]);
535+ }
536+
537+ if (preg_match ('/^"([^\$]*)"$/Us ' , $ string , $ matches )
538+ && strpos (str_replace ('\\" ' , '' , $ matches [1 ]), '" ' ) === false ) {
539+
540+ return str_replace ('\\" ' , '" ' , $ matches [1 ]);
541+ }
542+
543+ return null ;
544+ }
545+
509546 protected static function extractArrayFromStringWithCheckOptionsTagIfFailed (string $ string ): array
510547 {
511548 try {
@@ -520,6 +557,16 @@ protected static function extractArrayFromStringWithCheckOptionsTagIfFailed(stri
520557 return $ options ;
521558 }
522559
560+ /**
561+ * Example:
562+ *
563+ * $string = "['class' => 'form-control', 'disabled']"
564+ *
565+ * return = [
566+ * "class" => "'form-control'",
567+ * 0 => "'disabled'",
568+ * ]
569+ */
523570 protected static function extractArrayFromString (string $ string ): array
524571 {
525572 $ array = [];
@@ -556,6 +603,17 @@ protected static function extractArrayFromString(string $string): array
556603 return $ array ;
557604 }
558605
606+ /**
607+ * Example:
608+ *
609+ * $string = "'champ', $value, ['class' => 'form-control', 'disabled']"
610+ *
611+ * return = [
612+ * "'champ'",
613+ * "$value",
614+ * "['class' => 'form-control', 'disabled']",
615+ * ]
616+ */
559617 protected static function extractArgsFromString (string $ string ): array
560618 {
561619 $ args = [];
@@ -632,7 +690,7 @@ protected static function extractArgsFromString(string $string): array
632690 && ! $ inSimpleQuotedString && ! $ inDoubleQuotedString
633691 && $ nbUnclosedParenthesis === 0 && $ nbUnclosedBrackets === 0 ) {
634692
635- $ args [$ argIndex ] = trim ($ args [$ argIndex ], " \n" );
693+ $ args [$ argIndex ] = trim ($ args [$ argIndex ]);
636694 $ argIndex ++;
637695 }
638696 }
0 commit comments