X Tutup
The Wayback Machine - https://web.archive.org/web/20211015112104/https://github.com/angular/angular/issues/39148
Skip to content
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 鈥淪ign up for GitHub鈥, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template parser produces wrong locations for HTML self-closing tags #39148

Closed
AlexanderFSP opened this issue Oct 6, 2020 路 1 comment
Closed

Template parser produces wrong locations for HTML self-closing tags #39148

AlexanderFSP opened this issue Oct 6, 2020 路 1 comment

Comments

@AlexanderFSP
Copy link

@AlexanderFSP AlexanderFSP commented Oct 6, 2020

馃悶 bug report

Affected Package

The issue is caused by package @angular/compiler

Is this a regression?

No

Description

Template parser from @angular/compiler package produces wrong locations for HTML self-closing tags.
Here the initial simple template:

  <input type="text" />


        <span>
    
    </span>

And if we pass this template to the exported parseTemplate function from @angular/compiler package we can see that sourceSpan.end for input node located in front of the opening span tag.

{
  "nodes": [
    {
      "name": "input",
      "sourceSpan": {
        "start": {
          "offset": 3,
          "line": 1,
          "col": 2
        },
        "end": {
          "offset": 35,
          "line": 4,
          "col": 8
        }
      },
      ...,
      "endSourceSpan": {
        "start": {
          "offset": 3,
          "line": 1,
          "col": 2
        },
        "end": {
          "offset": 35,
          "line": 4,
          "col": 8
        }
      }
    },
    {
      "name": "span",
      "sourceSpan": {
        "start": {
          "offset": 35,
          "line": 4,
          "col": 8
        },
        ...
      },
      ...
    }
  ]
}

But from my point of view (let's say "expected result") sourceSpan.end should be pointed after the second input field's angle bracket and shouldn't include spaces and linebreaks.

馃敩 Minimal Reproduction

https://stackblitz.com/edit/ng-tpl-parser-self-closing-tags?file=index.ts

馃敟 Exception or Error

馃實 Your Environment

Angular Version:

@angular/compiler@10.1.4

Anything else relevant?
This issue produces some difficulties for static template code analysis (in this case I would like to mention https://github.com/angular-eslint/angular-eslint which uses @angular/compiler to generate AST). And here you can see the issue that produces by the wrong sourceSpan location.

image

@ngbot ngbot bot added this to the needsTriage milestone Oct 7, 2020
@ngbot ngbot bot removed this from the needsTriage milestone Oct 7, 2020
@ngbot ngbot bot added this to the Backlog milestone Oct 7, 2020
@JoostK JoostK self-assigned this Jan 17, 2021
JoostK added a commit to JoostK/angular that referenced this issue Jan 21, 2021
If the template parse option `leadingTriviaChars` is configured to
consider whitespace as trivia, any trailing whitespace of an element
would be considered as leading trivia of the subsequent element, such
that its `start` span would start _after_ the whitespace. This means
that the start span cannot be used to mark the end of the current
element, as its trailing whitespace would then be included in its span.
Instead, the full start of the subsequent element should be used.

To harden the tests that for the Ivy parser, the test utility `parseR3`
has been adjusted to use the same configuration for `leadingTriviaChars`
as would be the case in its production counterpart `parseTemplate`. This
uncovered another bug in offset handling of the interpolation parser,
where the absolute offset was computed from the start source span
(which excludes leading trivia) whereas the interpolation expression
would include the leading trivia. As such, the absolute offset now also
uses the full start span.

Fixes angular#39148
JoostK added a commit to JoostK/angular that referenced this issue Jan 22, 2021
If the template parse option `leadingTriviaChars` is configured to
consider whitespace as trivia, any trailing whitespace of an element
would be considered as leading trivia of the subsequent element, such
that its `start` span would start _after_ the whitespace. This means
that the start span cannot be used to mark the end of the current
element, as its trailing whitespace would then be included in its span.
Instead, the full start of the subsequent element should be used.

To harden the tests that for the Ivy parser, the test utility `parseR3`
has been adjusted to use the same configuration for `leadingTriviaChars`
as would be the case in its production counterpart `parseTemplate`. This
uncovered another bug in offset handling of the interpolation parser,
where the absolute offset was computed from the start source span
(which excludes leading trivia) whereas the interpolation expression
would include the leading trivia. As such, the absolute offset now also
uses the full start span.

Fixes angular#39148
JoostK added a commit to JoostK/angular that referenced this issue Jan 26, 2021
If the template parse option `leadingTriviaChars` is configured to
consider whitespace as trivia, any trailing whitespace of an element
would be considered as leading trivia of the subsequent element, such
that its `start` span would start _after_ the whitespace. This means
that the start span cannot be used to mark the end of the current
element, as its trailing whitespace would then be included in its span.
Instead, the full start of the subsequent element should be used.

To harden the tests that for the Ivy parser, the test utility `parseR3`
has been adjusted to use the same configuration for `leadingTriviaChars`
as would be the case in its production counterpart `parseTemplate`. This
uncovered another bug in offset handling of the interpolation parser,
where the absolute offset was computed from the start source span
(which excludes leading trivia) whereas the interpolation expression
would include the leading trivia. As such, the absolute offset now also
uses the full start span.

Fixes angular#39148
@mhevery mhevery closed this in c18c7e2 Jan 28, 2021
mhevery added a commit that referenced this issue Jan 28, 2021
鈥�40513)

If the template parse option `leadingTriviaChars` is configured to
consider whitespace as trivia, any trailing whitespace of an element
would be considered as leading trivia of the subsequent element, such
that its `start` span would start _after_ the whitespace. This means
that the start span cannot be used to mark the end of the current
element, as its trailing whitespace would then be included in its span.
Instead, the full start of the subsequent element should be used.

To harden the tests that for the Ivy parser, the test utility `parseR3`
has been adjusted to use the same configuration for `leadingTriviaChars`
as would be the case in its production counterpart `parseTemplate`. This
uncovered another bug in offset handling of the interpolation parser,
where the absolute offset was computed from the start source span
(which excludes leading trivia) whereas the interpolation expression
would include the leading trivia. As such, the absolute offset now also
uses the full start span.

Fixes #39148

PR Close #40513
mhevery added a commit that referenced this issue Jan 28, 2021
鈥�40513)

If the template parse option `leadingTriviaChars` is configured to
consider whitespace as trivia, any trailing whitespace of an element
would be considered as leading trivia of the subsequent element, such
that its `start` span would start _after_ the whitespace. This means
that the start span cannot be used to mark the end of the current
element, as its trailing whitespace would then be included in its span.
Instead, the full start of the subsequent element should be used.

To harden the tests that for the Ivy parser, the test utility `parseR3`
has been adjusted to use the same configuration for `leadingTriviaChars`
as would be the case in its production counterpart `parseTemplate`. This
uncovered another bug in offset handling of the interpolation parser,
where the absolute offset was computed from the start source span
(which excludes leading trivia) whereas the interpolation expression
would include the leading trivia. As such, the absolute offset now also
uses the full start span.

Fixes #39148

PR Close #40513
@angular-automatic-lock-bot
Copy link

@angular-automatic-lock-bot angular-automatic-lock-bot bot commented Feb 28, 2021

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Feb 28, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

4 participants
X Tutup