diff options
-rw-r--r-- | engine/classes/ElggAutoP.php (renamed from engine/classes/ElggAutop.php) | 30 | ||||
-rw-r--r-- | engine/lib/output.php | 11 | ||||
-rw-r--r-- | engine/tests/api/output.php | 138 |
3 files changed, 92 insertions, 87 deletions
diff --git a/engine/classes/ElggAutop.php b/engine/classes/ElggAutoP.php index fa0c34225..89d77e583 100644 --- a/engine/classes/ElggAutop.php +++ b/engine/classes/ElggAutoP.php @@ -7,11 +7,8 @@ * * In DIV elements, Ps are only added when there would be at * least two of them. - * - * @author Steve Clay <steve@mrclay.org> - * @license http://www.opensource.org/licenses/mit-license.php MIT License */ -class ElggAutop { +class ElggAutoP { public $encoding = 'UTF-8'; @@ -56,8 +53,7 @@ class ElggAutop { protected $_unique = ''; - public function __construct() - { + public function __construct() { $this->_blocks = preg_split('@\\s+@', $this->_blocks); $this->_descendList = preg_split('@\\s+@', $this->_descendList); $this->_alterList = preg_split('@\\s+@', $this->_alterList); @@ -67,13 +63,13 @@ class ElggAutop { /** * Intance of class for singleton pattern. - * @var ElggAutop + * @var ElggAutoP */ private static $instance; /** * Singleton pattern. - * @return ElggAutop + * @return ElggAutoP */ public static function getInstance() { $className = __CLASS__; @@ -94,8 +90,7 @@ class ElggAutop { * @param string $html snippet * @return string|false output or false if parse error occurred */ - public function process($html) - { + public function process($html) { // normalize whitespace $html = str_replace(array("\r\n", "\r"), "\n", $html); @@ -107,7 +102,8 @@ class ElggAutop { // parse to DOM, suppressing loadHTML warnings // http://www.php.net/manual/en/domdocument.loadhtml.php#95463 libxml_use_internal_errors(true); - if (! @$this->_doc->loadHTML("<html><meta http-equiv='content-type' " + + if (!$this->_doc->loadHTML("<html><meta http-equiv='content-type' " . "content='text/html; charset={$this->encoding}'><body>{$html}</body>" . "</html>")) { return false; @@ -130,7 +126,7 @@ class ElggAutop { // re-parse so we can handle new AUTOP elements - if (! @$this->_doc->loadHTML($html)) { + if (!$this->_doc->loadHTML($html)) { return false; } // must re-create XPath object after DOM load @@ -149,14 +145,13 @@ class ElggAutop { } } } - if (! $hasContent) { + if (!$hasContent) { // strip w/ preg_replace later (faster than moving nodes out) $autop->setAttribute("r", "1"); } } // remove a single AUTOP inside certain elements - foreach ($this->_xpath->query('//div') as $el) { $autops = $this->_xpath->query('./autop', $el); if ($autops->length === 1) { @@ -164,9 +159,9 @@ class ElggAutop { $autops->item(0)->setAttribute("r", "1"); } } - + $html = $this->_doc->saveHTML(); - + // trim to the contents of BODY $bodyStart = strpos($html, '<body>'); $bodyEnd = strpos($html, '</body>', $bodyStart + 6); @@ -189,8 +184,7 @@ class ElggAutop { * * @param DOMElement $el */ - protected function _addParagraphs(DOMElement $el) - { + protected function _addParagraphs(DOMElement $el) { // no need to recurse, just queue up $elsToProcess = array($el); $inlinesToProcess = array(); diff --git a/engine/lib/output.php b/engine/lib/output.php index cce1c7cba..bff0bf6e9 100644 --- a/engine/lib/output.php +++ b/engine/lib/output.php @@ -16,7 +16,7 @@ **/ function parse_urls($text) { // @todo this causes problems with <attr = "val"> - // must be ing <attr="val"> format (no space). + // must be in <attr="val"> format (no space). // By default htmlawed rewrites tags to this format. // if PHP supported conditional negative lookbehinds we could use this: // $r = preg_replace_callback('/(?<!=)(?<![ ])?(?<!["\'])((ht|f)tps?:\/\/[^\s\r\n\t<>"\'\!\(\),]+)/i', @@ -46,6 +46,7 @@ function parse_urls($text) { * * @param string $pee The string * @deprecated Use elgg_autop instead + * @todo Add deprecation warning in 1.9 * * @return string **/ @@ -56,12 +57,12 @@ function autop($pee) { /** * Create paragraphs from text with line spacing * - * @param string $pee The string + * @param string $string The string * * @return string **/ -function elgg_autop($pee) { - return ElggAutop::getInstance()->process($pee); +function elgg_autop($string) { + return ElggAutoP::getInstance()->process($string); } /** @@ -358,7 +359,7 @@ function elgg_get_friendly_time($time) { /** * Strip tags and offer plugins the chance. * Plugins register for output:strip_tags plugin hook. - * Original string included in $params['original_string'] + * Original string included in $params['original_string'] * * @param string $string Formatted string * diff --git a/engine/tests/api/output.php b/engine/tests/api/output.php index eb1a66b29..c3d5aa8c6 100644 --- a/engine/tests/api/output.php +++ b/engine/tests/api/output.php @@ -1,64 +1,74 @@ -<?php
-/**
- * Test case for ElggAutop functionality.
- * @author Steve Clay <steve@mrclay.org>
- */
-class ElggCoreOutputAutoPTest extends ElggCoreUnitTest {
-
- /**
- * @var ElggAutop
- */
- protected $_autop;
-
- public function setUp() {
- $this->_autop = new ElggAutop();
- }
-
- public function testDomRoundtrip()
- {
- $d = dir(dirname(__DIR__) . '/test_files/output/autop');
- $in = file_get_contents($d->path . "/domdoc_in.html");
- $exp = file_get_contents($d->path . "/domdoc_exp.html");
-
- $doc = new DOMDocument();
- libxml_use_internal_errors(true);
- $doc->loadHTML("<html><meta http-equiv='content-type' content='text/html; charset=utf-8'><body>"
- . $in . '</body></html>');
- $serialized = $doc->saveHTML();
- list(,$out) = explode('<body>', $serialized, 2);
- list($out) = explode('</body>', $out, 2);
-
- $this->assertEqual($exp, $out, "DOMDocument's parsing/serialization roundtrip");
- }
-
- public function testProcess()
- {
- $data = $this->provider();
- foreach ($data as $row) {
- list($test, $in, $exp) = $row;
- $out = $this->_autop->process($in);
- $this->assertEqual($exp, $out, "Equality case {$test}");
- }
- }
-
- public function provider()
- {
- $d = dir(dirname(__DIR__) . '/test_files/output/autop');
- $tests = array();
- while (false !== ($entry = $d->read())) {
- if (preg_match('/^([a-z\\-]+)\.in\.html$/i', $entry, $m)) {
- $tests[] = $m[1];
- }
- }
-
- $data = array();
- foreach ($tests as $test) {
- $data[] = array(
- $test,
- file_get_contents($d->path . '/' . "{$test}.in.html"),
- file_get_contents($d->path . '/' . "{$test}.exp.html"),
- );
- }
- return $data;
- }
-}
+<?php +/** + * Test case for ElggAutoP functionality. + */ +class ElggCoreOutputAutoPTest extends ElggCoreUnitTest { + + /** + * @var ElggAutoP + */ + protected $_autop; + + public function setUp() { + $this->_autop = new ElggAutoP(); + } + + public function testDomRoundtrip() { + $d = dir(dirname(dirname(__FILE__)) . '/test_files/output/autop'); + $in = file_get_contents($d->path . "/domdoc_in.html"); + $exp = file_get_contents($d->path . "/domdoc_exp.html"); + $exp = $this->flattenString($exp); + + $doc = new DOMDocument(); + libxml_use_internal_errors(true); + $doc->loadHTML("<html><meta http-equiv='content-type' content='text/html; charset=utf-8'><body>" + . $in . '</body></html>'); + $serialized = $doc->saveHTML(); + list(,$out) = explode('<body>', $serialized, 2); + list($out) = explode('</body>', $out, 2); + $out = $this->flattenString($out); + + $this->assertEqual($exp, $out, "DOMDocument's parsing/serialization roundtrip"); + } + + public function testProcess() { + $data = $this->provider(); + foreach ($data as $row) { + list($test, $in, $exp) = $row; + $exp = $this->flattenString($exp); + $out = $this->_autop->process($in); + $out = $this->flattenString($out); + + $this->assertEqual($exp, $out, "Equality case {$test}"); + } + } + + public function provider() { + $d = dir(dirname(dirname(__FILE__)) . '/test_files/output/autop'); + $tests = array(); + while (false !== ($entry = $d->read())) { + if (preg_match('/^([a-z\\-]+)\.in\.html$/i', $entry, $m)) { + $tests[] = $m[1]; + } + } + + $data = array(); + foreach ($tests as $test) { + $data[] = array( + $test, + file_get_contents($d->path . '/' . "{$test}.in.html"), + file_get_contents($d->path . '/' . "{$test}.exp.html"), + ); + } + return $data; + } + + /** + * Different versions of PHP return different whitespace between tags. + * Removing all line breaks normalizes that. + */ + public function flattenString($string) { + $r = preg_replace('/[\n\r]+/', '', $string); + return $r; + } +}
\ No newline at end of file |