Wrong highlighting in PHP in JS in HTML attributes in PHP file
Closed this issue · 4 comments
From @jntx in microsoft/vscode#85195:
The code highlighting fails when you mix PHP code in the JS code inside an HTML attribute inside a PHP file.
This code fragment shows the behavior:
<?php
// Non Javascript attribute is ok
?>
<button data-stuff="document.location.href = '<?=$_SERVER["PHP_SELF"]."?p1=".$_REQUEST["p1"]."&p2=dosomething"?>';">:O</button>
<?php
// Wrong highlighting happens in Javascript attributes
?>
<button onclick="document.location.href = '<?=$_SERVER["PHP_SELF"]."?p1=".$_REQUEST["p1"]."&p2=dosomething"?>';">:O</button>
when the code is not on an event attribute (so it's not interpreted ad JS) the highlight is ok.
VS Code version: Code 1.40.1 (8795a9889db74563ddd43eb0a897a2384129a619, 2019-11-13T16:49:35.976Z)
OS version: Windows_NT x64 10.0.18363
It looks like VS Code attempts to highlight contents of onevent handlers as JavaScript. Atom does not do that.
The bug does not seem to be with this PHP grammar though, I am quite sure that the quote before PHP_SELF
is marked as the end of the entire value of the onclick
attribute and in this case PHP_SELF
is considered a new HTML attribute (it is valid to not have a space between attributes in this case). Since it expects an equals sign after the attribute name and not a quote, rest of it is considered invalid HTML and that is why it is also marked red.
If that weren't the case, then at minimum the second snippet should have PHP_SELF
colored the same brown as above.
@alexr00 To clarify, everything starting from <?=
is parsed as PHP as expected, the problem likely occurs due to PHP grammar not seeing the double quote as it is never exposed to it, which is to be expected if the highlighting for JavaScript is done through the matches
field.
It's ok to assume in pure HTML that the attribute won't contain any double quotes, which makes it easy/possible constraining the contents of it for parsing the language used inside, but this does not work with templating languages which may contain their own quotes that are not displayed in the final HTML output.