]> gitweb.fluxo.info Git - lorea/elgg.git/commitdiff
Fixes #1479. Added ElggAutoP. Removing [\n\r] from test strings before compare to...
authorBrett Profitt <brett.profitt@gmail.com>
Mon, 10 Dec 2012 20:50:25 +0000 (15:50 -0500)
committerBrett Profitt <brett.profitt@gmail.com>
Mon, 10 Dec 2012 20:50:25 +0000 (15:50 -0500)
engine/classes/ElggAutoP.php [moved from engine/classes/ElggAutop.php with 95% similarity]
engine/lib/output.php
engine/tests/api/output.php

similarity index 95%
rename from engine/classes/ElggAutop.php
rename to engine/classes/ElggAutoP.php
index fa0c34225d838b27426302f412b20941577dd6af..89d77e583f6e240138efe6ca0360ac0679f0731a 100644 (file)
@@ -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();
index cce1c7cbab434a86822c794f81da893765c88554..bff0bf6e93f882113b258ae01f454a3fde1c3fa3 100644 (file)
@@ -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
  *
index eb1a66b298b9c71211e8d6f1bdff12000eef930f..c3d5aa8c6a082f3edf6c81ba0fa554f464fe8e19 100644 (file)
@@ -1,64 +1,74 @@
-<?php\r
-/**\r
- * Test case for ElggAutop functionality.\r
- * @author Steve Clay <steve@mrclay.org>\r
- */\r
-class ElggCoreOutputAutoPTest extends ElggCoreUnitTest {\r
-\r
-       /**\r
-        * @var ElggAutop\r
-        */\r
-       protected $_autop;\r
-\r
-       public function setUp() {\r
-               $this->_autop = new ElggAutop();\r
-       }\r
-       \r
-       public function testDomRoundtrip()\r
-       {\r
-               $d = dir(dirname(__DIR__) . '/test_files/output/autop');\r
-               $in = file_get_contents($d->path . "/domdoc_in.html");\r
-               $exp = file_get_contents($d->path . "/domdoc_exp.html");\r
-\r
-               $doc = new DOMDocument();\r
-               libxml_use_internal_errors(true);\r
-               $doc->loadHTML("<html><meta http-equiv='content-type' content='text/html; charset=utf-8'><body>"\r
-                               . $in . '</body></html>');\r
-               $serialized = $doc->saveHTML();\r
-               list(,$out) = explode('<body>', $serialized, 2);\r
-               list($out) = explode('</body>', $out, 2);\r
-\r
-               $this->assertEqual($exp, $out, "DOMDocument's parsing/serialization roundtrip");\r
-       }\r
-\r
-       public function testProcess()\r
-       {\r
-               $data = $this->provider();\r
-               foreach ($data as $row) {\r
-                       list($test, $in, $exp) = $row;\r
-                       $out = $this->_autop->process($in);\r
-                       $this->assertEqual($exp, $out, "Equality case {$test}");\r
-               }\r
-       }\r
-\r
-       public function provider()\r
-       {\r
-               $d = dir(dirname(__DIR__) . '/test_files/output/autop');\r
-               $tests = array();\r
-               while (false !== ($entry = $d->read())) {\r
-                       if (preg_match('/^([a-z\\-]+)\.in\.html$/i', $entry, $m)) {\r
-                               $tests[] = $m[1];\r
-                       }\r
-               }\r
-\r
-               $data = array();\r
-               foreach ($tests as $test) {\r
-                       $data[] = array(\r
-                               $test,\r
-                               file_get_contents($d->path . '/' . "{$test}.in.html"),\r
-                               file_get_contents($d->path . '/' . "{$test}.exp.html"),\r
-                       );\r
-               }\r
-               return $data;\r
-       }\r
-}\r
+<?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