← Back to context

Comment by IshKebab

4 days ago

> In html you can just view comments as another node in a uniform tree.

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.

> in HTML you have a tree of nodes but comments can be anywhere

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").

  • Yes, anywhere in the node tree. Imagine if CSS was specified in HTML-style. We might write this selector:

      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.

    • Problem is, the CSSOM models that more like this:

          <rule selector="h1, h2">
            <style>
              <property name="color" value="red" />
            </style>
          </rule>
      

      Perhaps your takeaway from this is "the CSSOM is bad" (and I don't necessarily disagree), but it's what the "mistake" is talking about.

      4 replies →