Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4d673d7

Browse filesBrowse files
committed
merged branch ezzatron/yaml-block-chomping (PR #6785)
This PR was merged into the 2.0 branch. Commits ------- ab0385c [Yaml] fixed #6773 Discussion ---------- [Yaml] fixed parsing of blocks with 'keep' chomping indicators (2.0 branch) | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #6773 | License | MIT | Doc PR | n/a Also added a full set of tests for all combinations of literal/folded blocks and strip/clip/keep chomping indicators in both mid-file and end-of file positions.
2 parents 4c437f6 + ab0385c commit 4d673d7
Copy full SHA for 4d673d7

File tree

Expand file treeCollapse file tree

2 files changed

+198
-6
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+198
-6
lines changed

‎src/Symfony/Component/Yaml/Parser.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Parser.php
+3-6Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,9 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
179179
}
180180
}
181181
} else {
182-
// 1-liner followed by newline
183-
if (2 == count($this->lines) && empty($this->lines[1])) {
182+
// 1-liner optionally followed by newline
183+
$lineCount = count($this->lines);
184+
if (1 === $lineCount || (2 === $lineCount && empty($this->lines[1]))) {
184185
try {
185186
$value = Inline::parse($this->lines[0], $exceptionOnInvalidType, $objectSupport);
186187
} catch (ParseException $e) {
@@ -527,10 +528,6 @@ private function cleanup($value)
527528
{
528529
$value = str_replace(array("\r\n", "\r"), "\n", $value);
529530

530-
if (!preg_match("#\n$#", $value)) {
531-
$value .= "\n";
532-
}
533-
534531
// strip YAML header
535532
$count = 0;
536533
$value = preg_replace('#^\%YAML[: ][\d\.]+.*\n#su', '', $value, -1, $count);

‎tests/Symfony/Tests/Component/Yaml/ParserTest.php

Copy file name to clipboardExpand all lines: tests/Symfony/Tests/Component/Yaml/ParserTest.php
+195Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,201 @@ public function testEndOfTheDocumentMarker()
105105
$this->assertEquals('foo', $this->parser->parse($yaml));
106106
}
107107

108+
public function getBlockChompingTests()
109+
{
110+
$tests = array();
111+
112+
$yaml = <<<'EOF'
113+
foo: |-
114+
one
115+
two
116+
117+
bar: |-
118+
one
119+
two
120+
121+
EOF;
122+
$expected = array(
123+
'foo' => "one\ntwo",
124+
'bar' => "one\ntwo",
125+
);
126+
$tests['Literal block chomping strip with trailing newline'] = array($expected, $yaml);
127+
128+
$yaml = <<<'EOF'
129+
foo: |-
130+
one
131+
two
132+
bar: |-
133+
one
134+
two
135+
EOF;
136+
$expected = array(
137+
'foo' => "one\ntwo",
138+
'bar' => "one\ntwo",
139+
);
140+
$tests['Literal block chomping strip without trailing newline'] = array($expected, $yaml);
141+
142+
$yaml = <<<'EOF'
143+
foo: |
144+
one
145+
two
146+
147+
bar: |
148+
one
149+
two
150+
151+
EOF;
152+
$expected = array(
153+
'foo' => "one\ntwo\n",
154+
'bar' => "one\ntwo\n",
155+
);
156+
$tests['Literal block chomping clip with trailing newline'] = array($expected, $yaml);
157+
158+
$yaml = <<<'EOF'
159+
foo: |
160+
one
161+
two
162+
bar: |
163+
one
164+
two
165+
EOF;
166+
$expected = array(
167+
'foo' => "one\ntwo\n",
168+
'bar' => "one\ntwo\n",
169+
);
170+
$tests['Literal block chomping clip without trailing newline'] = array($expected, $yaml);
171+
172+
$yaml = <<<'EOF'
173+
foo: |+
174+
one
175+
two
176+
177+
bar: |+
178+
one
179+
two
180+
181+
EOF;
182+
$expected = array(
183+
'foo' => "one\ntwo\n\n",
184+
'bar' => "one\ntwo\n\n",
185+
);
186+
$tests['Literal block chomping keep with trailing newline'] = array($expected, $yaml);
187+
188+
$yaml = <<<'EOF'
189+
foo: |+
190+
one
191+
two
192+
bar: |+
193+
one
194+
two
195+
EOF;
196+
$expected = array(
197+
'foo' => "one\ntwo\n",
198+
'bar' => "one\ntwo\n",
199+
);
200+
$tests['Literal block chomping keep without trailing newline'] = array($expected, $yaml);
201+
202+
$yaml = <<<'EOF'
203+
foo: >-
204+
one
205+
two
206+
207+
bar: >-
208+
one
209+
two
210+
211+
EOF;
212+
$expected = array(
213+
'foo' => "one two",
214+
'bar' => "one two",
215+
);
216+
$tests['Folded block chomping strip with trailing newline'] = array($expected, $yaml);
217+
218+
$yaml = <<<'EOF'
219+
foo: >-
220+
one
221+
two
222+
bar: >-
223+
one
224+
two
225+
EOF;
226+
$expected = array(
227+
'foo' => "one two",
228+
'bar' => "one two",
229+
);
230+
$tests['Folded block chomping strip without trailing newline'] = array($expected, $yaml);
231+
232+
$yaml = <<<'EOF'
233+
foo: >
234+
one
235+
two
236+
237+
bar: >
238+
one
239+
two
240+
241+
EOF;
242+
$expected = array(
243+
'foo' => "one two\n",
244+
'bar' => "one two\n",
245+
);
246+
$tests['Folded block chomping clip with trailing newline'] = array($expected, $yaml);
247+
248+
$yaml = <<<'EOF'
249+
foo: >
250+
one
251+
two
252+
bar: >
253+
one
254+
two
255+
EOF;
256+
$expected = array(
257+
'foo' => "one two\n",
258+
'bar' => "one two\n",
259+
);
260+
$tests['Folded block chomping clip without trailing newline'] = array($expected, $yaml);
261+
262+
$yaml = <<<'EOF'
263+
foo: >+
264+
one
265+
two
266+
267+
bar: >+
268+
one
269+
two
270+
271+
EOF;
272+
$expected = array(
273+
'foo' => "one two\n\n",
274+
'bar' => "one two\n\n",
275+
);
276+
$tests['Folded block chomping keep with trailing newline'] = array($expected, $yaml);
277+
278+
$yaml = <<<'EOF'
279+
foo: >+
280+
one
281+
two
282+
bar: >+
283+
one
284+
two
285+
EOF;
286+
$expected = array(
287+
'foo' => "one two\n",
288+
'bar' => "one two\n",
289+
);
290+
$tests['Folded block chomping keep without trailing newline'] = array($expected, $yaml);
291+
292+
return $tests;
293+
}
294+
295+
/**
296+
* @dataProvider getBlockChompingTests
297+
*/
298+
public function testBlockChomping($expected, $yaml)
299+
{
300+
$this->assertSame($expected, $this->parser->parse($yaml));
301+
}
302+
108303
public function testObjectSupportEnabled()
109304
{
110305
$input = <<<EOF

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.