Skip to content

Commit f36b414

Browse files
preprocess self-closing script and style tags (#5082)
1 parent 4910f57 commit f36b414

File tree

7 files changed

+36
-4
lines changed

7 files changed

+36
-4
lines changed

src/compiler/preprocess/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ export default async function preprocess(
9494
for (const fn of script) {
9595
source = await replace_async(
9696
source,
97-
/<!--[^]*?-->|<script(\s[^]*?)?>([^]*?)<\/script>/gi,
98-
async (match, attributes = '', content) => {
97+
/<!--[^]*?-->|<script(\s[^]*?)?(?:>([^]*?)<\/script>|\/>)/gi,
98+
async (match, attributes = '', content = '') => {
9999
if (!attributes && !content) {
100100
return match;
101101
}
@@ -114,8 +114,8 @@ export default async function preprocess(
114114
for (const fn of style) {
115115
source = await replace_async(
116116
source,
117-
/<!--[^]*?-->|<style(\s[^]*?)?>([^]*?)<\/style>/gi,
118-
async (match, attributes = '', content) => {
117+
/<!--[^]*?-->|<style(\s[^]*?)?(?:>([^]*?)<\/style>|\/>)/gi,
118+
async (match, attributes = '', content = '') => {
119119
if (!attributes && !content) {
120120
return match;
121121
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import * as assert from "assert";
2+
3+
export default {
4+
preprocess: {
5+
script: ({ content, attributes }) => {
6+
assert.equal(content, "");
7+
return {
8+
code: `console.log("${attributes["the-answer"]}");`
9+
};
10+
}
11+
}
12+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<script the-answer="42"/>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<script the-answer="42">console.log("42");</script>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import * as assert from "assert";
2+
3+
export default {
4+
preprocess: {
5+
style: ({ content, attributes: { color } }) => {
6+
assert.equal(content, "");
7+
return {
8+
code: `div { color: ${color}; }`
9+
};
10+
}
11+
}
12+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div class='brand-color'>$brand</div>
2+
3+
<style color="red"/>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div class='brand-color'>$brand</div>
2+
3+
<style color="red">div { color: red; }</style>

0 commit comments

Comments
 (0)