Lint and format src/compiler/compile/render_ssr/handlers/utils/remove_whitespace_children.js

pull/8569/head
Simon Holthausen 3 years ago
parent e6b61ce981
commit c33b34fa62

@ -11,58 +11,55 @@ import { regex_starts_with_whitespace } from '../../../../utils/patterns';
* @returns {INode[]} * @returns {INode[]}
*/ */
export default function remove_whitespace_children(children, next) { export default function remove_whitespace_children(children, next) {
/** @type {INode[]} */
const nodes = [];
/** @type {INode[]} */ /** @type {import('../../../nodes/interfaces.js').INode} */
const nodes = []; let last_child;
let i = children.length;
/** @type {import('../../../nodes/interfaces.js').INode} */ while (i--) {
let last_child; const child = children[i];
let i = children.length; if (child.type === 'Text') {
while (i--) { if (child.should_skip()) {
const child = children[i]; continue;
if (child.type === 'Text') { }
if (child.should_skip()) { let { data } = child;
continue; if (nodes.length === 0) {
} const should_trim = next
let { data } = child; ? next.type === 'Text' &&
if (nodes.length === 0) { regex_starts_with_whitespace.test(next.data) &&
const should_trim = next trimmable_at(child, next)
? next.type === 'Text' && : !child.has_ancestor('EachBlock');
regex_starts_with_whitespace.test(next.data) && if (should_trim && !child.keep_space()) {
trimmable_at(child, next) data = trim_end(data);
: !child.has_ancestor('EachBlock'); if (!data) continue;
if (should_trim && !child.keep_space()) { }
data = trim_end(data); }
if (!data) // glue text nodes (which could e.g. be separated by comments) together
continue; if (last_child && last_child.type === 'Text') {
} last_child.data = data + last_child.data;
} continue;
// glue text nodes (which could e.g. be separated by comments) together }
if (last_child && last_child.type === 'Text') { child.data = data;
last_child.data = data + last_child.data; nodes.unshift(child);
continue; link(last_child, (last_child = child));
} } else {
child.data = data; nodes.unshift(child);
nodes.unshift(child); link(last_child, (last_child = child));
link(last_child, (last_child = child)); }
} }
else { const first = nodes[0];
nodes.unshift(child); if (first && first.type === 'Text' && !first.keep_space()) {
link(last_child, (last_child = child)); first.data = trim_start(first.data);
} if (!first.data) {
} first.var = null;
const first = nodes[0]; nodes.shift();
if (first && first.type === 'Text' && !first.keep_space()) { if (nodes[0]) {
first.data = trim_start(first.data); nodes[0].prev = null;
if (!first.data) { }
first.var = null; }
nodes.shift(); }
if (nodes[0]) { return nodes;
nodes[0].prev = null;
}
}
}
return nodes;
} }
/** /**
@ -71,13 +68,11 @@ export default function remove_whitespace_children(children, next) {
* @returns {boolean} * @returns {boolean}
*/ */
function trimmable_at(child, next_sibling) { 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 (next_sibling.find_nearest(/EachBlock/) === child.find_nearest(/EachBlock/) || return (
next_sibling.prev.type === 'EachBlock'); next_sibling.find_nearest(/EachBlock/) === child.find_nearest(/EachBlock/) ||
next_sibling.prev.type === 'EachBlock'
);
} }

Loading…
Cancel
Save