Bug 509943 - [HTML] Handle optional open/close tags according to HTML 5 spec
Summary: [HTML] Handle optional open/close tags according to HTML 5 spec
Status: RESOLVED FIXED
Alias: None
Product: Orion (Archived)
Classification: ECD
Component: JS Tools (show other bugs)
Version: 13.0   Edit
Hardware: PC Windows 7
: P3 normal (vote)
Target Milestone: 14.0   Edit
Assignee: Curtis Windatt CLA
QA Contact:
URL:
Whiteboard:
Keywords: noteworthy
Depends on:
Blocks:
 
Reported: 2017-01-04 11:49 EST by libing wang CLA
Modified: 2017-01-10 16:27 EST (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description libing wang CLA 2017-01-04 11:49:21 EST
If you use the following html snippet to see the "no matching closing tag" warning/error, it shows the the warning at the line where <p> appears.

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<title>title</title>
</head>
<body>
	<h1>header</h1>
	<p>
</body>
</html>

However if I add another line under <p>, like <h2>, there is a error marker at <h2> but hovering on the marker tells nothing. If I change to <h2></h2>, the error marker is gone. But there is no error marker at <p> now.

I think there is a hole here...
Comment 1 libing wang CLA 2017-01-04 11:51:10 EST
Paste the following to an html file, you should still see the error marker at <p> but you dont.

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<title>title</title>
</head>
<body>
	<h1>header</h1>
	<p>
        <h2></h2>
</body>
</html>
Comment 2 Michael Rennie CLA 2017-01-04 11:59:23 EST
(In reply to libing wang from comment #1)
> Paste the following to an html file, you should still see the error marker
> at <p> but you dont.
> 
> <!DOCTYPE html>
> <html lang="en">
> <head>
> 	<meta charset="utf-8">
> 	<title>title</title>
> </head>
> <body>
> 	<h1>header</h1>
> 	<p>
>         <h2></h2>
> </body>
> </html>

Some tags do not require a closing tag, they are assumed.

We should be very careful to follow the spec here: https://www.w3.org/TR/html5/syntax.html#optional-tags

It would also be very handy to have a linting rule to detect and warn about optional tag use.
Comment 3 Curtis Windatt CLA 2017-01-10 11:33:20 EST
The parser is attempting to follow the HTML 4 spec which marks certain attributes as having optional end tags.  I'm going to look at updating it to the html5 spec.

var openImpliesClose = {
		tr      : { tr:true, th:true, td:true },
		th      : { th:true },
		td      : { thead:true, th:true, td:true },
		body    : { head:true, link:true, script:true },
		li      : { li:true },
		p       : { p:true },
		h1      : { p:true },
		h2      : { p:true },
		h3      : { p:true },
		h4      : { p:true },
		h5      : { p:true },
		h6      : { p:true },
		select  : formTags,
		input   : formTags,
		output  : formTags,
		button  : formTags,
		datalist: formTags,
		textarea: formTags,
		option  : { option:true },
		optgroup: { optgroup:true }
	};
Comment 4 Curtis Windatt CLA 2017-01-10 16:20:29 EST
(In reply to Michael Rennie from comment #2)
> It would also be very handy to have a linting rule to detect and warn about
> optional tag use.

How to you see this working?  It could warn you when a tag isn't necessary, but there are many times where adding a close tag makes things much more readable.
Comment 5 Curtis Windatt CLA 2017-01-10 16:27:22 EST
http://git.eclipse.org/c/orion/org.eclipse.orion.client.git/commit/?id=7bdfab7b2e0fb87e64f1607aa0be86571346919a
Fixed in master with quite a few tests

I had to make changes on both the parser side and our validator side.
Parser - Recreated the openImpliesClose list to reflect html 5 spec and I fixed a bug where the ranges would be wrong.
Validator - Add a new check to the tag-close rule to see if the end tag is optional if it occurs at the end of the element content.

I did not implement any of the optional open tag rules because a) it would require major modification of htmlparser2 and b) it has no affect on our validation rules.

This does not solve the problem of explaining to the user why <body><p><h2></h2></body> is acceptable but <body><p><hz></hz></body> is not.  If there is a good suggestion on implementing that please open a new bug.