microsoft / microsoft/vscode-html-languageservice

Support for optional (implicit) closing tags (e.g. paragraphs and list items) in Breadcrumbs, Outline and formatters

Open
#63 2 comments 10 reactions 1 assignee View on GitHub

@aeschli is already working on this.

Since Jun 9, 2019.

feature-request
Dominant language
TypeScript
Stars
284
Forks
138
Avg merge
19h 11m
Merged PRs (30d)
5

Description

For HTML document

<!doctype html>
<head><title>Test</title>
<body><p>a<p>b<p>c

Placing cursor at the end of the "c" paragraph makes Breadcrumbs display

head > body > p > p > p

It is incorrect per the HTML spec and in effect differs from from what happens in real HTML parsers. (It is still valid, though.)

Correct outcome should be (not considering implied HTML):

body > p

VSC Version: 1.35.0 showing above snippet and Breadcrumbs that contain aforementioned wrong multiple paragraphs nested in each other inside head.


I assume it must have been discussed in the past but couldn't find any trace of prior issue.
Yes, not using optional closing tags is generally considered bad coding practice (not considering "google output compression"), and I assume that fixing this would not be a trivial task (considering complexity of specs), but current state when VSCode Outline lies about resulting HTML structure is not fortunate.

(I'm not sure about support for implied elements and their corresponding tags, but I assume it might be confusing to some seeing e.g. tbody nodes inside table that has not explicit opening tbody tag authored in the source code. But it would probably not spoil anything to see it there, since it would better reflect the reality. Different issue, perhaps?)

More verbose descriptive article
<html>
<!--
	This tag does not have to be present in the source code: <html> tag (without attributes) is implied
-->
	<head><!-- Ditto <head>. -->
		<title>Optional closing tags test for VS Code</title>
		<!-- <title> is the only mandatory tag in HTML document -->
	<body>
	<!--
		Neither <body> is mandatory.
		Presence of <body> here automatically finished <head>.
		In fact occurrence of any element that is not allowed in HEAD would do the same.
	-->
		<p>HTML syntax tree in current Visual Studio Code is not correct.
		<h2>Trivia:</h2>
		<p>There are several block level elements with optional closing tags in HTML:
		<dl>
			<dt><code>P</code>
			<dd>paragraph cannot contain block-level elements so any of them finishes it, 
			<dt><code>HTML</code>, <code>HEAD</code>, <code>BODY</code> <code>TBODY</code> <code>COLGROUP</code>
			<dd>those are even <i>implied</i>, so even their starting tags are optional; presence of elements that belongs there creates them
			<dt><code>LI</code>, <code>DT</code>, <code>DD</code>
			<dd>finished by parent <code>/UL</code>, <code>/OL</code>, <code>/DL</code> or following <code>LI</code>, <code>DT</code>, <code>DD</code>
		</dl>
		<p>If you place cursor in the end of the source of definition list above, you'll observe 
            <code>html > head > body > p > p > dl > dt > dd > dt > dd > dt > dd</code> being displayed in breadcrumbs, what implies that given definition is nested in of preceding definitions what in two nested paragraphs which are children of body <i>in</i> head.
        <p>It is completely different of how HTML5 conforming agent interprets it. Correct path should be <code>html > body > dl > dd</code>
        <p>This affects Breacrubs and logicaly Outline and to some degree Format document action. (IIRC formatting is currently done in other library than language service, but mentioning it for completeness).
<!-- end of file finishes all opened tags -->

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.