Skip to content

Commit

Permalink
preprocess self-closing script and style tags (sveltejs#5082)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasmoellerch authored and taylorzane committed Dec 17, 2020
1 parent 406be31 commit dc540ad
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/compiler/preprocess/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ export default async function preprocess(
for (const fn of script) {
source = await replace_async(
source,
/<!--[^]*?-->|<script(\s[^]*?)?>([^]*?)<\/script>/gi,
async (match, attributes = '', content) => {
/<!--[^]*?-->|<script(\s[^]*?)?(?:>([^]*?)<\/script>|\/>)/gi,
async (match, attributes = '', content = '') => {
if (!attributes && !content) {
return match;
}
Expand All @@ -114,8 +114,8 @@ export default async function preprocess(
for (const fn of style) {
source = await replace_async(
source,
/<!--[^]*?-->|<style(\s[^]*?)?>([^]*?)<\/style>/gi,
async (match, attributes = '', content) => {
/<!--[^]*?-->|<style(\s[^]*?)?(?:>([^]*?)<\/style>|\/>)/gi,
async (match, attributes = '', content = '') => {
if (!attributes && !content) {
return match;
}
Expand Down
12 changes: 12 additions & 0 deletions test/preprocess/samples/script-self-closing/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as assert from "assert";

export default {
preprocess: {
script: ({ content, attributes }) => {
assert.equal(content, "");
return {
code: `console.log("${attributes["the-answer"]}");`
};
}
}
};
1 change: 1 addition & 0 deletions test/preprocess/samples/script-self-closing/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<script the-answer="42"/>
1 change: 1 addition & 0 deletions test/preprocess/samples/script-self-closing/output.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<script the-answer="42">console.log("42");</script>
12 changes: 12 additions & 0 deletions test/preprocess/samples/style-self-closing/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as assert from "assert";

export default {
preprocess: {
style: ({ content, attributes: { color } }) => {
assert.equal(content, "");
return {
code: `div { color: ${color}; }`
};
}
}
};
3 changes: 3 additions & 0 deletions test/preprocess/samples/style-self-closing/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class='brand-color'>$brand</div>

<style color="red"/>
3 changes: 3 additions & 0 deletions test/preprocess/samples/style-self-closing/output.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class='brand-color'>$brand</div>

<style color="red">div { color: red; }</style>

0 comments on commit dc540ad

Please sign in to comment.