Skip to content

Commit 645b6a1

Browse files
author
Jackson Owens
committed
Merge pull request #27 from astax-t/master
Fixed namespace for exception
2 parents ae932fe + 0aeb0bf commit 645b6a1

File tree

3 files changed

+132
-3
lines changed

3 files changed

+132
-3
lines changed

JBBCode/CodeDefinitionBuilder.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,12 @@ public function setParseContent($parseContent)
8484
* Sets the nest limit for this code definition.
8585
*
8686
* @param $nestLimit a positive integer, or -1 if there is no limit.
87-
* @throws InvalidArgumentException if the nest limit is invalid
87+
* @throws \InvalidArgumentException if the nest limit is invalid
8888
*/
8989
public function setNestLimit($limit)
9090
{
9191
if(!is_int($limit) || ($limit <= 0 && -1 != $limit)) {
92-
throw new InvalidArgumentException("A nest limit must be a positive integer " .
92+
throw new \InvalidArgumentException("A nest limit must be a positive integer " .
9393
"or -1.");
9494
}
9595
$this->nestLimit = $limit;
@@ -138,7 +138,7 @@ public function removeBodyValidator()
138138
$this->bodyValidator = null;
139139
return $this;
140140
}
141-
141+
142142
/**
143143
* Builds a CodeDefinition with the current state of the builder.
144144
*

JBBCode/tests/HTMLSafeTest.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
require_once(dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'Parser.php');
4+
5+
/**
6+
* Test cases testing the HTMLSafe visitor, which escapes all html characters in the source text
7+
*
8+
* @author astax-t
9+
*/
10+
class HTMLSafeTest extends PHPUnit_Framework_TestCase
11+
{
12+
/**
13+
* Asserts that the given bbcode string produces the given html string
14+
* when parsed with the default bbcodes.
15+
*/
16+
public function assertProduces($bbcode, $html)
17+
{
18+
$parser = new \JBBCode\Parser();
19+
$parser->addCodeDefinitionSet(new JBBCode\DefaultCodeDefinitionSet());
20+
$parser->parse($bbcode);
21+
22+
$htmlsafer = new JBBCode\visitors\HTMLSafeVisitor();
23+
$parser->accept($htmlsafer);
24+
25+
$this->assertEquals($html, $parser->getAsHtml());
26+
}
27+
28+
/**
29+
* Tests escaping quotes and ampersands in simple text
30+
*/
31+
public function testQuoteAndAmp()
32+
{
33+
$this->assertProduces('te"xt te&xt', 'te&quot;xt te&amp;xt');
34+
}
35+
36+
/**
37+
* Tests escaping quotes and ampersands inside a BBCode tag
38+
*/
39+
public function testQuoteAndAmpInTag()
40+
{
41+
$this->assertProduces('[b]te"xt te&xt[/b]', '<strong>te&quot;xt te&amp;xt</strong>');
42+
}
43+
44+
/**
45+
* Tests escaping HTML tags
46+
*/
47+
public function testHtmlTag()
48+
{
49+
$this->assertProduces('<b>not bold</b>', '&lt;b&gt;not bold&lt;/b&gt;');
50+
$this->assertProduces('[b]<b>bold</b>[/b] <hr>', '<strong>&lt;b&gt;bold&lt;/b&gt;</strong> &lt;hr&gt;');
51+
}
52+
53+
/**
54+
* Tests escaping ampersands in URL using [url]...[/url]
55+
*/
56+
public function testUrlParam()
57+
{
58+
$this->assertProduces('text [url]http://example.com/?a=b&c=d[/url] more text', 'text <a href="http://example.com/?a=b&amp;c=d">http://example.com/?a=b&amp;c=d</a> more text');
59+
}
60+
61+
/**
62+
* Tests escaping ampersands in URL using [url=...] tag
63+
*/
64+
public function testUrlOption()
65+
{
66+
$this->assertProduces('text [url=http://example.com/?a=b&c=d]this is a "link"[/url]', 'text <a href="http://example.com/?a=b&amp;c=d">this is a &quot;link&quot;</a>');
67+
}
68+
69+
/**
70+
* Tests escaping ampersands in URL using [url=...] tag when URL is in quotes
71+
*/
72+
public function testUrlOptionQuotes()
73+
{
74+
$this->assertProduces('text [url="http://example.com/?a=b&c=d"]this is a "link"[/url]', 'text <a href="http://example.com/?a=b&amp;c=d">this is a &quot;link&quot;</a>');
75+
}
76+
77+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace JBBCode\visitors;
4+
5+
/**
6+
* This visitor escapes html content of all strings and attributes
7+
*
8+
* @author Alexander Polyanskikh
9+
*/
10+
class HTMLSafeVisitor implements \JBBCode\NodeVisitor
11+
{
12+
public function visitDocumentElement(\JBBCode\DocumentElement $documentElement)
13+
{
14+
foreach ($documentElement->getChildren() as $child) {
15+
$child->accept($this);
16+
}
17+
}
18+
19+
public function visitTextNode(\JBBCode\TextNode $textNode)
20+
{
21+
$textNode->setValue($this->htmlSafe($textNode->getValue()));
22+
}
23+
24+
public function visitElementNode(\JBBCode\ElementNode $elementNode)
25+
{
26+
$attrs = $elementNode->getAttribute();
27+
if (is_array($attrs))
28+
{
29+
foreach ($attrs as &$el)
30+
$el = $this->htmlSafe($el);
31+
32+
$elementNode->setAttribute($attrs);
33+
}
34+
35+
foreach ($elementNode->getChildren() as $child) {
36+
$child->accept($this);
37+
}
38+
}
39+
40+
protected function htmlSafe($str, $options = null)
41+
{
42+
if (is_null($options))
43+
{
44+
if (defined('ENT_DISALLOWED'))
45+
$options = ENT_QUOTES | ENT_DISALLOWED | ENT_HTML401; // PHP 5.4+
46+
else
47+
$options = ENT_QUOTES; // PHP 5.3
48+
}
49+
50+
return htmlspecialchars($str, $options, 'UTF-8');
51+
}
52+
}

0 commit comments

Comments
 (0)