From 885155291f8badcc694a6894ebff7b6a64eddec3 Mon Sep 17 00:00:00 2001 From: Ivan Demchuk Date: Fri, 14 Feb 2025 12:56:37 +0200 Subject: [PATCH 1/4] Add test missed case --- __tests__/vue/ssr.spec.ts | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/__tests__/vue/ssr.spec.ts b/__tests__/vue/ssr.spec.ts index 1d0fe007..caabee32 100644 --- a/__tests__/vue/ssr.spec.ts +++ b/__tests__/vue/ssr.spec.ts @@ -2,14 +2,14 @@ import type { FluentVue } from '../../src' import { FluentBundle, FluentResource } from '@fluent/bundle' import ftl from '@fluent/dedent' -import { beforeEach, describe, expect, it } from 'vitest' +import { beforeEach, describe, expect, it, vi } from 'vitest' import { isVue3 } from 'vue-demi' import { createFluentVue } from '../../src' import { renderSSR } from '../utils/ssr' -describe.skipIf(!isVue3)('sSR directive', () => { +describe.skipIf(!isVue3)('ssr directive', () => { let fluent: FluentVue let bundle: FluentBundle @@ -45,4 +45,20 @@ describe.skipIf(!isVue3)('sSR directive', () => { // Text will be translated using directive transform expect(rendered).toEqual('Fallback text') }) + + it('warns when missing translation key', async () => { + // Arrange + const warnSpy = vi.spyOn(console, 'warn') + + const component = { + template: 'Fallback text', + } + + // Act + const rendered = await renderSSR(fluent, component) + + // Assert + expect(rendered).toEqual('Fallback text') + expect(warnSpy).toHaveBeenCalledWith('[fluent-vue] v-t directive is missing arg with translation key') + }) }) From c4d584cb74642099afac8bd59ddabb98a52b7381 Mon Sep 17 00:00:00 2001 From: Ivan Demchuk Date: Fri, 14 Feb 2025 13:21:45 +0200 Subject: [PATCH 2/4] Support textContent in the directive when using SSR --- src/vue/directive.ts | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/vue/directive.ts b/src/vue/directive.ts index e5aa84f4..24efe5ec 100644 --- a/src/vue/directive.ts +++ b/src/vue/directive.ts @@ -44,26 +44,26 @@ export function createVue3Directive(rootContext: TranslationContext): Vue3Direct getSSRProps(binding) { const context = getContext(rootContext, binding.instance) - const key = binding.arg - if (key === void 0) { + if (binding.arg === void 0) { warn('v-t directive is missing arg with translation key') return {} } - const translation = context.formatWithAttrs(key, binding.value) - const allowedAttrs = Object.keys(binding.modifiers) - const attrs: Record = {} - for (const [attr, attrValue] of Object.entries(translation.attributes)) { - // Vue 3 does not expose the element in the binding object - // so we can't check if the attribute is allowed - // we assume that all attributes are allowed - // this could lead to SSR hydration mismatches if translation - // contains attributes that are not allowed - // There is a runtime warning in the browser console in case translation contains not allowed attributes - if (isAttrNameLocalizable(attr, {} as HTMLElement, allowedAttrs)) - attrs[attr] = attrValue + + const translation = context.formatWithAttrs(binding.arg, binding.value) + + // Vue 3 does not expose the element in the binding object during SSR. + // So we can't check if the attribute is allowed. + // We assume that all attributes are allowed. + // This could lead to SSR hydration mismatches if translation + // contains attributes that are not allowed. + // There is a runtime warning in the browser console in case translation + // contains not allowed attributes, this should catch this case. + const attrs = translation.attributes + + if (translation.hasValue) { + attrs.textContent = translation.value } - // TODO: Include textContent when https://github.com/vuejs/core/issues/8112 is resolved return attrs }, } From 7f2508b7ed07e5f64b0a95cbc87488c0efe87f83 Mon Sep 17 00:00:00 2001 From: Ivan Demchuk Date: Fri, 14 Feb 2025 13:24:54 +0200 Subject: [PATCH 3/4] Add directive SSR tests --- __tests__/vue/ssr.spec.ts | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/__tests__/vue/ssr.spec.ts b/__tests__/vue/ssr.spec.ts index caabee32..ba0b7f2b 100644 --- a/__tests__/vue/ssr.spec.ts +++ b/__tests__/vue/ssr.spec.ts @@ -34,16 +34,14 @@ describe.skipIf(!isVue3)('ssr directive', () => { data: () => ({ name: 'John', }), - template: 'Fallback text', + template: '', } // Act const rendered = await renderSSR(fluent, component) // Assert - // This has fallback text because the textContent is not supported by Vue getSSRProps - // Text will be translated using directive transform - expect(rendered).toEqual('Fallback text') + expect(rendered).toEqual('Link text') }) it('warns when missing translation key', async () => { @@ -51,14 +49,38 @@ describe.skipIf(!isVue3)('ssr directive', () => { const warnSpy = vi.spyOn(console, 'warn') const component = { - template: 'Fallback text', + template: '', } // Act const rendered = await renderSSR(fluent, component) // Assert - expect(rendered).toEqual('Fallback text') + expect(rendered).toEqual('') expect(warnSpy).toHaveBeenCalledWith('[fluent-vue] v-t directive is missing arg with translation key') }) + + it('only allows certain attributes', async () => { + // Arrange + bundle.addResource( + new FluentResource(ftl` + message = Hello, { $name }! + .aria-label = Link aria label + .href = Link href + `), + ) + + const component = { + data: () => ({ + name: 'John', + }), + template: '', + } + + // Act + const rendered = await renderSSR(fluent, component) + + // Assert + expect(rendered).toEqual('Hello, \u{2068}John\u{2069}!') + }) }) From d12d01c78415b59f71d8da75fabf32e34ab37549 Mon Sep 17 00:00:00 2001 From: Ivan Demchuk Date: Fri, 14 Feb 2025 13:56:19 +0200 Subject: [PATCH 4/4] Add test for no textContent --- __tests__/vue/ssr.spec.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/__tests__/vue/ssr.spec.ts b/__tests__/vue/ssr.spec.ts index ba0b7f2b..ee744505 100644 --- a/__tests__/vue/ssr.spec.ts +++ b/__tests__/vue/ssr.spec.ts @@ -44,6 +44,29 @@ describe.skipIf(!isVue3)('ssr directive', () => { expect(rendered).toEqual('Link text') }) + it('skips text content if not specified', async () => { + // Arrange + bundle.addResource( + new FluentResource(ftl` + link = + .aria-label = Link aria label + `), + ) + + const component = { + data: () => ({ + name: 'John', + }), + template: 'Test', + } + + // Act + const rendered = await renderSSR(fluent, component) + + // Assert + expect(rendered).toEqual('Test') + }) + it('warns when missing translation key', async () => { // Arrange const warnSpy = vi.spyOn(console, 'warn') pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy