Skip to content

Commit 07aeb65

Browse files
authored
Fix wrong AST for nesting script and style tags. (#68)
1 parent 3cff6cc commit 07aeb65

14 files changed

+3269
-13
lines changed

Diff for: src/context/index.ts

+31-13
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ import { LetDirectiveCollections } from "./let-directive-collection"
77
import { getParserName } from "../parser/resolve-parser"
88

99
export class ScriptsSourceCode {
10-
private readonly raw
10+
private raw: string
11+
12+
private trimmedRaw: string
1113

1214
public readonly attrs: Record<string, string | undefined>
1315

14-
private _vcode: string | null = null
16+
private _appendScriptLets: string | null = null
1517

1618
public separateSemiIndex: number
1719

@@ -20,32 +22,44 @@ export class ScriptsSourceCode {
2022
attrs: Record<string, string | undefined>,
2123
) {
2224
this.raw = script
25+
this.trimmedRaw = script.trimEnd()
2326
this.attrs = attrs
2427
this.separateSemiIndex = script.length
2528
}
2629

2730
public get vcode(): string {
28-
if (this._vcode == null) {
31+
if (this._appendScriptLets == null) {
2932
return this.raw
3033
}
31-
return this._vcode
34+
return this.trimmedRaw + this._appendScriptLets
3235
}
3336

3437
public addLet(letCode: string): { start: number; end: number } {
35-
if (this._vcode == null) {
36-
this._vcode = this.raw.trimEnd()
37-
this.separateSemiIndex = this._vcode.length
38-
this._vcode += ";"
39-
const after = this.raw.slice(this._vcode.length)
40-
this._vcode += after
38+
if (this._appendScriptLets == null) {
39+
this._appendScriptLets = ""
40+
this.separateSemiIndex = this.vcode.length
41+
this._appendScriptLets += ";"
42+
const after = this.raw.slice(this.vcode.length)
43+
this._appendScriptLets += after
4144
}
42-
const start = this._vcode.length
43-
this._vcode += letCode
45+
const start = this.vcode.length
46+
this._appendScriptLets += letCode
4447
return {
4548
start,
46-
end: this._vcode.length,
49+
end: this.vcode.length,
4750
}
4851
}
52+
53+
public stripCode(start: number, end: number): void {
54+
this.raw =
55+
this.raw.slice(0, start) +
56+
this.raw.slice(start, end).replace(/[^\n\r ]/g, " ") +
57+
this.raw.slice(end)
58+
this.trimmedRaw =
59+
this.trimmedRaw.slice(0, start) +
60+
this.trimmedRaw.slice(start, end).replace(/[^\n\r ]/g, " ") +
61+
this.trimmedRaw.slice(end)
62+
}
4963
}
5064
export type ContextSourceCode = {
5165
template: string
@@ -202,6 +216,10 @@ export class Context {
202216

203217
return (this.state.isTypeScript = false)
204218
}
219+
220+
public stripScriptCode(start: number, end: number): void {
221+
this.sourceCode.scripts.stripCode(start, end)
222+
}
205223
}
206224

207225
/** Extract <script> blocks */

Diff for: src/parser/converts/element.ts

+14
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,20 @@ function convertHTMLElement(
236236
},
237237
})
238238

239+
if (element.name.name === "script" || element.name.name === "style") {
240+
for (const child of element.children) {
241+
if (child.type === "SvelteText") {
242+
child.value = ctx.code.slice(...child.range)
243+
}
244+
}
245+
if (element.name.name === "script") {
246+
ctx.stripScriptCode(
247+
element.startTag.range[1],
248+
element.endTag?.range[0] ?? element.range[1],
249+
)
250+
}
251+
}
252+
239253
return element
240254
}
241255

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<div>
2+
<style>
3+
/* this style tag will be inserted as-is */
4+
div {
5+
/* this will apply to all `<div>` elements in the DOM */
6+
color: red;
7+
}
8+
</style>
9+
</div>

0 commit comments

Comments
 (0)