Convert src/compiler/compile/render_ssr/handlers/shared/get_attribute_value.ts to JavaScript

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

@ -1,43 +1,52 @@
import Attribute from '../../../nodes/Attribute';
import { string_literal } from '../../../utils/stringify';
import Text from '../../../nodes/Text';
import { string_literal } from '../../../utils/stringify.js';
import { x } from 'code-red';
import Expression from '../../../nodes/shared/Expression';
import { Expression as ESTreeExpression } from 'estree';
import { regex_double_quotes } from '../../../../utils/patterns';
import { regex_double_quotes } from '../../../../utils/patterns.js';
export function get_class_attribute_value(attribute: Attribute): ESTreeExpression {
// handle special case — `class={possiblyUndefined}` with scoped CSS
if (attribute.chunks.length === 2 && (attribute.chunks[1] as Text).synthetic) {
const value = (attribute.chunks[0] as Expression).node;
return x`@escape(@null_to_empty(${value}), true) + "${(attribute.chunks[1] as Text).data}"`;
}
/**
* @param {import('../../../nodes/Attribute.js').default} attribute
* @returns {import("C:/repos/svelte/svelte/node_modules/.pnpm/@types+estree@1.0.0/node_modules/@types/estree/index").Expression}
*/
export function get_class_attribute_value(attribute) {
// handle special case — `class={possiblyUndefined}` with scoped CSS
if (attribute.chunks.length === 2 && ( /** @type {import('../../../nodes/Text.js').default} */(attribute.chunks[1])).synthetic) {
const value = ( /** @type {import('../../../nodes/shared/Expression.js').default} */(attribute.chunks[0])).node;
return x `@escape(@null_to_empty(${value}), true) + "${( /** @type {import('../../../nodes/Text.js').default} */(attribute.chunks[1])).data}"`;
}
return get_attribute_value(attribute);
}
return get_attribute_value(attribute);
/**
* @param {import('../../../nodes/Attribute.js').default} attribute
* @returns {import("C:/repos/svelte/svelte/node_modules/.pnpm/@types+estree@1.0.0/node_modules/@types/estree/index").Expression}
*/
export function get_attribute_value(attribute) {
if (attribute.chunks.length === 0)
return x `""`;
/**
* For value attribute of textarea, it will render as child node of `<textarea>` element.
* Therefore, we need to escape as content (not attribute).
*/
const is_textarea_value = attribute.parent.name.toLowerCase() === 'textarea' && attribute.name.toLowerCase() === 'value';
return attribute.chunks
.map((chunk) => {
return chunk.type === 'Text'
? ( /** @type {ESTreeExpression} */(string_literal(chunk.data.replace(regex_double_quotes, '&quot;'))))
: x `@escape(${chunk.node}, ${is_textarea_value ? 'false' : 'true'})`;
})
.reduce((lhs, rhs) => x `${lhs} + ${rhs}`);
}
/**
* @param {import('../../../nodes/Attribute.js').default} attribute
* @returns {import("C:/repos/svelte/svelte/node_modules/.pnpm/@types+estree@1.0.0/node_modules/@types/estree/index").Expression}
*/
export function get_attribute_expression(attribute) {
if (attribute.chunks.length === 1 && attribute.chunks[0].type === 'Expression') {
return /** @type {ESTreeExpression} */ (( /** @type {import('../../../nodes/shared/Expression.js').default} */(attribute.chunks[0])).node);
}
return get_attribute_value(attribute);
}
export function get_attribute_value(attribute: Attribute): ESTreeExpression {
if (attribute.chunks.length === 0) return x`""`;
/**
* For value attribute of textarea, it will render as child node of `<textarea>` element.
* Therefore, we need to escape as content (not attribute).
*/
const is_textarea_value =
attribute.parent.name.toLowerCase() === 'textarea' && attribute.name.toLowerCase() === 'value';
return attribute.chunks
.map((chunk) => {
return chunk.type === 'Text'
? (string_literal(chunk.data.replace(regex_double_quotes, '&quot;')) as ESTreeExpression)
: x`@escape(${chunk.node}, ${is_textarea_value ? 'false' : 'true'})`;
})
.reduce((lhs, rhs) => x`${lhs} + ${rhs}`);
}
export function get_attribute_expression(attribute: Attribute): ESTreeExpression {
if (attribute.chunks.length === 1 && attribute.chunks[0].type === 'Expression') {
return (attribute.chunks[0] as Expression).node as ESTreeExpression;
}
return get_attribute_value(attribute);
}

@ -1,17 +1,18 @@
import { trim_end, trim_start } from '../../../../utils/trim.js';
import { link } from '../../../../utils/link';
import { regex_starts_with_whitespace } from '../../../../utils/patterns';
import { link } from '../../../../utils/link.js';
import { regex_starts_with_whitespace } from '../../../../utils/patterns.js';
// similar logic from `compile/render_dom/wrappers/Fragment`
// We want to remove trailing whitespace inside an element/component/block,
// *unless* there is no whitespace between this node and its next sibling
/**
* @param {INode[]} children
* @param {import('../../../nodes/interfaces.js').INode[]} children
* @param {import('../../../nodes/interfaces.js').INode} [next]
* @returns {INode[]}
* @returns {import('../../../nodes/interfaces.js').INode[]}
*/
export default function remove_whitespace_children(children, next) {
/** @type {INode[]} */
/** @type {import('../../../nodes/interfaces.js').INode[]} */
const nodes = [];
/** @type {import('../../../nodes/interfaces.js').INode} */

Loading…
Cancel
Save