Skip to content

Commit

Permalink
fix(extractor): serialize xml entities read from a source file
Browse files Browse the repository at this point in the history
  • Loading branch information
ocombe authored May 2, 2018
2 parents 7071cd7 + 425e554 commit 86cb31e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 24 deletions.
7 changes: 7 additions & 0 deletions example/src/i18n/source.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@
<context context-type="linenumber">1</context>
</context-group>
</trans-unit>
<trans-unit id="0cd84cefa60f8c0e31d02904644807a6" datatype="html">
<source>special &amp; characters</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/app.component.ts</context>
<context context-type="linenumber">1</context>
</context-group>
</trans-unit>
</body>
</file>
</xliff>
25 changes: 5 additions & 20 deletions lib/src/serializers/xml_helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Visitor implements IVisitor {
}

visitText(text: Text): string {
return text.value;
return _escapeXml(text.value);
}

visitElement(element: ml.Element) {
Expand All @@ -46,7 +46,7 @@ class Visitor implements IVisitor {

private _serializeAttributes(attrs: {[k: string]: string}) {
const strAttrs = Object.keys(attrs)
.map((name: string) => `${name}="${attrs[name]}"`)
.map((name: string) => `${name}="${_escapeXml(attrs[name])}"`)
.join(" ");
return strAttrs.length > 0 ? " " + strAttrs : "";
}
Expand All @@ -67,13 +67,7 @@ export interface Node {
}

export class Declaration implements Node {
public attrs: {[k: string]: string} = {};

constructor(unescapedAttrs: {[k: string]: string}) {
Object.keys(unescapedAttrs).forEach((k: string) => {
this.attrs[k] = _escapeXml(unescapedAttrs[k]);
});
}
constructor(public attrs: {[k: string]: string}) {}

visit(visitor: IVisitor): any {
return visitor.visitDeclaration(this);
Expand All @@ -89,24 +83,15 @@ export class Doctype implements Node {
}

export class Tag implements Node {
public attrs: {[k: string]: string} = {};

constructor(public name: string, unescapedAttrs: {[k: string]: string} = {}, public children: Node[] = []) {
Object.keys(unescapedAttrs).forEach((k: string) => {
this.attrs[k] = _escapeXml(unescapedAttrs[k]);
});
}
constructor(public name: string, public attrs: {[k: string]: string} = {}, public children: Node[] = []) {}

visit(visitor: IVisitor): any {
return visitor.visitTag(this);
}
}

export class Text implements Node {
value: string;
constructor(unescapedValue: string) {
this.value = _escapeXml(unescapedValue);
}
constructor(public value: string) {}

visit(visitor: IVisitor): any {
return visitor.visitText(this);
Expand Down
6 changes: 6 additions & 0 deletions test/extractor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ describe("Extractor", () => {
<context context-type="sourcefile">src/app/app.component.ts</context>
<context context-type="linenumber">1</context>
</context-group>
</trans-unit><trans-unit id="0cd84cefa60f8c0e31d02904644807a6" datatype="html">
<source>special &amp; characters</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/app.component.ts</context>
<context context-type="linenumber">1</context>
</context-group>
</trans-unit>
<trans-unit id="customId" datatype="html">
<source>Some value</source>
Expand Down
8 changes: 4 additions & 4 deletions test/serializers/xmb.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,14 @@ describe("Xmb serializer", () => {
<!ELEMENT ex (#PCDATA)>
]>
<messagebundle>
<msg id="7056919470098446707"><source>file.ts:3</source>translatable element <ph name="START_BOLD_TEXT"><ex><b></ex></ph>with placeholders<ph name="CLOSE_BOLD_TEXT"><ex></b></ex></ph> <ph name="INTERPOLATION"><ex>{{ interpolation}}</ex></ph></msg>
<msg id="2981514368455622387"><source>file.ts:4</source>{VAR_PLURAL, plural, =0 {<ph name="START_PARAGRAPH"><ex><p></ex></ph>test<ph name="CLOSE_PARAGRAPH"><ex></p></ex></ph>} }</msg>
<msg id="7056919470098446707"><source>file.ts:3</source>translatable element <ph name="START_BOLD_TEXT"><ex>&lt;b&gt;</ex></ph>with placeholders<ph name="CLOSE_BOLD_TEXT"><ex>&lt;/b&gt;</ex></ph> <ph name="INTERPOLATION"><ex>{{ interpolation}}</ex></ph></msg>
<msg id="2981514368455622387"><source>file.ts:4</source>{VAR_PLURAL, plural, =0 {<ph name="START_PARAGRAPH"><ex>&lt;p&gt;</ex></ph>test<ph name="CLOSE_PARAGRAPH"><ex>&lt;/p&gt;</ex></ph>} }</msg>
<msg id="7999024498831672133" desc="d" meaning="m"><source>file.ts:5</source>foo</msg>
<msg id="i" desc="d" meaning="m"><source>file.ts:6</source>foo</msg>
<msg id="bar"><source>file.ts:7</source>foo</msg>
<msg id="baz"><source>file.ts:8</source>{VAR_PLURAL, plural, =0 {{VAR_SELECT, select, other {<ph name="START_PARAGRAPH"><ex><p></ex></ph>deeply nested<ph name="CLOSE_PARAGRAPH"><ex></p></ex></ph>} } } }</msg>
<msg id="baz"><source>file.ts:8</source>{VAR_PLURAL, plural, =0 {{VAR_SELECT, select, other {<ph name="START_PARAGRAPH"><ex>&lt;p&gt;</ex></ph>deeply nested<ph name="CLOSE_PARAGRAPH"><ex>&lt;/p&gt;</ex></ph>} } } }</msg>
<msg id="6997386649824869937"><source>file.ts:9</source>Test: <ph name="ICU"><ex>{ count, plural, =0 {...} =other {...}}</ex></ph></msg>
<msg id="5229984852258993423"><source>file.ts:9</source>{VAR_PLURAL, plural, =0 {{VAR_SELECT, select, other {<ph name="START_PARAGRAPH"><ex><p></ex></ph>deeply nested<ph name="CLOSE_PARAGRAPH"><ex></p></ex></ph>} } } =other {a lot} }</msg>
<msg id="5229984852258993423"><source>file.ts:9</source>{VAR_PLURAL, plural, =0 {{VAR_SELECT, select, other {<ph name="START_PARAGRAPH"><ex>&lt;p&gt;</ex></ph>deeply nested<ph name="CLOSE_PARAGRAPH"><ex>&lt;/p&gt;</ex></ph>} } } =other {a lot} }</msg>
<msg id="2340165783990709777"><source>file.ts:10,11</source>multi
lines</msg>
<msg id="5980763297918130233"><source>file.ts:1</source>This is a test message <ph name="ICU"><ex>{sex, select, other {...}}</ex></ph></msg>
Expand Down

0 comments on commit 86cb31e

Please sign in to comment.