mirror of https://github.com/sveltejs/svelte
parent
c4b8af9b21
commit
6e00b68015
@ -0,0 +1,28 @@
|
|||||||
|
import Attribute from '../../../nodes/Attribute';
|
||||||
|
import { string_literal } from '../../../utils/stringify';
|
||||||
|
import Text from '../../../nodes/Text';
|
||||||
|
import { x } from 'code-red';
|
||||||
|
import Expression from '../../../nodes/shared/Expression';
|
||||||
|
import { Expression as ESTreeExpression } from 'estree';
|
||||||
|
|
||||||
|
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})) + "${(attribute.chunks[1] as Text).data}"`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return get_attribute_value(attribute);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function get_attribute_value(attribute: Attribute): ESTreeExpression {
|
||||||
|
if (attribute.chunks.length === 0) return x`""`;
|
||||||
|
|
||||||
|
return attribute.chunks
|
||||||
|
.map((chunk) => {
|
||||||
|
return chunk.type === 'Text'
|
||||||
|
? string_literal(chunk.data.replace(/"/g, '"')) as ESTreeExpression
|
||||||
|
: x`@escape(${chunk.node})`;
|
||||||
|
})
|
||||||
|
.reduce((lhs, rhs) => x`${lhs} + ${rhs}`);
|
||||||
|
}
|
@ -1,28 +0,0 @@
|
|||||||
import Attribute from '../nodes/Attribute';
|
|
||||||
import { string_literal } from './stringify';
|
|
||||||
import { snip } from './snip';
|
|
||||||
import Text from '../nodes/Text';
|
|
||||||
import { x } from 'code-red';
|
|
||||||
|
|
||||||
export function stringify_class_attribute(attribute: Attribute) {
|
|
||||||
// handle special case — `class={possiblyUndefined}` with scoped CSS
|
|
||||||
if (attribute.chunks.length === 2 && (attribute.chunks[1] as Text).synthetic) {
|
|
||||||
return '${@escape(@null_to_empty(' + snip(attribute.chunks[0]) + '))}' + (attribute.chunks[1] as Text).data;
|
|
||||||
}
|
|
||||||
|
|
||||||
return stringify_attribute(attribute, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function stringify_attribute(attribute: Attribute, is_ssr: boolean) {
|
|
||||||
return attribute.chunks
|
|
||||||
.map((chunk) => {
|
|
||||||
if (chunk.type === 'Text') {
|
|
||||||
return string_literal(chunk.data.replace(/"/g, '"'));
|
|
||||||
}
|
|
||||||
|
|
||||||
return is_ssr
|
|
||||||
? x`@escape(${chunk.node})`
|
|
||||||
: chunk.node;
|
|
||||||
})
|
|
||||||
.reduce((lhs, rhs) => x`${lhs} + ${rhs}`);
|
|
||||||
}
|
|
Loading…
Reference in new issue