-
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.
Merge pull request #27 from astax-t/master
Fixed namespace for exception
- Loading branch information
Showing
3 changed files
with
132 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
require_once(dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'Parser.php'); | ||
|
||
/** | ||
* Test cases testing the HTMLSafe visitor, which escapes all html characters in the source text | ||
* | ||
* @author astax-t | ||
*/ | ||
class HTMLSafeTest extends PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* Asserts that the given bbcode string produces the given html string | ||
* when parsed with the default bbcodes. | ||
*/ | ||
public function assertProduces($bbcode, $html) | ||
{ | ||
$parser = new \JBBCode\Parser(); | ||
$parser->addCodeDefinitionSet(new JBBCode\DefaultCodeDefinitionSet()); | ||
$parser->parse($bbcode); | ||
|
||
$htmlsafer = new JBBCode\visitors\HTMLSafeVisitor(); | ||
$parser->accept($htmlsafer); | ||
|
||
$this->assertEquals($html, $parser->getAsHtml()); | ||
} | ||
|
||
/** | ||
* Tests escaping quotes and ampersands in simple text | ||
*/ | ||
public function testQuoteAndAmp() | ||
{ | ||
$this->assertProduces('te"xt te&xt', 'te"xt te&xt'); | ||
} | ||
|
||
/** | ||
* Tests escaping quotes and ampersands inside a BBCode tag | ||
*/ | ||
public function testQuoteAndAmpInTag() | ||
{ | ||
$this->assertProduces('[b]te"xt te&xt[/b]', '<strong>te"xt te&xt</strong>'); | ||
} | ||
|
||
/** | ||
* Tests escaping HTML tags | ||
*/ | ||
public function testHtmlTag() | ||
{ | ||
$this->assertProduces('<b>not bold</b>', '<b>not bold</b>'); | ||
$this->assertProduces('[b]<b>bold</b>[/b] <hr>', '<strong><b>bold</b></strong> <hr>'); | ||
} | ||
|
||
/** | ||
* Tests escaping ampersands in URL using [url]...[/url] | ||
*/ | ||
public function testUrlParam() | ||
{ | ||
$this->assertProduces('text [url]http://example.com/?a=b&c=d[/url] more text', 'text <a href="http://example.com/?a=b&c=d">http://example.com/?a=b&c=d</a> more text'); | ||
} | ||
|
||
/** | ||
* Tests escaping ampersands in URL using [url=...] tag | ||
*/ | ||
public function testUrlOption() | ||
{ | ||
$this->assertProduces('text [url=http://example.com/?a=b&c=d]this is a "link"[/url]', 'text <a href="http://example.com/?a=b&c=d">this is a "link"</a>'); | ||
} | ||
|
||
/** | ||
* Tests escaping ampersands in URL using [url=...] tag when URL is in quotes | ||
*/ | ||
public function testUrlOptionQuotes() | ||
{ | ||
$this->assertProduces('text [url="http://example.com/?a=b&c=d"]this is a "link"[/url]', 'text <a href="http://example.com/?a=b&c=d">this is a "link"</a>'); | ||
} | ||
|
||
} |
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,52 @@ | ||
<?php | ||
|
||
namespace JBBCode\visitors; | ||
|
||
/** | ||
* This visitor escapes html content of all strings and attributes | ||
* | ||
* @author Alexander Polyanskikh | ||
*/ | ||
class HTMLSafeVisitor implements \JBBCode\NodeVisitor | ||
{ | ||
public function visitDocumentElement(\JBBCode\DocumentElement $documentElement) | ||
{ | ||
foreach ($documentElement->getChildren() as $child) { | ||
$child->accept($this); | ||
} | ||
} | ||
|
||
public function visitTextNode(\JBBCode\TextNode $textNode) | ||
{ | ||
$textNode->setValue($this->htmlSafe($textNode->getValue())); | ||
} | ||
|
||
public function visitElementNode(\JBBCode\ElementNode $elementNode) | ||
{ | ||
$attrs = $elementNode->getAttribute(); | ||
if (is_array($attrs)) | ||
{ | ||
foreach ($attrs as &$el) | ||
$el = $this->htmlSafe($el); | ||
|
||
$elementNode->setAttribute($attrs); | ||
} | ||
|
||
foreach ($elementNode->getChildren() as $child) { | ||
$child->accept($this); | ||
} | ||
} | ||
|
||
protected function htmlSafe($str, $options = null) | ||
{ | ||
if (is_null($options)) | ||
{ | ||
if (defined('ENT_DISALLOWED')) | ||
$options = ENT_QUOTES | ENT_DISALLOWED | ENT_HTML401; // PHP 5.4+ | ||
else | ||
$options = ENT_QUOTES; // PHP 5.3 | ||
} | ||
|
||
return htmlspecialchars($str, $options, 'UTF-8'); | ||
} | ||
} |