Skip to content

Commit 1453fa7

Browse files
authored
Merge pull request #1100 from zeitgeist87/FixPHP
Fix the PHP language
2 parents 6530709 + 3ce7488 commit 1453fa7

File tree

8 files changed

+47
-70
lines changed

8 files changed

+47
-70
lines changed

components/prism-php.js

+21-26
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ Prism.languages.php = Prism.languages.extend('clike', {
1616
'constant': /\b[A-Z0-9_]{2,}\b/,
1717
'comment': {
1818
pattern: /(^|[^\\])(?:\/\*[\s\S]*?\*\/|\/\/.*)/,
19-
lookbehind: true,
20-
greedy: true
19+
lookbehind: true
2120
}
2221
});
2322

@@ -32,7 +31,10 @@ Prism.languages.insertBefore('php', 'class-name', {
3231
});
3332

3433
Prism.languages.insertBefore('php', 'keyword', {
35-
'delimiter': /\?>|<\?(?:php)?/i,
34+
'delimiter': {
35+
pattern: /\?>|<\?(?:php|=)?/i,
36+
alias: 'important'
37+
},
3638
'variable': /\$\w+\b/i,
3739
'package': {
3840
pattern: /(\\|namespace\s+|use\s+)[\w\\]+/,
@@ -51,61 +53,54 @@ Prism.languages.insertBefore('php', 'operator', {
5153
}
5254
});
5355

54-
// Add HTML support of the markup language exists
56+
// Add HTML support if the markup language exists
5557
if (Prism.languages.markup) {
5658

5759
// Tokenize all inline PHP blocks that are wrapped in <?php ?>
5860
// This allows for easy PHP + markup highlighting
5961
Prism.hooks.add('before-highlight', function(env) {
60-
if (env.language !== 'php') {
62+
if (env.language !== 'php' || !/(?:<\?php|<\?)/ig.test(env.code)) {
6163
return;
6264
}
6365

6466
env.tokenStack = [];
6567

6668
env.backupCode = env.code;
67-
env.code = env.code.replace(/(?:<\?php|<\?)[\s\S]*?(?:\?>)/ig, function(match) {
69+
env.code = env.code.replace(/(?:<\?php|<\?)[\s\S]*?(?:\?>|$)/ig, function(match) {
6870
env.tokenStack.push(match);
6971

70-
return '{{{PHP' + env.tokenStack.length + '}}}';
72+
return '___PHP' + env.tokenStack.length + '___';
7173
});
74+
75+
// Switch the grammar to markup
76+
env.grammar = Prism.languages.markup;
7277
});
7378

7479
// Restore env.code for other plugins (e.g. line-numbers)
7580
Prism.hooks.add('before-insert', function(env) {
76-
if (env.language === 'php') {
81+
if (env.language === 'php' && env.backupCode) {
7782
env.code = env.backupCode;
7883
delete env.backupCode;
7984
}
8085
});
8186

8287
// Re-insert the tokens after highlighting
8388
Prism.hooks.add('after-highlight', function(env) {
84-
if (env.language !== 'php') {
89+
if (env.language !== 'php' || !env.tokenStack) {
8590
return;
8691
}
8792

93+
// Switch the grammar back
94+
env.grammar = Prism.languages.php;
95+
8896
for (var i = 0, t; t = env.tokenStack[i]; i++) {
8997
// The replace prevents $$, $&, $`, $', $n, $nn from being interpreted as special patterns
90-
env.highlightedCode = env.highlightedCode.replace('{{{PHP' + (i + 1) + '}}}', Prism.highlight(t, env.grammar, 'php').replace(/\$/g, '$$$$'));
98+
env.highlightedCode = env.highlightedCode.replace('___PHP' + (i + 1) + '___',
99+
"<span class=\"token php language-php\">" +
100+
Prism.highlight(t, env.grammar, 'php').replace(/\$/g, '$$$$') +
101+
"</span>");
91102
}
92103

93104
env.element.innerHTML = env.highlightedCode;
94105
});
95-
96-
// Wrap tokens in classes that are missing them
97-
Prism.hooks.add('wrap', function(env) {
98-
if (env.language === 'php' && env.type === 'markup') {
99-
env.content = env.content.replace(/(\{\{\{PHP\d+\}\}\})/g, "<span class=\"token php\">$1</span>");
100-
}
101-
});
102-
103-
// Add the rules before all others
104-
Prism.languages.insertBefore('php', 'comment', {
105-
'markup': {
106-
pattern: /<[^?]\/?(.*?)>/,
107-
inside: Prism.languages.markup
108-
},
109-
'php': /\{\{\{PHP\d+\}\}\}/
110-
});
111106
}

components/prism-php.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/helper/test-case.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ module.exports = {
175175
code: code
176176
};
177177
Prism.hooks.run('before-highlight', env);
178-
env.highlightedCode = Prism.highlight(env.code, Prism.languages[usedLanguages.mainLanguage], usedLanguages.mainLanguage);
178+
env.highlightedCode = Prism.highlight(env.code, env.grammar, env.language);
179179
Prism.hooks.run('before-insert', env);
180180
env.element.innerHTML = env.highlightedCode;
181181
Prism.hooks.run('after-highlight', env);

tests/languages/markup+php/markup_feature.test

-37
This file was deleted.

tests/languages/markup+php/php_in_markup_feature.js

+5-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/languages/php/comment_feature.test

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
/**/
44
/* foo
55
bar */
6+
/* <me@example.com> */
67

78
----------------------------------------------------
89

910
[
1011
["comment", "//"],
1112
["comment", "// foobar"],
1213
["comment", "/**/"],
13-
["comment", "/* foo\r\nbar */"]
14+
["comment", "/* foo\r\nbar */"],
15+
["comment", "/* <me@example.com> */"]
1416
]
1517

1618
----------------------------------------------------

tests/languages/php/delimiter_feature.test

+3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
<? ?>
22
<?php ?>
3+
<?= ?>
34

45
----------------------------------------------------
56

67
[
78
["delimiter", "<?"],
89
["delimiter", "?>"],
910
["delimiter", "<?php"],
11+
["delimiter", "?>"],
12+
["delimiter", "<?="],
1013
["delimiter", "?>"]
1114
]
1215

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"https://example.com"
2+
" /* not a comment */ "
3+
4+
----------------------------------------------------
5+
6+
[
7+
["string", "\"https://example.com\""],
8+
["string", "\" /* not a comment */ \""]
9+
]
10+
11+
----------------------------------------------------
12+
13+
Checks for strings with comments.

0 commit comments

Comments
 (0)