Skip to content

Commit

Permalink
merged branch fabpot/yaml-fix (PR #6790)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.0 branch.

Commits
-------

53ccc2c [Yaml] fixed ignored text when parsing an inlined mapping or sequence (closes #6786)

Discussion
----------

[Yaml] fixed ignored text when parsing an inlined mapping or sequence (closes #6786)

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #6786
| License       | MIT
| Doc PR        | n/a
  • Loading branch information
fabpot committed Jan 18, 2013
2 parents 9773c8a + 53ccc2c commit b9963ea
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/Symfony/Component/Yaml/Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,23 @@ public static function parse($value, $exceptionOnInvalidType = false, $objectSup
mb_internal_encoding('ASCII');
}

$i = 0;
switch ($value[0]) {
case '[':
$result = self::parseSequence($value);
$result = self::parseSequence($value, $i);
++$i;
break;
case '{':
$result = self::parseMapping($value);
$result = self::parseMapping($value, $i);
++$i;
break;
default:
$result = self::parseScalar($value);
$result = self::parseScalar($value, null, array('"', "'"), $i);
}

// some comments are allowed at the end
if (preg_replace('/\s+#.*$/A', '', substr($value, $i))) {
throw new ParseException(sprintf('Unexpected characters near "%s".', substr($value, $i)));
}

if (isset($mbEncoding)) {
Expand Down Expand Up @@ -185,6 +193,13 @@ public static function parseScalar($scalar, $delimiters = null, $stringDelimiter
if (in_array($scalar[$i], $stringDelimiters)) {
// quoted scalar
$output = self::parseQuotedScalar($scalar, $i);

if (null !== $delimiters) {
$tmp = ltrim(substr($scalar, $i), ' ');
if (!in_array($tmp[0], $delimiters)) {
throw new ParseException(sprintf('Unexpected characters (%s).', substr($scalar, $i)));
}
}
} else {
// "normal" string
if (!$delimiters) {
Expand Down Expand Up @@ -220,6 +235,11 @@ public static function parseScalar($scalar, $delimiters = null, $stringDelimiter
*/
private static function parseQuotedScalar($scalar, &$i)
{
// Only check the current item we're dealing with (for sequences)
$subject = substr($scalar, $i);
$items = preg_split('/[\'"]\s*(?:[,:]|[}\]]\s*,)/', $subject);
$subject = substr($subject, 0, strlen($items[0]) + 1);

if (!preg_match('/'.self::REGEX_QUOTED_STRING.'/Au', substr($scalar, $i), $match)) {
throw new ParseException(sprintf('Malformed inline YAML string (%s).', substr($scalar, $i)));
}
Expand Down
51 changes: 51 additions & 0 deletions tests/Symfony/Tests/Component/Yaml/InlineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,57 @@ public function testHashStringsResemblingExponentialNumericsShouldNotBeChangedTo
$this->assertSame($value, Inline::parse(Inline::dump($value)));
}

/**
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
*/
public function testParseScalarWithIncorrectlyQuotedStringShouldThrowException()
{
$value = "'don't do somthin' like that'";
Inline::parse($value);
}

/**
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
*/
public function testParseScalarWithIncorrectlyDoubleQuotedStringShouldThrowException()
{
$value = '"don"t do somthin" like that"';
Inline::parse($value);
}

/**
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
*/
public function testParseInvalidMappingKeyShouldThrowException()
{
$value = '{ "foo " bar": "bar" }';
Inline::parse($value);
}

/**
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
*/
public function testParseInvalidMappingShouldThrowException()
{
Inline::parse('[foo] bar');
}

/**
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
*/
public function testParseInvalidSequenceShouldThrowException()
{
Inline::parse('{ foo: bar } bar');
}

public function testParseScalarWithCorrectlyQuotedStringShouldReturnString()
{
$value = "'don''t do somthin'' like that'";
$expect = "don't do somthin' like that";

$this->assertSame($expect, Inline::parseScalar($value));
}

protected function getTestsForParse()
{
return array(
Expand Down

0 comments on commit b9963ea

Please sign in to comment.