Convert src/compiler/compile/render_dom/wrappers/Fragment.ts to JavaScript

pull/8569/head
Simon Holthausen 3 years ago
parent 57f5f5e3a4
commit f921b755a5

@ -3,6 +3,7 @@ import { b, x } from 'code-red';
import EventHandler from './Element/EventHandler.js'; import EventHandler from './Element/EventHandler.js';
import add_event_handlers from './shared/add_event_handlers.js'; import add_event_handlers from './shared/add_event_handlers.js';
import add_actions from './shared/add_actions.js'; import add_actions from './shared/add_actions.js';
const associated_events = { const associated_events = {
fullscreenElement: ['fullscreenchange'], fullscreenElement: ['fullscreenchange'],
visibilityState: ['visibilitychange'] visibilityState: ['visibilitychange']

@ -419,7 +419,7 @@ export default class EachBlockWrapper extends Wrapper {
* initial_mount_node: import('estree').Identifier; * initial_mount_node: import('estree').Identifier;
* update_anchor_node: import('estree').Identifier; * update_anchor_node: import('estree').Identifier;
* update_mount_node: import('estree').Identifier; * update_mount_node: import('estree').Identifier;
* }} * }} params
*/ */
render_keyed({ render_keyed({
block, block,

@ -1,30 +1,24 @@
import Wrapper from './shared/Wrapper'; import AwaitBlock from './AwaitBlock.js';
import AwaitBlock from './AwaitBlock'; import Body from './Body.js';
import Body from './Body'; import DebugTag from './DebugTag.js';
import DebugTag from './DebugTag'; import Document from './Document.js';
import Document from './Document'; import EachBlock from './EachBlock.js';
import EachBlock from './EachBlock'; import Element from './Element.js';
import Element from './Element'; import Head from './Head.js';
import Head from './Head'; import IfBlock from './IfBlock.js';
import IfBlock from './IfBlock'; import KeyBlock from './KeyBlock.js';
import KeyBlock from './KeyBlock'; import InlineComponent from './InlineComponent/index.js';
import InlineComponent from './InlineComponent/index'; import MustacheTag from './MustacheTag.js';
import MustacheTag from './MustacheTag'; import RawMustacheTag from './RawMustacheTag.js';
import RawMustacheTag from './RawMustacheTag'; import Slot from './Slot.js';
import Slot from './Slot'; import SlotTemplate from './SlotTemplate.js';
import SlotTemplate from './SlotTemplate'; import Text from './Text.js';
import Text from './Text'; import Comment from './Comment.js';
import Comment from './Comment'; import Title from './Title.js';
import Title from './Title'; import Window from './Window.js';
import Window from './Window'; import { trim_start, trim_end } from '../../../utils/trim.js';
import { INode } from '../../nodes/interfaces'; import { link } from '../../../utils/link.js';
import Renderer from '../Renderer'; import { regex_starts_with_whitespace } from '../../../utils/patterns.js';
import Block from '../Block';
import { trim_start, trim_end } from '../../../utils/trim';
import { link } from '../../../utils/link';
import { Identifier } from 'estree';
import { regex_starts_with_whitespace } from '../../../utils/patterns';
const wrappers = { const wrappers = {
AwaitBlock, AwaitBlock,
Body, Body,
@ -47,54 +41,56 @@ const wrappers = {
Window Window
}; };
function trimmable_at(child: INode, next_sibling: Wrapper): boolean { /**
* @param {import('../../nodes/interfaces.js').INode} child
* @param {import('./shared/Wrapper.js').default} next_sibling
* @returns {boolean}
*/
function trimmable_at(child, next_sibling) {
// Whitespace is trimmable if one of the following is true: // Whitespace is trimmable if one of the following is true:
// The child and its sibling share a common nearest each block (not at an each block boundary) // The child and its sibling share a common nearest each block (not at an each block boundary)
// The next sibling's previous node is an each block // The next sibling's previous node is an each block
return ( return (next_sibling.node.find_nearest(/EachBlock/) === child.find_nearest(/EachBlock/) ||
next_sibling.node.find_nearest(/EachBlock/) === child.find_nearest(/EachBlock/) || next_sibling.node.prev.type === 'EachBlock');
next_sibling.node.prev.type === 'EachBlock'
);
} }
export default class FragmentWrapper { export default class FragmentWrapper {
nodes: Wrapper[];
constructor( /** @type {import('./shared/Wrapper.js').default[]} */
renderer: Renderer, nodes;
block: Block,
nodes: INode[], /**
parent: Wrapper, * @param {import('../Renderer.js').default} renderer
strip_whitespace: boolean, * @param {import('../Block.js').default} block
next_sibling: Wrapper * @param {import('../../nodes/interfaces.js').INode[]} nodes
) { * @param {import('./shared/Wrapper.js').default} parent
* @param {boolean} strip_whitespace
* @param {import('./shared/Wrapper.js').default} next_sibling
*/
constructor(renderer, block, nodes, parent, strip_whitespace, next_sibling) {
this.nodes = []; this.nodes = [];
let last_child: Wrapper; /** @type {import('./shared/Wrapper.js').default} */
let window_wrapper: Window | undefined; let last_child;
/** @type {import('./Window.js').default | undefined} */
let window_wrapper;
let i = nodes.length; let i = nodes.length;
while (i--) { while (i--) {
const child = nodes[i]; const child = nodes[i];
if (!child.type) { if (!child.type) {
throw new Error('missing type'); throw new Error('missing type');
} }
if (!(child.type in wrappers)) { if (!(child.type in wrappers)) {
throw new Error(`TODO implement ${child.type}`); throw new Error(`TODO implement ${child.type}`);
} }
// special case — this is an easy way to remove whitespace surrounding // special case — this is an easy way to remove whitespace surrounding
// <svelte:window/>. lil hacky but it works // <svelte:window/>. lil hacky but it works
if (child.type === 'Window') { if (child.type === 'Window') {
window_wrapper = new Window(renderer, block, parent, child); window_wrapper = new Window(renderer, block, parent, child);
continue; continue;
} }
if (child.type === 'Text') { if (child.type === 'Text') {
let { data } = child; let { data } = child;
// We want to remove trailing whitespace inside an element/component/block, // We want to remove trailing whitespace inside an element/component/block,
// *unless* there is no whitespace between this node and its next sibling // *unless* there is no whitespace between this node and its next sibling
if (this.nodes.length === 0) { if (this.nodes.length === 0) {
@ -103,68 +99,63 @@ export default class FragmentWrapper {
regex_starts_with_whitespace.test(next_sibling.node.data) && regex_starts_with_whitespace.test(next_sibling.node.data) &&
trimmable_at(child, next_sibling) trimmable_at(child, next_sibling)
: !child.has_ancestor('EachBlock'); : !child.has_ancestor('EachBlock');
if (should_trim && !child.keep_space()) { if (should_trim && !child.keep_space()) {
data = trim_end(data); data = trim_end(data);
if (!data) continue; if (!data)
continue;
} }
} }
// glue text nodes (which could e.g. be separated by comments) together // glue text nodes (which could e.g. be separated by comments) together
if (last_child && last_child.node.type === 'Text') { if (last_child && last_child.node.type === 'Text') {
(last_child as Text).data = data + (last_child as Text).data; ( /** @type {import('./Text.js').default} */(last_child)).data = data + ( /** @type {import('./Text.js').default} */(last_child)).data;
continue; continue;
} }
const wrapper = new Text(renderer, block, parent, child, data); const wrapper = new Text(renderer, block, parent, child, data);
if (wrapper.skip) continue; if (wrapper.skip)
continue;
this.nodes.unshift(wrapper); this.nodes.unshift(wrapper);
link(last_child, (last_child = wrapper)); link(last_child, (last_child = wrapper));
} else { }
else {
const Wrapper = wrappers[child.type]; const Wrapper = wrappers[child.type];
if (!Wrapper || (child.type === 'Comment' && !renderer.options.preserveComments)) continue; if (!Wrapper || (child.type === 'Comment' && !renderer.options.preserveComments))
continue;
const wrapper = new Wrapper( const wrapper = new Wrapper(renderer, block, parent, child, strip_whitespace, last_child || next_sibling);
renderer,
block,
parent,
child,
strip_whitespace,
last_child || next_sibling
);
this.nodes.unshift(wrapper); this.nodes.unshift(wrapper);
link(last_child, (last_child = wrapper)); link(last_child, (last_child = wrapper));
} }
} }
if (strip_whitespace) { if (strip_whitespace) {
const first = this.nodes[0] as Text; const first = /** @type {import('./Text.js').default} */ (this.nodes[0]);
if (first && first.node.type === 'Text' && !first.node.keep_space()) { if (first && first.node.type === 'Text' && !first.node.keep_space()) {
first.data = trim_start(first.data); first.data = trim_start(first.data);
if (!first.data) { if (!first.data) {
first.var = null; first.var = null;
this.nodes.shift(); this.nodes.shift();
if (this.nodes[0]) { if (this.nodes[0]) {
this.nodes[0].prev = null; this.nodes[0].prev = null;
} }
} }
} }
} }
if (window_wrapper) { if (window_wrapper) {
this.nodes.unshift(window_wrapper); this.nodes.unshift(window_wrapper);
link(last_child, window_wrapper); link(last_child, window_wrapper);
} }
} }
render(block: Block, parent_node: Identifier, parent_nodes: Identifier) { /**
* @param {import('../Block.js').default} block
* @param {import('estree').Identifier} parent_node
* @param {import('estree').Identifier} parent_nodes
*/
render(block, parent_node, parent_nodes) {
for (let i = 0; i < this.nodes.length; i += 1) { for (let i = 0; i < this.nodes.length; i += 1) {
this.nodes[i].render(block, parent_node, parent_nodes); this.nodes[i].render(block, parent_node, parent_nodes);
} }
} }
} }

Loading…
Cancel
Save