HTML comments work in JavaScript too
smitop.com
smitop.com
Then again, I'd be even more horrified if the above statement is not true anyways.
Of course, because javascript, there are other string values of `f` for which the two statements are equal as well. (e.g for empty string both statements are true because `-"" === -0`, and for "1" both values are false)
f = 0 (type: object, constructor: Number)
0<!f = false
0<!-f = truehttps://dorey.github.io/JavaScript-Equality-Table/
I particularly like the note on the "if" tab:
> Note: This row does not match up with any of the rows in the other table.
No, it is less-than-not, not less-than-or-not-equal-to.
That is <!x is the same as < (!x); if x is true-ish, it is “< false” and if x is false-ish it is “< true”.
Can it maybe shut off if it detects strict mode or something? Seems wild to find this behavior in Node and evergreen browsers.
https://github.com/v8/v8/blob/main/src/parsing/scanner-inl.h...
and
https://github.com/v8/v8/blob/main/src/parsing/scanner.cc#L3...
And also this nice article:
"The scanner chooses a specific scanner method or token based on a maximum lookahead of 4 characters, the longest ambiguous sequence of characters in JavaScript"
<script>
alert("Hello <!-- Comment 0 -->world");
<!-- Comment 1
alert("Bonjour le monde");
</script>
Comment 0 gets removed by the XML parser so it doesn't go into the alert string. Comment 1 is seen by the JavaScript parser and gets removed at that stage. data:application/xhtml+xml,<script xmlns="http://www.w3.org/1999/xhtml">%0Aalert("Hello <!-- Comment 0 -->world");%0A<!-- Comment 1%0Aalert("Bonjour le monde");%0A</script>
(Observe also how what I’ve written here nets you a document that lacks html, head and body tags—HTML syntax fills those in through the magic of optional start and end tags, but XML syntax takes what it’s given and can be used to do things like nesting hyperlinks or putting a heading inside a paragraph. The HTML and XML syntaxes for HTML are actually mutually incompatible.)Other common things that HTML syntax won't let you do: <p><p></p></p>, <table><tr><td> without an implicit <tbody>.
Why I know this obscure corner of the HTML standard is because I've been running my website on XHTML mode for more than a decade continuously. https://www.nayuki.io/page/practical-guide-to-xhtml
A fun thing that I learned recently is that `table > tr` is actually valid, the tbody is genuinely optional in the spec, even though the HTML syntax injects it.
IIRC optional in the HTML spec only. Not in the XHMTL one and not in the polyglot syntax.
But tbody was optional even in XHTML 1.0 Strict; quoting https://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd,
<!ELEMENT table
(caption?, (col*|colgroup*), thead?, tfoot?, (tbody+|tr+))>
I’m pretty sure that this will be the reason it’s optional in the HTML spec, since there’d be no point in it being optional otherwise, since it’s not attainable in the HTML syntax. <!ELEMENT TABLE - -
(CAPTION?, (COL*|COLGROUP*), THEAD?, TFOOT?, TBODY+)>
<!ELEMENT TBODY O O (TR)+ -- table body -->[0]: https://annevankesteren.nl/
[1]: https://annevankesteren.nl/2003/09/send-applicationxhtmlxml
JSX also by default doesn't have an easy way to insert comments into the generated HTML, so I don't think JSX is the reason why it wouldn't be supported in Babel. My understanding was always that it has more to do with comments being treated slightly differently than normal document nodes, but I could be wrong.
I guess more likely the reasoning is that it's just obscure. I've been programming JS for a reasonably long time and never knew this was supported.
And if, for some reason, you wanted to inject an HTML comment into the output, you could abuse the `dangerouslySetInnerHTML` prop like this:
function Comment(props)
{
return <span dangerouslySetInnerHTML={{__html: `<!-- ${props.comment} -->` }} />;
}
Then use it like this: ...
<Comment comment="my comment" />
...
Using `React.Fragment` doesn't work, sadly. Use the "Inspect Element" tool on the output here: https://playcode.io/870223And while it doesn’t work in React’s Fragment, it can certainly work with another compiler. JSX doesn’t specify anything about what it compiles to, React.createElement/_jsx are just defaults.
<Comment>some comment</Comment>
Reading through the specs reveals lots of curious historical details. The HTML spec is a glorious tangled mess because it’s an exhaustive definition of a large amount of functionality that grew⸺ ah⸺ organically.
However, the way I understand the spec, you shouldn't be able to start a line comment with `-->' after an actual multi-line comment (i.e. a delimited comment that contains line terminators); however, both Firefox and Chrome seem to disagree with my interpretation; for example
i = 1;
while (i /*
*/ --> 0
)
console.log(i); // this loop never terminates <!-- ok if it's a script only
--> console.log("ok"); const isScript = (x = true) => x <!--x
console.log(isScript()) // true or false depending on whether the code is ESM
What's happening is that `<!--` is a comment token in script mode, but it's parsed as `< ! --` in module mode.- Authors can define custom properties as long as they start with a double-dash, like --demo
- Authors can leave these values empty, or put any code that doesn't break CSS's syntax inside. This can be CSS values, it can be JSON, it can be other languages, even some JavaScript could be stored in CSS to be read later
- Using JavaScript you can read the values of CSS custom properties, and set them
So it's possible with a tiny bit of JS to re-create what the old IE expression() function used to do in any modern browser in a fully standards-compliant way
Not really. expression() was evaluated by just moving the mouse around, which is what allowed attaching stuff to your cursor with "just some CSS"
https://web.archive.org/web/20120420154658/http://msdn.micro...
To do what expression() could do you need several event listeners at least which, I mean, of course you can still do it, but it's really unrelated to CSS Properties and just JavaScript at this point.
It's worth pointing out that many of the old ugly things were IE specific including the feature referenced by the parent comment... it was a time when "standard" had little weight, although some of the good ideas like box-sizing were adopted by W3C.
(Which is probably fine, if your background is in scientific UNIVACs.)
My impression is this form might actually be the original way of expressing network paths, what with UNC in Windows (\\server\share\file etc.) and POSIX carving out an exception specifically for two slashes at the beginning of a pathname (that is, foo//bar is the same as foo/bar and ///foo/bar is the same as /foo/bar, but //foo/bar may or may not be the same as /foo/bar).
[1] https://datatracker.ietf.org/doc/html/rfc3986#section-4.2
selector {
//property: value;
}
This sets the property named "//property" to "value". Close enough. (Expressed otherwise: it’s approximately a comment until the next semicolon, barring quoted strings and parentheses.) //selector {
property: value;
}
This is an invalid selector, and so the entire rule is ignored. (Expressed otherwise: it’s approximately a comment until the next opening curly brace’s matching closing curly brace.) /*
// blah
*/
Edit: I mistakenly parsed this as using a dummy selector to hold the comment property, not just putting it inside real selectors.Further because I write a lot of JavaScript too I sometimes make the mistake of putting a // -comment into my CSS which breaks things. And when things break in CSS you usually don't get a clear error-message about it.
It is always difficult to switch between two languages. I would prefer something like JavaScript stylesheets, if that was possible.
Single-line comments have the benefit that it is always clear to the reader which lines are comments. Whereas if you use multi-line comments to comment a large section of code it is no longer clear when reading it whether it is indeed "inside" a comment.
And finally having both types of comments available is useful because you can multi-line-comment a section which already contains // -comments, whereas you can not multi-line comment a section which already contains multi-line comments.
// => ////
//////////////////////////////////////////////////////////////
// just like those devs that like to decorate their code with
//////////////////////////////////////////////////////////////
/*
* type of stuff
*/
Edit: almost forgot my shell friends
########################
## works too
########################
But what about the below, does it work in your language? I assume it may work in some languages but not all.
/*
let a = 1;
/* a is one */
let b = 2;
*/
Personally, I don't like the /**/ style comments either. I was just playing devil's advocate.
{
"//": "lets us consume uncompiled protobufs (see issue #123)",
"extern": "protobuf.js"
} .Component__child {
TODO: add theming support
}
Of course, you have some restrictions around syntax here (again, I didn't say I advise doing this), and of course if CSS ever adds a TODO rule you might run into problems.Instead, any author is allowed to invent _any_ property, so long as the property name begins with a double-dash (--) and you can put anything inside as a value so long as it doesn't break CSS syntax
a {
--note:
So you could do this instead and it would be standard CSS, perfectly fine for all people forever!
;
}I still don't think I recommend it, because sure it gets rid of the problem of accidentally breaking CSS namespace, but I still think in most cases sticking comments inside of a property is going to end up confusing people on a team more often than it will help, and there are still some syntax issues and conflicts with your own internal variables that can happen.
But for sure, if you're going to use properties for notes, make them custom properties.
----
Edit: I particularly don't recommend this, but off the top of my head using custom properties you also might be able to rig this up with something like `:before` pseudo-classes to even display some of your CSS comments visibly in the page via `content` or by building style toggles[0] or something.
Again, I feel like this is getting too clever for its own good, but as long as we're playing around...
[0]: https://css-tricks.com/logical-operations-with-css-variables...
{
"//": "comment goes here"
}
Most editors and tooling also know this is commonly use and ignore validation of repeated keys. Unfortunately it doesn’t much help with arrays unless you specifically handle the case (and depending on usage, handle escaped cases as well).For open source, the fake comment won't be removed by CSS compilers, so you end up wasting a little bandwidth.
(Kind of like the <plaintext> element in HTML, once you write the <plaintext> open tag all text after that is plain text, so you can't write a </plaintext> to get out of it - the whole rest of the file is in that mode with no return)
Some editors and text viewers render them as zero width spaces, so you can see something that seems like innocent line comment but what is actually executed:
data:text/html;charset=utf-8,<script>//%E2%80%A9alert(1)</script>
view-source of that document displays just `<script>//alert(1)</script>` in Firefox. In Chrome there is "P SEP" in dashed rectangle between `//` and `alert`, so you at least get a hint there is something fishy.