Skip to content

Commit

Permalink
Check for possible pre-existing marker strings in PHP
Browse files Browse the repository at this point in the history
  • Loading branch information
zeitgeist87 committed May 13, 2017
1 parent ba64f8d commit 36bc560
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
17 changes: 13 additions & 4 deletions components/prism-php.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,15 @@ if (Prism.languages.markup) {

env.backupCode = env.code;
env.code = env.code.replace(/(?:<\?php|<\?)[\s\S]*?(?:\?>|$)/ig, function(match) {
env.tokenStack.push(match);
var i = env.tokenStack.length;
// Check for existing strings
while (env.backupCode.indexOf('___PHP' + i + '___') !== -1)
++i;

return '___PHP' + env.tokenStack.length + '___';
// Create a sparse array
env.tokenStack[i] = match;

return '___PHP' + i + '___';
});

// Switch the grammar to markup
Expand All @@ -93,9 +99,12 @@ if (Prism.languages.markup) {
// Switch the grammar back
env.grammar = Prism.languages.php;

for (var i = 0, t; t = env.tokenStack[i]; i++) {
for (var i = 0, keys = Object.keys(env.tokenStack); i < keys.length; ++i) {
var k = keys[i];
var t = env.tokenStack[k];

// The replace prevents $$, $&, $`, $', $n, $nn from being interpreted as special patterns
env.highlightedCode = env.highlightedCode.replace('___PHP' + (i + 1) + '___',
env.highlightedCode = env.highlightedCode.replace('___PHP' + k + '___',
"<span class=\"token php language-php\">" +
Prism.highlight(t, env.grammar, 'php').replace(/\$/g, '$$$$') +
"</span>");
Expand Down
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.

3 changes: 2 additions & 1 deletion tests/languages/markup+php/php_in_markup_feature.js

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

0 comments on commit 36bc560

Please sign in to comment.