Skip to content

Commit 1f8e133

Browse files
authored
Exclude <title> from HTML sanitization (#438)
1 parent f8cc0f4 commit 1f8e133

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

fluent-dom/src/overlay.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,11 @@ export default function translateElement(element, translation) {
6767
const {value} = translation;
6868

6969
if (typeof value === "string") {
70-
if (!reOverlay.test(value)) {
70+
if (element.localName === "title" &&
71+
element.namespaceURI === "http://www.w3.org/1999/xhtml") {
72+
// A special case for the HTML title element whose content must be text.
73+
element.textContent = value;
74+
} else if (!reOverlay.test(value)) {
7175
// If the translation doesn't contain any markup skip the overlay logic.
7276
element.textContent = value;
7377
} else {

fluent-dom/test/overlay_test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import assert from 'assert';
2+
import translateElement from '../src/overlay';
3+
import {elem} from './index';
4+
5+
suite('Applying translations', function() {
6+
test('Skipping sanitization for the title element', function() {
7+
const element = elem('title')``;
8+
const translation = {
9+
value: '<input type="text"/> - HTML: Input Element',
10+
attributes: null
11+
};
12+
13+
translateElement(element, translation);
14+
assert.strictEqual(
15+
element.innerHTML,
16+
'&lt;input type="text"/&gt; - HTML: Input Element'
17+
);
18+
});
19+
});

0 commit comments

Comments
 (0)