Преглед изворни кода

[Yaml] fixed offset when the document use --- or the %YAML element (patch from redotheoffice)

Fabien Potencier пре 15 година
родитељ
комит
83023764b6

+ 11 - 3
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;
   }

+ 11 - 1
tests/fixtures/Symfony/Components/Yaml/sfComments.yml

@@ -38,4 +38,14 @@ brief: >
 yaml: | 
     foo:   '#bar' 
 php: | 
-    array('foo' => '#bar') 
+    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')

+ 1 - 1
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();

+ 1 - 1
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();