Merge pull request #9872 from creative-commoners/pulls/4.7/empty-shortscode-attributes

BUG Tweak shortcode parser so it properly parse empty attributes
This commit is contained in:
Steve Boyd 2021-02-27 14:42:53 +13:00 committed by GitHub
commit 705b746d0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 25 deletions

View File

@ -263,8 +263,8 @@ class ShortcodeParser
([^\s\/\'"=,]+) # Name
\s* = \s*
(?:
(?:\'([^\']+)\') | # Value surrounded by \'
(?:"([^"]+)") | # Value surrounded by "
(?:\'([^\']*)\') | # Value surrounded by \'
(?:"([^"]*)") | # Value surrounded by "
([^\s,\]]+) # Bare value
)
';

View File

@ -79,38 +79,55 @@ class ShortcodeParserTest extends SapphireTest
);
}
public function testSimpleTag()
public function simpleTagDataProvider()
{
$tests = [
'[test_shortcode]',
'[test_shortcode ]', '[test_shortcode,]', '[test_shortcode, ]' . '[test_shortcode/]', '[test_shortcode /]', '[test_shortcode,/]', '[test_shortcode, /]'
return [
['[test_shortcode]'],
['[test_shortcode ]'],
['[test_shortcode,]'],
['[test_shortcode, ][test_shortcode/]'],
['[test_shortcode /]'],
['[test_shortcode,/]'],
['[test_shortcode, /]']
];
}
foreach ($tests as $test) {
/**
* @dataProvider simpleTagDataProvider
*/
public function testSimpleTag($test)
{
$this->parser->parse($test);
$this->assertEquals([], $this->arguments, $test);
$this->assertEquals('', $this->contents, $test);
$this->assertEquals('test_shortcode', $this->tagName, $test);
}
public function oneArgumentDataProvider()
{
return [
['[test_shortcode foo="bar"]'],
['[test_shortcode,foo="bar"]'],
["[test_shortcode foo='bar']"],
["[test_shortcode,foo='bar']"],
["[test_shortcode foo=bar]"],
["[test_shortcode,foo=bar]"],
['[test_shortcode foo = "bar" /]'],
['[test_shortcode, foo = "bar" /]']
];
}
public function testOneArgument()
/**
* @dataProvider oneArgumentDataProvider
*/
public function testOneArgument($test)
{
$tests = [
'[test_shortcode foo="bar"]', '[test_shortcode,foo="bar"]',
"[test_shortcode foo='bar']", "[test_shortcode,foo='bar']",
'[test_shortcode foo = "bar" /]', '[test_shortcode, foo = "bar" /]'
];
foreach ($tests as $test) {
$this->parser->parse($test);
$this->assertEquals(['foo' => 'bar'], $this->arguments, $test);
$this->assertEquals('', $this->contents, $test);
$this->assertEquals('test_shortcode', $this->tagName, $test);
}
}
public function testMultipleArguments()
{
@ -121,6 +138,26 @@ class ShortcodeParserTest extends SapphireTest
$this->assertEquals('test_shortcode', $this->tagName);
}
public function emptyArgumentsDataProvider()
{
return [
['[test_shortcode foo=""]'],
['[test_shortcode,foo=\'\']'],
['[test_shortcode foo=""][/test_shortcode]'],
];
}
/**
* @dataProvider emptyArgumentsDataProvider
*/
public function testEmptyArguments($test)
{
$this->parser->parse($test);
$this->assertEquals(['foo' => ''], $this->arguments);
$this->assertEquals('', $this->contents);
$this->assertEquals('test_shortcode', $this->tagName);
}
public function testEnclosing()
{
$this->parser->parse('[test_shortcode]foo[/test_shortcode]');