1
0
Fork 0

Fix json manipulator handling of escaped backslashes, fixes #1588

pull/1594/head
Jordi Boggiano 2013-02-18 17:27:43 +01:00
parent 1e15edc43d
commit e348642aa7
3 changed files with 30 additions and 12 deletions

View File

@ -58,12 +58,12 @@ class JsonManipulator
// link exists already // link exists already
if (preg_match('{"'.$packageRegex.'"\s*:}i', $links)) { if (preg_match('{"'.$packageRegex.'"\s*:}i', $links)) {
$links = preg_replace('{"'.$packageRegex.'"(\s*:\s*)"[^"]+"}i', JsonFile::encode($package).'${1}"'.$constraint.'"', $links); $links = preg_replace('{"'.$packageRegex.'"(\s*:\s*)"[^"]+"}i', addcslashes(JsonFile::encode($package).'${1}"'.$constraint.'"', '\\'), $links);
} elseif (preg_match('#[^\s](\s*)$#', $links, $match)) { } elseif (preg_match('#[^\s](\s*)$#', $links, $match)) {
// link missing but non empty links // link missing but non empty links
$links = preg_replace( $links = preg_replace(
'#'.$match[1].'$#', '#'.$match[1].'$#',
',' . $this->newline . $this->indent . $this->indent . JsonFile::encode($package).': '.JsonFile::encode($constraint) . $match[1], addcslashes(',' . $this->newline . $this->indent . $this->indent . JsonFile::encode($package).': '.JsonFile::encode($constraint) . $match[1], '\\'),
$links $links
); );
} else { } else {
@ -71,7 +71,7 @@ class JsonManipulator
$links = $this->newline . $this->indent . $this->indent . JsonFile::encode($package).': '.JsonFile::encode($constraint) . $links; $links = $this->newline . $this->indent . $this->indent . JsonFile::encode($package).': '.JsonFile::encode($constraint) . $links;
} }
$this->contents = preg_replace($linksRegex, '${1}'.$links.'$3', $this->contents); $this->contents = preg_replace($linksRegex, addcslashes('${1}'.$links.'$3', '\\'), $this->contents);
return true; return true;
} }
@ -144,7 +144,7 @@ class JsonManipulator
// child missing but non empty children // child missing but non empty children
$children = preg_replace( $children = preg_replace(
'#'.$match[1].'$#', '#'.$match[1].'$#',
',' . $this->newline . $this->indent . $this->indent . JsonFile::encode($name).': '.$this->format($value, 1) . $match[1], addcslashes(',' . $this->newline . $this->indent . $this->indent . JsonFile::encode($name).': '.$this->format($value, 1) . $match[1], '\\'),
$children $children
); );
} else { } else {
@ -156,7 +156,7 @@ class JsonManipulator
$children = $this->newline . $this->indent . $this->indent . JsonFile::encode($name).': '.$this->format($value, 1) . $children; $children = $this->newline . $this->indent . $this->indent . JsonFile::encode($name).': '.$this->format($value, 1) . $children;
} }
$this->contents = preg_replace($nodeRegex, '${1}'.$children.'$3', $this->contents); $this->contents = preg_replace($nodeRegex, addcslashes('${1}'.$children.'$3', '\\'), $this->contents);
return true; return true;
} }
@ -225,10 +225,6 @@ class JsonManipulator
return true; return true;
} }
if ($subName !== null) {
}
$that = $this; $that = $this;
$this->contents = preg_replace_callback($nodeRegex, function ($matches) use ($that, $name, $subName, $childrenClean) { $this->contents = preg_replace_callback($nodeRegex, function ($matches) use ($that, $name, $subName, $childrenClean) {
if ($subName !== null) { if ($subName !== null) {
@ -248,13 +244,13 @@ class JsonManipulator
if (preg_match('#[^{\s](\s*)\}$#', $this->contents, $match)) { if (preg_match('#[^{\s](\s*)\}$#', $this->contents, $match)) {
$this->contents = preg_replace( $this->contents = preg_replace(
'#'.$match[1].'\}$#', '#'.$match[1].'\}$#',
',' . $this->newline . $this->indent . JsonFile::encode($key). ': '. $content . $this->newline . '}', addcslashes(',' . $this->newline . $this->indent . JsonFile::encode($key). ': '. $content . $this->newline . '}', '\\'),
$this->contents $this->contents
); );
} else { } else {
$this->contents = preg_replace( $this->contents = preg_replace(
'#\}$#', '#\}$#',
$this->indent . JsonFile::encode($key). ': '.$content . $this->newline . '}', addcslashes($this->indent . JsonFile::encode($key). ': '.$content . $this->newline . '}', '\\'),
$this->contents $this->contents
); );
} }

View File

@ -179,12 +179,18 @@ class JsonFileTest extends \PHPUnit_Framework_TestCase
public function testEscapedSlashes() public function testEscapedSlashes()
{ {
$data = "\\/foo"; $data = "\\/foo";
$this->assertJsonFormat('"\\\\\\/foo"', $data, 0); $this->assertJsonFormat('"\\\\\\/foo"', $data, 0);
} }
public function testEscapedBackslashes()
{
$data = "a\\b";
$this->assertJsonFormat('"a\\\\b"', $data, 0);
}
public function testEscapedUnicode() public function testEscapedUnicode()
{ {
$data = "ƌ"; $data = "ƌ";

View File

@ -392,6 +392,22 @@ class JsonManipulatorTest extends \PHPUnit_Framework_TestCase
', $manipulator->getContents()); ', $manipulator->getContents());
} }
public function testAddConfigSettingEscapes()
{
$manipulator = new JsonManipulator('{
"config": {
}
}');
$this->assertTrue($manipulator->addConfigSetting('test', 'a\b'));
$this->assertEquals('{
"config": {
"test": "a\\\\b"
}
}
', $manipulator->getContents());
}
public function testAddConfigSettingCanAdd() public function testAddConfigSettingCanAdd()
{ {
$manipulator = new JsonManipulator('{ $manipulator = new JsonManipulator('{