Skip to content

Commit 07aed41

Browse files
committed
colocate
1 parent 1be9120 commit 07aed41

File tree

4 files changed

+97
-108
lines changed

4 files changed

+97
-108
lines changed

packages/svelte/src/compiler/phases/3-transform/client/transform-template/index.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
/** @import { Node } from './types.js' */
55
import { dev, locator } from '../../../../state.js';
66
import * as b from '../../../../utils/builders.js';
7-
import { template_to_functions } from './to-functions.js';
8-
import { template_to_string } from './to-string.js';
97

108
/**
119
*
@@ -60,8 +58,8 @@ function build_locations(nodes) {
6058
export function transform_template(state, namespace, flags) {
6159
const expression =
6260
state.options.templatingMode === 'functional'
63-
? template_to_functions(state.template.nodes)
64-
: template_to_string(state.template.nodes);
61+
? state.template.as_objects()
62+
: state.template.as_string();
6563

6664
let call = b.call(
6765
get_template_function(namespace, state),

packages/svelte/src/compiler/phases/3-transform/client/transform-template/template.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
/** @import { AST } from '#compiler' */
22
/** @import { Node, Element } from './types'; */
3+
import { escape_html } from '../../../../../escaping.js';
4+
import { is_void } from '../../../../../utils.js';
5+
import * as b from '#compiler/builders';
6+
import fix_attribute_casing from './fix-attribute-casing.js';
7+
import { regex_starts_with_newline } from '../../../patterns.js';
38

49
export class Template {
510
/**
@@ -65,4 +70,94 @@ export class Template {
6570
set_prop(key, value) {
6671
/** @type {Element} */ (this.#element).attributes[key] = value;
6772
}
73+
74+
as_string() {
75+
return b.template([b.quasi(this.nodes.map(stringify).join(''), true)], []);
76+
}
77+
78+
as_objects() {
79+
// if the first item is a comment we need to add another comment for effect.start
80+
if (this.nodes[0].type === 'anchor') {
81+
this.nodes.unshift({ type: 'anchor', data: undefined });
82+
}
83+
84+
return b.array(this.nodes.map(objectify));
85+
}
86+
}
87+
88+
/**
89+
* @param {Node} item
90+
*/
91+
function stringify(item) {
92+
if (item.type === 'text') {
93+
return item.nodes.map((node) => node.raw).join('');
94+
}
95+
96+
if (item.type === 'anchor') {
97+
return item.data ? `<!--${item.data}-->` : '<!>';
98+
}
99+
100+
let str = `<${item.name}`;
101+
102+
for (const key in item.attributes) {
103+
const value = item.attributes[key];
104+
105+
str += ` ${key}`;
106+
if (value !== undefined) str += `="${escape_html(value, true)}"`;
107+
}
108+
109+
str += `>`;
110+
str += item.children.map(stringify).join('');
111+
112+
if (!is_void(item.name)) {
113+
str += `</${item.name}>`;
114+
}
115+
116+
return str;
117+
}
118+
119+
/** @param {Node} item */
120+
function objectify(item) {
121+
if (item.type === 'text') {
122+
return b.literal(item.nodes.map((node) => node.data).join(''));
123+
}
124+
125+
if (item.type === 'anchor') {
126+
return item.data ? b.array([b.literal(`// ${item.data}`)]) : null;
127+
}
128+
129+
const element = b.array([b.literal(item.name)]);
130+
131+
const attributes = b.object([]);
132+
133+
for (const key in item.attributes) {
134+
const value = item.attributes[key];
135+
136+
attributes.properties.push(
137+
b.prop(
138+
'init',
139+
b.key(fix_attribute_casing(key)),
140+
value === undefined ? b.void0 : b.literal(value)
141+
)
142+
);
143+
}
144+
145+
if (attributes.properties.length > 0 || item.children.length > 0) {
146+
element.elements.push(attributes.properties.length > 0 ? attributes : b.null);
147+
}
148+
149+
if (item.children.length > 0) {
150+
const children = item.children.map(objectify);
151+
element.elements.push(...children);
152+
153+
// special case — strip leading newline from `<pre>` and `<textarea>`
154+
if (item.name === 'pre' || item.name === 'textarea') {
155+
const first = children[0];
156+
if (first?.type === 'Literal') {
157+
first.value = /** @type {string} */ (first.value).replace(regex_starts_with_newline, '');
158+
}
159+
}
160+
}
161+
162+
return element;
68163
}

packages/svelte/src/compiler/phases/3-transform/client/transform-template/to-functions.js

Lines changed: 0 additions & 62 deletions
This file was deleted.

packages/svelte/src/compiler/phases/3-transform/client/transform-template/to-string.js

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)