Skip to content

Commit 79ba4e5

Browse files
committed
Update SourceCleaner to keep data-*
1 parent 123f64a commit 79ba4e5

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

ux.symfony.com/src/Model/RecipeFileTree.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function __construct()
2323
->addFile('assets/app.js', 'Your main JavaScript file. It\'s job is to import and load all other files.')
2424
->addFile('assets/controllers.json', 'Configures 3rd-party Stimulus controllers. This file is automatically updated when you install a UX package.')
2525
->addDirectory('assets/controllers', 'The home of your custom Stimulus controllers!')
26-
->addFile('assets/controllers/hello_controller.js', 'An example controller. Add it to any element with <code class="text-nowrap">{{ stimulus_controller(\'hello\') }}</code>')
26+
->addFile('assets/controllers/hello_controller.js', 'An example controller. Add it to any element with <code class="text-nowrap">data-controller="hello"</code>')
2727
->addDirectory('assets/styles')
2828
->addFile('assets/styles/app.css', 'Your main CSS file')
2929
->addFile('package.json', 'Holds your node dependencies, most importantly Stimulus & Webpack Encore.')

ux.symfony.com/src/Util/SourceCleaner.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,13 @@ public static function extractTwigBlock(string $content, string $targetTwigBlock
151151
public static function removeExcessHtml(string $content): string
152152
{
153153
// remove all HTML attributes and values + whitespace around them
154-
$content = preg_replace('/\s+[a-z0-9-]+="[^"]*"/', '', $content);
154+
$content = preg_replace_callback('/\s+[a-z0-9-]+="[^"]*"/', function($matches) {
155+
if (str_starts_with(trim($matches[0]), 'data-')) {
156+
return $matches[0];
157+
}
158+
159+
return '';
160+
}, $content);
155161

156162
// Find all the <div> elements without attributes
157163
preg_match_all('/<div>\s*(.*?)\s*<\/div>/s', $content, $matches);

ux.symfony.com/tests/Unit/Util/SourceCleanerTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,34 +109,34 @@ public function testItExtractsTwigBlock(): void
109109
public function testItRemovesExcessHtml(): void
110110
{
111111
$input = <<<EOF
112-
<div class="p-4 markdown-container shadow-blur shadow-blur--rainbow mt-5 row" {{ stimulus_controller('markdown') }}>
112+
<div class="p-4 markdown-container shadow-blur shadow-blur--rainbow mt-5 row" data-controller="markdown">
113113
<div class="col-12 col-md-5">
114114
<textarea rows="3" class="form-control" aria-label="Type markdown into this box"
115-
{{ stimulus_target('markdown', 'input') }}
115+
data-markdown-target="input"
116116
>Writing JavaScript is a **dream** with Stimulus 🤩</textarea>
117117
</div>
118118
<div class="col-12 col-md-2 text-center">
119-
<button class="btn btn-sm btn-dark mt-3" {{ stimulus_action('markdown', 'render') }}>
119+
<button class="btn btn-sm btn-dark mt-3" data-action="markdown#render">
120120
Convert <i class="fa fa-arrow-right"></i>
121121
</button>
122122
</div>
123123
<div class="col-12 col-md-5 mt-3 mt-md-0">
124-
<div style="min-height: 86px;" class="markdown-form-render-container p-2" {{ stimulus_target('markdown', 'preview') }} >
124+
<div style="min-height: 86px;" class="markdown-form-render-container p-2" data-markdown-target="preview">
125125
<small class="fw-light">(click "Convert")</small>
126126
</div>
127127
</div>
128128
</div>
129129
EOF;
130130

131131
$expected = <<<EOF
132-
<div {{ stimulus_controller('markdown') }}>
132+
<div data-controller="markdown">
133133
<textarea
134-
{{ stimulus_target('markdown', 'input') }}
134+
data-markdown-target="input"
135135
>Writing JavaScript is a **dream** with Stimulus 🤩</textarea>
136-
<button {{ stimulus_action('markdown', 'render') }}>
136+
<button data-action="markdown#render">
137137
Convert <i></i>
138138
</button>
139-
<div {{ stimulus_target('markdown', 'preview') }} >
139+
<div data-markdown-target="preview">
140140
<small>(click "Convert")</small>
141141
</div>
142142
</div>

0 commit comments

Comments
 (0)