highlightjs / highlight.js

JavaScript syntax highlighter with language auto-detection and zero dependencies.
https://highlightjs.org/
BSD 3-Clause "New" or "Revised" License
23.72k stars 3.6k forks source link

PHP highlighting breaks on here-docs #888

Closed gnprice closed 9 years ago

gnprice commented 9 years ago

On PHP code that contains a here-doc, the "string" highlight for the here-doc continues on long after the here-doc has actually been closed -- it seems to miss the end of the here-doc.

For example if this snippet is highlighted as PHP:

<?php
    print(<<<EOTEXT
      this is a heredoc ...
EOTEXT
        );
    print("... but this line is code.");
?>

then everything from <<<EOTEXT to the end goes in a span of class hljs-string.

See repro at https://jsfiddle.net/5pdjee5v/ .

(/cc @christoffer, and thanks @jboning for spotting the issue!)

isagalaev commented 9 years ago

As currently defined, it wants a semicolon at the end of "EOTEXT" here. Is this wrong?

jhurwitz commented 9 years ago

From what I can tell, heredocs should end with a semicolon unless used as a function argument (as in the example given above). See also example #3 here: http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

But even in the semicolon case, I think it should be just like any normal string -- the semicolon need not immediately follow, but can have whitespace preceding it. Highlighting also seems to break if you include the semicolon, but with intermediate whitespace.

jboning commented 9 years ago

Even in statements, it seems to work fine without a trailing semicolon on the same line:

php > $a = <<<EOTEXT
<<< > foo
<<< > EOTEXT
php > ;
php > print $a;
foo
Sannis commented 9 years ago

Yeah, many rare use-cases are not highlighted properly in many language and we always try to improve them on bug reports if it is not overkill.

isagalaev commented 9 years ago

Fixed (sort of) in a9e6bd6 (see commit message).

Sannis commented 9 years ago

Thanks!