Permalink
Browse files

Fixed `$1` and `\\1` being lost inside `<pre>` and `<code>` blocks (#…

  • Loading branch information...
1 parent 61dfd7c commit a925ec3bdf958d5d87cdafd45ba1a1897e63625a @mahagr mahagr committed Jan 9, 2017
Showing with 26 additions and 3 deletions.
  1. +2 −0 CHANGELOG.md
  2. +24 −3 src/classes/Gantry/Framework/Base/Document.php
View
@@ -5,6 +5,8 @@
1. [](#new)
- Add particle caching when in production mode making a noticeable speed increase on particle heavy pages.
Installed Gantry 5 themes should to be updated to a version which supports particle caching.
+ 3. [](#bugfix)
+ - Fixed `$1` and `\\1` being lost inside `<pre>` and `<code>` blocks (#1782)
2. [Joomla](#joomla)
3. [](#bugfix)
- Fixed loading template styles `preg_replace(): Compilation failed` error (#1769)
@@ -567,12 +567,14 @@ public static function urlFilter($html, $domain = false, $timestamp_age = null,
{
static::$urlFilterParams = [$domain, $timestamp_age, $streamOnly];
- // Tokenize all PRE and CODE tags to avoid modifying any src|href|url in them
+ // Tokenize all PRE, CODE and SCRIPT tags to avoid modifying any src|href|url in them
$tokens = [];
$html = preg_replace_callback('#<(pre|code|script)(\s[^>]+)?>.*?</\\1>#ius', function($matches) use (&$tokens) {
$token = '@@'. uniqid('g5_token_') . '@@';
- $tokens['#' . preg_quote($token, '#') . '#u'] = $matches[0];
+ $match = $matches[0];
+
+ $tokens[$token] = $match;
return $token;
}, $html);
@@ -601,7 +603,7 @@ public static function urlFilter($html, $domain = false, $timestamp_age = null,
$html = preg_replace_callback('^(\s)(src|href)="' . $match . '"^u', 'static::linkHandler', $html);
$html = preg_replace_callback('^(\s)url\(' . $match . '\)^u', 'static::urlHandler', $html);
- $html = preg_replace(array_keys($tokens), array_values($tokens), $html); // restore tokens
+ $html = static::replaceTokens($tokens, $html);
return $html;
}
@@ -635,6 +637,25 @@ public static function urlHandler(array $matches)
}
/**
+ * Replace tokens with strings.
+ *
+ * @param array $tokens
+ * @param $html
+ * @return string|NUll
+ */
+ protected static function replaceTokens(array $tokens, $html)
+ {
+ foreach ($tokens as $token => $replacement) {
+ // We need to use callbacks to turn off backreferences ($1, \\1) in the replacement string.
+ $callback = function() use ($replacement) { return $replacement; };
+
+ $html = preg_replace_callback('#' . preg_quote($token, '#') . '#u', $callback, $html);
+ }
+
+ return $html;
+ }
+
+ /**
* @param array $target
* @param array $source
* @return array

0 comments on commit a925ec3

Please sign in to comment.