Skip to content

Commit 878d63b

Browse files
committed
don't try to replace references in quoted strings
1 parent 3abcc4d commit 878d63b

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

Inline.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ private static function parseSequence(string $sequence, int $flags, int &$i = 0,
387387
}
388388
}
389389

390-
if (\is_string($value) && '' !== $value && '&' === $value[0] && Parser::preg_match(Parser::REFERENCE_PATTERN, $value, $matches)) {
390+
if (!$isQuoted && \is_string($value) && '' !== $value && '&' === $value[0] && Parser::preg_match(Parser::REFERENCE_PATTERN, $value, $matches)) {
391391
$references[$matches['ref']] = $matches['value'];
392392
$value = $matches['value'];
393393
}
@@ -521,6 +521,7 @@ private static function parseMapping(string $mapping, int $flags, int &$i = 0, a
521521
}
522522
break;
523523
default:
524+
$isValueQuoted = \in_array($mapping[$i], ['"', "'"]);
524525
$value = self::parseScalar($mapping, $flags, [',', '}', "\n"], $i, null === $tag, $references);
525526
// Spec: Keys MUST be unique; first one wins.
526527
// Parser cannot abort this mapping earlier, since lines
@@ -529,7 +530,7 @@ private static function parseMapping(string $mapping, int $flags, int &$i = 0, a
529530
if ('<<' === $key) {
530531
$output += $value;
531532
} elseif ($allowOverwrite || !isset($output[$key])) {
532-
if (\is_string($value) && '' !== $value && '&' === $value[0] && Parser::preg_match(Parser::REFERENCE_PATTERN, $value, $matches)) {
533+
if (!$isValueQuoted && \is_string($value) && '' !== $value && '&' === $value[0] && Parser::preg_match(Parser::REFERENCE_PATTERN, $value, $matches)) {
533534
$references[$matches['ref']] = $matches['value'];
534535
$value = $matches['value'];
535536
}

Tests/InlineTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,4 +905,22 @@ public function ideographicSpaceProvider(): array
905905
["'a b'", 'a b'],
906906
];
907907
}
908+
909+
public function testParseQuotedReferenceLikeStringsInMapping()
910+
{
911+
$yaml = <<<YAML
912+
{foo: '&foo', bar: "&bar"}
913+
YAML;
914+
915+
$this->assertSame(['foo' => '&foo', 'bar' => '&bar'], Inline::parse($yaml));
916+
}
917+
918+
public function testParseQuotedReferenceLikeStringsInSequence()
919+
{
920+
$yaml = <<<YAML
921+
['&foo', "&bar" ]
922+
YAML;
923+
924+
$this->assertSame(['&foo', '&bar'], Inline::parse($yaml));
925+
}
908926
}

0 commit comments

Comments
 (0)