Author: fabien
Date: 2010-02-23 11:49:31 +0100 (Tue, 23 Feb 2010)
New Revision: 28204

Modified:
   components/yaml/branches/1.0/lib/sfYamlParser.php
   components/yaml/branches/1.0/test/fixtures/sfComments.yml
   components/yaml/branches/1.0/test/sfYamlDumperTest.php
   components/yaml/branches/1.0/test/sfYamlParserTest.php
   components/yaml/trunk/lib/sfYamlParser.php
   components/yaml/trunk/test/fixtures/sfComments.yml
   components/yaml/trunk/test/sfYamlDumperTest.php
   components/yaml/trunk/test/sfYamlParserTest.php
Log:
[YAML] fixed offset when the document use --- or the %YAML element (patch from 
redotheoffice)

Modified: components/yaml/branches/1.0/lib/sfYamlParser.php
===================================================================
--- components/yaml/branches/1.0/lib/sfYamlParser.php   2010-02-23 10:40:10 UTC 
(rev 28203)
+++ components/yaml/branches/1.0/lib/sfYamlParser.php   2010-02-23 10:49:31 UTC 
(rev 28204)
@@ -554,10 +554,18 @@
     }
 
     // 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;
   }

Modified: components/yaml/branches/1.0/test/fixtures/sfComments.yml
===================================================================
--- components/yaml/branches/1.0/test/fixtures/sfComments.yml   2010-02-23 
10:40:10 UTC (rev 28203)
+++ components/yaml/branches/1.0/test/fixtures/sfComments.yml   2010-02-23 
10:49:31 UTC (rev 28204)
@@ -38,4 +38,14 @@
 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')

Modified: components/yaml/branches/1.0/test/sfYamlDumperTest.php
===================================================================
--- components/yaml/branches/1.0/test/sfYamlDumperTest.php      2010-02-23 
10:40:10 UTC (rev 28203)
+++ components/yaml/branches/1.0/test/sfYamlDumperTest.php      2010-02-23 
10:49:31 UTC (rev 28204)
@@ -15,7 +15,7 @@
 
 sfYaml::setSpecVersion('1.1');
 
-$t = new lime_test(148);
+$t = new lime_test(149);
 
 $parser = new sfYamlParser();
 $dumper = new sfYamlDumper();

Modified: components/yaml/branches/1.0/test/sfYamlParserTest.php
===================================================================
--- components/yaml/branches/1.0/test/sfYamlParserTest.php      2010-02-23 
10:40:10 UTC (rev 28203)
+++ components/yaml/branches/1.0/test/sfYamlParserTest.php      2010-02-23 
10:49:31 UTC (rev 28204)
@@ -14,7 +14,7 @@
 
 sfYaml::setSpecVersion('1.1');
 
-$t = new lime_test(148);
+$t = new lime_test(149);
 
 $parser = new sfYamlParser();
 

Modified: components/yaml/trunk/lib/sfYamlParser.php
===================================================================
--- components/yaml/trunk/lib/sfYamlParser.php  2010-02-23 10:40:10 UTC (rev 
28203)
+++ components/yaml/trunk/lib/sfYamlParser.php  2010-02-23 10:49:31 UTC (rev 
28204)
@@ -554,10 +554,18 @@
     }
 
     // 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;
   }

Modified: components/yaml/trunk/test/fixtures/sfComments.yml
===================================================================
--- components/yaml/trunk/test/fixtures/sfComments.yml  2010-02-23 10:40:10 UTC 
(rev 28203)
+++ components/yaml/trunk/test/fixtures/sfComments.yml  2010-02-23 10:49:31 UTC 
(rev 28204)
@@ -38,4 +38,14 @@
 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')

Modified: components/yaml/trunk/test/sfYamlDumperTest.php
===================================================================
--- components/yaml/trunk/test/sfYamlDumperTest.php     2010-02-23 10:40:10 UTC 
(rev 28203)
+++ components/yaml/trunk/test/sfYamlDumperTest.php     2010-02-23 10:49:31 UTC 
(rev 28204)
@@ -15,7 +15,7 @@
 
 sfYaml::setSpecVersion('1.1');
 
-$t = new lime_test(149);
+$t = new lime_test(150);
 
 $parser = new sfYamlParser();
 $dumper = new sfYamlDumper();

Modified: components/yaml/trunk/test/sfYamlParserTest.php
===================================================================
--- components/yaml/trunk/test/sfYamlParserTest.php     2010-02-23 10:40:10 UTC 
(rev 28203)
+++ components/yaml/trunk/test/sfYamlParserTest.php     2010-02-23 10:49:31 UTC 
(rev 28204)
@@ -14,7 +14,7 @@
 
 sfYaml::setSpecVersion('1.1');
 
-$t = new lime_test(149);
+$t = new lime_test(150);
 
 $parser = new sfYamlParser();
 

-- 
You received this message because you are subscribed to the Google Groups 
"symfony SVN" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/symfony-svn?hl=en.

Reply via email to