From 83023764b68c3e923c00fe1dff422d925ef33ee2 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 23 Feb 2010 11:52:41 +0100 Subject: [PATCH] [Yaml] fixed offset when the document use --- or the %YAML element (patch from redotheoffice) --- src/Symfony/Components/Yaml/Parser.php | 14 +++++++++++--- .../Symfony/Components/Yaml/sfComments.yml | 12 +++++++++++- tests/unit/Symfony/Components/Yaml/DumperTest.php | 2 +- tests/unit/Symfony/Components/Yaml/ParserTest.php | 2 +- 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Components/Yaml/Parser.php b/src/Symfony/Components/Yaml/Parser.php index 2fe6c1d5a3..824a651f55 100644 --- a/src/Symfony/Components/Yaml/Parser.php +++ b/src/Symfony/Components/Yaml/Parser.php @@ -547,10 +547,18 @@ class Parser } // strip YAML header - preg_replace('#^\%YAML[: ][\d\.]+.*\n#s', '', $value); + $count = 0; + $value = preg_replace('#^\%YAML[: ][\d\.]+.*\n#s', '', $value, -1, $count); + $this->offset += $count; - // remove --- - $value = preg_replace('#^\-\-\-.*?\n#s', '', $value); + // remove leading comments and/or --- + $trimmedValue = preg_replace('#^((\#.*?\n)|(\-\-\-.*?\n))*#s', '', $value, -1, $count); + if ($count == 1) + { + // items have been removed, update the offset + $this->offset += substr_count($value, "\n") - substr_count($trimmedValue, "\n"); + $value = $trimmedValue; + } return $value; } diff --git a/tests/fixtures/Symfony/Components/Yaml/sfComments.yml b/tests/fixtures/Symfony/Components/Yaml/sfComments.yml index bbc425bade..c02940a588 100644 --- a/tests/fixtures/Symfony/Components/Yaml/sfComments.yml +++ b/tests/fixtures/Symfony/Components/Yaml/sfComments.yml @@ -38,4 +38,14 @@ brief: > yaml: | foo: '#bar' php: | - array('foo' => '#bar') \ No newline at end of file + array('foo' => '#bar') +--- +test: Document starting with a comment and a separator +brief: > + Commenting before document start is allowed +yaml: | + # document comment + --- + foo: bar # a comment +php: | + array('foo' => 'bar') diff --git a/tests/unit/Symfony/Components/Yaml/DumperTest.php b/tests/unit/Symfony/Components/Yaml/DumperTest.php index 5b21ce9b63..e0643ef3d4 100644 --- a/tests/unit/Symfony/Components/Yaml/DumperTest.php +++ b/tests/unit/Symfony/Components/Yaml/DumperTest.php @@ -16,7 +16,7 @@ use Symfony\Components\Yaml\Dumper; Yaml::setSpecVersion('1.1'); -$t = new LimeTest(149); +$t = new LimeTest(150); $parser = new Parser(); $dumper = new Dumper(); diff --git a/tests/unit/Symfony/Components/Yaml/ParserTest.php b/tests/unit/Symfony/Components/Yaml/ParserTest.php index e4918af4ae..ead30c7c3c 100644 --- a/tests/unit/Symfony/Components/Yaml/ParserTest.php +++ b/tests/unit/Symfony/Components/Yaml/ParserTest.php @@ -16,7 +16,7 @@ use Symfony\Components\Yaml\ParserException; Yaml::setSpecVersion('1.1'); -$t = new LimeTest(149); +$t = new LimeTest(150); $parser = new Parser();