Skip to content

Commit e705a67

Browse files
committed
half working css props
1 parent d6c3738 commit e705a67

File tree

3 files changed

+65
-50
lines changed

3 files changed

+65
-50
lines changed

packages/svelte/src/compiler/phases/3-transform/server/transform-server.js

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -960,9 +960,12 @@ function serialize_element_spread_attributes(
960960
* @param {import('#compiler').Component | import('#compiler').SvelteComponent | import('#compiler').SvelteSelf} node
961961
* @param {string | import('estree').Expression} component_name
962962
* @param {import('./types').ComponentContext} context
963-
* @returns {import('estree').Statement}
963+
* @returns {import('estree').Statement[]}
964964
*/
965965
function serialize_inline_component(node, component_name, context) {
966+
/** @type {import('./types').Template[]} */
967+
const parts = [];
968+
966969
/** @type {Array<import('estree').Property[] | import('estree').Expression>} */
967970
const props_and_spreads = [];
968971

@@ -1124,37 +1127,43 @@ function serialize_inline_component(node, component_name, context) {
11241127
b.array(props_and_spreads.map((p) => (Array.isArray(p) ? b.object(p) : p)))
11251128
);
11261129

1127-
/** @type {import('estree').Statement} */
1128-
let statement = b.stmt(
1129-
(typeof component_name === 'string' ? b.call : b.maybe_call)(
1130-
context.state.options.dev
1131-
? b.call(
1132-
'$.validate_component',
1133-
typeof component_name === 'string' ? b.id(component_name) : component_name
1134-
)
1135-
: component_name,
1136-
b.id('$$payload'),
1137-
props_expression
1138-
)
1139-
);
1130+
/** @type {import('estree').Statement[]} */
1131+
let statements = [
1132+
b.stmt(b.assignment('+=', b.id('$$payload.out'), b.literal(block_open.value))),
1133+
b.stmt(
1134+
(typeof component_name === 'string' ? b.call : b.maybe_call)(
1135+
context.state.options.dev
1136+
? b.call(
1137+
'$.validate_component',
1138+
typeof component_name === 'string' ? b.id(component_name) : component_name
1139+
)
1140+
: component_name,
1141+
b.id('$$payload'),
1142+
props_expression
1143+
)
1144+
),
1145+
b.stmt(b.assignment('+=', b.id('$$payload.out'), b.literal(block_close.value)))
1146+
];
11401147

11411148
if (custom_css_props.length > 0) {
1142-
statement = b.stmt(
1143-
b.call(
1144-
'$.css_props',
1145-
b.id('$$payload'),
1146-
b.literal(context.state.metadata.namespace === 'svg' ? false : true),
1147-
b.object(custom_css_props),
1148-
b.thunk(b.block([statement]))
1149+
statements = [
1150+
b.stmt(
1151+
b.call(
1152+
'$.css_props',
1153+
b.id('$$payload'),
1154+
b.literal(context.state.metadata.namespace === 'svg' ? false : true),
1155+
b.object(custom_css_props),
1156+
b.thunk(b.block(statements))
1157+
)
11491158
)
1150-
);
1159+
];
11511160
}
11521161

11531162
if (snippet_declarations.length > 0) {
1154-
statement = b.block([...snippet_declarations, statement]);
1163+
statements.unshift(...snippet_declarations);
11551164
}
11561165

1157-
return statement;
1166+
return statements;
11581167
}
11591168

11601169
/**
@@ -1653,29 +1662,27 @@ const template_visitors = {
16531662
}
16541663
},
16551664
Component(node, context) {
1656-
const state = context.state;
1657-
state.template.push(block_open);
1658-
const call = serialize_inline_component(node, node.name, context);
1659-
state.template.push(t_statement(call));
1660-
state.template.push(block_close);
1665+
context.state.template.push(
1666+
...serialize_inline_component(node, node.name, context).map((statement) =>
1667+
t_statement(statement)
1668+
)
1669+
);
16611670
},
16621671
SvelteSelf(node, context) {
1663-
const state = context.state;
1664-
state.template.push(block_open);
1665-
const call = serialize_inline_component(node, context.state.analysis.name, context);
1666-
state.template.push(t_statement(call));
1667-
state.template.push(block_close);
1672+
context.state.template.push(
1673+
...serialize_inline_component(node, context.state.analysis.name, context).map((statement) =>
1674+
t_statement(statement)
1675+
)
1676+
);
16681677
},
16691678
SvelteComponent(node, context) {
1670-
const state = context.state;
1671-
state.template.push(block_open);
1672-
const call = serialize_inline_component(
1673-
node,
1674-
/** @type {import('estree').Expression} */ (context.visit(node.expression)),
1675-
context
1679+
context.state.template.push(
1680+
...serialize_inline_component(
1681+
node,
1682+
/** @type {import('estree').Expression} */ (context.visit(node.expression)),
1683+
context
1684+
).map((statement) => t_statement(statement))
16761685
);
1677-
state.template.push(t_statement(call));
1678-
state.template.push(block_close);
16791686
},
16801687
LetDirective(node, { state }) {
16811688
if (node.expression && node.expression.type !== 'Identifier') {

packages/svelte/src/internal/client/dom/blocks/css-props.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
11
import { namespace_svg } from '../../../../constants.js';
2-
import { hydrate_anchor, hydrate_start, hydrating } from '../hydration.js';
2+
import { hydrate_anchor, hydrating } from '../hydration.js';
33
import { empty } from '../operations.js';
44
import { render_effect } from '../../reactivity/effects.js';
55

66
/**
7-
* @param {Element | Text | Comment} anchor
7+
* @param {HTMLElement | SVGElement | Comment} node
88
* @param {boolean} is_html
99
* @param {() => Record<string, string>} props
1010
* @param {(anchor: Element | Text | Comment) => any} component
1111
* @returns {void}
1212
*/
13-
export function css_props(anchor, is_html, props, component) {
13+
export function css_props(node, is_html, props, component) {
1414
/** @type {HTMLElement | SVGElement} */
1515
let element;
1616

17+
/** @type {Comment} */
18+
let anchor;
19+
1720
/** @type {Text | Comment} */
1821
let component_anchor;
1922

2023
if (hydrating) {
2124
// Hydration: css props element is surrounded by a ssr comment ...
22-
element = /** @type {HTMLElement | SVGElement} */ (hydrate_start);
25+
element = /** @type {HTMLElement | SVGElement} */ (node);
26+
27+
anchor = /** @type {Comment} */ (element.nextSibling);
28+
2329
// ... and the child(ren) of the css props element is also surround by a ssr comment
2430
component_anchor = /** @type {Comment} */ (
2531
hydrate_anchor(/** @type {Comment} */ (element.firstChild))
@@ -32,6 +38,8 @@ export function css_props(anchor, is_html, props, component) {
3238
element = document.createElementNS(namespace_svg, 'g');
3339
}
3440

41+
anchor = /** @type {Comment} */ (node);
42+
3543
anchor.before(element);
3644
component_anchor = element.appendChild(empty());
3745
}

packages/svelte/src/internal/server/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,15 +171,15 @@ export function attr(name, value, boolean) {
171171
export function css_props(payload, is_html, props, component) {
172172
const styles = style_object_to_string(props);
173173
if (is_html) {
174-
payload.out += `<div style="display: contents; ${styles}"><!--[-->`;
174+
payload.out += `<div style="display: contents; ${styles}">`;
175175
} else {
176-
payload.out += `<g style="${styles}"><!--[-->`;
176+
payload.out += `<g style="${styles}">`;
177177
}
178178
component();
179179
if (is_html) {
180-
payload.out += `<!--]--></div>`;
180+
payload.out += `<!----></div>`;
181181
} else {
182-
payload.out += `<!--]--></g>`;
182+
payload.out += `<!----></g>`;
183183
}
184184
}
185185

0 commit comments

Comments
 (0)