-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed generation of bbcode for uppercased tags
- Loading branch information
Showing
5 changed files
with
260 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
namespace JBBCode; | ||
|
||
require_once 'CodeDefinition.php'; | ||
require_once 'CodeDefinitionBuilder.php'; | ||
require_once 'CodeDefinitionSet.php'; | ||
require_once 'validators/CssColorValidator.php'; | ||
require_once 'validators/UrlValidator.php'; | ||
|
||
/** | ||
* Provides a uppercased default set of common bbcode definitions. | ||
* | ||
* @author jbowens|frastel | ||
*/ | ||
class UppercasedCodeDefinitionSet implements CodeDefinitionSet | ||
{ | ||
|
||
/** @var CodeDefinition[] The default code definitions in this set. */ | ||
protected $definitions = array(); | ||
|
||
/** | ||
* Constructs the default code definitions. | ||
*/ | ||
public function __construct() | ||
{ | ||
/* [B] bold tag */ | ||
$builder = new CodeDefinitionBuilder('B', '<strong>{param}</strong>'); | ||
$this->definitions[] = $builder->build(); | ||
|
||
/* [I] italics tag */ | ||
$builder = new CodeDefinitionBuilder('I', '<em>{param}</em>'); | ||
$this->definitions[] = $builder->build(); | ||
|
||
/* [U] underline tag */ | ||
$builder = new CodeDefinitionBuilder('U', '<u>{param}</u>'); | ||
$this->definitions[] = $builder->build(); | ||
|
||
$urlValidator = new \JBBCode\validators\UrlValidator(); | ||
|
||
/* [URL] link tag */ | ||
$builder = new CodeDefinitionBuilder('URL', '<a href="{param}">{param}</a>'); | ||
$builder->setParseContent(false)->setBodyValidator($urlValidator); | ||
$this->definitions[] = $builder->build(); | ||
|
||
/* [URL=http://example.com] link tag */ | ||
$builder = new CodeDefinitionBuilder('URL', '<a href="{option}">{param}</a>'); | ||
$builder->setUseOption(true)->setParseContent(true)->setOptionValidator($urlValidator); | ||
$this->definitions[] = $builder->build(); | ||
|
||
/* [IMG] image tag */ | ||
$builder = new CodeDefinitionBuilder('IMG', '<img src="{param}" />'); | ||
$builder->setUseOption(false)->setParseContent(false)->setBodyValidator($urlValidator); | ||
$this->definitions[] = $builder->build(); | ||
|
||
/* [IMG=alt text] image tag */ | ||
$builder = new CodeDefinitionBuilder('IMG', '<img src="{param}" alt="{option}" />'); | ||
$builder->setUseOption(true)->setParseContent(false)->setBodyValidator($urlValidator); | ||
$this->definitions[] = $builder->build(); | ||
|
||
/* [COLOR] color tag */ | ||
$builder = new CodeDefinitionBuilder('COLOR', '<span style="color: {option}">{param}</span>'); | ||
$builder->setUseOption(true)->setOptionValidator(new \JBBCode\validators\CssColorValidator()); | ||
$this->definitions[] = $builder->build(); | ||
} | ||
|
||
/** | ||
* Returns an array of the default code definitions. | ||
* | ||
* @return CodeDefinition[] | ||
*/ | ||
public function getCodeDefinitions() | ||
{ | ||
return $this->definitions; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
<?php | ||
|
||
|
||
class ParsingUpperCaseTest extends PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var JBBCode\Parser | ||
*/ | ||
private $_parser; | ||
|
||
protected function setUp() | ||
{ | ||
$this->_parser = new JBBCode\Parser(); | ||
$this->_parser->addCodeDefinitionSet(new JBBCode\UppercasedCodeDefinitionSet()); | ||
} | ||
|
||
/** | ||
* @param string $code | ||
* @param string[] $expected | ||
* @dataProvider textCodeProvider | ||
*/ | ||
public function testParse($code, $expected) | ||
{ | ||
$parser = $this->_parser->parse($code); | ||
$this->assertEquals($expected['text'], $parser->getAsText()); | ||
$this->assertEquals($expected['html'], $parser->getAsHTML()); | ||
$this->assertEquals($expected['bbcode'], $parser->getAsBBCode()); | ||
} | ||
|
||
public function textCodeProvider() | ||
{ | ||
return array( | ||
array( | ||
'foo', | ||
array( | ||
'text' => 'foo', | ||
'html' => 'foo', | ||
'bbcode' => 'foo', | ||
) | ||
), | ||
array( | ||
'[B]this is bold[/B]', | ||
array( | ||
'text' => 'this is bold', | ||
'html' => '<strong>this is bold</strong>', | ||
'bbcode' => '[B]this is bold[/B]', | ||
) | ||
), | ||
array( | ||
'[B]this is bold', | ||
array( | ||
'text' => 'this is bold', | ||
'html' => '<strong>this is bold</strong>', | ||
'bbcode' => '[B]this is bold[/B]', | ||
) | ||
), | ||
array( | ||
'buffer text [B]this is bold[/B] buffer text', | ||
array( | ||
'text' => 'buffer text this is bold buffer text', | ||
'html' => 'buffer text <strong>this is bold</strong> buffer text', | ||
'bbcode' => 'buffer text [B]this is bold[/B] buffer text', | ||
) | ||
), | ||
array( | ||
'this is some text with [B]bold tags[/B] and [I]italics[/I] and things like [U]that[/U].', | ||
array( | ||
'text' => 'this is some text with bold tags and italics and things like that.', | ||
'html' => 'this is some text with <strong>bold tags</strong> and <em>italics</em> and things like <u>that</u>.', | ||
'bbcode' => 'this is some text with [B]bold tags[/B] and [I]italics[/I] and things like [U]that[/U].', | ||
) | ||
), | ||
array( | ||
'This contains a [URL=http://jbbcode.com]url[/URL] which uses an option.', | ||
array( | ||
'text' => 'This contains a url which uses an option.', | ||
'html' => 'This contains a <a href="http://jbbcode.com">url</a> which uses an option.', | ||
'bbcode' => 'This contains a [URL=http://jbbcode.com]url[/URL] which uses an option.', | ||
) | ||
), | ||
array( | ||
'This doesn\'t use the url option [URL]http://jbbcode.com[/URL].', | ||
array( | ||
'text' => 'This doesn\'t use the url option http://jbbcode.com.', | ||
'html' => 'This doesn\'t use the url option <a href="http://jbbcode.com">http://jbbcode.com</a>.', | ||
'bbcode' => 'This doesn\'t use the url option [URL]http://jbbcode.com[/URL].', | ||
) | ||
), | ||
); | ||
} | ||
|
||
/** | ||
* @param string $code | ||
* | ||
* @dataProvider textCodeProviderWithInvalidCode | ||
*/ | ||
public function testParseInvalidCode($code) | ||
{ | ||
$parser = $this->_parser->parse($code); | ||
$this->assertEquals($code, $parser->getAsText()); | ||
$this->assertEquals($code, $parser->getAsHTML()); | ||
$this->assertEquals($code, $parser->getAsBBCode()); | ||
} | ||
|
||
public function textCodeProviderWithInvalidCode() | ||
{ | ||
return array( | ||
array('This is some text with an [URL]I N V A L I D[/URL] URL tag.'), | ||
array('This is some text with an [URL foo=bar]INVALID[/URL] URL tag.'), | ||
array('This is some text with an invalid [URL=INVALID]URL[/URL] tag.') | ||
); | ||
} | ||
} |