Browse Source

Issue #2906707 by hanoii: Improve preg_replace.

8.x-1.x 8.x-1.9
Chi 7 years ago
parent
commit
88bde29d85
  1. 5
      src/TwigExtension.php
  2. 6
      tests/src/Functional/TwigTweakTest.php
  3. 3
      tests/twig_tweak_test/templates/twig-tweak-test.html.twig

5
src/TwigExtension.php

@ -384,8 +384,13 @@ class TwigExtension extends \Twig_Extension {
* The new text if matches are found, otherwise unchanged text. * The new text if matches are found, otherwise unchanged text.
*/ */
public function pregReplaceFilter($text, $pattern, $replacement) { public function pregReplaceFilter($text, $pattern, $replacement) {
// BC layer. Before version 8.x-1.8 the pattern was without delimiters.
// @todo Remove this in Drupal 9.
if (strpos($pattern, '/') !== 0) {
return preg_replace("/$pattern/", $replacement, $text); return preg_replace("/$pattern/", $replacement, $text);
} }
return preg_replace($pattern, $replacement, $text);
}
/** /**
* Returns the URL of this image derivative for an original image path or URI. * Returns the URL of this image derivative for an original image path or URI.

6
tests/src/Functional/TwigTweakTest.php

@ -148,7 +148,11 @@ class TwigTweakTest extends BrowserTestBase {
$this->assertByXpath($xpath); $this->assertByXpath($xpath);
// Test preg replacement. // Test preg replacement.
$xpath = '//div[@class = "tt-preg-replace" and text() = "foo-bar"]'; $xpath = '//div[@class = "tt-preg-replace" and text() = "FOO-bar"]';
$this->assertByXpath($xpath);
// Test preg replacement (legacy).
$xpath = '//div[@class = "tt-preg-replace-legacy" and text() = "foo-bar"]';
$this->assertByXpath($xpath); $this->assertByXpath($xpath);
// Test image style. // Test image style.

3
tests/twig_tweak_test/templates/twig-tweak-test.html.twig

@ -36,7 +36,8 @@
<div class="tt-title">{{ drupal_title() }}</div> <div class="tt-title">{{ drupal_title() }}</div>
<div class="tt-url">{{ drupal_url('node/1', {absolute: true}) }}</div> <div class="tt-url">{{ drupal_url('node/1', {absolute: true}) }}</div>
<div class="tt-token-replace">{{ 'Site name: [site:name]' | token_replace }}</div> <div class="tt-token-replace">{{ 'Site name: [site:name]' | token_replace }}</div>
<div class="tt-preg-replace">{{ 'foo' | preg_replace('(foo)', '$1-bar') }}</div> <div class="tt-preg-replace">{{ 'FOO' | preg_replace('/(foo)/i', '$1-bar') }}</div>
<div class="tt-preg-replace-legacy">{{ 'foo' | preg_replace('(foo)', '$1-bar') }}</div>
<div class="tt-image-style">{{ 'public://images/ocean.jpg' | image_style('thumbnail') }}</div> <div class="tt-image-style">{{ 'public://images/ocean.jpg' | image_style('thumbnail') }}</div>
<div class="tt-transliterate">{{ 'Привет!' | transliterate('ru') }}</div> <div class="tt-transliterate">{{ 'Привет!' | transliterate('ru') }}</div>
<div class="tt-check-markup">{{ '<b>bold</b> <strong>strong</strong>' | check_markup('twig_tweak_test') }}</div> <div class="tt-check-markup">{{ '<b>bold</b> <strong>strong</strong>' | check_markup('twig_tweak_test') }}</div>

Loading…
Cancel
Save