mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-18 02:26:17 +00:00
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
This commit is contained in:
parent
550b85fdbb
commit
e1fc6ad5fd
2 changed files with 81 additions and 21 deletions
|
@ -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: '/(?<!\\)\:%s/' (Overwrites before, after, breaks escape / clean)
|
||||
* clean: A boolean, if set to true all variable placeholders that were not overwritten with $data items are going to be removed, including whitespace around them.
|
||||
* clean: A boolean or array with instructions for String::cleanInsert
|
||||
*
|
||||
* @param string $str A string containing variable placeholders
|
||||
* @param string $data A key => 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();
|
||||
if (!$options['clean']) {
|
||||
return $str;
|
||||
}
|
||||
$options['clean'] = am(array(
|
||||
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,]+',
|
||||
'gap' => '[\s]*(?:(?:and|or)[\s]*)?'
|
||||
), $options);
|
||||
'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'],
|
||||
$clean['word'],
|
||||
preg_quote($options['after'], '/'),
|
||||
$options['clean']['gap'],
|
||||
$options['clean']['gap'],
|
||||
$clean['gap'],
|
||||
$clean['gap'],
|
||||
preg_quote($options['before'], '/'),
|
||||
$options['clean']['word'],
|
||||
$clean['word'],
|
||||
preg_quote($options['after'], '/')
|
||||
);
|
||||
$str = preg_replace($kleenex, '', $str);
|
||||
$str = preg_replace($kleenex, $clean['replacement'], $str);
|
||||
break;
|
||||
}
|
||||
return $str;
|
||||
}
|
||||
|
|
|
@ -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 = '<img src=":src" alt=":alt" class="foo :extra bar"/>';
|
||||
$expected = '<img src="foo" class="foo bar"/>';
|
||||
$result = String::insert($string, array('src' => 'foo'), array('clean' => 'html'));
|
||||
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$string = '<img src=":src" class=":no :extra"/>';
|
||||
$expected = '<img src="foo"/>';
|
||||
$result = String::insert($string, array('src' => 'foo'), array('clean' => 'html'));
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$string = '<img src=":src" class=":no :extra"/>';
|
||||
$expected = '<img src="foo" class="bar"/>';
|
||||
$result = String::insert($string, array('src' => 'foo', 'extra' => 'bar'), array('clean' => 'html'));
|
||||
$this->assertEqual($result, $expected);
|
||||
}
|
||||
|
||||
function testTokenize() {
|
||||
|
|
Loading…
Add table
Reference in a new issue