From 6ed962fa6e92e761243b7e5c794698d2be97b4fc Mon Sep 17 00:00:00 2001 From: Kim Biesbjerg Date: Sun, 22 Mar 2020 10:23:45 +0100 Subject: [PATCH] Remove line breaks and tabs from extracted keys in templates. Closes #144, #167 --- src/parsers/directive.parser.ts | 15 ++++++++++----- tests/parsers/directive.parser.spec.ts | 22 ++++++++++++++++++++++ 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/parsers/directive.parser.ts b/src/parsers/directive.parser.ts index 59493cc..fc0b4fa 100644 --- a/src/parsers/directive.parser.ts +++ b/src/parsers/directive.parser.ts @@ -14,7 +14,7 @@ export class DirectiveParser implements ParserInterface { const nodes: TmplAstNode[] = this.parseTemplate(source, filePath); 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); }); @@ -35,7 +35,7 @@ export class DirectiveParser implements ParserInterface { 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 if (this.isTranslatable(node)) { return [node]; @@ -73,10 +73,15 @@ export class DirectiveParser implements ParserInterface { return attr?.value ?? ''; } - protected getElementContents(element: TmplAstElement): string { - const contents = element.sourceSpan.start.file.content; + protected getElementContent(element: TmplAstElement): string { + const content = element.sourceSpan.start.file.content; const start = element.startSourceSpan.end.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, ''); } } diff --git a/tests/parsers/directive.parser.spec.ts b/tests/parsers/directive.parser.spec.ts index fd34dde..bc1b9fc 100644 --- a/tests/parsers/directive.parser.spec.ts +++ b/tests/parsers/directive.parser.spec.ts @@ -85,4 +85,26 @@ describe('DirectiveParser', () => { const keys = parser.extract(contents, componentFilename).keys(); expect(keys).to.deep.equal([]); }); + + it('should extract contents without line breaks', () => { + const contents = ` +

+ Please leave a message for your client letting them know why you + rejected the field and what they need to do to fix it. +

+ `; + 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 = ` +
There + are currently no students in this class. The good news is, adding students is really easy! Just use the options + at the top. +
+ `; + 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.']); + }); });