merged branch paul-matthews/master (PR #4042)

Commits
-------

478227d Fixed quoting issues with Yaml Inline Parser

Discussion
----------

[Yaml] fix parsing quotes problem

Added some basic checking for quotes on their own within strings. Used single quote ' followed by "," and ":" to denote the end of the current string to test.

Bug fix: yes
Feature addition: no
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: #4021
Todo: N/A

---------------------------------------------------------------------------

by fabpot at 2012-05-07T09:22:56Z

After doing the requested changes, can you squash your commits? Thanks.

---------------------------------------------------------------------------

by paul-matthews at 2012-05-09T13:29:45Z

Resolved and squashed

---------------------------------------------------------------------------

by travisbot at 2012-05-09T13:30:30Z

This pull request [fails](http://travis-ci.org/symfony/symfony/builds/1285048) (merged 5ce6d4e1 into 6c714095).

---------------------------------------------------------------------------

by travisbot at 2012-05-09T13:31:06Z

This pull request [fails](http://travis-ci.org/symfony/symfony/builds/1285057) (merged 3592fcec into e54f4e46).

---------------------------------------------------------------------------

by travisbot at 2012-05-09T13:33:40Z

This pull request [fails](http://travis-ci.org/symfony/symfony/builds/1285094) (merged e209f786 into e54f4e46).

---------------------------------------------------------------------------

by travisbot at 2012-05-09T13:37:42Z

This pull request [fails](http://travis-ci.org/symfony/symfony/builds/1285153) (merged 0967b513 into e54f4e46).

---------------------------------------------------------------------------

by fabpot at 2012-05-09T14:00:29Z

It looks like that another commit is in your PR that should not be there.

---------------------------------------------------------------------------

by paul-matthews at 2012-05-09T15:52:35Z

Right-o will remove

---------------------------------------------------------------------------

by paul-matthews at 2012-05-09T15:54:15Z

resolved

---------------------------------------------------------------------------

by travisbot at 2012-05-09T15:57:15Z

This pull request [fails](http://travis-ci.org/symfony/symfony/builds/1286339) (merged fc276209 into e66a0bb3).

---------------------------------------------------------------------------

by fabpot at 2012-05-10T05:20:18Z

Apparently, some tests do not pass anymore after the patch. Can you have a look at them? Thanks.

---------------------------------------------------------------------------

by paul-matthews at 2012-05-10T11:20:45Z

Sure - looking into it.

---------------------------------------------------------------------------

by paul-matthews at 2012-05-10T17:55:53Z

I believe that's fixed.

The entire suite works locally now.

---------------------------------------------------------------------------

by travisbot at 2012-05-10T17:55:54Z

This pull request [fails](http://travis-ci.org/symfony/symfony/builds/1297641) (merged 884c02ed into fae4523f).

---------------------------------------------------------------------------

by paul-matthews at 2012-05-10T17:57:03Z

Unsure why travis fails.

---------------------------------------------------------------------------

by travisbot at 2012-05-10T18:00:28Z

This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1297650) (merged 478227d9 into fae4523f).

---------------------------------------------------------------------------

by stof at 2012-06-09T13:24:52Z

@fabpot 👍
This commit is contained in:
Fabien Potencier 2012-06-09 16:40:39 +02:00
commit 37678e1715
2 changed files with 43 additions and 1 deletions

View File

@ -21,6 +21,8 @@ use Symfony\Component\Yaml\Exception\DumpException;
class Inline
{
const REGEX_QUOTED_STRING = '(?:"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'([^\']*(?:\'\'[^\']*)*)\')';
const REGEX_SINGLE_QUOTED_STRING = '(?:\'([^\']*(?:\'\'[^\']*)*)\')(?!.*\')';
const REGEX_DOUBLE_QUOTED_STRING = '(?:"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)")(?!.*")';
/**
* Converts a YAML string to a PHP array.
@ -196,7 +198,16 @@ class Inline
*/
static private function parseQuotedScalar($scalar, &$i)
{
if (!preg_match('/'.self::REGEX_QUOTED_STRING.'/Au', substr($scalar, $i), $match)) {
// 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 (($scalar[$i] == "'"
&& !preg_match('/'.self::REGEX_SINGLE_QUOTED_STRING.'/Au', $subject, $match))
|| ($scalar[$i] == '"'
&& !preg_match('/'.self::REGEX_DOUBLE_QUOTED_STRING.'/Au', $subject, $match))
) {
throw new ParseException(sprintf('Malformed inline YAML string (%s).', substr($scalar, $i)));
}

View File

@ -65,6 +65,34 @@ class InlineTest extends \PHPUnit_Framework_TestCase
$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::parseScalar($value);
}
/**
*
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
*/
public function testParseScalarWithIncorrectlyDoubleQuotedStringShouldThrowException()
{
$value = '"don"t do somthin" like that"';
Inline::parseScalar($value);
}
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(
@ -124,6 +152,7 @@ class InlineTest extends \PHPUnit_Framework_TestCase
'[foo, {bar: foo, foo: [foo, {bar: foo}]}, [foo, {bar: foo}]]' => array('foo', array('bar' => 'foo', 'foo' => array('foo', array('bar' => 'foo'))), array('foo', array('bar' => 'foo'))),
'[foo, bar: { foo: bar }]' => array('foo', '1' => array('bar' => array('foo' => 'bar'))),
'[foo, \'@foo.baz\', { \'%foo%\': \'foo is %foo%\', bar: \'%foo%\' }, true, \'@service_container\']' => array('foo', '@foo.baz', array('%foo%' => 'foo is %foo%', 'bar' => '%foo%',), true, '@service_container',),
);
}
@ -167,6 +196,8 @@ class InlineTest extends \PHPUnit_Framework_TestCase
'[foo, { bar: foo }]' => array('foo', array('bar' => 'foo')),
'[foo, { bar: foo, foo: [foo, { bar: foo }] }, [foo, { bar: foo }]]' => array('foo', array('bar' => 'foo', 'foo' => array('foo', array('bar' => 'foo'))), array('foo', array('bar' => 'foo'))),
'[foo, \'@foo.baz\', { \'%foo%\': \'foo is %foo%\', bar: \'%foo%\' }, true, \'@service_container\']' => array('foo', '@foo.baz', array('%foo%' => 'foo is %foo%', 'bar' => '%foo%',), true, '@service_container',),
);
}
}