@@ -37,15 +37,15 @@ public static function prepend(string $target, string $html): string
3737 */
3838 public static function replace (string $ target , string $ html , bool $ morph = false ): string
3939 {
40- return self ::custom ('replace ' , $ target , $ html , $ morph ? ['method=" morph" ' ] : []);
40+ return self ::custom ('replace ' , $ target , $ html , $ morph ? ['method ' => ' morph ' ] : []);
4141 }
4242
4343 /**
4444 * Updates the content of the element(s) designated by the target CSS selector.
4545 */
4646 public static function update (string $ target , string $ html , bool $ morph = false ): string
4747 {
48- return self ::custom ('update ' , $ target , $ html , $ morph ? ['method=" morph" ' ] : []);
48+ return self ::custom ('update ' , $ target , $ html , $ morph ? ['method ' => ' morph ' ] : []);
4949 }
5050
5151 /**
@@ -87,17 +87,32 @@ public static function refresh(?string $requestId = null): string
8787 }
8888
8989 /**
90- * @param array<string> $attr
90+ * Custom action and attributes
91+ * @param array<string, string|int|null> $attr
9192 */
9293 public static function custom (string $ action , string $ target , string $ html , array $ attr = []): string
9394 {
94- // Join array elements with a space and prepend a leading space
95- $ atrrString = empty ($ attr ) ? '' : ' ' .implode (' ' , $ attr );
95+ if (array_key_exists ('action ' , $ attr ) || array_key_exists ('targets ' , $ attr )) {
96+ throw new \InvalidArgumentException ('The "action" and "targets" attributes are reserved and cannot be used. ' );
97+ }
98+
99+ $ attr ['action ' ] = $ action ;
100+ $ attr ['targets ' ] = $ target ;
101+ $ attr = array_merge (['action ' => $ action , 'targets ' => $ target ], $ attr );
102+
103+ $ attrString = '' ;
104+ foreach ($ attr as $ key => $ value ) {
105+ if ($ value === null ) {
106+ $ attrString .= sprintf (' %s ' , $ key );
107+ }else {
108+ $ attrString .= sprintf (' %s="%s" ' , $ key , htmlspecialchars ($ value ));
109+ }
110+ }
96111
97112 return \sprintf (<<<EOHTML
98- <turbo-stream action="%s" targets="%s" %s>
113+ <turbo-stream%s>
99114 <template>%s</template>
100115 </turbo-stream>
101- EOHTML , $ action , htmlspecialchars ( $ target ), $ atrrString , $ html );
116+ EOHTML , $ attrString , $ html );
102117 }
103118}
0 commit comments