Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9a130ae

Browse files
bertramakersxabbuh
authored andcommitted
Fix issue 40507: Tabs as separators between tokens
1 parent 5dd56a6 commit 9a130ae

File tree

2 files changed

+58
-17
lines changed

2 files changed

+58
-17
lines changed

src/Symfony/Component/Yaml/Parser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ private function doParse(string $value, int $flags)
200200
array_pop($this->refsBeingParsed);
201201
}
202202
} elseif (
203-
self::preg_match('#^(?P<key>(?:![^\s]++\s++)?(?:'.Inline::REGEX_QUOTED_STRING.'|(?:!?!php/const:)?[^ \'"\[\{!].*?)) *\:( ++(?P<value>.+))?$#u', rtrim($this->currentLine), $values)
203+
self::preg_match('#^(?P<key>(?:![^\s]++\s++)?(?:'.Inline::REGEX_QUOTED_STRING.'|(?:!?!php/const:)?[^ \'"\[\{!].*?)) *\:(( |\t)++(?P<value>.+))?$#u', rtrim($this->currentLine), $values)
204204
&& (false === strpos($values['key'], ' #') || \in_array($values['key'][0], ['"', "'"]))
205205
) {
206206
if ($context && 'sequence' == $context) {

src/Symfony/Component/Yaml/Tests/ParserTest.php

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,26 +52,67 @@ public function getNonStringMappingKeysData()
5252
return $this->loadTestsFromFixtureFiles('nonStringKeys.yml');
5353
}
5454

55-
public function testTabsInYaml()
55+
/**
56+
* @dataProvider invalidIndentation
57+
*/
58+
public function testTabsAsIndentationInYaml(string $given, string $expectedMessage)
5659
{
57-
// test tabs in YAML
58-
$yamls = [
59-
"foo:\n bar",
60-
"foo:\n bar",
61-
"foo:\n bar",
62-
"foo:\n bar",
60+
$this->expectException(ParseException::class);
61+
$this->expectExceptionMessage($expectedMessage);
62+
$this->parser->parse($given);
63+
}
64+
65+
public function invalidIndentation(): array
66+
{
67+
return [
68+
[
69+
"foo:\n\tbar",
70+
"A YAML file cannot contain tabs as indentation at line 2 (near \"\tbar\").",
71+
],
72+
[
73+
"foo:\n \tbar",
74+
"A YAML file cannot contain tabs as indentation at line 2 (near \"\tbar\").",
75+
],
76+
[
77+
"foo:\n\t bar",
78+
"A YAML file cannot contain tabs as indentation at line 2 (near \"\t bar\").",
79+
],
80+
[
81+
"foo:\n \t bar",
82+
"A YAML file cannot contain tabs as indentation at line 2 (near \"\t bar\").",
83+
],
6384
];
85+
}
6486

65-
foreach ($yamls as $yaml) {
66-
try {
67-
$this->parser->parse($yaml);
87+
/**
88+
* @dataProvider validTokenSeparators
89+
*/
90+
public function testValidTokenSeparation(string $given, array $expected)
91+
{
92+
$actual = $this->parser->parse($given);
93+
$this->assertEquals($expected, $actual);
94+
}
6895

69-
$this->fail('YAML files must not contain tabs');
70-
} catch (\Exception $e) {
71-
$this->assertInstanceOf(\Exception::class, $e, 'YAML files must not contain tabs');
72-
$this->assertEquals('A YAML file cannot contain tabs as indentation at line 2 (near "'.strpbrk($yaml, "\t").'").', $e->getMessage(), 'YAML files must not contain tabs');
73-
}
74-
}
96+
public function validTokenSeparators(): array
97+
{
98+
return [
99+
[
100+
'foo: bar',
101+
['foo' => 'bar'],
102+
],
103+
[
104+
"foo:\tbar",
105+
['foo' => 'bar'],
106+
],
107+
[
108+
"foo: \tbar",
109+
['foo' => 'bar'],
110+
],
111+
[
112+
"foo:\t bar",
113+
['foo' => 'bar'],
114+
],
115+
];
75116
}
76117

77118
public function testEndOfTheDocumentMarker()

0 commit comments

Comments
 (0)