-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] fixed generation of bbcode for uppercased tags #76
Open
frastel
wants to merge
1
commit into
jbowens:master
Choose a base branch
from
chefkoch-dev:fix/tag-normalization
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.') | ||
); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
<?php | ||
require_once dirname(__DIR__) . DIRECTORY_SEPARATOR . 'Parser.php'; | ||
require_once dirname(__DIR__) . DIRECTORY_SEPARATOR . 'Parser.php'; | ||
require_once dirname(__DIR__) . DIRECTORY_SEPARATOR . 'UppercasedCodeDefinitionSet.php'; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Strange, I needed this for Travis. It worked fine without it on my local setup.