From: Evan You Date: Tue, 6 Aug 2024 10:14:47 +0000 (+0800) Subject: fix(ssr): respect textContent/innerHTML from getSSRProps in optimized SSR output X-Git-Tag: v3.4.36~8 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=79602f9ecd9559954f844774a90286305b13e056;p=thirdparty%2Fvuejs%2Fcore.git fix(ssr): respect textContent/innerHTML from getSSRProps in optimized SSR output close #8112 --- diff --git a/packages/compiler-ssr/__tests__/ssrElement.spec.ts b/packages/compiler-ssr/__tests__/ssrElement.spec.ts index 4056b4c3c7..723ef7b359 100644 --- a/packages/compiler-ssr/__tests__/ssrElement.spec.ts +++ b/packages/compiler-ssr/__tests__/ssrElement.spec.ts @@ -288,12 +288,27 @@ describe('ssr: element', () => { }>\`" `) }) + }) - test('custom dir', () => { + describe('custom directives', () => { + // #8112 should respect textContent / innerHTML from directive getSSRProps + // if the element has no children + test('custom dir without children', () => { expect(getCompiledString(`
`)).toMatchInlineSnapshot(` + "\`\${ + ("textContent" in _temp0) ? _ssrInterpolate(_temp0.textContent) : _temp0.innerHTML ?? '' + }
\`" + `) + }) + + test('custom dir with children', () => { + expect(getCompiledString(`
hello
`)) + .toMatchInlineSnapshot(` "\`\`" + }>hello\`" `) }) @@ -301,30 +316,36 @@ describe('ssr: element', () => { expect(getCompiledString(`
`)) .toMatchInlineSnapshot(` "\`
\`" + _ssrRenderAttrs(_temp0 = _mergeProps({ class: "foo" }, _ssrGetDirectiveProps(_ctx, _directive_xxx))) + }>\${ + ("textContent" in _temp0) ? _ssrInterpolate(_temp0.textContent) : _temp0.innerHTML ?? '' + }\`" `) }) test('custom dir with v-bind', () => { expect(getCompiledString(`
`)) .toMatchInlineSnapshot(` - "\`
\`" - `) + "\`\${ + ("textContent" in _temp0) ? _ssrInterpolate(_temp0.textContent) : _temp0.innerHTML ?? '' + }\`" + `) }) test('custom dir with object v-bind', () => { expect(getCompiledString(`
`)) .toMatchInlineSnapshot(` - "\`
\`" - `) + "\`\${ + ("textContent" in _temp0) ? _ssrInterpolate(_temp0.textContent) : _temp0.innerHTML ?? '' + }\`" + `) }) test('custom dir with object v-bind + normal bindings', () => { @@ -332,11 +353,13 @@ describe('ssr: element', () => { getCompiledString(`
`), ).toMatchInlineSnapshot(` "\`
\`" + }>\${ + ("textContent" in _temp0) ? _ssrInterpolate(_temp0.textContent) : _temp0.innerHTML ?? '' + }\`" `) }) }) diff --git a/packages/compiler-ssr/src/transforms/ssrTransformElement.ts b/packages/compiler-ssr/src/transforms/ssrTransformElement.ts index 7175b797d4..45d4d76fe9 100644 --- a/packages/compiler-ssr/src/transforms/ssrTransformElement.ts +++ b/packages/compiler-ssr/src/transforms/ssrTransformElement.ts @@ -163,6 +163,25 @@ export const ssrTransformElement: NodeTransform = (node, context) => { ]), ] } + } else if (directives.length && !node.children.length) { + const tempId = `_temp${context.temps++}` + propsExp.arguments = [ + createAssignmentExpression( + createSimpleExpression(tempId, false), + mergedProps, + ), + ] + rawChildrenMap.set( + node, + createConditionalExpression( + createSimpleExpression(`"textContent" in ${tempId}`, false), + createCallExpression(context.helper(SSR_INTERPOLATE), [ + createSimpleExpression(`${tempId}.textContent`, false), + ]), + createSimpleExpression(`${tempId}.innerHTML ?? ''`, false), + false, + ), + ) } if (needTagForRuntime) {