Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@
"@nextcloud/eslint-config": "^8.4.2",
"@nextcloud/stylelint-config": "^3.1.1",
"@nextcloud/vite-config": "^2.5.2",
"@types/escape-html": "^1.0.4",
"@vitejs/plugin-vue": "^6.0.7",
"@vue/test-utils": "^2.4.11",
"@vue/tsconfig": "^0.9.1",
"escape-html": "^1.0.3",
"jsdom": "^30.0.1",
"typescript": "^5.9.3",
"vite": "^7.2.7",
Expand Down
28 changes: 28 additions & 0 deletions src/test-utils/translateMock.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { describe, expect, it } from 'vitest'
import { translate } from '@nextcloud/l10n'

// vitest.setup.ts mocks translate() for every spec in the suite. Pin its
// contract against the real implementation's default behaviour — real
// translate() escapes every placeholder value (escape-html) before
// substitution, and a mock that skips this hides a whole class of bug: a
// component whose translated string reaches an HTML sink (or, conversely,
// an ARIA attribute, where the escaping is actually wrong) behaves
// differently under test than for a real user.
describe('translate() mock', () => {
it('escapes HTML-significant characters in a placeholder value', () => {
const result = translate('office', 'Shared by {owner}', { owner: '<b>&"\'' })

expect(result).toBe('Shared by &lt;b&gt;&amp;&quot;&#39;')
})

it('leaves the surrounding template text alone', () => {
const result = translate('office', 'Shared by {owner}', { owner: 'Bob' })

expect(result).toBe('Shared by Bob')
})
})
6 changes: 5 additions & 1 deletion vitest.setup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { config } from '@vue/test-utils'
import { vi } from 'vitest'
import escapeHTML from 'escape-html'

// shallowMount()'s auto-stubs render nothing by default, including slot
// content passed to them — without this, any content nested inside a
Expand All @@ -20,12 +21,15 @@ function substitutePlaceholders(text: string, vars?: Record<string, unknown>): s
})
}

// Real translate() escapes every placeholder value by default (escape-html) —
// match that here, or a component relying on the default silently ships
// double-escaped output that only breaks against the real implementation.
vi.mock('@nextcloud/l10n', async (importOriginal) => ({
...(await importOriginal<typeof import('@nextcloud/l10n')>()),
translate: (app: string, text: string, placeholders?: Record<string, unknown>) =>
text.replace(/{([^{}]*)}/g, (match: string, key: string) => {
const value = placeholders?.[key]
return value !== undefined ? String(value) : match
return value !== undefined ? escapeHTML(String(value)) : match
}),
}))

Expand Down
Loading