From 5df26e23f10f54d84a25878c00e7cb77207c2229 Mon Sep 17 00:00:00 2001 From: Andreas Rohner Date: Sat, 13 May 2017 09:57:11 +0200 Subject: [PATCH] Check for possible pre-existing marker strings in Smarty --- components/prism-smarty.js | 20 ++++++++++++++----- components/prism-smarty.min.js | 2 +- .../smarty/smarty_in_markup_feature.js | 3 ++- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/components/prism-smarty.js b/components/prism-smarty.js index 6c7b2e48b8..b1ece8bbb4 100644 --- a/components/prism-smarty.js +++ b/components/prism-smarty.js @@ -9,7 +9,7 @@ var smarty_litteral_start = '{literal}'; var smarty_litteral_end = '{/literal}'; var smarty_litteral_mode = false; - + Prism.languages.smarty = Prism.languages.extend('markup', { 'smarty': { pattern: smarty_pattern, @@ -93,9 +93,16 @@ if(match === smarty_litteral_start) { smarty_litteral_mode = true; } - env.tokenStack.push(match); - return '___SMARTY' + env.tokenStack.length + '___'; + var i = env.tokenStack.length; + // Check for existing strings + while (env.backupCode.indexOf('___SMARTY' + i + '___') !== -1) + ++i; + + // Create a sparse array + env.tokenStack[i] = match; + + return '___SMARTY' + i + '___'; } return match; }); @@ -116,9 +123,12 @@ return; } - 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('___SMARTY' + (i + 1) + '___', Prism.highlight(t, env.grammar, 'smarty').replace(/\$/g, '$$$$')); + env.highlightedCode = env.highlightedCode.replace('___SMARTY' + k + '___', Prism.highlight(t, env.grammar, 'smarty').replace(/\$/g, '$$$$')); } env.element.innerHTML = env.highlightedCode; diff --git a/components/prism-smarty.min.js b/components/prism-smarty.min.js index 7617174b75..8a9000fd09 100644 --- a/components/prism-smarty.min.js +++ b/components/prism-smarty.min.js @@ -1 +1 @@ -!function(e){var t=/\{\*[\s\S]+?\*\}|\{[\s\S]+?\}/g,a="{literal}",n="{/literal}",o=!1;e.languages.smarty=e.languages.extend("markup",{smarty:{pattern:t,inside:{delimiter:{pattern:/^\{|\}$/i,alias:"punctuation"},string:/(["'])(?:\\?.)*?\1/,number:/\b-?(?:0x[\dA-Fa-f]+|\d*\.?\d+(?:[Ee][-+]?\d+)?)\b/,variable:[/\$(?!\d)\w+/,/#(?!\d)\w+#/,{pattern:/(\.|->)(?!\d)\w+/,lookbehind:!0},{pattern:/(\[)(?!\d)\w+(?=\])/,lookbehind:!0}],"function":[{pattern:/(\|\s*)@?(?!\d)\w+/,lookbehind:!0},/^\/?(?!\d)\w+/,/(?!\d)\w+(?=\()/],"attr-name":{pattern:/\w+\s*=\s*(?:(?!\d)\w+)?/,inside:{variable:{pattern:/(=\s*)(?!\d)\w+/,lookbehind:!0},operator:/=/}},punctuation:[/[\[\]().,:`]|\->/],operator:[/[+\-*\/%]|==?=?|[!<>]=?|&&|\|\|?/,/\bis\s+(?:not\s+)?(?:div|even|odd)(?:\s+by)?\b/,/\b(?:eq|neq?|gt|lt|gt?e|lt?e|not|mod|or|and)\b/],keyword:/\b(?:false|off|on|no|true|yes)\b/}}}),e.languages.insertBefore("smarty","tag",{"smarty-comment":{pattern:/\{\*[\s\S]*?\*\}/,alias:["smarty","comment"]}}),e.hooks.add("before-highlight",function(e){"smarty"===e.language&&(e.tokenStack=[],e.backupCode=e.code,e.code=e.code.replace(t,function(t){return t===n&&(o=!1),o?t:(t===a&&(o=!0),e.tokenStack.push(t),"___SMARTY"+e.tokenStack.length+"___")}))}),e.hooks.add("before-insert",function(e){"smarty"===e.language&&(e.code=e.backupCode,delete e.backupCode)}),e.hooks.add("after-highlight",function(t){if("smarty"===t.language){for(var a,n=0;a=t.tokenStack[n];n++)t.highlightedCode=t.highlightedCode.replace("___SMARTY"+(n+1)+"___",e.highlight(a,t.grammar,"smarty").replace(/\$/g,"$$$$"));t.element.innerHTML=t.highlightedCode}})}(Prism); \ No newline at end of file +!function(e){var t=/\{\*[\s\S]+?\*\}|\{[\s\S]+?\}/g,a="{literal}",n="{/literal}",o=!1;e.languages.smarty=e.languages.extend("markup",{smarty:{pattern:t,inside:{delimiter:{pattern:/^\{|\}$/i,alias:"punctuation"},string:/(["'])(?:\\?.)*?\1/,number:/\b-?(?:0x[\dA-Fa-f]+|\d*\.?\d+(?:[Ee][-+]?\d+)?)\b/,variable:[/\$(?!\d)\w+/,/#(?!\d)\w+#/,{pattern:/(\.|->)(?!\d)\w+/,lookbehind:!0},{pattern:/(\[)(?!\d)\w+(?=\])/,lookbehind:!0}],"function":[{pattern:/(\|\s*)@?(?!\d)\w+/,lookbehind:!0},/^\/?(?!\d)\w+/,/(?!\d)\w+(?=\()/],"attr-name":{pattern:/\w+\s*=\s*(?:(?!\d)\w+)?/,inside:{variable:{pattern:/(=\s*)(?!\d)\w+/,lookbehind:!0},operator:/=/}},punctuation:[/[\[\]().,:`]|\->/],operator:[/[+\-*\/%]|==?=?|[!<>]=?|&&|\|\|?/,/\bis\s+(?:not\s+)?(?:div|even|odd)(?:\s+by)?\b/,/\b(?:eq|neq?|gt|lt|gt?e|lt?e|not|mod|or|and)\b/],keyword:/\b(?:false|off|on|no|true|yes)\b/}}}),e.languages.insertBefore("smarty","tag",{"smarty-comment":{pattern:/\{\*[\s\S]*?\*\}/,alias:["smarty","comment"]}}),e.hooks.add("before-highlight",function(e){"smarty"===e.language&&(e.tokenStack=[],e.backupCode=e.code,e.code=e.code.replace(t,function(t){if(t===n&&(o=!1),!o){t===a&&(o=!0);for(var r=e.tokenStack.length;-1!==e.backupCode.indexOf("___SMARTY"+r+"___");)++r;return e.tokenStack[r]=t,"___SMARTY"+r+"___"}return t}))}),e.hooks.add("before-insert",function(e){"smarty"===e.language&&(e.code=e.backupCode,delete e.backupCode)}),e.hooks.add("after-highlight",function(t){if("smarty"===t.language){for(var a=0,n=Object.keys(t.tokenStack);a{$foo}': '<div>{$foo}</div>', - '
': '<div class="{$foo}">' + '
': '<div class="{$foo}">', + '___SMARTY1___{$foo}': '___SMARTY1___{$foo}' }; \ No newline at end of file