Browse Source

Fix missing preg_quote around highlight searches.

Highlight strings should be literal values not regexp fragments.
Fixes #2111

Conflicts:

	cake/libs/view/helpers/text.php
	cake/tests/cases/libs/view/helpers/text.test.php
mark_story 14 years ago
parent
commit
7f0f224c56

+ 5 - 0
lib/Cake/Test/Case/View/Helper/TextHelperTest.php

@@ -104,6 +104,11 @@ class TextHelperTest extends CakeTestCase {
 		$result = $this->Text->highlight($text, $phrases, array('format' => '<b>\1</b>'));
 		$this->assertEqual($result, $text);
 
+		$text = 'This is a (test) text';
+		$phrases = '(test';
+		$result = $this->Text->highlight($text, $phrases, array('format' => '<b>\1</b>'));
+		$this->assertEqual('This is a <b>(test</b>) text', $result);
+
 		$text = 'Ich saß in einem Café am Übergang';
 		$expected = 'Ich <b>saß</b> in einem <b>Café</b> am <b>Übergang</b>';
 		$phrases = array('saß', 'café', 'übergang');

+ 2 - 2
lib/Cake/View/Helper/TextHelper.php

@@ -77,7 +77,7 @@ class TextHelper extends AppHelper {
 			$with = array();
 
 			foreach ($phrase as $key => $segment) {
-				$segment = "($segment)";
+				$segment = '(' . preg_quote($segment, '|') . ')';
 				if ($html) {
 					$segment = "(?![^<]+>)$segment(?![^<]+>)";
 				}
@@ -88,7 +88,7 @@ class TextHelper extends AppHelper {
 
 			return preg_replace($replace, $with, $text);
 		} else {
-			$phrase = "($phrase)";
+			$phrase = '(' . preg_quote($phrase, '|') . ')';
 			if ($html) {
 				$phrase = "(?![^<]+>)$phrase(?![^<]+>)";
 			}