consider template literal the same as text with expression

pull/5210/head
Tan Li Hau 5 years ago
parent 02e10b1159
commit 0c4cca7948

@ -7,6 +7,7 @@ import { string_literal } from '../../../utils/stringify';
import add_to_set from '../../../utils/add_to_set'; import add_to_set from '../../../utils/add_to_set';
import Expression from '../../../nodes/shared/Expression'; import Expression from '../../../nodes/shared/Expression';
import Text from '../../../nodes/Text'; import Text from '../../../nodes/Text';
import { TemplateLiteral } from 'estree';
export interface StyleProp { export interface StyleProp {
key: string; key: string;
@ -19,7 +20,7 @@ export default class StyleAttributeWrapper extends AttributeWrapper {
parent: ElementWrapper; parent: ElementWrapper;
render(block: Block) { render(block: Block) {
const style_props = optimize_style(this.node.chunks); const style_props = optimize_style(template_literal_to_text(this.node.chunks, this));
if (!style_props) return super.render(block); if (!style_props) return super.render(block);
style_props.forEach((prop: StyleProp) => { style_props.forEach((prop: StyleProp) => {
@ -190,3 +191,37 @@ function get_style_value(chunks: Array<Text | Expression>) {
function is_dynamic(value: Array<Text|Expression>) { function is_dynamic(value: Array<Text|Expression>) {
return value.length > 1 || value[0].type !== 'Text'; return value.length > 1 || value[0].type !== 'Text';
} }
function template_literal_to_text(
value: Array<Text | Expression>,
wrapper: StyleAttributeWrapper
) {
if (value.length > 1) return value;
const expression = value[0];
if (expression.type !== "Expression") return value;
if (expression.node.type !== "TemplateLiteral") return value;
const template = expression.node as TemplateLiteral;
const chunks = [];
for (let i = 0; i < template.expressions.length; i++) {
chunks.push({
type: "Text",
data: template.quasis[i].value.raw,
});
chunks.push(
new Expression(
wrapper.node.component,
wrapper.node,
wrapper.node.scope,
template.expressions[i]
)
);
}
chunks.push({
type: "Text",
data: template.quasis[template.quasis.length - 1].value.raw,
});
return chunks;
}

@ -7,34 +7,53 @@ import {
insert, insert,
noop, noop,
safe_not_equal, safe_not_equal,
set_style set_style,
space
} from "svelte/internal"; } from "svelte/internal";
function create_fragment(ctx) { function create_fragment(ctx) {
let div; let div0;
let t;
let div1;
return { return {
c() { c() {
div = element("div"); div0 = element("div");
set_style(div, "color", /*color*/ ctx[0]); t = space();
set_style(div, "transform", "translate(" + /*x*/ ctx[1] + "px," + /*y*/ ctx[2] + "px)"); div1 = element("div");
set_style(div0, "color", /*color*/ ctx[0]);
set_style(div0, "transform", "translate(" + /*x*/ ctx[1] + "px," + /*y*/ ctx[2] + "px)");
set_style(div1, "color", /*color*/ ctx[0]);
set_style(div1, "transform", "translate(" + /*x*/ ctx[1] + "px," + /*y*/ ctx[2] + "px)");
}, },
m(target, anchor) { m(target, anchor) {
insert(target, div, anchor); insert(target, div0, anchor);
insert(target, t, anchor);
insert(target, div1, anchor);
}, },
p(ctx, [dirty]) { p(ctx, [dirty]) {
if (dirty & /*color*/ 1) { if (dirty & /*color*/ 1) {
set_style(div, "color", /*color*/ ctx[0]); set_style(div0, "color", /*color*/ ctx[0]);
} }
if (dirty & /*x, y*/ 6) { if (dirty & /*x, y*/ 6) {
set_style(div, "transform", "translate(" + /*x*/ ctx[1] + "px," + /*y*/ ctx[2] + "px)"); set_style(div0, "transform", "translate(" + /*x*/ ctx[1] + "px," + /*y*/ ctx[2] + "px)");
}
if (dirty & /*color*/ 1) {
set_style(div1, "color", /*color*/ ctx[0]);
}
if (dirty & /*x, y*/ 6) {
set_style(div1, "transform", "translate(" + /*x*/ ctx[1] + "px," + /*y*/ ctx[2] + "px)");
} }
}, },
i: noop, i: noop,
o: noop, o: noop,
d(detaching) { d(detaching) {
if (detaching) detach(div); if (detaching) detach(div0);
if (detaching) detach(t);
if (detaching) detach(div1);
} }
}; };
} }

@ -5,3 +5,4 @@
</script> </script>
<div style='color: {color}; transform: translate({x}px,{y}px);'></div> <div style='color: {color}; transform: translate({x}px,{y}px);'></div>
<div style={`color: ${color}; transform: translate(${x}px,${y}px);`}></div>

@ -7,29 +7,43 @@ import {
insert, insert,
noop, noop,
safe_not_equal, safe_not_equal,
set_style set_style,
space
} from "svelte/internal"; } from "svelte/internal";
function create_fragment(ctx) { function create_fragment(ctx) {
let div; let div0;
let t;
let div1;
return { return {
c() { c() {
div = element("div"); div0 = element("div");
set_style(div, "background", "url(data:image/png;base64," + /*data*/ ctx[0] + ")"); t = space();
div1 = element("div");
set_style(div0, "background", "url(data:image/png;base64," + /*data*/ ctx[0] + ")");
set_style(div1, "background", "url(data:image/png;base64," + /*data*/ ctx[0] + ")");
}, },
m(target, anchor) { m(target, anchor) {
insert(target, div, anchor); insert(target, div0, anchor);
insert(target, t, anchor);
insert(target, div1, anchor);
}, },
p(ctx, [dirty]) { p(ctx, [dirty]) {
if (dirty & /*data*/ 1) { if (dirty & /*data*/ 1) {
set_style(div, "background", "url(data:image/png;base64," + /*data*/ ctx[0] + ")"); set_style(div0, "background", "url(data:image/png;base64," + /*data*/ ctx[0] + ")");
}
if (dirty & /*data*/ 1) {
set_style(div1, "background", "url(data:image/png;base64," + /*data*/ ctx[0] + ")");
} }
}, },
i: noop, i: noop,
o: noop, o: noop,
d(detaching) { d(detaching) {
if (detaching) detach(div); if (detaching) detach(div0);
if (detaching) detach(t);
if (detaching) detach(div1);
} }
}; };
} }

@ -3,3 +3,4 @@
</script> </script>
<div style='background: url(data:image/png;base64,{data})'></div> <div style='background: url(data:image/png;base64,{data})'></div>
<div style={`background: url(data:image/png;base64,${data})`}></div>

@ -7,29 +7,43 @@ import {
insert, insert,
noop, noop,
safe_not_equal, safe_not_equal,
set_style set_style,
space
} from "svelte/internal"; } from "svelte/internal";
function create_fragment(ctx) { function create_fragment(ctx) {
let div; let div0;
let t;
let div1;
return { return {
c() { c() {
div = element("div"); div0 = element("div");
set_style(div, "color", /*color*/ ctx[0]); t = space();
div1 = element("div");
set_style(div0, "color", /*color*/ ctx[0]);
set_style(div1, "color", /*color*/ ctx[0]);
}, },
m(target, anchor) { m(target, anchor) {
insert(target, div, anchor); insert(target, div0, anchor);
insert(target, t, anchor);
insert(target, div1, anchor);
}, },
p(ctx, [dirty]) { p(ctx, [dirty]) {
if (dirty & /*color*/ 1) { if (dirty & /*color*/ 1) {
set_style(div, "color", /*color*/ ctx[0]); set_style(div0, "color", /*color*/ ctx[0]);
}
if (dirty & /*color*/ 1) {
set_style(div1, "color", /*color*/ ctx[0]);
} }
}, },
i: noop, i: noop,
o: noop, o: noop,
d(detaching) { d(detaching) {
if (detaching) detach(div); if (detaching) detach(div0);
if (detaching) detach(t);
if (detaching) detach(div1);
} }
}; };
} }

@ -3,3 +3,4 @@
</script> </script>
<div style='color: {color}'></div> <div style='color: {color}'></div>
<div style={`color: ${color}`}></div>
Loading…
Cancel
Save