Search code examples
htmlvimsyntax-highlightingstrict

Vim syntax file to be stricter about HTML syntax


I just got bit by a bug that was

 <link rel=stylesheet" type="text/css" href="stylesheet.css" media="all">

rather than

 <link rel="stylesheet" type="text/css" href="stylesheet.css" media="all">

It seems like vim's syntax highlighting for html doesn't differentiate between the two. Is there a stricter syntax file I can get from somewhere?

(and for the haters: vim questions belong on stackoverflow, not superuser)


Solution

  • As a stop-gap measure, I put a copy of $VIMRUNTIME/syntax/html.vim in ~/.vim/syntax/html.vim and modified it:

    38c38
    < syn match   htmlValue    contained "=[\t ]*[^'" \t>][^ \t>]*"hs=s+1   contains=javaScriptExpression,@htmlPreproc
    ---
    > syn match   htmlValue    contained /=[\t ]*\%('\%(\\.\|[^\\']\)*'\|"\%(\\.\|[^\\"]\)*\|[^'" \t>]*\)/hs=s+1   contains=htmlString,javaScriptExpression,@htmlPreproc
    40c40
    < syn region  htmlTag                start=+<[^/]+   end=+>+ contains=htmlTagN,htmlString,htmlArg,htmlValue,htmlTagError,htmlEvent,htmlCssDefinition,@htmlPreproc,@htmlArgCluster
    ---
    > syn region  htmlTag                start=+<[^/]+   end=+>+ contains=htmlTagN,htmlArg,htmlValue,htmlTagError,htmlEvent,htmlCssDefinition,@htmlPreproc,@htmlArgCluster
    43a44
    > syn match   htmlTagError contained /['"].*/
    168c169
    <   syn region  htmlScriptTag     contained start=+<script+ end=+>+       contains=htmlTagN,htmlString,htmlArg,htmlValue,htmlTagError,htmlEvent
    ---
    >   syn region  htmlScriptTag     contained start=+<script+ end=+>+       contains=htmlTagN,htmlArg,htmlValue,htmlTagError,htmlEvent
    

    This just catches this particular error (mismatched quotes), so I'm still very interested in a better syntax file for strict html.