From c29697c215f7290ee87e9bb0fd3b405fdcb41812 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Fri, 9 Dec 2016 07:44:36 +0100 Subject: [PATCH] [Yaml] parse mappings with a colon in a key --- src/Symfony/Component/Yaml/Inline.php | 28 +++++++++++++++++-- .../Component/Yaml/Tests/InlineTest.php | 2 ++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Yaml/Inline.php b/src/Symfony/Component/Yaml/Inline.php index 624088fbba767..15f1e3cd6487e 100644 --- a/src/Symfony/Component/Yaml/Inline.php +++ b/src/Symfony/Component/Yaml/Inline.php @@ -231,7 +231,23 @@ public static function parseScalar($scalar, $delimiters = null, $stringDelimiter if (null !== $delimiters) { $tmp = ltrim(substr($scalar, $i), ' '); - if (!in_array($tmp[0], $delimiters)) { + $delimiterFound = false; + + foreach ($delimiters as $delimiter) { + // usually, delimiters should be one-character strings + // in this case, we can save some function calls + if ($tmp[0] === $delimiter) { + $delimiterFound = true; + break; + } + + if (substr($tmp, 0, strlen($delimiter)) === $delimiter) { + $delimiterFound = true; + break; + } + } + + if (!$delimiterFound) { throw new ParseException(sprintf('Unexpected characters (%s).', substr($scalar, $i))); } } @@ -382,7 +398,15 @@ private static function parseMapping($mapping, &$i = 0, $references = array()) } // key - $key = self::parseScalar($mapping, array(':', ' '), array('"', "'"), $i, false); + try { + // colons after YAML keys must be followed by a space + $positionBeforeKeyParsing = $i; + $key = self::parseScalar($mapping, array(': ', ' '), array('"', "'"), $i, false); + } catch (ParseException $e) { + // fall back to search for colons without a following space to support the legacy behavior + $key = self::parseScalar($mapping, array(':', ' '), array('"', "'"), $positionBeforeKeyParsing, false); + $i = $positionBeforeKeyParsing; + } // value $done = false; diff --git a/src/Symfony/Component/Yaml/Tests/InlineTest.php b/src/Symfony/Component/Yaml/Tests/InlineTest.php index 755dfce592df8..86c0c65a03927 100644 --- a/src/Symfony/Component/Yaml/Tests/InlineTest.php +++ b/src/Symfony/Component/Yaml/Tests/InlineTest.php @@ -246,6 +246,8 @@ public function getTestsForParse() array('{\'foo\': \'bar\', "bar": \'foo: bar\'}', array('foo' => 'bar', 'bar' => 'foo: bar')), array('{\'foo\'\'\': \'bar\', "bar\"": \'foo: bar\'}', array('foo\'' => 'bar', 'bar"' => 'foo: bar')), array('{\'foo: \': \'bar\', "bar: ": \'foo: bar\'}', array('foo: ' => 'bar', 'bar: ' => 'foo: bar')), + array('{foo:bar: "baz"}', array('foo:bar' => 'baz')), + array('{foo:bar:baz}', array('foo' => 'bar:baz')), // nested sequences and mappings array('[foo, [bar, foo]]', array('foo', array('bar', 'foo'))),