Skip to content

Commit

Permalink
PHP: Fixed closing tag issue (#1652)
Browse files Browse the repository at this point in the history
This fixes that PHP's closing tag (`?>`) was detected inside strings and comments.
  • Loading branch information
RunDevelopment committed Feb 28, 2019
1 parent 152a68e commit 289ddd9
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 9 deletions.
15 changes: 9 additions & 6 deletions components/prism-php.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,14 @@
}
});

Prism.languages.insertBefore('php', 'keyword', {
Prism.languages.insertBefore('php', 'comment', {
'delimiter': {
pattern: /\?>|<\?(?:php|=)?/i,
pattern: /\?>$|^<\?(?:php(?=\s)|=)?/i,
alias: 'important'
},
}
});

Prism.languages.insertBefore('php', 'keyword', {
'variable': /\$+(?:\w+\b|(?={))/i,
'package': {
pattern: /(\\|namespace\s+|use\s+)[\w\\]+/,
Expand Down Expand Up @@ -114,16 +117,16 @@
delete Prism.languages.php['string'];

Prism.hooks.add('before-tokenize', function(env) {
if (!/(?:<\?php|<\?)/ig.test(env.code)) {
if (!/<\?/.test(env.code)) {
return;
}

var phpPattern = /(?:<\?php|<\?)[\s\S]*?(?:\?>|$)/ig;
var phpPattern = /<\?(?:[^"'/#]|\/(?![*/])|("|')(?:\\[\s\S]|(?!\1)[^\\])*\1|(?:\/\/|#)(?:[^?\n\r]|\?(?!>))*|\/\*[\s\S]*?(?:\*\/|$))*?(?:\?>|$)/ig;
Prism.languages['markup-templating'].buildPlaceholders(env, 'php', phpPattern);
});

Prism.hooks.add('after-tokenize', function(env) {
Prism.languages['markup-templating'].tokenizePlaceholders(env, 'php');
});

}(Prism));
}(Prism));
2 changes: 1 addition & 1 deletion components/prism-php.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions tests/languages/markup+php/issue1582.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

echo '<?xml version="1.0" encoding="UTF-8"?><tracker />';
echo PHP_EOL;

----------------------------------------------------

[
["php", [
["delimiter", "<?php"],
["keyword", "echo"],
["single-quoted-string", "'<?xml version=\"1.0\" encoding=\"UTF-8\"?><tracker />'"],
["punctuation", ";"],
["keyword", "echo"],
["constant", "PHP_EOL"],
["punctuation", ";"]
]]
]

----------------------------------------------------

Checks for PHP closing tags '?>' inside of strings.
See #1582 for details.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

// '
# "
//*
echo '?>' + "?>"; /* ?> */
echo PHP_EOL;
// */
// ?>

<?php # ?>

----------------------------------------------------

[
["php", [
["delimiter", "<?php"],
["comment", "// '"],
["shell-comment", "# \""],
["comment", "//*"],
["keyword", "echo"],
["single-quoted-string", "'?>'"],
["operator", "+"],
["double-quoted-string", ["\"?>\""]],
["punctuation", ";"],
["comment", "/* ?> */"],
["keyword", "echo"],
["constant", "PHP_EOL"],
["punctuation", ";"],
["comment", "// */"],
["comment", "// "],
["delimiter", "?>"]
]],

["php", [
["delimiter", "<?php"],
["shell-comment", "# "],
["delimiter", "?>"]
]]
]

----------------------------------------------------

Checks for PHP closing tags '?>' inside of strings and comments.
5 changes: 3 additions & 2 deletions tests/languages/php/delimiter_feature.test
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<? ?>
<?php ?>
<?= ?>
<?= // ?>

----------------------------------------------------

Expand All @@ -15,10 +15,11 @@
]],
["php", [
["delimiter", "<?="],
["comment", "// "],
["delimiter", "?>"]
]]
]

----------------------------------------------------

Checks for delimiters.
Checks for delimiters.

0 comments on commit 289ddd9

Please sign in to comment.