Sitelet https://github.com/angular/angular/commit/b8e9b3d
Skip to content

Commit b8e9b3d

Browse files
petebacondarwinmhevery
authored andcommitted
fix(compiler): skipping leading whitespace should not break placeholder source-spans (#39486)
Tokenized text node may have leading whitespace skipped from their source-span. But the source-span is used to compute where there are interpolated blocks, resulting in placeholder nodes whose source-spans are offset by the amount of skipped characters. This fix uses the `fullStart` location of text source-spans for computing the source-span of placeholders, so that they are accurate. Fixes #39195 PR Close #39486
1 parent 94d0013 commit b8e9b3d

4 files changed

Lines changed: 30 additions & 7 deletions

File tree

‎packages/compiler/src/i18n/i18n_parser.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ class _I18nVisitor implements html.Visitor {
203203

204204
function getOffsetSourceSpan(
205205
sourceSpan: ParseSourceSpan, {start, end}: {start: number, end: number}): ParseSourceSpan {
206-
return new ParseSourceSpan(sourceSpan.start.moveBy(start), sourceSpan.start.moveBy(end));
206+
return new ParseSourceSpan(sourceSpan.fullStart.moveBy(start), sourceSpan.fullStart.moveBy(end));
207207
}
208208

209209
const _CUSTOM_PH_EXP =

‎packages/compiler/src/render3/view/template.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const EVENT_BINDING_SCOPE_GLOBALS = new Set<string>(['$event']);
5555
const GLOBAL_TARGET_RESOLVERS = new Map<string, o.ExternalReference>(
5656
[['window', R3.resolveWindow], ['document', R3.resolveDocument], ['body', R3.resolveBody]]);
5757

58-
const LEADING_TRIVIA_CHARS = [' ', '\n', '\r', '\t'];
58+
export const LEADING_TRIVIA_CHARS = [' ', '\n', '\r', '\t'];
5959

6060
// if (rf & flags) { .. }
6161
export function renderFlagCheckIfStmt(

‎packages/compiler/test/render3/view/i18n_spec.ts‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {serializeIcuNode} from '../../../src/render3/view/i18n/icu_serializer';
1818
import {serializeI18nMessageForLocalize} from '../../../src/render3/view/i18n/localize_utils';
1919
import {I18nMeta, parseI18nMeta} from '../../../src/render3/view/i18n/meta';
2020
import {formatI18nPlaceholderName} from '../../../src/render3/view/i18n/util';
21+
import {LEADING_TRIVIA_CHARS} from '../../../src/render3/view/template';
2122

2223
import {parseR3 as parse} from './util';
2324

@@ -386,7 +387,7 @@ describe('serializeI18nMessageForGetMsg', () => {
386387

387388
describe('serializeI18nMessageForLocalize', () => {
388389
const serialize = (input: string) => {
389-
const tree = parse(`<div i18n>${input}</div>`);
390+
const tree = parse(`<div i18n>${input}</div>`, {leadingTriviaChars: LEADING_TRIVIA_CHARS});
390391
const root = tree.nodes[0] as t.Element;
391392
return serializeI18nMessageForLocalize(root.i18n as i18n.Message);
392393
};
@@ -477,7 +478,7 @@ describe('serializeI18nMessageForLocalize', () => {
477478
expect(messageParts[3].text).toEqual('');
478479
expect(messageParts[3].sourceSpan.toString()).toEqual('');
479480
expect(messageParts[4].text).toEqual(' D');
480-
expect(messageParts[4].sourceSpan.toString()).toEqual(' D');
481+
expect(messageParts[4].sourceSpan.toString()).toEqual('D');
481482

482483
expect(placeHolders[0].text).toEqual('START_TAG_SPAN');
483484
expect(placeHolders[0].sourceSpan.toString()).toEqual('<span>');
@@ -509,6 +510,26 @@ describe('serializeI18nMessageForLocalize', () => {
509510
expect(humanizeSourceSpan(placeHolders[2].sourceSpan)).toEqual('"</b>" (22-26)');
510511
});
511512

513+
it('should create the correct placeholder source-spans when there is skipped leading whitespace',
514+
() => {
515+
const {messageParts, placeHolders} = serialize('<b> {{value}}</b>');
516+
expect(messageParts[0].text).toEqual('');
517+
expect(humanizeSourceSpan(messageParts[0].sourceSpan)).toEqual('"" (10-10)');
518+
expect(messageParts[1].text).toEqual(' ');
519+
expect(humanizeSourceSpan(messageParts[1].sourceSpan)).toEqual('" " (13-16)');
520+
expect(messageParts[2].text).toEqual('');
521+
expect(humanizeSourceSpan(messageParts[2].sourceSpan)).toEqual('"" (25-25)');
522+
expect(messageParts[3].text).toEqual('');
523+
expect(humanizeSourceSpan(messageParts[3].sourceSpan)).toEqual('"" (29-29)');
524+
525+
expect(placeHolders[0].text).toEqual('START_BOLD_TEXT');
526+
expect(humanizeSourceSpan(placeHolders[0].sourceSpan)).toEqual('"<b> " (10-16)');
527+
expect(placeHolders[1].text).toEqual('INTERPOLATION');
528+
expect(humanizeSourceSpan(placeHolders[1].sourceSpan)).toEqual('"{{value}}" (16-25)');
529+
expect(placeHolders[2].text).toEqual('CLOSE_BOLD_TEXT');
530+
expect(humanizeSourceSpan(placeHolders[2].sourceSpan)).toEqual('"</b>" (25-29)');
531+
});
532+
512533
it('should serialize simple ICU for `$localize()`', () => {
513534
expect(serialize('{age, plural, 10 {ten} other {other}}')).toEqual({
514535
messageParts: [literal('{VAR_PLURAL, plural, 10 {ten} other {other}}')],

‎packages/compiler/test/render3/view/util.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,13 @@ export function toStringExpression(expr: e.AST): string {
7878

7979
// Parse an html string to IVY specific info
8080
export function parseR3(
81-
input: string, options: {preserveWhitespaces?: boolean} = {}): Render3ParseResult {
81+
input: string, options: {preserveWhitespaces?: boolean, leadingTriviaChars?: string[]} = {}):
82+
Render3ParseResult {
8283
const htmlParser = new HtmlParser();
8384

84-
const parseResult =
85-
htmlParser.parse(input, 'path:://to/template', {tokenizeExpansionForms: true});
85+
const parseResult = htmlParser.parse(
86+
input, 'path:://to/template',
87+
{tokenizeExpansionForms: true, leadingTriviaChars: options.leadingTriviaChars});
8688

8789
if (parseResult.errors.length > 0) {
8890
const msg = parseResult.errors.map(e => e.toString()).join('\n');

0 commit comments

Comments
 (0)