HTML comment missing symmetry - html

In HTML we usually do comments alike:
<!-- Comment here -->
Why is the comment not symmetrical as in:
<!-- Comment here --!>
I'm looking for the historical answer.

Please look into HTML 2.0 RFC
here's and interesting part:
To include comments in an HTML document, use a comment declaration. A
comment declaration consists of <!' followed by zero or more comments followed by >'. Each comment starts with --' and includes all text up to and including the next occurrence of --'. In a
comment declaration, white space is allowed after each comment, but
not before the first comment. The entire comment declaration is
ignored.
NOTE - Some historical HTML implementations incorrectly consider
any `>' character to be the termination of a comment.
For example:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HEAD>
<TITLE>HTML Comment Example</TITLE>
<!-- Id: html-sgml.sgm,v 1.5 1995/05/26 21:29:50 connolly Exp -->
<!-- another -- -- comment -->
<!>
</HEAD>
<BODY>
<p> <!- not a comment, just regular old data characters ->

Simply put, it's because it's derived from SGML where that used to be the standard
From sitepoint:
The HTML comment looks like this: . It’s derived from SGML, which starts with an <! and ends with an >
This page has quite some information about the history of HTML and SGML

Related

How to fix consecutive hyphens did not terminate a comment error?

with the w3 validator (https://validator.w3.org) scan my project but that's found a error.
Error: Consecutive hyphens did not terminate a comment. -- is not permitted inside a comment, but e.g. - - is.
At line 135, column 8
↩
all of the that line :
</ul></div></div><!-- end #main-nav -->
Why I get this error? How can fix that?
Thank you
It is an old post but I've stumbled upon a similar problem.
The validation error states that -- are not permitted INSIDE a comment so the following code will throw an error:
<!-- Commented resource
Some HTML here
<!-- /comment stops here -->
This code will not return a validation error:
<!-- Commented resource
Some HTML here
/comments stop here -->
This usually happens when you have somenthing with comments and then decide to comment everything and you leave the closing comment intact.
If your doctype is correct, there should be no problem. I've tested
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>
<body>
<div><ul><li>li</li></ul></div><!-- end #main-nav -->
</body>
</html>
with no warnings and errors. There is something wrong somewhere else. (Maybe <<, >> or missing closing tag />)
I can't see the validator having any problem with such code.
Are you sure it's not spitting-the-dummy over a BEM-esque class name using double hyphens? For example, using class="MyComponent--modifier" will generate your error, even though it is perfectly valid1.
1 According to the spec a class name cannot begin with two consecutive hyphens.
Sadly the first comment is irrelevant as the example offered does not have two consecutive hyphens embedded in a comment.
My website is replete with consecutive dashes inside comments, as I have long been in the habit of using <!-- ---------- --> as a separator. Fortunately the validator is only warning not declaring an error. Do I care if my document is not mappable to XML 1.0? I do not. Recommendation to self and others: Ignore this warning and carry on.
https://validator.w3.org/ warns me
"The document is not mappable to XML 1.0 due to two
consecutive hyphens in a comment. (6)"

HTML5 Tag Omission: Spec Clarification With Regards To Language

The HTML5 specification for tag omission (http://www.w3.org/TR/html51/syntax.html#syntax-tag-omission) starts with the following two statements (emphasis mine):
An html element's start tag may be omitted if the first thing inside
the html element is not a comment.
An html element's end tag may be omitted if the html element is not
immediately followed by a comment.
Those to statements read similarly, but not the same and I am wondering if someon can offer clarification on what they mean.
The following case seems unambiguous - you can't remove the start or close tags:
<html><!-- start --> ... </html><!-- end -->
But what about when whitespace is introduced into the mix. Can the start tag for html be eliminated in the following case?
<html>
<!-- comment after whitespace -->
...
Can the end tag be eliminated in a similar scenario?
...
</html>
<!-- comment after whitespace -->
Some of the other rules make specific mention of whitespace characters which leads me to believe that they should be taken into account. Most of the rules say "...immediately followed by..." which is different than the first bullet point listed.
The important factor here is that the phrases first thing inside and immediately following are talking about nodes i.e. the DOM, not tags or other markup, so the distinction it is making is about whether the node is a child (first thing inside) or a following sibling (immediately following).
As far as spaces go:
An html element's start tag may be omitted if the first thing inside
the html element is not a comment.
The first thing inside an html element cannot be a space character because at that point in the parser algorithm, space character tokens are discarded and not added to the DOM.
An html element's end tag may be omitted if the html element is not
immediately followed by a comment.
Space characters, regardless of whether they appear just before or just after the </html> tag, end up inside the html element (in fact, also inside the body element), so the comment will be immediately following the html element regardless of whether there are spaces in between in the markup.
In html, space between tags doesn't matter. <html> <head> and <html><head> are the same thing to the browser. In content (e.g. between words inside a span/p tag) it's rendered up by the browser, but when you want to use space between elements (as a design resource) you should use &nbsp.
So, as I see, immediately followed by doesn't mean "the next character" but "the first thing after the place that end tag was supposed to be, no matter how many spaces between them.
Then, removing html tags in both cases would invalidate the html, because no matter how many spaces are betweeen the place </html> was supposed to be and the comment.
edit: I think they were trying to express the same thing by using another words and avoid being repetitive, but ended up being confuse;
The rules about tag omission are somewhat misleading in that for the most part they're not actually saying when tags can be omitted, but rather how they should be interpreted when they are omitted. Take, for example, the following document:
<!DOCTYPE html><!-- A comment --><title>A title</title>
This is a valid HTML5: you can run it through the W3C validator yourself. But the tag omission rules clearly state that
[a]n html element's start tag may be omitted if the first thing inside the html element is not a comment.
How do we reconcile this? The answer is that these are disambiguation rules. Because an html element's start tag may not be omitted if the first thing inside it is a comment, we are free to assume when parsing that the comment is not the first thing inside the html element. Similarly, the tag omission rules state that
[a] body element's start tag may be omitted if the element is empty, or if the first thing inside the body element is not a space character or a comment [...]
So we are free to assume that the comment is also not the first thing inside the body element. So in fact this document can be unambiguously parsed as equivalent to
<!DOCTYPE html><!-- A comment --><html><head><title>A title</title></head><body></body></html>
The parser algorithm for HTML5 specifies that if we are in the before html insertion mode, which is the state the parser transitions to after seeing <!DOCTYPE html>, and we see
A character token that is one of U+0009 CHARACTER TABULATION, "LF" (U+000A), "FF" (U+000C), "CR" (U+000D), or U+0020 SPACE
then we are to "Ignore the token." If on the other hand we see a comment token, then we are to
Insert a comment as the last child of the Document object.
It's not until we see some other kind of tag that we emit an html element. So we should expect this behavior not to be affected by whitespace, and indeed both Firefox 54 and Chrome 60 interpret the document
<!DOCTYPE html>
<!-- A comment -->
<title>A title</title>
identically to
<!DOCTYPE html><!-- A comment --><title>A title</title>
That is, both of them are treated like
<!DOCTYPE html><!-- A comment --><html><head><title>A title</title></head><body></body></html>

How to add block comments in HTML? [duplicate]

I have some HTML code on a page that I don't want to erase, but make inactive for the short term. How can I make the browser ignore parts of the page in the same way the // works in some programming languages?
Behold HTML comments:
<!-- comment -->
http://www.w3.org/TR/html401/intro/sgmltut.html#idx-HTML
The proper way to delete code without deleting it, of course, is to use version control, which enables you to resurrect old code from the past. Don't get into the habit of accumulating commented-out code in your pages, it's no fun. :)
HTML Comments:
<!-- <div> This div will not show </div> -->
See Reference
CSS Comments:
/* This style will not apply */
See Reference
JavaScript Comments:
// This single line JavaScript code will not execute
OR
/*
The whole block of JavaScript code will not execute
*/
See Reference
To comment block with nested comments: substitute inner (block) comments from "--" to "++"
<!-- *********************************************************************
* IMPORTANT: To uncomment section
* sub inner comments "++" -> "--" & remove this comment
*********************************************************************
<head>
<title>My document's title</title> <++! My document's title ++>
<link rel=stylesheet href="mydoc.css" type="text/css">
</head>
<body>
<++! My document's important HTML stuff ++>
...
...
...
</body>
*********************************************************************
* IMPORTANT: To uncomment section
* sub inner comments "++" -> "--" & remove this comment
*********************************************************************
-->
Thus, the outer most comment ignores all "invalid" inner (block) comments.
Just create a multi-line comment around it. When you want it back, just erase the comment tags.
For example, <!-- Stuff to comment out or make inactive -->
Use:
<!-- This is a comment for an HTML page and it will not display in the browser -->
For more information, I think 3 On SGML and HTML may help you.
If you are using Eclipse then the keyboard shortcut is Ctrl + Shift + / to add a group of code. To make a comment line or select the code, right click -> Source -> Add Block Comment.
To remove the block comment, Ctrl + Shift + \ or right click -> Source -> Remove Block comment.
Reason of comments:
Comment out elements temporarily rather than removing them, especially if they've been left unfinished.
Write notes or reminders to yourself inside your actual HTML documents.
Create notes for other scripting languages like JavaScript which requires them
HTML Comments
<!-- Everything is invisible -->

Google privacy page html invalid

Viewing the html source for Googles Privacy Page the header is:
<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<title>Google Privacy Center</title>
<link rel="stylesheet" href="//www.google.com/css/privacy.css">
<h1><img src="//www.google.com/intl/en/images/logo_sm.gif" alt="Google"> Privacy Center</h1>
I noticed there is no body tag here or in the footer. Also, no ending </html>.
Is this valid markup?
HTML5 (which is what they're declaring that page as) allows you to omit a lot of stuff. For instance, the body tag's start and end tags are both optional, as is the ending html tag (ref).
The validator says it's valid, but the validator's HTML5 support is also still experimental. YMMV
The validation page from validator.w3.org says it's valid HTML5.
But note that Google doesn't really care if their pages are valid markup or not, as long as they display correctly. Google's main page (google.com) is riddled with invalid markup.
HTML allows to omit certain start and/or end tags:
Some HTML element types allow authors to omit end tags (e.g., the P and LI element types). A few element types also allow the start tags to be omitted; for example, HEAD and BODY. The HTML DTD indicates for each element type whether the start tag and end tag are required.
If you examine a document type definition like the one of HTML 4.01, the elements are declared with the element declarations <!ELEMENT … >. And within such an element declarations, two characters specify whether the start or end tag of an element can be omitted. See the definition of P for example:
<!ELEMENT P - O (%inline;)* -- paragraph -->
Here the - after the element name P denotes that the start tag is required and the O denotes that the end tag may be omitted. Another example, the HEAD element:
<!ELEMENT HEAD O O (%head.content;) +(%head.misc;) -- document head -->
Here the two O specify that both the start and end tag can be omitted.
Omitting both tags on elements is only possible as such elements are implicit in their context. In case of HEAD, the content model of the parent element HTML is specified as follows:
<!ELEMENT HTML O O (%html.content;) -- document root element -->
Where the parameter entity html.content is defined as follows:
<!ENTITY % html.content "HEAD, BODY">
That means the content model of HTML is implicitly defined as HEAD followed by BODY.
You can take a look at the index of HTML 4.01 elements to see what tags of what elements can be omitted.
Reading W3C HTML5 Spec syntax tag omission :
A body element's start tag may be
omitted if the element is empty, or if
the first thing inside the body
element is not a space character or a
comment, except if the first thing
inside the body element is a script or
style element. A body element's end
tag may be omitted if the body element
is not immediately followed by a
comment.
An html element's end tag may be
omitted if the html element is not
immediately followed by a comment.
Funny thing is that the editor of the document is Ian Hickson of Google, Inc.
If you run it through the W3C validator you get that This document was successfully checked as HTML5!.
It is valid HTML5.
However, it is not XHTML.
Yes this is HTML5
http://validator.w3.org/check?verbose=1&uri=http%3A%2F%2Fwww.google.com%2Fintl%2Fen%2Fprivacy.html

double hyphen in script makes firefox render strangely

<!-- <script type="text/javascript">/*<![CDATA[*/ c-- ;//]]></script> -->
When I have the above line in the <head> section of a plain html page, Firefox 3.5.5 renders the trailing --> as text. If I change c-- to c- it doesn't. Any ideas what's going on here? I getting an artifact on my pages with this due to a very large script that's been crunched. I can change the statement to c-=1 and avoid the problem for now but.... I'd like to know what bit/byte is biting my a$$.
This is due to Firefox implementing SGML (on which HTML was based) comments strictly. This will only occur when the document is loaded in standards mode (i.e. there is a DOCTYPE).
The first <! starts a comment. The first -- enters a section in which > characters are allowed. The second -- (in your script) leaves the section in which > characters are allowed. The > at the end of </script> then ends the comment. The following --> is therefore no longer part of the the comment and gets rendered as text.
See http://www.howtocreate.co.uk/SGMLComments.html for a comprehensive guide to the issue.
Its also worth noting that the HTML 4 Specification says that 'authors should avoid putting two or more adjacent hyphens inside comments' and the HTML 5 Specification says comments must not 'contain two consecutive U+002D HYPHEN-MINUS characters (--)'.
The solution, as you've found, is to not include -- in the middle of a comment.
Technically you are not allowed to have double hyphen in a comment in HTML (or XML). So even if browsers "allow" if it is not valid and should fail an HTML validator.
See Comment section of HTML 4 Specification
I can't replicate this. Doesn't show up on 3.0.1.