From e1fc6ad5fd4ee480b29414ed76b98943b4e701a3 Mon Sep 17 00:00:00 2001 From: the_undefined Date: Sun, 18 May 2008 06:11:51 +0000 Subject: [PATCH] Refactored parts of String::insert into String::cleanInsert Added a html clean method and test cases git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6921 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/string.php | 86 ++++++++++++++++++++------- cake/tests/cases/libs/string.test.php | 16 +++++ 2 files changed, 81 insertions(+), 21 deletions(-) diff --git a/cake/libs/string.php b/cake/libs/string.php index 18ca86fc5..3bc0d7eff 100644 --- a/cake/libs/string.php +++ b/cake/libs/string.php @@ -203,7 +203,7 @@ class String extends Object { * after: The character or string after the name of the variable placeholder (Defaults to null) * escape: The character or string used to escape the before character / string (Defaults to '\') * format: A regex to use for matching variable placeholders. Default is: '/(? val array where each key stands for a placeholder variable name to be replaced with val @@ -236,27 +236,71 @@ class String extends Object { if (!isset($options['format']) && isset($options['before'])) { $str = str_replace($options['escape'].$options['before'], $options['before'], $str); } - if ($options['clean']) { - if ($options['clean'] === true) { - $options['clean'] = array(); - } - $options['clean'] = am(array( - 'word' => '[\w,]+', - 'gap' => '[\s]*(?:(?:and|or)[\s]*)?' - ), $options); + if (!$options['clean']) { + return $str; + } + return String::cleanInsert($str, $options); + } +/** + * Cleans up a Set::insert formated string with given $options depending on the 'clean' key in $options. The default method used is + * text but html is also available. The goal of this function is to replace all whitespace and uneeded markup around placeholders + * that did not get replaced by Set::insert. + * + * @param string $str + * @param string $options + * @return void + * @access public + */ + function cleanInsert($str, $options) { + $clean = $options['clean']; + if (!$clean) { + return $str; + } + if ($clean === true) { + $clean = array('method' => 'text'); + } + if (!is_array($clean)) { + $clean = array('method' => $options['clean']); + } + switch ($clean['method']) { + case 'html': + $clean = am(array( + 'word' => '[\w,]+', + 'andText' => true, + 'replacement' => '', + ), $clean); + $kleenex = sprintf( + '/[\s]*[a-z]+=(")(%s%s%s[\s]*)+\\1/i', + preg_quote($options['before'], '/'), + $clean['word'], + preg_quote($options['after'], '/') + ); + $str = preg_replace($kleenex, $clean['replacement'], $str); + if ($clean['andText']) { + $options['clean'] = array('method' => 'text'); + $str = String::cleanInsert($str, $options); + } + break; + case 'text': + $clean = am(array( + 'word' => '[\w,]+', + 'gap' => '[\s]*(?:(?:and|or)[\s]*)?', + 'replacement' => '', + ), $clean); - $kleenex = sprintf( - '/(%s%s%s%s|%s%s%s%s)/', - preg_quote($options['before'], '/'), - $options['clean']['word'], - preg_quote($options['after'], '/'), - $options['clean']['gap'], - $options['clean']['gap'], - preg_quote($options['before'], '/'), - $options['clean']['word'], - preg_quote($options['after'], '/') - ); - $str = preg_replace($kleenex, '', $str); + $kleenex = sprintf( + '/(%s%s%s%s|%s%s%s%s)/', + preg_quote($options['before'], '/'), + $clean['word'], + preg_quote($options['after'], '/'), + $clean['gap'], + $clean['gap'], + preg_quote($options['before'], '/'), + $clean['word'], + preg_quote($options['after'], '/') + ); + $str = preg_replace($kleenex, $clean['replacement'], $str); + break; } return $str; } diff --git a/cake/tests/cases/libs/string.test.php b/cake/tests/cases/libs/string.test.php index 00eb600b0..73ad3656a 100644 --- a/cake/tests/cases/libs/string.test.php +++ b/cake/tests/cases/libs/string.test.php @@ -125,6 +125,22 @@ class StringTest extends UnitTestCase { $expected = '"1, 2"'; $result = String::insert($string, array('a' => 1, 'b' => 2), array('before' => '${', 'after' => '}', 'clean' => true)); $this->assertEqual($result, $expected); + + $string = ':alt'; + $expected = ''; + $result = String::insert($string, array('src' => 'foo'), array('clean' => 'html')); + + $this->assertEqual($result, $expected); + + $string = ''; + $expected = ''; + $result = String::insert($string, array('src' => 'foo'), array('clean' => 'html')); + $this->assertEqual($result, $expected); + + $string = ''; + $expected = ''; + $result = String::insert($string, array('src' => 'foo', 'extra' => 'bar'), array('clean' => 'html')); + $this->assertEqual($result, $expected); } function testTokenize() {