Are Nested HTML Comments Possible

Are nested HTML comments possible?

When you nest a comment, replace "--" with "- -". When you un-nest, reverse the procedure. It's not the <!-- that is forbidden but the --.

Example:

<!-- some stuff
<!- - some inner stuff - ->
<!- - a sibling - ->
the footer -->

How do I nest HTML comments?

If I understand you correctly, you want to be able to nest your HTML comments. In order to do this you need to replace double dash -- with a two single dashes and a space - -.

Basically,

<!--
This is a comment.
<!- -
This is a nested comment.
- ->
-->

Applying this to your code, you should end up with something like this:

<!--  
{if $scenes}
<!- - Scenes - ->
{include file="$tpl_dir./scenes.tpl" scenes=$scenes}
{else}
<!- - Category image - ->
{if $category->id_image}
<div class="align_center">
<img src="{$link->getCatImageLink($category->link_rewrite, $category->id_image, 'category')}" alt="{$category->name|escape:'htmlall':'UTF-8'}" title="{$category->name|escape:'htmlall':'UTF-8'}" id="categoryImage" width="{$categorySize.width}" height="{$categorySize.height}" />
</div>
{/if}
{/if}
-->

HTML comments within comments?

I think the key point is this:

Note that comments are markup.

http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.2.4

This is not valid markup:

<div <span/> />

... so neither is the one you mention.


Since all my sites are written in PHP I normally comment out code with PHP comments:

<?/*?>
<div>...</div>
<p>...</p>
<?*/?>

Perhaps you can use a similar trick.

Why, in general, are nested block comments NOT allowed?

The reason is historical and has to do with the architecture of compilers.

For the sake of efficiency, most compilers traditionally parse the source code in two stages: the lexical analysis and the actual parsing of a token stream (that was produced by said lexical analysis). The lexical analysis is the part that recognises individual tokens, such as keywords, strings, number literals – and comments.

Again for reasons of efficiency, lexical analysis is traditionally implemented via a finite-state machine. These finite-state machines happen to recognise (= handle) regular languages, which fits perfectly for the above-mentioned tokens. However, it is not able to recognise nested constructs – this would require a more powerful machine (augmented by a stack).

Not allowing nested comments was thus simply a decision that traded off convenience for performance, and subsequent languages have by and large adopted the convention.

And as a plus, are there any other (not so obscure) languages that DO allow nested block comments?

There are some. The comments already mentioned Haskell and Pascal. Other languages are D and F#.

CSS Nested Comments

CSS does not have a nestable comment syntax.

You could instead wrap the rules in something which does nest, but will not match anything, such as a non-existent media type:

@media DISABLED {
#container {
width: 90%; /* nested comment here */
margin: 0 auto;
}

#container .class {
width: 25%;
}
}

This does have the caveat that it will not be obviously a comment (so it will not be removed by tools that remove comments), and will cause the browser to spend time on parsing it and finding that it doesn't match. However, neither of these should be a problem for temporary development use, which is the usual reason one would want to comment out a large chunk simply.

Commenting a commented markup (Nested comments)

If you have no </style> and */ tag in comment area use:

<style>
/*
comment
*/
</style>

If you have no </script> and */ tag in comment area use:

<script>
/*
comment
*/
</script>

How do I position nested comments in PHP/CSS?

It should be simply:

<style>
.post {margin-left: 0;}
.post div {margin-left: 16px;}
</style>
<div class="post">
POST HERE
<div>
reply
<div>
2nd reply
<div>
4th level
</div>
</div>
</div>
<div>
reply 2
</div>
</div>
<div class="post">
another post
</div>

http://jsfiddle.net/4f2wzwne/

How to use wrap comment in HTML

If you really can't change any comments within the original source, but you can (or so it seems) wrap some lines in the source with something. You could turn the lines into a JavaScript multi line comment like I have done in this jsFiddle.

<script type="text/javascript">
/*
<div>1111111111</div> <!-- First line -->
<div>2222222222</div> <!-- Second line -->
<div>3333333333</div> <!-- Third line -->
*/
</script>

Why are double hyphens invalid in HTML comments?

[Definition: Comments may appear anywhere in a document outside other
markup; in addition, they may appear within the document type
declaration at places allowed by the grammar. They are not part of the
document's character data; an XML processor may, but need not, make it
possible for an application to retrieve the text of comments. For
compatibility, the string " -- " (double-hyphen) must not occur within
comments.] Parameter entity references must not be recognized within
comments.

The grammar does not allow a comment ending in --->

It seems to be a feature of XML included solely to ensure that XML remains compatible with SGML
http://www.w3.org/TR/REC-xml/#sec-comments



Related Topics



Leave a reply



Submit