Skip to content

Commit

Permalink
Merge pull request #122 from daniel-sc/121-invalid-attribute-name
Browse files Browse the repository at this point in the history
fix: handle id with escaped characters - fixes #121
  • Loading branch information
daniel-sc authored Nov 27, 2024
2 parents d94509d + 9dad964 commit 51a6720
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
18 changes: 17 additions & 1 deletion src/model/translationFileSerialization.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,23 @@ describe('translationFileSerialization', () => {
</file>
</xliff>`);
});
it('should output additinalAttribute', () => {
it('should output id with quotes', () => {
const input = new TranslationFile([{
id: 'idWithQuote"',
source: 'some source',
locations: []
}], 'en', 'de', '');
expect(toXlf1(input, {prettyNestedTags: true})).toEqual(`<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="en" target-language="de" datatype="plaintext" original="ng2.template">
<body>
<trans-unit id="idWithQuote&quot;" datatype="html">
<source>some source</source>
</trans-unit>
</body>
</file>
</xliff>`);
});
it('should output additionalAttribute', () => {
const input = new TranslationFile([{
id: 'ID1',
source: 'source val',
Expand Down
6 changes: 4 additions & 2 deletions src/model/translationFileSerialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ export function toXlf2(translationFile: TranslationFile, options: Pick<Options,
}
const file = doc.childNamed('file')!;
file.children = translationFile.units.map(unit => {
const u = new XmlDocument(`<unit id="${unit.id}"><segment><source>${unit.source}</source></segment></unit>`);
const u = new XmlDocument(`<unit id=""><segment><source>${unit.source}</source></segment></unit>`);
u.attr.id = unit.id;
const segment = u.childNamed('segment')!;
if (unit.target !== undefined) {
segment.children.push(new XmlDocument(`<target>${unit.target}</target>`));
Expand Down Expand Up @@ -171,9 +172,10 @@ export function toXlf1(translationFile: TranslationFile, options: Pick<Options,
}
const body = file.childNamed('body')!;
body.children = translationFile.units.map(unit => {
const transUnit = new XmlDocument(`<trans-unit id="${unit.id}" datatype="html">
const transUnit = new XmlDocument(`<trans-unit id="" datatype="html">
<source>${unit.source}</source>
</trans-unit>`);
transUnit.attr.id = unit.id;
if (unit.target !== undefined) {
const target = new XmlDocument(`<target>${unit.target}</target>`);
if (unit.state !== undefined) {
Expand Down

0 comments on commit 51a6720

Please sign in to comment.