by yread · 11mo ago · 0 comments
>Comments shouldn't have been allowed basically everywhere in CSS (compare to HTML, which basically only allows them where content goes), because it makes them basically unrepresentable in the object model, which in turn makes building editing directly on top of the object model impossible
When you parse an AST with comments (or a CST), they do become "just another node in a uniform tree". Think about it - in HTML you have a tree of nodes but comments can be anywhere, which is exactly as obnoxious to deal with as a CST where comment nodes can be anywhere.
This is a terrible reason to not support comments. If it was really important, then probably you should support comments in only a couple of places, e.g. on attributes or on selectors.
But I don't think it is very important. I can't think of a single tool that loads CSS, rearranges it, and then spits it back out in a form that would benefit from comments.
Maybe I'm misunderstanding, but no they can't. For example the comment here is not a comment, but part of the URL:
<a href="https://example.com<!-- comment -->">click me</a>
And HTML like this is simply broken: <div<!-- comment -->>content</div>
Maybe you meant "they can be anywhere that a Node can be in the DOM", but I think that's more or less what the CSS "mistake" is suggesting should be true about CSS (just replace "DOM" with "CSSOM"). h1,
/* don't forget h2! */
h2 {
color: /* I love red */ "red";
}
Like this: <rule>
<selector>
<alternative>
h1
</alternative> <!-- don't forget h2 -->
<alternative>
h2
</alternative>
<selector>
<attributes>
<color><!-- I love red --> red</color>
</attributes>
</rule>
Which is pretty much exactly the same as what you'd get as a CST from parsing the CSS.HTML comments are basically just a HTML tag that isn't rendered. Tools that 'compile' the HTML code into a document tree, including browsers, preserve comments as nodes without any extra effort.
CSS comments can go anywhere:
/*wow*/ .selector /*x*/ {animation /*z*/: 2s /*z*/ linear /*z*/ bounce;}
Tools that transform/parse CSS can either:
1. Strip comments before parsing, meaning anything based on the parsed version will lose the comments.
2. Dedicate a disproportionate amount of complexity to retaining the comments, and still not really have a good way to handle them through edits/transformations.EDIT: also why is it useful to have comments in the object model in the first place? To access them from js?
A similar issue is CDATA in XML which is not retained when round-tripped. Very annoying, but in line with the spec.
Same thing with white space: JSON can not round trip white space or comments the way XML or HTML can, because XML and HTML DOM represents comments and white space explicitly.
Also you can't put comments inside elements like <div <--! comment --> class="foo">. There is no way to represent a comment inside an element in XML or HTML DOM.
But CSS lets you, so you can't round trip comments in CSS.