Procházet zdrojové kódy

fix(compiler-vapor): convert to display string for each text

三咲智子 Kevin Deng před 2 roky
rodič
revize
68b0caf495

+ 4 - 4
packages/compiler-vapor/__tests__/__snapshots__/compile.spec.ts.snap

@@ -8,7 +8,7 @@ const t0 = _template("<div></div>")
 export function render(_ctx) {
   const n0 = t0()
   const { 0: [n1],} = _children(n0)
-  _renderEffect(() => _setText(n1, "count is " + _ctx.count + "."))
+  _renderEffect(() => _setText(n1, "count is ", _ctx.count, "."))
   return n0
 }"
 `;
@@ -180,7 +180,7 @@ export function render(_ctx) {
   const n0 = t0()
   const n1 = _createTextNode()
   _append(n0, n1)
-  _renderEffect(() => _setText(n1, "" + 1 + 2))
+  _renderEffect(() => _setText(n1, 1, 2))
   return n0
 }"
 `;
@@ -194,7 +194,7 @@ export function render(_ctx) {
   const n0 = t0()
   const { 0: [n1],} = _children(n0)
   _on(n1, "click", () => _ctx.handleClick)
-  _renderEffect(() => _setText(n1, _ctx.count + "foo" + _ctx.count + "foo" + _ctx.count))
+  _renderEffect(() => _setText(n1, _ctx.count, "foo", _ctx.count, "foo", _ctx.count))
   _renderEffect(() => _setDynamicProp(n1, "id", _ctx.count))
   return n0
 }"
@@ -243,7 +243,7 @@ export function render(_ctx) {
   const n0 = t0()
   const n1 = _createTextNode()
   _append(n0, n1)
-  _renderEffect(() => _setText(n1, "" + 1 + 2 + "3" + 4 + 5 + "6" + 7 + 8 + "9" + 'A' + 'B'))
+  _renderEffect(() => _setText(n1, 1, 2, "3", 4, 5, "6", 7, 8, "9", 'A', 'B'))
   return n0
 }"
 `;

+ 1 - 1
packages/compiler-vapor/__tests__/transforms/__snapshots__/vOnce.spec.ts.snap

@@ -22,7 +22,7 @@ export function render(_ctx) {
   const n0 = t0()
   const { 0: [n3, { 0: [n2],}],} = _children(n0)
   const n1 = _createTextNode()
-  _setText(n1, _ctx.msg + " ")
+  _setText(n1, _ctx.msg, " ")
   _setClass(n2, _ctx.clz)
   _prepend(n3, n1)
   return n0

+ 2 - 13
packages/compiler-vapor/src/generators/text.ts

@@ -6,25 +6,14 @@ export function genSetText(
   oper: SetTextIRNode,
   context: CodegenContext,
 ): CodeFragment[] {
-  const { call, multi, vaporHelper } = context
+  const { call, vaporHelper } = context
   const { values } = oper
   return [
     NEWLINE,
     ...call(
       vaporHelper('setText'),
       `n${oper.element}`,
-      multi(
-        ['', '', ' + '],
-        ...values.map((value, idx) => [
-          idx === 0 &&
-          values.length > 1 &&
-          !value.isStatic &&
-          !values[1].isStatic
-            ? '"" + '
-            : '',
-          ...genExpression(value, context),
-        ]),
-      ),
+      ...values.map(value => genExpression(value, context)),
     ),
   ]
 }

+ 5 - 8
packages/runtime-vapor/src/dom/prop.ts

@@ -190,14 +190,11 @@ function mergeProps(...args: Data[]) {
   return ret
 }
 
-export function setText(el: Node, value: any) {
-  const oldVal = recordPropMetadata(
-    el,
-    'textContent',
-    (value = toDisplayString(value)),
-  )
-  if (value !== oldVal) {
-    el.textContent = value
+export function setText(el: Node, ...values: any[]) {
+  const text = values.map(v => toDisplayString(v)).join('')
+  const oldVal = recordPropMetadata(el, 'textContent', text)
+  if (text !== oldVal) {
+    el.textContent = text
   }
 }