10000 bug #15793 [Yaml] Allow tabs before comments at the end of a line (su… · symfony/symfony@bd9997e · GitHub
[go: up one dir, main page]

Skip to content

Commit bd9997e

Browse files
committed
bug #15793 [Yaml] Allow tabs before comments at the end of a line (superdav42)
This PR was merged into the 2.3 branch. Discussion ---------- [Yaml] Allow tabs before comments at the end of a line | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT If a yml file has a tab character before a line ending comment the comment will be included in the parsed value. Yaml spec allows tab or space as whitespace characters so we need to check for tab as well. See included test. Recently caused an odd and hard to find bug in our project. See spec: http://www.yaml.org/spec/1.2/spec.html#s-b-comment http://www.yaml.org/spec/1.2/spec.html#s-separate-in-line http://www.yaml.org/spec/1.2/spec.html#s-white This is a new PR replacing #15747 @fabpot Commits ------- d040be7 [Yaml] Allow tabs before comments at the end of a line
2 parents 65b9adc + d040be7 commit bd9997e

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

src/Symfony/Component/Yaml/Inline.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,8 @@ public static function parseScalar($scalar, $delimiters = null, $stringDelimiter
212212
$i += strlen($output);
213213

214214
// remove comments
215-
if (false !== $strpos = strpos($output, ' #')) {
216-
$output = rtrim(substr($output, 0, $strpos));
215+
if (preg_match('/[ \t]+#/', $output, $match, PREG_OFFSET_CAPTURE)) {
216+
$output = substr($output, 0, $match[0][1]);
217217
}
218218
} elseif (preg_match('/^(.+?)('.implode('|', $delimiters).')/', substr($scalar, $i), $match)) {
219219
$output = $match[1];

src/Symfony/Component/Yaml/Tests/Fixtures/sfComments.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ yaml: |
77
ex2: "foo # bar" # comment
88
ex3: 'foo # bar' # comment
99
ex4: foo # comment
10+
ex5: foo # comment with tab before
11+
ex6: foo#foo # comment here
12+
ex7: foo # ignore me # and me
1013
php: |
11-
array('ex1' => 'foo # bar', 'ex2' => 'foo # bar', 'ex3' => 'foo # bar', 'ex4' => 'foo')
14+
array('ex1' => 'foo # bar', 'ex2' => 'foo # bar', 'ex3' => 'foo # bar', 'ex4' => 'foo', 'ex5' => 'foo', 'ex6' => 'foo#foo', 'ex7' => 'foo')
1215
---
1316
test: Comments in the middle
1417
brief: >

0 commit comments

Comments
 (0)
0