From aa16555d16f439ef50e1042c4ab83dc57dcd51b0 Mon Sep 17 00:00:00 2001 From: natepage Date: Wed, 10 Mar 2021 17:18:34 +1100 Subject: [PATCH 1/5] [Yaml] Support blockChompingIndicator when dumping TaggedValue --- src/Symfony/Component/Yaml/Dumper.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Yaml/Dumper.php b/src/Symfony/Component/Yaml/Dumper.php index dcb104ccff065..0604534d1ba9d 100644 --- a/src/Symfony/Component/Yaml/Dumper.php +++ b/src/Symfony/Component/Yaml/Dumper.php @@ -101,7 +101,16 @@ public function dump($input, int $inline = 0, int $indent = 0, int $flags = 0): // If the first line starts with a space character, the spec requires a blockIndicationIndicator // http://www.yaml.org/spec/1.2/spec.html#id2793979 $blockIndentationIndicator = (' ' === substr($value->getValue(), 0, 1)) ? (string) $this->indentation : ''; - $output .= sprintf(' |%s', $blockIndentationIndicator); + + if (isset($value->getValue()[-2]) && "\n" === $value->getValue()[-2] && "\n" === $value->getValue()[-1]) { + $blockChompingIndicator = '+'; + } elseif ("\n" === $value->getValue()[-1]) { + $blockChompingIndicator = ''; + } else { + $blockChompingIndicator = '-'; + } + + $output .= sprintf(' |%s%s', $blockIndentationIndicator, $blockChompingIndicator); foreach (explode("\n", $value->getValue()) as $row) { $output .= sprintf("\n%s%s%s", $prefix, str_repeat(' ', $this->indentation), $row); From adb61b220abf7dbb4e7b948dc5b54fd2ec876d89 Mon Sep 17 00:00:00 2001 From: natepage Date: Wed, 10 Mar 2021 18:10:19 +1100 Subject: [PATCH 2/5] [Yaml] Fix tests --- src/Symfony/Component/Yaml/Tests/DumperTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Yaml/Tests/DumperTest.php b/src/Symfony/Component/Yaml/Tests/DumperTest.php index 6329aec86ccff..97a7c5f341ce1 100644 --- a/src/Symfony/Component/Yaml/Tests/DumperTest.php +++ b/src/Symfony/Component/Yaml/Tests/DumperTest.php @@ -494,7 +494,7 @@ public function testDumpingMultiLineStringAsScalarBlockTaggedValue() $data = [ 'foo' => new TaggedValue('bar', "foo\nline with trailing spaces:\n \nbar\ninteger like line:\n123456789\nempty line:\n\nbaz"), ]; - $expected = "foo: !bar |\n". + $expected = "foo: !bar |-\n". " foo\n". " line with trailing spaces:\n". " \n". From 6d5de2b7336af8e293845344c6850a446c007ce9 Mon Sep 17 00:00:00 2001 From: natepage Date: Mon, 29 Mar 2021 08:59:15 +1100 Subject: [PATCH 3/5] [Yaml] Fix difference between multiline block logic for normal and tagged values --- src/Symfony/Component/Yaml/Dumper.php | 8 +++- .../Component/Yaml/Tests/DumperTest.php | 39 ++++++++++++++++++- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Yaml/Dumper.php b/src/Symfony/Component/Yaml/Dumper.php index 0604534d1ba9d..8472f4c45b294 100644 --- a/src/Symfony/Component/Yaml/Dumper.php +++ b/src/Symfony/Component/Yaml/Dumper.php @@ -97,7 +97,7 @@ public function dump($input, int $inline = 0, int $indent = 0, int $flags = 0): if ($value instanceof TaggedValue) { $output .= sprintf('%s%s !%s', $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', $value->getTag()); - if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value->getValue()) && false !== strpos($value->getValue(), "\n") && false === strpos($value->getValue(), "\r\n")) { + if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value->getValue()) && false !== strpos($value->getValue(), "\n") && false === strpos($value->getValue(), "\r")) { // If the first line starts with a space character, the spec requires a blockIndicationIndicator // http://www.yaml.org/spec/1.2/spec.html#id2793979 $blockIndentationIndicator = (' ' === substr($value->getValue(), 0, 1)) ? (string) $this->indentation : ''; @@ -113,7 +113,11 @@ public function dump($input, int $inline = 0, int $indent = 0, int $flags = 0): $output .= sprintf(' |%s%s', $blockIndentationIndicator, $blockChompingIndicator); foreach (explode("\n", $value->getValue()) as $row) { - $output .= sprintf("\n%s%s%s", $prefix, str_repeat(' ', $this->indentation), $row); + if ('' === $row) { + $output .= "\n"; + } else { + $output .= sprintf("\n%s%s%s", $prefix, str_repeat(' ', $this->indentation), $row); + } } continue; diff --git a/src/Symfony/Component/Yaml/Tests/DumperTest.php b/src/Symfony/Component/Yaml/Tests/DumperTest.php index 97a7c5f341ce1..1cb1eb05bfd42 100644 --- a/src/Symfony/Component/Yaml/Tests/DumperTest.php +++ b/src/Symfony/Component/Yaml/Tests/DumperTest.php @@ -502,7 +502,7 @@ public function testDumpingMultiLineStringAsScalarBlockTaggedValue() " integer like line:\n". " 123456789\n". " empty line:\n". - " \n". + "\n". ' baz'; $this->assertSame($expected, $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK)); @@ -580,6 +580,43 @@ public function testNoExtraTrailingNewlineWhenDumpingAsMultiLineLiteralBlock() $this->assertSame($data, Yaml::parse($yaml)); } + public function testDumpTrailingNewlineInMultiLineLiteralBlocksForTaggedValues() + { + $data = [ + 'clip 1' => new TaggedValue('my-tag', "one\ntwo\n"), + 'clip 2' => new TaggedValue('my-tag', "one\ntwo\n"), + 'keep 1' => new TaggedValue('my-tag', "one\ntwo\n"), + 'keep 2' => new TaggedValue('my-tag', "one\ntwo\n\n"), + 'strip 1' => new TaggedValue('my-tag', "one\ntwo"), + 'strip 2' => new TaggedValue('my-tag', "one\ntwo"), + ]; + $yaml = $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK); + + $expected = <<assertSame($expected, $yaml); + } + public function testDumpTrailingNewlineInMultiLineLiteralBlocks() { $data = [ From 7fe12b5676c7822a1f3173301b785799f98d7f7e Mon Sep 17 00:00:00 2001 From: natepage Date: Mon, 29 Mar 2021 21:48:17 +1100 Subject: [PATCH 4/5] [Yaml] Add test for CR preserved with TaggedValue --- src/Symfony/Component/Yaml/Tests/DumperTest.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/Symfony/Component/Yaml/Tests/DumperTest.php b/src/Symfony/Component/Yaml/Tests/DumperTest.php index 1cb1eb05bfd42..e125b85ec7400 100644 --- a/src/Symfony/Component/Yaml/Tests/DumperTest.php +++ b/src/Symfony/Component/Yaml/Tests/DumperTest.php @@ -553,6 +553,21 @@ public function testCarriageReturnFollowedByNewlineIsMaintainedWhenDumpingAsMult $this->assertSame("- \"a\\r\\nb\\nc\"\n", $this->dumper->dump(["a\r\nb\nc"], 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK)); } + public function testCarriageReturnNotFollowedByNewlineIsPreservedWhenDumpingTaggedValueAsMultiLineLiteralBlock() + { + $expected = <<<'YAML' +parent: + foo: !my-tag "bar\n\rbaz: qux" + +YAML; + + $this->assertSame($expected, $this->dumper->dump([ + 'parent' => [ + 'foo' => new TaggedValue('my-tag', "bar\n\rbaz: qux"), + ], + ], 4, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK)); + } + public function testCarriageReturnNotFollowedByNewlineIsPreservedWhenDumpingAsMultiLineLiteralBlock() { $expected = <<<'YAML' From 7abeafca0540e7fbfa34f51d50d72d451efc9092 Mon Sep 17 00:00:00 2001 From: natepage Date: Mon, 3 May 2021 23:53:15 +1000 Subject: [PATCH 5/5] Clean up test for duplicates --- .../Component/Yaml/Tests/DumperTest.php | 32 +++---------------- 1 file changed, 4 insertions(+), 28 deletions(-) diff --git a/src/Symfony/Component/Yaml/Tests/DumperTest.php b/src/Symfony/Component/Yaml/Tests/DumperTest.php index e125b85ec7400..2c27a8d5dae49 100644 --- a/src/Symfony/Component/Yaml/Tests/DumperTest.php +++ b/src/Symfony/Component/Yaml/Tests/DumperTest.php @@ -599,11 +599,8 @@ public function testDumpTrailingNewlineInMultiLineLiteralBlocksForTaggedValues() { $data = [ 'clip 1' => new TaggedValue('my-tag', "one\ntwo\n"), - 'clip 2' => new TaggedValue('my-tag', "one\ntwo\n"), - 'keep 1' => new TaggedValue('my-tag', "one\ntwo\n"), - 'keep 2' => new TaggedValue('my-tag', "one\ntwo\n\n"), + 'keep 1' => new TaggedValue('my-tag', "one\ntwo\n\n"), 'strip 1' => new TaggedValue('my-tag', "one\ntwo"), - 'strip 2' => new TaggedValue('my-tag', "one\ntwo"), ]; $yaml = $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK); @@ -611,22 +608,13 @@ public function testDumpTrailingNewlineInMultiLineLiteralBlocksForTaggedValues() 'clip 1': !my-tag | one two -'clip 2': !my-tag | - one - two -'keep 1': !my-tag | - one - two -'keep 2': !my-tag |+ +'keep 1': !my-tag |+ one two 'strip 1': !my-tag |- one two -'strip 2': !my-tag |- - one - two YAML; $this->assertSame($expected, $yaml); @@ -636,11 +624,8 @@ public function testDumpTrailingNewlineInMultiLineLiteralBlocks() { $data = [ 'clip 1' => "one\ntwo\n", - 'clip 2' => "one\ntwo\n", - 'keep 1' => "one\ntwo\n", - 'keep 2' => "one\ntwo\n\n", + 'keep 1' => "one\ntwo\n\n", 'strip 1' => "one\ntwo", - 'strip 2' => "one\ntwo", ]; $yaml = $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK); @@ -648,22 +633,13 @@ public function testDumpTrailingNewlineInMultiLineLiteralBlocks() 'clip 1': | one two -'clip 2': | - one - two -'keep 1': | - one - two -'keep 2': |+ +'keep 1': |+ one two 'strip 1': |- one two -'strip 2': |- - one - two YAML; $this->assertSame($expected, $yaml); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy