|
| 1 | +import { x } from 'code-red'; |
| 2 | +import { TemplateNode } from '../../interfaces'; |
| 3 | +import { regex_only_whitespaces } from '../../utils/patterns'; |
| 4 | +import compiler_errors from '../compiler_errors'; |
| 5 | +import Component from '../Component'; |
| 6 | + |
| 7 | +export function extract_children_to_slot_templates(component: Component, node: TemplateNode, extract_default_slot: boolean) { |
| 8 | + const result = []; |
| 9 | + for (let i = node.children.length - 1; i >= 0; i--) { |
| 10 | + const child = node.children[i]; |
| 11 | + if (child.type === 'SlotTemplate') { |
| 12 | + result.push(child); |
| 13 | + node.children.splice(i, 1); |
| 14 | + } else if ((child.type === 'Element' || child.type === 'InlineComponent' || child.type === 'Slot') && child.attributes.find(attribute => attribute.name === 'slot')) { |
| 15 | + let slot_template = { |
| 16 | + start: child.start, |
| 17 | + end: child.end, |
| 18 | + type: 'SlotTemplate', |
| 19 | + name: 'svelte:fragment', |
| 20 | + attributes: [], |
| 21 | + children: [child] |
| 22 | + }; |
| 23 | + |
| 24 | + // transfer attributes |
| 25 | + for (let i = child.attributes.length - 1; i >= 0; i--) { |
| 26 | + const attribute = child.attributes[i]; |
| 27 | + if (attribute.type === 'Let') { |
| 28 | + slot_template.attributes.push(attribute); |
| 29 | + child.attributes.splice(i, 1); |
| 30 | + } else if (attribute.type === 'Attribute' && attribute.name === 'slot') { |
| 31 | + slot_template.attributes.push(attribute); |
| 32 | + } |
| 33 | + } |
| 34 | + // transfer const |
| 35 | + for (let i = child.children.length - 1; i >= 0; i--) { |
| 36 | + const child_child = child.children[i]; |
| 37 | + if (child_child.type === 'ConstTag') { |
| 38 | + slot_template.children.push(child_child); |
| 39 | + child.children.splice(i, 1); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + // if the <slot slot="x"> does not have any fallback |
| 44 | + // then we make <slot slot="x" name="b" /> |
| 45 | + // into {#if $$slots.x}<slot slot="x" name="b" />{/if} |
| 46 | + // this makes the slots forwarding to passthrough |
| 47 | + if (child.type === 'Slot' && child.children.length === 0) { |
| 48 | + const slot_template_name = child.attributes.find(attribute => attribute.name === 'name')?.value[0].data ?? 'default'; |
| 49 | + slot_template = { |
| 50 | + start: slot_template.start, |
| 51 | + end: slot_template.end, |
| 52 | + type: 'SlotTemplateIfBlock', |
| 53 | + expression: x`$$slots.${slot_template_name}`, |
| 54 | + children: [slot_template] |
| 55 | + } as any; |
| 56 | + } |
| 57 | + |
| 58 | + result.push(slot_template); |
| 59 | + node.children.splice(i, 1); |
| 60 | + } else if (child.type === 'Comment' && result.length > 0) { |
| 61 | + result[result.length - 1].children.unshift(child); |
| 62 | + node.children.splice(i, 1); |
| 63 | + } else if (child.type === 'Text' && regex_only_whitespaces.test(child.data)) { |
| 64 | + // ignore |
| 65 | + } else if (child.type === 'ConstTag') { |
| 66 | + if (!extract_default_slot) { |
| 67 | + result.push(child); |
| 68 | + } |
| 69 | + } else if (child.type === 'IfBlock' && if_block_contains_slot_template(child)) { |
| 70 | + result.push({ |
| 71 | + ...child, |
| 72 | + type: 'SlotTemplateIfBlock' |
| 73 | + }); |
| 74 | + node.children.splice(i, 1); |
| 75 | + } else if (!extract_default_slot) { |
| 76 | + component.error(child, compiler_errors.invalid_mix_element_and_conditional_slot); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + if (extract_default_slot) { |
| 81 | + if (node.children.some(node => not_whitespace_text(node))) { |
| 82 | + result.push({ |
| 83 | + start: node.start, |
| 84 | + end: node.end, |
| 85 | + type: 'SlotTemplate', |
| 86 | + name: 'svelte:fragment', |
| 87 | + attributes: [], |
| 88 | + children: node.children |
| 89 | + }); |
| 90 | + } |
| 91 | + } |
| 92 | + return result.reverse(); |
| 93 | +} |
| 94 | + |
| 95 | + |
| 96 | +function not_whitespace_text(node) { |
| 97 | + return !(node.type === 'Text' && regex_only_whitespaces.test(node.data)); |
| 98 | +} |
| 99 | + |
| 100 | +function if_block_contains_slot_template(node: TemplateNode) { |
| 101 | + for (const child of node.children) { |
| 102 | + if (child.type === 'SlotTemplate') return true; |
| 103 | + if (child.type === 'IfBlock' && if_block_contains_slot_template(child)) return true; |
| 104 | + if ((child.type === 'Element' || child.type === 'InlineComponent' || child.type === 'Slot') && child.attributes.find(attribute => attribute.name === 'slot')) return true; |
| 105 | + } |
| 106 | + return false; |
| 107 | +} |
0 commit comments