mirror of https://github.com/sveltejs/svelte
parent
747d24a8ad
commit
60813bc18d
@ -1,51 +1,46 @@
|
|||||||
/** @import { Node } from './types.js' */
|
/** @import { Node } from './types.js' */
|
||||||
import { escape_html } from '../../../../../escaping.js';
|
import { escape_html } from '../../../../../escaping.js';
|
||||||
import { is_void } from '../../../../../utils.js';
|
import { is_void } from '../../../../../utils.js';
|
||||||
|
import * as b from '../../../../utils/builders.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Node[]} items
|
* @param {Node[]} items
|
||||||
*/
|
*/
|
||||||
export function template_to_string(items) {
|
export function template_to_string(items) {
|
||||||
return items.map((el) => stringify(el)).join('');
|
return b.template([b.quasi(items.map(stringify).join(''), true)], []);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* @param {Node} node
|
||||||
* @param {Node} el
|
|
||||||
* @returns
|
|
||||||
*/
|
*/
|
||||||
function stringify(el) {
|
function stringify(node) {
|
||||||
let str = ``;
|
switch (node.type) {
|
||||||
if (el.type === 'element') {
|
case 'element': {
|
||||||
// we create the <tagname part
|
let str = `<${node.name}`;
|
||||||
str += `<${el.name}`;
|
|
||||||
// we concatenate all the prop to it
|
for (const key in node.attributes) {
|
||||||
for (let [prop, value] of Object.entries(el.attributes ?? {})) {
|
const value = node.attributes[key];
|
||||||
if (value == null) {
|
|
||||||
str += ` ${prop}`;
|
str += ` ${key}`;
|
||||||
} else {
|
if (value !== undefined) str += `="${escape_html(value, true)}"`;
|
||||||
str += ` ${prop}="${escape_html(value, true)}"`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// then we close the opening tag
|
|
||||||
str += `>`;
|
|
||||||
// we stringify all the children and concatenate them
|
|
||||||
for (let child of el.children ?? []) {
|
|
||||||
str += stringify(child);
|
|
||||||
}
|
}
|
||||||
// if it's not void we also add the closing tag
|
|
||||||
if (!is_void(el.name)) {
|
str += `>`;
|
||||||
str += `</${el.name}>`;
|
str += node.children.map(stringify);
|
||||||
|
|
||||||
|
if (!is_void(node.name)) {
|
||||||
|
str += `</${node.name}>`;
|
||||||
}
|
}
|
||||||
} else if (el.type === 'text') {
|
|
||||||
str += el.nodes.map((node) => node.raw).join('');
|
return str;
|
||||||
} else if (el.type === 'anchor') {
|
|
||||||
if (el.data) {
|
|
||||||
str += `<!--${el.data}-->`;
|
|
||||||
} else {
|
|
||||||
str += `<!>`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case 'text': {
|
||||||
|
return node.nodes.map((node) => node.raw).join('');
|
||||||
}
|
}
|
||||||
|
|
||||||
return str;
|
case 'anchor': {
|
||||||
|
return node.data ? `<!--${node.data}-->` : '<!>';
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in new issue