FIX Make ShortcodeParser obey error_behavior in attribute scope

This commit is contained in:
Hamish Friedlander 2013-02-22 11:34:15 +13:00
parent 1ee01c39d4
commit 7afcd64418
2 changed files with 28 additions and 1 deletions

View File

@ -332,7 +332,21 @@ class ShortcodeParser {
if($tags) { if($tags) {
$node->nodeValue = $this->replaceTagsWithText($node->nodeValue, $tags, function($idx, $tag) use ($parser){ $node->nodeValue = $this->replaceTagsWithText($node->nodeValue, $tags, function($idx, $tag) use ($parser){
return $parser->callShortcode($tag['open'], $tag['attrs'], $tag['content']); $content = $parser->callShortcode($tag['open'], $tag['attrs'], $tag['content']);
if ($content === false) {
if(ShortcodeParser::$error_behavior == ShortcodeParser::ERROR) {
user_error('Unknown shortcode tag '.$tag['open'], E_USER_ERRROR);
}
else if(ShortcodeParser::$error_behavior == ShortcodeParser::STRIP) {
return '';
}
else {
return $tag['text'];
}
}
return $content;
}); });
} }
} }

View File

@ -19,18 +19,26 @@ class ShortcodeParserTest extends SapphireTest {
*/ */
public function testNotRegisteredShortcode() { public function testNotRegisteredShortcode() {
ShortcodeParser::$error_behavior = ShortcodeParser::STRIP; ShortcodeParser::$error_behavior = ShortcodeParser::STRIP;
$this->assertEquals( $this->assertEquals(
'', '',
$this->parser->parse('[not_shortcode]') $this->parser->parse('[not_shortcode]')
); );
$this->assertEquals(
'<img class="">',
$this->parser->parse('<img class="[not_shortcode]">')
);
ShortcodeParser::$error_behavior = ShortcodeParser::WARN; ShortcodeParser::$error_behavior = ShortcodeParser::WARN;
$this->assertEquals( $this->assertEquals(
'<strong class="warning">[not_shortcode]</strong>', '<strong class="warning">[not_shortcode]</strong>',
$this->parser->parse('[not_shortcode]') $this->parser->parse('[not_shortcode]')
); );
ShortcodeParser::$error_behavior = ShortcodeParser::LEAVE; ShortcodeParser::$error_behavior = ShortcodeParser::LEAVE;
$this->assertEquals('[not_shortcode]', $this->assertEquals('[not_shortcode]',
$this->parser->parse('[not_shortcode]')); $this->parser->parse('[not_shortcode]'));
$this->assertEquals('[not_shortcode /]', $this->assertEquals('[not_shortcode /]',
@ -41,6 +49,11 @@ class ShortcodeParserTest extends SapphireTest {
$this->parser->parse('[not_shortcode]a[/not_shortcode]')); $this->parser->parse('[not_shortcode]a[/not_shortcode]'));
$this->assertEquals('[/not_shortcode]', $this->assertEquals('[/not_shortcode]',
$this->parser->parse('[/not_shortcode]')); $this->parser->parse('[/not_shortcode]'));
$this->assertEquals(
'<img class="[not_shortcode]">',
$this->parser->parse('<img class="[not_shortcode]">')
);
} }
public function testSimpleTag() { public function testSimpleTag() {