diff --git a/framework/Yaml/lib/Horde/Yaml/Dumper.php b/framework/Yaml/lib/Horde/Yaml/Dumper.php index 40ff021..994c7be 100644 --- a/framework/Yaml/lib/Horde/Yaml/Dumper.php +++ b/framework/Yaml/lib/Horde/Yaml/Dumper.php @@ -130,11 +130,13 @@ class Horde_Yaml_Dumper */ protected function _dumpNode($key, $value, $indent) { + $literal = false; // Do some folding here, for blocks. if (strpos($value, "\n") !== false || strpos($value, ': ') !== false || strpos($value, '- ') !== false) { $value = $this->_doLiteralBlock($value, $indent); + $literal = true; } else { $value = $this->_fold($value, $indent); } @@ -153,6 +155,11 @@ class Horde_Yaml_Dumper $spaces = str_repeat(' ', $indent); + // quote strings if necessary, and not folder + if (!$literal && strpos($value, "\n") === false && strchr($value, '#')) { + $value = "'{$value}'"; + } + if (is_int($key)) { // It's a sequence. $string = $spaces . '- ' . $value . "\n"; diff --git a/framework/Yaml/test/Horde/Yaml/DumperTest.php b/framework/Yaml/test/Horde/Yaml/DumperTest.php index e7ed0eb..65240f2 100644 --- a/framework/Yaml/test/Horde/Yaml/DumperTest.php +++ b/framework/Yaml/test/Horde/Yaml/DumperTest.php @@ -188,4 +188,30 @@ class Horde_Yaml_DumperTest extends PHPUnit_Framework_TestCase $this->assertEquals($expected, $actual); } + public function testShouldWrapStringsWithCommentDelimiterInQuotes() + { + $value = array('foo' => 'string # this is not a comment'); + $expected = "---\n" + . "foo: '{$value['foo']}'\n"; + $actual = $this->dumper->dump($value); + $this->assertEquals($expected, $actual); + // round-trip assert + $this->assertEquals($value, Horde_Yaml::load($actual), "Error with: >{$actual}<"); + } + + public function testShouldNotWrapStringsWithCommentDelimiterForFoldedStrings() + { + // stringWithHash: 'string # this is part of the string, not a comment' + $value = array('foo' => 'string # this is not a comment but it is a long string that gets folded', 'bar' => 2); + $expected = "---\n" + . "foo: >\n" + . " string # this is not a comment but it is\n" + . " a long string that gets folded\n" + . "bar: 2\n"; + + $actual = $this->dumper->dump($value); + $this->assertEquals($expected, $actual); + // round-trip assert + $this->assertEquals($value, Horde_Yaml::load($actual), "Error: expected: " . print_r($value, true) . ", actual: " . print_r(Horde_Yaml::load($actual), true) . ", from: >" . $actual . "<"); // fails presently due to bug in loader which causes an extra "\n" at end of string + } } diff --git a/framework/Yaml/test/Horde/Yaml/LoaderTest.php b/framework/Yaml/test/Horde/Yaml/LoaderTest.php index 1146ec1..9cdd9a3 100644 --- a/framework/Yaml/test/Horde/Yaml/LoaderTest.php +++ b/framework/Yaml/test/Horde/Yaml/LoaderTest.php @@ -787,6 +787,19 @@ class Horde_Yaml_LoaderTest extends PHPUnit_Framework_TestCase return dirname(__FILE__) . "/fixtures/{$name}.yml"; } + public function testUnfolding() + { + $parsed = Horde_Yaml::loadFile($this->fixture('basic')); + $expected = "Line 1 Line 2"; + $this->assertEquals($expected, $parsed['foldedStringTest']); + } + + public function testUnliteraling() + { + $parsed = Horde_Yaml::loadFile($this->fixture('basic')); + $expected = "Line #1\nLine #2"; + $this->assertEquals($expected, $parsed['literalStringTest']); + } } diff --git a/framework/Yaml/test/Horde/Yaml/fixtures/basic.yml b/framework/Yaml/test/Horde/Yaml/fixtures/basic.yml index da6a584..b706bd9 100644 --- a/framework/Yaml/test/Horde/Yaml/fixtures/basic.yml +++ b/framework/Yaml/test/Horde/Yaml/fixtures/basic.yml @@ -1,5 +1,4 @@ foo: bar - # A folded block as a mapped value no time: > There isn't any time @@ -11,4 +10,9 @@ no time: > some time: | There is nothing but time for your tricks. - +literalStringTest: | + Line #1 + Line #2 +foldedStringTest: > + Line 1 + Line 2