Skip to content

Commit 144d71b

Browse files
committed
Support HTML comments in localized lit templates
1 parent 421cc2d commit 144d71b

File tree

9 files changed

+41
-7
lines changed

9 files changed

+41
-7
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2222
- Fix incorrect JSON schema error about `targetLocales` field not being a
2323
`string[]`.
2424

25+
- Fix bug where `html` templates could not contain `<!-- comments -->`. HTML
26+
comments are now preserved as placeholders, similar to other HTML markup.
27+
2528
## [0.2.3] - 2020-05-13
2629

2730
- Fix missing `<xliff>` element in XLIFF output.

src/program-analysis.ts

+21-7
Original file line numberDiff line numberDiff line change
@@ -438,17 +438,20 @@ function replaceHtmlWithPlaceholders(
438438
const components: Array<string | Placeholder> = [];
439439

440440
const traverse = (node: parse5.DefaultTreeNode): void => {
441-
const text =
442-
node.nodeName === '#text'
443-
? (node as parse5.DefaultTreeTextNode).value
444-
: null;
445-
if (text !== null) {
441+
if (node.nodeName === '#text') {
442+
const text = (node as parse5.DefaultTreeTextNode).value;
446443
components.push(text);
444+
} else if (node.nodeName === '#comment') {
445+
components.push({
446+
untranslatable: serializeComment(node as parse5.DefaultTreeCommentNode),
447+
});
447448
} else {
448449
const {open, close} = serializeOpenCloseTags(node);
449450
components.push({untranslatable: open});
450-
for (const child of (node as parse5.DefaultTreeParentNode).childNodes) {
451-
traverse(child);
451+
if ('childNodes' in node) {
452+
for (const child of (node as parse5.DefaultTreeParentNode).childNodes) {
453+
traverse(child);
454+
}
452455
}
453456
components.push({untranslatable: close});
454457
}
@@ -481,6 +484,17 @@ function serializeOpenCloseTags(
481484
return {open, close};
482485
}
483486

487+
/**
488+
* Serialize an HTML comment node.
489+
*
490+
* Example:
491+
*
492+
* {data: "foo"} --> "<!-- foo -->"
493+
*/
494+
function serializeComment(comment: parse5.DefaultTreeCommentNode): string {
495+
return parse5.serialize({childNodes: [comment]});
496+
}
497+
484498
/**
485499
* E.g. "foo", 'foo', or `foo`, but not `foo${bar}`.
486500
*/

testdata/xliff/goldens/foo.ts

+2
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,5 @@ msg(
3636
</b>`,
3737
'x'
3838
);
39+
40+
msg('comment', html`Hello <b><!-- comment -->World!</b>`);

testdata/xliff/goldens/tsout/es-419.ts

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {html} from 'lit-html';
77
/* eslint-disable @typescript-eslint/no-explicit-any */
88

99
export const templates = {
10+
comment: html`Hola <b><!-- comment -->Mundo!</b>`,
1011
lit: html`Hola <b><i>Galaxia!</i></b>`,
1112
lit_variables_1: (url: any, name: any) =>
1213
html`Hola ${name}, clic <a href="${url}">aquí</a>!`,

testdata/xliff/goldens/tsout/zh_CN.ts

+1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ export const templates = {
2828
<b>
2929
${x}
3030
</b>`,
31+
comment: html`Hello <b><!-- comment -->World!</b>`,
3132
};

testdata/xliff/goldens/xliff/es-419.xlf

+4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838
${x}
3939
&lt;/b></ph></source>
4040
</trans-unit>
41+
<trans-unit id="comment">
42+
<source>Hello <ph id="0">&lt;b>&lt;!-- comment --></ph>World!<ph id="1">&lt;/b></ph></source>
43+
<target>Hola <ph id="0">&lt;b>&lt;!-- comment --></ph>Mundo!<ph id="1">&lt;/b></ph></target>
44+
</trans-unit>
4145
</body>
4246
</file>
4347
</xliff>

testdata/xliff/goldens/xliff/zh_CN.xlf

+3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
${x}
3535
&lt;/b></ph></source>
3636
</trans-unit>
37+
<trans-unit id="comment">
38+
<source>Hello <ph id="0">&lt;b>&lt;!-- comment --></ph>World!<ph id="1">&lt;/b></ph></source>
39+
</trans-unit>
3740
</body>
3841
</file>
3942
</xliff>

testdata/xliff/input/foo.ts

+2
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,5 @@ msg(
3636
</b>`,
3737
'x'
3838
);
39+
40+
msg('comment', html`Hello <b><!-- comment -->World!</b>`);

testdata/xliff/input/xliff/es-419.xlf

+4
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,9 @@
3030
id="1"
3131
>&lt;a href="${url}"></ph>aquí<ph id="2">&lt;/a></ph>!</target>
3232
</trans-unit>
33+
<trans-unit id="comment">
34+
<source>Hello <ph id="0">&lt;b>&lt;!-- comment --></ph>World!<ph id="1">&lt;/b></ph></source>
35+
<target>Hola <ph id="0">&lt;b>&lt;!-- comment --></ph>Mundo!<ph id="1">&lt;/b></ph></target>
36+
</trans-unit>
3337
</body>
3438
</file>

0 commit comments

Comments
 (0)