Permalink
Browse files

Unit tested all main functions, text/raw/binary/script/style/cdata; f…

…ixed bugs
  • Loading branch information...
1 parent 315ce44 commit 976e4387812fd23d1de3846c30479ef873d2e4c4 @tenorviol committed Nov 12, 2012
Showing with 42 additions and 12 deletions.
  1. +11 −10 out.php
  2. +31 −2 test/outTest.php
View
@@ -5,37 +5,38 @@
const REPLACEMENT_CHARACTER = "\xEF\xBF\xBD";
function text($s) {
- echo htmlentities($s, ENT_QUOTES, 'UTF-8');
+ $s = replace_control_characters($s);
+ echo htmlentities($s, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
}
function raw($s) {
- echo htmlentities($s, 0, 'UTF-8');
+ $s = replace_non_utf8($s);
echo $s;
}
-function bin($s) {
+function binary($s) {
echo $s;
}
function script($s) {
- if (str_pos($s, '</script') !== false) {
+ if (strpos($s, '</script') !== false) {
throw new RuntimeException('HTML script elements cannot contain "</script"');
}
- echo $s;
+ raw($s);
}
function style($s) {
- if (str_pos($s, '</style') !== false) {
+ if (strpos($s, '</style') !== false) {
throw new RuntimeException('HTML style elements cannot contain "</style"');
}
- echo $s;
+ raw($s);
}
function cdata($s) {
- if (str_pos($s, ']]>') !== false) {
+ if (strpos($s, ']]>') !== false) {
throw new RuntimeException('CDATA elements cannot contain "]]>"');
}
- echo $s;
+ raw($s);
}
function replace_non_utf8($s) {
@@ -60,7 +61,7 @@ function replace_non_utf8($s) {
}, $s);
}
-function replace_control_chars($s) {
+function replace_control_characters($s) {
$re = '/( [\x00-\x08]
| \x0B
| [\x0E-\x1F]
View
@@ -4,6 +4,35 @@
class outTest extends PHPUnit_Framework_TestCase {
+ public function outProvider() {
+ $R = out\REPLACEMENT_CHARACTER;
+ return array(
+ array('', 'text', ''),
+ array('', 'raw', ''),
+ array('', 'binary', ''),
+ array('', 'script', ''),
+ array('', 'style', ''),
+ array('', 'cdata', ''),
+ array("<>&'\" \x7F \xFF\n\xC0\x80 </script </style ]]> ", 'text', "&lt;&gt;&amp;&#039;&quot; $R $R\n$R$R &lt;/script &lt;/style ]]&gt; "),
+ array("<>&'\" \x7F \xFF\n\xC0\x80 </script </style ]]> ", 'raw', "<>&'\" \x7F $R\n$R$R </script </style ]]> "),
+ array("<>&'\" \x7F \xFF\n\xC0\x80 </script </style ]]> ", 'binary', "<>&'\" \x7F \xFF\n\xC0\x80 </script </style ]]> "),
+ array("<>&'\" \x7F \xFF\n\xC0\x80 </scr ipt </style ]]>", 'script', "<>&'\" \x7F $R\n$R$R </scr ipt </style ]]>"),
+ array("<>&'\" \x7F \xFF\n\xC0\x80 </script </st yle ]]>", 'style', "<>&'\" \x7F $R\n$R$R </script </st yle ]]>"),
+ array("<>&'\" \x7F \xFF\n\xC0\x80 </script </style ]] >", 'cdata', "<>&'\" \x7F $R\n$R$R </script </style ]] >"),
+ );
+ }
+
+ /**
+ * @dataProvider outProvider
+ */
+ public function test($s, $func, $expect) {
+ $outfunc = "out\\$func";
+ ob_start();
+ $outfunc($s);
+ $result = ob_get_clean();
+ $this->assertEquals($expect, $result);
+ }
+
public function utf8Provider() {
$R = out\REPLACEMENT_CHARACTER;
return array(
@@ -217,11 +246,11 @@ public function controlProvider() {
/**
* @dataProvider controlProvider
*/
- public function test_replace_control_chars($s, $expect = null) {
+ public function test_replace_control_characters($s, $expect = null) {
if ($expect === null) {
$expect = $s;
}
- $result = out\replace_control_chars($s);
+ $result = out\replace_control_characters($s);
$this->assertEquals($expect, $result, 's='.urlencode($s) . "\nexpect=".urlencode($expect) . "\nresult=".urlencode($result));
}
}

0 comments on commit 976e438

Please sign in to comment.