chore: simplify `process_children`

custom-render-shim-dom
paoloricciuti 6 months ago
parent 95ce311613
commit 59902ccead

@ -69,6 +69,7 @@ export function Fragment(node, context) {
template: [], template: [],
locations: [], locations: [],
transform: { ...context.state.transform }, transform: { ...context.state.transform },
is_functional_template_mode: context.state.is_functional_template_mode,
metadata: { metadata: {
context: { context: {
template_needs_import_node: false, template_needs_import_node: false,
@ -125,40 +126,22 @@ export function Fragment(node, context) {
// special case — we can use `$.text` instead of creating a unique template // special case — we can use `$.text` instead of creating a unique template
const id = b.id(context.state.scope.generate('text')); const id = b.id(context.state.scope.generate('text'));
process_children( process_children(trimmed, () => id, false, {
trimmed,
() => id,
false,
{
...context, ...context,
state state
}, });
context.state.is_functional_template_mode
);
body.push(b.var(id, b.call('$.text'))); body.push(b.var(id, b.call('$.text')));
close = b.stmt(b.call('$.append', b.id('$$anchor'), id)); close = b.stmt(b.call('$.append', b.id('$$anchor'), id));
} else { } else {
if (is_standalone) { if (is_standalone) {
// no need to create a template, we can just use the existing block's anchor // no need to create a template, we can just use the existing block's anchor
process_children( process_children(trimmed, () => b.id('$$anchor'), false, { ...context, state });
trimmed,
() => b.id('$$anchor'),
false,
{ ...context, state },
context.state.is_functional_template_mode
);
} else { } else {
/** @type {(is_text: boolean) => Expression} */ /** @type {(is_text: boolean) => Expression} */
const expression = (is_text) => b.call('$.first_child', id, is_text && b.true); const expression = (is_text) => b.call('$.first_child', id, is_text && b.true);
process_children( process_children(trimmed, expression, false, { ...context, state });
trimmed,
expression,
false,
{ ...context, state },
context.state.is_functional_template_mode
);
let flags = TEMPLATE_FRAGMENT; let flags = TEMPLATE_FRAGMENT;

@ -375,7 +375,8 @@ export function RegularElement(node, context) {
locations: [], locations: [],
scope: /** @type {Scope} */ (context.state.scopes.get(node.fragment)), scope: /** @type {Scope} */ (context.state.scopes.get(node.fragment)),
preserve_whitespace: preserve_whitespace:
context.state.preserve_whitespace || node.name === 'pre' || node.name === 'textarea' context.state.preserve_whitespace || node.name === 'pre' || node.name === 'textarea',
is_functional_template_mode: context.state.is_functional_template_mode
}; };
const { hoisted, trimmed } = clean_nodes( const { hoisted, trimmed } = clean_nodes(
@ -430,16 +431,10 @@ export function RegularElement(node, context) {
arg = b.member(arg, 'content'); arg = b.member(arg, 'content');
} }
process_children( process_children(trimmed, (is_text) => b.call('$.child', arg, is_text && b.true), true, {
trimmed,
(is_text) => b.call('$.child', arg, is_text && b.true),
true,
{
...context, ...context,
state: child_state state: child_state
}, });
context.state.is_functional_template_mode
);
if (needs_reset) { if (needs_reset) {
child_state.init.push(b.stmt(b.call('$.reset', context.state.node))); child_state.init.push(b.stmt(b.call('$.reset', context.state.node)));

@ -15,15 +15,8 @@ import { build_template_chunk } from './utils.js';
* @param {(is_text: boolean) => Expression} initial * @param {(is_text: boolean) => Expression} initial
* @param {boolean} is_element * @param {boolean} is_element
* @param {ComponentContext} context * @param {ComponentContext} context
* @param {boolean} [is_functional_template_mode]
*/ */
export function process_children( export function process_children(nodes, initial, is_element, { visit, state }) {
nodes,
initial,
is_element,
{ visit, state },
is_functional_template_mode
) {
const within_bound_contenteditable = state.metadata.bound_contenteditable; const within_bound_contenteditable = state.metadata.bound_contenteditable;
let prev = initial; let prev = initial;
let skipped = 0; let skipped = 0;
@ -74,7 +67,9 @@ export function process_children(
state.template.push({ state.template.push({
kind: 'create_text', kind: 'create_text',
args: [ args: [
sequence.map((node) => (is_functional_template_mode ? node.data : node.raw)).join('') sequence
.map((node) => (state.is_functional_template_mode ? node.data : node.raw))
.join('')
] ]
}); });
return; return;

@ -20,7 +20,7 @@ export function Fragment(node, context) {
context.state, context.state,
context.state.preserve_whitespace, context.state.preserve_whitespace,
context.state.options.preserveComments, context.state.options.preserveComments,
// prevent template cloning should always be false on the server // templating mode doesn't affect server builds
false false
); );

@ -48,7 +48,7 @@ export function RegularElement(node, context) {
}, },
state.preserve_whitespace, state.preserve_whitespace,
state.options.preserveComments, state.options.preserveComments,
// prevent template cloning should always be false on the server // templating mode doesn't affect server builds
false false
); );

Loading…
Cancel
Save