Refactor ODS escaper (#363)
This commit is contained in:
parent
e276b4378e
commit
e255895cff
@ -14,31 +14,6 @@ class ODS implements EscaperInterface
|
|||||||
{
|
{
|
||||||
use Singleton;
|
use Singleton;
|
||||||
|
|
||||||
/** @var string Regex pattern to detect control characters that need to be escaped */
|
|
||||||
protected $escapableControlCharactersPattern;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initializes the singleton instance
|
|
||||||
*/
|
|
||||||
protected function init()
|
|
||||||
{
|
|
||||||
$this->escapableControlCharactersPattern = $this->getEscapableControlCharactersPattern();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string Regex pattern containing all escapable control characters
|
|
||||||
*/
|
|
||||||
protected function getEscapableControlCharactersPattern()
|
|
||||||
{
|
|
||||||
// control characters values are from 0 to 1F (hex values) in the ASCII table
|
|
||||||
// some characters should not be escaped though: "\t", "\r" and "\n".
|
|
||||||
return '[\x00-\x08' .
|
|
||||||
// skipping "\t" (0x9) and "\n" (0xA)
|
|
||||||
'\x0B-\x0C' .
|
|
||||||
// skipping "\r" (0xD)
|
|
||||||
'\x0E-\x1F]';
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Escapes the given string to make it compatible with XLSX
|
* Escapes the given string to make it compatible with XLSX
|
||||||
*
|
*
|
||||||
@ -49,15 +24,24 @@ class ODS implements EscaperInterface
|
|||||||
{
|
{
|
||||||
if (defined('ENT_DISALLOWED')) {
|
if (defined('ENT_DISALLOWED')) {
|
||||||
// 'ENT_DISALLOWED' ensures that invalid characters in the given document type are replaced.
|
// 'ENT_DISALLOWED' ensures that invalid characters in the given document type are replaced.
|
||||||
// Otherwise characters like a vertical tab "\v" will make the XML document unreadable by the XML processor
|
// Otherwise control characters like a vertical tab "\v" will make the XML document unreadable by the XML processor
|
||||||
// @link https://github.com/box/spout/issues/329
|
// @link https://github.com/box/spout/issues/329
|
||||||
return htmlspecialchars($string, ENT_QUOTES | ENT_DISALLOWED);
|
$replacedString = htmlspecialchars($string, ENT_QUOTES | ENT_DISALLOWED);
|
||||||
} else {
|
} else {
|
||||||
// We are on hhvm or any other engine that does not support ENT_DISALLOWED
|
// We are on hhvm or any other engine that does not support ENT_DISALLOWED
|
||||||
$escapedString = htmlspecialchars($string, ENT_QUOTES);
|
$escapedString = htmlspecialchars($string, ENT_QUOTES);
|
||||||
$replacedString = preg_replace('/'.$this->escapableControlCharactersPattern.'/', '<27>', $escapedString);
|
|
||||||
return $replacedString;
|
// control characters values are from 0 to 1F (hex values) in the ASCII table
|
||||||
|
// some characters should not be escaped though: "\t", "\r" and "\n".
|
||||||
|
$regexPattern = '[\x00-\x08' .
|
||||||
|
// skipping "\t" (0x9) and "\n" (0xA)
|
||||||
|
'\x0B-\x0C' .
|
||||||
|
// skipping "\r" (0xD)
|
||||||
|
'\x0E-\x1F]';
|
||||||
|
$replacedString = preg_replace("/$regexPattern/", '<27>', $escapedString);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $replacedString;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user