Skip to content

fix(compiler): incorrect spans for AST inside input value with leading space #63082

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/compiler/src/render3/r3_template_transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -697,8 +697,8 @@ class HtmlAstToIvyAst implements html.Visitor {
const value = attribute.value;
const srcSpan = attribute.sourceSpan;
const absoluteOffset = attribute.valueSpan
? attribute.valueSpan.start.offset
: srcSpan.start.offset;
? attribute.valueSpan.fullStart.offset
: srcSpan.fullStart.offset;

function createKeySpan(srcSpan: ParseSourceSpan, prefix: string, identifier: string) {
// We need to adjust the start location for the keySpan to account for the removed 'data-'
Expand Down
19 changes: 19 additions & 0 deletions packages/compiler/test/render3/r3_ast_spans_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* found in the LICENSE file at https://angular.dev/license
*/

import {ASTWithSource} from '../../src/expression_parser/ast';
import {ParseSourceSpan} from '../../src/parse_util';
import * as t from '../../src/render3/r3_ast';

Expand Down Expand Up @@ -409,6 +410,24 @@ describe('R3 AST source spans', () => {
['BoundAttribute', '@animation', 'animation', '<empty>'],
]);
});

it('should not throw off span of value in bound attribute when leading spaces are present', () => {
const assertValueSpan = (template: string, start: number, end: number) => {
const result = parse(template);
const boundAttribute = (result.nodes[0] as t.Element).inputs[0];
const span = (boundAttribute.value as ASTWithSource).ast.sourceSpan;

expect(span.start).toBe(start);
expect(span.end).toBe(end);
};

assertValueSpan('<a [b]="helloWorld"></a>', 8, 18);
assertValueSpan('<a [b]=" helloWorld"></a>', 9, 19);
assertValueSpan('<a [b]=" helloWorld"></a>', 10, 20);
assertValueSpan('<a [b]=" helloWorld"></a>', 11, 21);
assertValueSpan('<a [b]=" helloWorld"></a>', 12, 22);
assertValueSpan('<a [b]=" helloWorld"></a>', 50, 60);
});
});

describe('templates', () => {
Expand Down