Skip to content

Commit f8a18bc

Browse files
authored
Fix crash on <title> (#32)
1 parent fd97e80 commit f8a18bc

File tree

5 files changed

+5269
-1
lines changed

5 files changed

+5269
-1
lines changed

src/parser/converts/element.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ export function* convertChildren(
133133
yield convertHeadElement(child, parent, ctx)
134134
continue
135135
}
136+
if (child.type === "Title") {
137+
yield convertTitleElement(child, parent, ctx)
138+
continue
139+
}
136140
if (child.type === "Options") {
137141
yield convertOptionsElement(child, parent, ctx)
138142
continue
@@ -170,7 +174,7 @@ function convertComment(
170174

171175
/** Convert for HTMLElement */
172176
function convertHTMLElement(
173-
node: SvAST.Element | SvAST.Slot,
177+
node: SvAST.Element | SvAST.Slot | SvAST.Title,
174178
parent: SvelteHTMLElement["parent"],
175179
ctx: Context,
176180
): SvelteHTMLElement {
@@ -499,6 +503,15 @@ function convertHeadElement(
499503
return convertSpecialElement(node, parent, ctx)
500504
}
501505

506+
/** Convert for title element. e.g. <title> */
507+
function convertTitleElement(
508+
node: SvAST.Title,
509+
parent: SvelteHTMLElement["parent"],
510+
ctx: Context,
511+
): SvelteHTMLElement {
512+
return convertHTMLElement(node, parent, ctx)
513+
}
514+
502515
/** Convert for options element. e.g. <svelte:options> */
503516
function convertOptionsElement(
504517
node: SvAST.Options,

src/parser/svelte-ast-types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export declare type TemplateNode =
2020
| Window
2121
| Body
2222
| Head
23+
| Title
2324
| Options
2425
| SlotTemplate
2526
| Slot
@@ -129,6 +130,12 @@ export interface Head extends BaseNode {
129130
children: TemplateNode[]
130131
attributes: AttributeOrDirective[]
131132
}
133+
export interface Title extends BaseNode {
134+
type: "Title"
135+
name: "title"
136+
children: TemplateNode[]
137+
attributes: AttributeOrDirective[]
138+
}
132139
export interface Options extends BaseNode {
133140
type: "Options"
134141
name: "svelte:options"
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<script context="module">
2+
export const prerender = true;
3+
</script>
4+
5+
<script>
6+
import Counter from '$lib/Counter/index.svelte';
7+
</script>
8+
9+
<svelte:head>
10+
<title>Home</title>
11+
</svelte:head>
12+
13+
<section>
14+
<h1>
15+
<div class="welcome">
16+
<picture>
17+
<source srcset="svelte-welcome.webp" type="image/webp" />
18+
<img src="svelte-welcome.png" alt="Welcome" />
19+
</picture>
20+
</div>
21+
22+
to your new<br />SvelteKit app
23+
</h1>
24+
25+
<h2>
26+
try editing <strong>src/routes/index.svelte</strong>
27+
</h2>
28+
29+
<Counter />
30+
</section>
31+
32+
<style>
33+
section {
34+
display: flex;
35+
flex-direction: column;
36+
justify-content: center;
37+
align-items: center;
38+
flex: 1;
39+
}
40+
41+
h1 {
42+
width: 100%;
43+
}
44+
45+
.welcome {
46+
position: relative;
47+
width: 100%;
48+
height: 0;
49+
padding: 0 0 calc(100% * 495 / 2048) 0;
50+
}
51+
52+
.welcome img {
53+
position: absolute;
54+
width: 100%;
55+
height: 100%;
56+
top: 0;
57+
display: block;
58+
}
59+
</style>

0 commit comments

Comments
 (0)