Remove line breaks and tabs from extracted keys in templates. Closes #144, #167

This commit is contained in:
Kim Biesbjerg 2020-03-22 10:23:45 +01:00
parent 8aa2774eca
commit 6ed962fa6e
2 changed files with 32 additions and 5 deletions

View File

@ -14,7 +14,7 @@ export class DirectiveParser implements ParserInterface {
const nodes: TmplAstNode[] = this.parseTemplate(source, filePath); const nodes: TmplAstNode[] = this.parseTemplate(source, filePath);
this.getTranslatableElements(nodes).forEach(element => { this.getTranslatableElements(nodes).forEach(element => {
const key = this.getElementTranslateAttrValue(element) || this.getElementContents(element); const key = this.getElementTranslateAttrValue(element) || this.getElementContent(element);
collection = collection.add(key); collection = collection.add(key);
}); });
@ -35,7 +35,7 @@ export class DirectiveParser implements ParserInterface {
return []; return [];
} }
// If element has translate attribute all its contents is translatable // If element has translate attribute all its content is translatable
// so we don't need to traverse any deeper // so we don't need to traverse any deeper
if (this.isTranslatable(node)) { if (this.isTranslatable(node)) {
return [node]; return [node];
@ -73,10 +73,15 @@ export class DirectiveParser implements ParserInterface {
return attr?.value ?? ''; return attr?.value ?? '';
} }
protected getElementContents(element: TmplAstElement): string { protected getElementContent(element: TmplAstElement): string {
const contents = element.sourceSpan.start.file.content; const content = element.sourceSpan.start.file.content;
const start = element.startSourceSpan.end.offset; const start = element.startSourceSpan.end.offset;
const end = element.endSourceSpan.start.offset; const end = element.endSourceSpan.start.offset;
return contents.substring(start, end).trim(); const val = content.substring(start, end);
return this.cleanKey(val);
}
protected cleanKey(val: string): string {
return val.replace(/\r?\n|\r|\t/g, '');
} }
} }

View File

@ -85,4 +85,26 @@ describe('DirectiveParser', () => {
const keys = parser.extract(contents, componentFilename).keys(); const keys = parser.extract(contents, componentFilename).keys();
expect(keys).to.deep.equal([]); expect(keys).to.deep.equal([]);
}); });
it('should extract contents without line breaks', () => {
const contents = `
<p translate>
Please leave a message for your client letting them know why you
rejected the field and what they need to do to fix it.
</p>
`;
const keys = parser.extract(contents, templateFilename).keys();
expect(keys).to.deep.equal(['Please leave a message for your client letting them know why you rejected the field and what they need to do to fix it.']);
});
it('should extract contents without indent spaces', () => {
const contents = `
<div *ngIf="!isLoading && studentsToGrid && studentsToGrid.length == 0" class="no-students" mt-rtl translate>There
are currently no students in this class. The good news is, adding students is really easy! Just use the options
at the top.
</div>
`;
const keys = parser.extract(contents, templateFilename).keys();
expect(keys).to.deep.equal(['There are currently no students in this class. The good news is, adding students is really easy! Just use the options at the top.']);
});
}); });