Skip to content

Commit 19fe8ba

Browse files
committed
Fix whitespace output issues with self-closing style tags
1 parent f44f6c6 commit 19fe8ba

File tree

6 files changed

+51
-4
lines changed

6 files changed

+51
-4
lines changed

globals.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ declare module 'vue-template-compiler' {
1515
type: string;
1616
content: string;
1717
start: number;
18-
end: number;
18+
end?: number;
1919
lang?: string;
2020
src?: string;
2121
scoped?: boolean;
@@ -29,7 +29,7 @@ declare module 'vue-template-compiler' {
2929
type: string;
3030
content: string;
3131
start: number;
32-
end: number;
32+
end?: number;
3333
src?: string;
3434
attrs: {
3535
[attribute: string]: string

src/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,16 @@ export const parse = (source: string): SFCDescriptor =>
1515
* Note that whitespace is preserved for debugging purposes
1616
*/
1717
export const removeStyleBlock = (source: string, styleDescriptor: SFCBlock): string => {
18+
const isSelfClosing = styleDescriptor.end === undefined;
19+
1820
const start = source.lastIndexOf('<', styleDescriptor.start - 1);
19-
const end = source.indexOf('>', styleDescriptor.end);
21+
const end = isSelfClosing
22+
? styleDescriptor.start
23+
: source.indexOf('>', styleDescriptor.end);
2024
const lines = source.slice(start, end).split('\n').length;
25+
2126
return source.substring(0, start)
22-
+ Array(lines).map(() => '').join('\n')
27+
+ '\n'.repeat(lines > 1 ? lines - 1 : isSelfClosing ? 1 : 0)
2328
+ source.substring(end + 1, source.length);
2429
};
2530

test/cases/mixed-use.in.vue

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<style>
2+
h1 {
3+
color: red;
4+
}
5+
</style>
6+
7+
<style theme="remove" src="./test.css"/>
8+
9+
<style theme="remove">h1 { color: pink }</style>
10+
11+
<style theme="test">
12+
div {
13+
box-sizing: border-box;
14+
}
15+
</style>

test/cases/mixed-use.out.vue

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<style>
2+
h1 {
3+
color: red;
4+
}
5+
</style>
6+
7+
8+
9+
10+
11+
<style theme="test">
12+
div {
13+
box-sizing: border-box;
14+
}
15+
</style>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
<style>
2+
h1 {
3+
color: blue;
4+
}
5+
</style>
6+
17
<style src="./style.css" theme="remove" />
28

39
<style src="./style2.css" />
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
<style>
2+
h1 {
3+
color: blue;
4+
}
5+
</style>
6+
17

28

39
<style src="./style2.css" />

0 commit comments

Comments
 (0)