1

if I edit the following block in vim:

<div> foo </div>
<!----------------------------------------------------------->
<div> bar </div>
<!----------------------------------------------------------->
<div> baz </div>

the second div ("bar") is shown as commented-out. As far as I can find a valid HTML comment starts with the <!-- tag and is closed by --> and there is no requirement to have or not have anything in between. is there a way to tweak the syntax settings ?

I have tried multiple versions of vim and they all show this behavior out-of-box

  • VIM 8.1.2269 under ubuntu
  • VIM 7.4 under redhat
  • even VIM 8.0 that comes with gitbash
7
  • I wonder if the built-in syntax file doesn't miss a keepend argument in the region definition... Maybe you can try to create your own ~/.vim/after/syntax/html.vim to fix these comments regions, but that might not be trivial (A much more trivial solution would be to stop using this kind of delimiter comments which IMO are not a best practice but that's another debate). If nobody shows up I'll try to have a look when I have some time.
    – statox
    Feb 24 at 13:51
  • Currently I am edining a really long HTML file mixed with JS(not even written by me), and it is really painful without proper syntax highlight. Anyway, we cannot always control the coding conventions, especially for the legacy code...
    – Dr Phil
    Feb 24 at 14:19
  • 3
    @statox keepend, of course, fixes the "runaway" issue, but the syntax is kind of FUBAR. For example, <!-----> will show the last (fifth) dash as htmlCommentError. I'm gonna try to deal with this.
    – Matt
    Feb 24 at 14:30
  • Sidenote: You're right that your comments are valid, but note that there are a few restrictions on what you can put in them.
    – Rich
    Feb 25 at 13:43
  • 2
    I have opened PR. If anyone wants to give it a try, just take runtime/syntax/html.vim from the forked repo and put it under ~/.vim/syntax.
    – Matt
    Feb 25 at 16:13
3

This was a bug in Vim's HTML syntax, which @Matt has fixed with lightning-speed.

Upgrade to an upcoming version of Vim to fix this, or simply copy runtime/syntax/html.vim from Matt's fork into ~/.vim/syntax to try out his fix immediately.

1
  • @Matt that was really an impressive turnaround time
    – Dr Phil
    Feb 25 at 17:52
3

To shed some light on what's going on. As you probably know, HTML originates from SGML. In SGML they could write something like that:

<!ELEMENT some serious stuff
 --
    Some stupid comments
    that can even spread across multiple lines
 --
 more serious stuff
 -- More stupid comments --
>

BTW. Note there's no space between <! and ELEMENT, but there can be space(s) between last -- and >.

However, in HTML there were only few "true" SGML entities, such as <!DOCTYPE ...>. And so HTML comments were like extra empty SGML entities:

<!-- This is SGML/HTML comment --

 -- Yet another SGML/HTML comment --
>

This is how HTML2 (RFC 1866) defined the comments. And also how Vim syntax file parsed them. So it was very sensitive to the number of consecutive dashes.

In HTML3 they removed "multiple comments" feature, and simply said "the comment is anything between <!-- and --\s*>"

But later they added "should avoid consecutive dashes inside comments". And after that even "must avoid". And so on from HTML4 down to HTML5.

And only in HTML 5.2 we've got very elaborated and formalized syntax for comments of its own.

So what I implemented is that new HTML 5.2 syntax. One can say that its main features (a) consecutive dashes are ok; (b) no space between -- and >. But in fact there's more than that.

While working on new syntax for Vim, I wrote HTML file to test for corner cases. Now, it displays well in browser, and also validator complains only where it's supposed to, but syntax highlighters seem to choke on it. I even wonder if there's any HTML syntax highlighter (except browser built-in, or Vim ;-) to pass this HTML 5.2 test?

But anyone who wants to play safe should avoid all this stuff and write only simple comments: <!-- Text without consecutive dashes -->.

2
  • It might turn out that some browsers are more forgiving than others when you have corner cases . At lest now we have chromium and effective safari duopoly..
    – Dr Phil
    Feb 25 at 21:46
  • @DrPhil The standard says that browsers should always forgive.
    – Matt
    Feb 25 at 22:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.