Skip to content

Commit

Permalink
[ticket/13847] Changed enquote() logic to use whichever is the shortest
Browse files Browse the repository at this point in the history
Will enclose attribute values in single- or double- quotes depending on
whichever requires the least escaping. Characters that need to be escaped
are always escaped regardless.

PHPBB3-13847
  • Loading branch information
JoshyPHP committed May 22, 2015
1 parent 7be6bf3 commit 43ee388
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
7 changes: 4 additions & 3 deletions phpBB/phpbb/textformatter/s9e/utils.php
Expand Up @@ -37,17 +37,18 @@ public function clean_formatting($xml)
/**
* Return given string between quotes
*
* Will use either single- or double- quotes depending on whichever requires to be escaped.
* Will use either single- or double- quotes depending on whichever requires less escaping.
* Quotes and backslashes are escaped with backslashes where necessary
*
* @param string $str Original string
* @return string Escaped string within quotes
*/
protected function enquote($str)
{
$quote = (strpos($str, '"') === false || strpos($str, "'") !== false) ? '"' : "'";
$singleQuoted = "'" . addcslashes($str, "\\'") . "'";
$doubleQuoted = '"' . addcslashes($str, '\\"') . '"';

return $quote . addcslashes($str, '\\' . $quote) . $quote;
return (strlen($singleQuoted) < strlen($doubleQuoted)) ? $singleQuoted : $doubleQuoted;
}

/**
Expand Down
15 changes: 15 additions & 0 deletions tests/text_formatter/s9e/utils_test.php
Expand Up @@ -147,6 +147,21 @@ public function get_generate_quote_tests()
array('author' => "\\\"'"),
'[quote="\\\\\\"\'"]...[/quote]',
),
array(
'...',
array('author' => 'Lots of doubles """ one single \' one backslash \\'),
'[quote=\'Lots of doubles """ one single \\\' one backslash \\\\\']...[/quote]',
),
array(
'...',
array('author' => "Lots of singles ''' one double \" one backslash \\"),
'[quote="Lots of singles \'\'\' one double \\" one backslash \\\\"]...[/quote]',
),
array(
'...',
array('author' => 'Defaults to doublequotes """\'\'\''),
'[quote="Defaults to doublequotes \\"\\"\\"\'\'\'"]...[/quote]',
),
array(
'...',
array(
Expand Down

0 comments on commit 43ee388

Please sign in to comment.