Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/tender-bobcats-greet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lit/localize-tools': patch
---

Fix issue with placing expressions as html attribute values in transform mode
19 changes: 17 additions & 2 deletions packages/localize-tools/src/modes/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ class Transformer {
'Internal error: string literal cannot be html-tagged'
);
}
return template;
return newTemplate;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops! Do the tests in this PR fail if this is switched back to template? If not, is there a test we could add that would cover it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Upon taking a closer look now, it seems we never enter this code block if a translation does occur because then newTemplate would always be a ts.TemplateLiteral. The only time we enter this block is when we don't have to translate, i.e. generating original locale's version of the source, in which case newTemplate would not have changed from it's original assignment from template here:

let newTemplate = template;

So.. it was technically an oversight but of no consequence, hence none of our existing tests failed. For readability's sake I think keeping newTemplate here still makes sense.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sgtm

}

// We may have ended up with template expressions that can be represented
Expand Down Expand Up @@ -534,7 +534,22 @@ class Transformer {
return true;
};

for (const span of template.templateSpans) {
for (let i = 0; i < template.templateSpans.length; i++) {
const span = template.templateSpans[i];
// A span preceded by `=` can be an attribute so skip subsume and
// keep it as an expression to produce valid lit-html template
// TODO(augustinekim) Consider optimizing to regular quoted string for
// regular html attributes
if (
(i === 0
? template.head.text
: template.templateSpans[i - 1].literal.text
).endsWith('=')
) {
fragments.push(ts.visitNode(span.expression, this.boundVisitNode));
fragments.push(span.literal.text);
continue;
}
let expression = span.expression;
// Can we directly subsume this span?
if (!subsume(expression)) {
Expand Down
72 changes: 72 additions & 0 deletions packages/localize-tools/src/tests/transform.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,78 @@ test('msg(string(<b>msg(string)</b>)) translated', () => {
);
});

test('html(msg(string)) with msg as attr value', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add one with a .bar property attribute, too? Same result, just for completeness.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, and also added additional tests for having multiple attributes in a single template expression so as to test the logic for looking at both the head and the middle.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

checkTransform(
'html`Hello <b bar=${msg("World", {id: "bar"})}>${"World"}</b>!`;',
'html`Hello <b bar=${"World"}>World</b>!`;'
);
});

test('html(msg(string)) with msg as attr value translated', () => {
checkTransform(
'html`Hello <b bar=${msg("world", {id: "bar"})}>${"World"}</b>!`;',
'html`Hello <b bar=${`Mundo`}>World</b>!`;',
{
messages: [
{
name: 'bar',
contents: ['Mundo'],
},
],
}
);
});

test('html(msg(string)) with multiple msg as attr value', () => {
checkTransform(
'html`<b foo=${msg("Hello", {id: "foo"})}>${"Hello"}</b>' +
'<b bar=${msg("World", {id: "bar"})}>${"World"}</b>!`;',
'html`<b foo=${"Hello"}>Hello</b><b bar=${"World"}>World</b>!`;'
);
});

test('html(msg(string)) with multiple msg as attr value translated', () => {
checkTransform(
'html`<b foo=${msg("Hello", {id: "foo"})}>${"Hello"}</b>' +
'<b bar=${msg("World", {id: "bar"})}>${"World"}</b>!`;',
'html`<b foo=${`Hola`}>Hello</b><b bar=${`Mundo`}>World</b>!`;',
{
messages: [
{
name: 'foo',
contents: ['Hola'],
},
{
name: 'bar',
contents: ['Mundo'],
},
],
}
);
});

test('html(msg(string)) with msg as property attr value', () => {
checkTransform(
'html`Hello <b .bar=${msg("World", {id: "bar"})}>${"World"}</b>!`;',
'html`Hello <b .bar=${"World"}>World</b>!`;'
);
});

test('html(msg(string)) with msg as property attr value translated', () => {
checkTransform(
'html`Hello <b .bar=${msg("World", {id: "bar"})}>${"World"}</b>!`;',
'html`Hello <b .bar=${`Mundo`}>World</b>!`;',
{
messages: [
{
name: 'bar',
contents: ['Mundo'],
},
],
}
);
});

test('import * as litLocalize', () => {
checkTransform(
`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,10 @@ export class MyElement2 extends LitElement {

// Escaped markup characters should remain escaped
msg(html`&lt;Hello<b>&lt;World &amp; Friends&gt;</b>!&gt;`);

// Expressions as attribute values should stay as expressions
html`Hello <b foo=${'World'}>World</b>`;
html`Hello <b foo=${msg('World')}>World</b>`;
html`<b foo=${msg('Hello')}>Hello</b><b bar=${msg('World')}>World</b>`;
html`Hello <b .foo=${'World'}>World</b>`;
html`Hello <b .foo=${msg('World')}>World</b>`;
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,9 @@ export class MyElement2 extends LitElement {
}
// Escaped markup characters should remain escaped
html`&lt;Hello<b>&lt;World &amp; Friends&gt;</b>!&gt;`;
// Expressions as attribute values should stay as expressions
html`Hello <b foo=${'World'}>World</b>`;
html`Hello <b foo=${'World'}>World</b>`;
html`<b foo=${'Hello'}>Hello</b><b bar=${'World'}>World</b>`;
html`Hello <b .foo=${'World'}>World</b>`;
html`Hello <b .foo=${'World'}>World</b>`;
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,9 @@ export class MyElement2 extends LitElement {
}
// Escaped markup characters should remain escaped
html`&lt;Hola<b>&lt;Mundo &amp; Amigos&gt;</b>!&gt;`;
// Expressions as attribute values should stay as expressions
html`Hello <b foo=${'World'}>World</b>`;
html`Hello <b foo=${`Mundo`}>World</b>`;
html`<b foo=${'Hello'}>Hello</b><b bar=${`Mundo`}>World</b>`;
html`Hello <b .foo=${'World'}>World</b>`;
html`Hello <b .foo=${`Mundo`}>World</b>`;
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,9 @@ export class MyElement2 extends LitElement {
}
// Escaped markup characters should remain escaped
html`&lt;Hello<b>&lt;World &amp; Friends&gt;</b>!&gt;`;
// Expressions as attribute values should stay as expressions
html`Hello <b foo=${'World'}>World</b>`;
html`Hello <b foo=${'World'}>World</b>`;
html`<b foo=${'Hello'}>Hello</b><b bar=${'World'}>World</b>`;
html`Hello <b .foo=${'World'}>World</b>`;
html`Hello <b .foo=${'World'}>World</b>`;
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,10 @@ export class MyElement2 extends LitElement {

// Escaped markup characters should remain escaped
msg(html`&lt;Hello<b>&lt;World &amp; Friends&gt;</b>!&gt;`);

// Expressions as attribute values should stay as expressions
html`Hello <b foo=${"World"}>World</b>`;
html`Hello <b foo=${msg("World")}>World</b>`;
html`<b foo=${msg("Hello")}>Hello</b><b bar=${msg("World")}>World</b>`;
html`Hello <b .foo=${"World"}>World</b>`;
html`Hello <b .foo=${msg("World")}>World</b>`;