VS Code为什么将HTML闭合标签中标签名称和右尖括号之间的空格标记为无效?

huangapple go评论113阅读模式
英文:

Why does VS Code mark closing HTML tags with space between the tag name and the right angle bracket as invalid?

问题

When I added an SVG to my footer, elements after the SVG (including the closing tag of the SVG) turned red. Is there a problem with the code? Prettier seems to work fine.

The issue can be reproduced minimally like this:

<svg></svg
>

I don't see this issue on all color themes. One that I do see it on is the Dark+ theme.

VS Code为什么将HTML闭合标签中标签名称和右尖括号之间的空格标记为无效?

Only after deleting the SVG do the elements revert to their original color.

I'm on VS Code 1.78, but I'm not sure if that detail is relevant here.

英文:

When I added an SVG to my footer, elements after the SVG (including the closing tag of the SVG) turned red. Is there a problem with the code? Prettier seems to work fine.

The issue can be reproduced minimally like this:

<svg></svg
>

I don't see this issue on all colour themes. One that I do see it on is the Dark+ theme.

VS Code为什么将HTML闭合标签中标签名称和右尖括号之间的空格标记为无效?

Only after deleting the SVG do the elements revert to their original colour.

I'm on VS Code 1.78, but I'm not sure if that detail is relevant here.

答案1

得分: 1

以下是您要翻译的内容:

您可以使用命令面板中的“开发者:检查编辑器标记和范围”命令来查看发生了什么情况。

对于通常突出显示的标记,我看到了这些TextMate范围:

entity.name.tag.html
meta.tag.object.svg.path.start.html
meta.element.object.svg.path.html
meta.element.structure.svg.g.html
meta.element.structure.svg.html
text.html.derivative

对于颜色不同的标记,我看到了这些TextMate范围:

invalid.illegal.unrecognized-tag.html
entity.name.tag.html
meta.tag.other.svg.html
meta.element.structure.svg.html
text.html.derivative

但是,如果您查看HTML规范,它表示您在闭合标记中的标记名称和其闭合尖括号之间使用换行和空格是合法的HTML。

https://html.spec.whatwg.org/multipage/syntax.html#end-tags 上写着:

> 4. 在标记名称之后,可能会有一个或多个ASCII空格。

https://infra.spec.whatwg.org/#ascii-whitespace 上写着:

> ASCII空格 是U+0009 TAB、U+000A LF、U+000C FF、U+000D CR或U+0020 SPACE。

还相关的:https://stackoverflow.com/q/3314535/11107541。

所以,这似乎是一个与VS Code内置的HTML TextMate语法有关的错误。

实际上,如果您搜索“github vscode issues html closing tag space between tag name and angle bracket invalid”并向下滚动一点,您会找到svg tag in HTML mode incorrectly marked invalid when closing tag spans more than one line #124215,该问题正好涵盖了这个问题,并已关闭并指向上游的Incorrect HTML Syntax highlighting on SVG with Line break #101。您可以点赞该问题以表明支持其修复,并订阅该问题以获取讨论和进展的通知(但请避免发布嘈杂的“我也是”的评论)。

您之所以在所有颜色主题中都看不到此行为,是因为某些主题将其对于“无效”标记的高亮设置为与有效标记相同的颜色,或者只是没有使颜色差异足够明显,以至于不容易注意到,或者可能它们只是未设置它,并且默认为看起来与它们为有效标记选择的颜色相似的颜色。

有趣的是,似乎Stack Exchange的语法高亮(使用highlight.js)也无法检测到这是有效的HTML。

英文:

You can use the Developer: Inspect Editor Tokens and Scopes command in the command palette to see what's going on.

For the tags that are highlighted normally, I see these TextMate scopes:

entity.name.tag.html
meta.tag.object.svg.path.start.html
meta.element.object.svg.path.html
meta.element.structure.svg.g.html
meta.element.structure.svg.html
text.html.derivative

For the differently coloured ones, I see these TextMate scopes:

invalid.illegal.unrecognized-tag.html
entity.name.tag.html
meta.tag.other.svg.html
meta.element.structure.svg.html
text.html.derivative

But if you look at the HTML spec, it says what you've written with a newline and spaces separating the tag name in the closing tag and its closing angle bracket is legal HTML.

https://html.spec.whatwg.org/multipage/syntax.html#end-tags says:

> 4. After the tag name, there may be one or more ASCII whitespace.

https://infra.spec.whatwg.org/#ascii-whitespace says:

> ASCII whitespace is U+0009 TAB, U+000A LF, U+000C FF, U+000D CR, or U+0020 SPACE.

Also related: https://stackoverflow.com/q/3314535/11107541.

So this seems like a bug with VS Code's builtin HTML TextMate grammar.

Actually, if you google "github vscode issues html closing tag space between tag name and angle bracket invalid" and scroll down a bit, you'll find svg tag in HTML mode incorrectly marked invalid when closing tag spans more than one line #124215, which covers exactly this issue, and is closed pointing upstream to Incorrect HTML Syntax highlighting on SVG with Line break #101. You can give the issue a thumbs up to show support for getting it fixed, and subscribe to it to get notified of discussion and progress (but please avoid making noisy "me too" comments).

The reason you don't see this behaviour for all colour themes is because some themes set their highlighting for "invalid" tags to be coloured the same as valid tags, or just don't make the colour difference apparent enough for it to be noticeable, or maybe they're just not setting it and it's defaulting to something that looks similar to the colour they chose for valid tags.

Funnily, it looks like Stack Exchange's syntax highlighting (which uses highlight.js) fails to detect that this is valid HTML as well.

答案2

得分: -1

"It's red because you forgot to close the svg tag"
这是因为你忘记关闭 svg 标签。

"Here's how should be: </g></svg>"
应该是这样的: </g></svg>

"It's because your close tag is one line down; if you move close to the svg, it should be fixed."
这是因为你的闭合标签在下一行;如果你将其移到 svg 附近,问题应该就解决了。

"You also can change your settings on VS Code to format your code when you save the file, to avoid this issue in the future."
你还可以在 VS Code 中更改设置,使代码在保存文件时自动格式化,以避免将来出现这个问题。

"Here's a quick reference: https://stackoverflow.com/a/50606759/2092969"
这里有一个快速参考链接: https://stackoverflow.com/a/50606759/2092969

英文:

It's red because you forgot to close the svg tag

Here's how should be: </g></svg>

--- Edited

It's because your close tag is one line down; if you move close to the svg, it should be fixed.

You also can change your settings on VS Code to format your code when you save the file, to avoid this issue in the future.

Here's a quick reference: https://stackoverflow.com/a/50606759/2092969

huangapple
  • 本文由 发表于 2023年5月7日 05:18:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76191193.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定