]> git.mxchange.org Git - friendica.git/blobdiff - src/Content/Text/HTML.php
Merge pull request #11230 from annando/account-type
[friendica.git] / src / Content / Text / HTML.php
index c77b84db8ab47a485f7219cdea899c2dfc673d11..ae9452abb0fd7d273be064048efb87346ad726dd 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2022, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -143,6 +143,7 @@ class HTML
         */
        public static function toBBCode($message, $basepath = '')
        {
+               DI::profiler()->startRecording('rendering');
                $message = str_replace("\r", "", $message);
 
                $message = Strings::performWithEscapedBlocks($message, '#<pre><code.*</code></pre>#iUs', function ($message) {
@@ -385,7 +386,7 @@ class HTML
                                        $prefix = '[code=' . $matches[1] . ']';
                                }
 
-                               return $prefix . PHP_EOL . trim($matches[2]) . PHP_EOL . '[/code]';
+                               return $prefix . "\n" . html_entity_decode($matches[2]) . "\n" . '[/code]';
                        },
                        $message
                );
@@ -396,6 +397,7 @@ class HTML
                        $message = self::qualifyURLs($message, $basepath);
                }
 
+               DI::profiler()->stopRecording();
                return $message;
        }
 
@@ -585,6 +587,7 @@ class HTML
         */
        public static function toPlaintext(string $html, $wraplength = 75, $compact = false)
        {
+               DI::profiler()->startRecording('rendering');
                $message = str_replace("\r", "", $html);
 
                $doc = new DOMDocument();
@@ -593,6 +596,7 @@ class HTML
                $message = mb_convert_encoding($message, 'HTML-ENTITIES', "UTF-8");
 
                if (empty($message)) {
+                       DI::profiler()->stopRecording();
                        return '';
                }
 
@@ -605,6 +609,11 @@ class HTML
                // Collecting all links
                $urls = self::collectURLs($message);
 
+               if (empty($message)) {
+                       DI::profiler()->stopRecording();
+                       return '';
+               }
+
                @$doc->loadHTML($message, LIBXML_HTML_NODEFDTD);
 
                self::tagToBBCode($doc, 'html', [], '', '');
@@ -685,6 +694,7 @@ class HTML
 
                $message = self::quoteLevel(trim($message), $wraplength);
 
+               DI::profiler()->stopRecording();
                return trim($message);
        }
 
@@ -697,9 +707,11 @@ class HTML
         */
        public static function toMarkdown($html)
        {
+               DI::profiler()->startRecording('rendering');
                $converter = new HtmlConverter(['hard_break' => true]);
                $markdown = $converter->convert($html);
 
+               DI::profiler()->stopRecording();
                return $markdown;
        }
 
@@ -772,16 +784,6 @@ class HTML
                return $text;
        }
 
-       /**
-        * return div element with class 'clear'
-        * @return string
-        * @deprecated
-        */
-       public static function clearDiv()
-       {
-               return '<div class="clear"></div>';
-       }
-
        /**
         * Loader for infinite scrolling
         *
@@ -797,22 +799,6 @@ class HTML
                ]);
        }
 
-       /**
-        * Get html for contact block.
-        *
-        * @deprecated since version 2019.03
-        * @see ContactBlock::getHTML()
-        * @return string
-        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
-        * @throws \ImagickException
-        */
-       public static function contactBlock()
-       {
-               $a = DI::app();
-
-               return ContactBlock::getHTML($a->profile);
-       }
-
        /**
         * Format contacts as picture links or as text links
         *
@@ -998,25 +984,32 @@ class HTML
                $config->set('Attr.AllowedRel', [
                        'noreferrer' => true,
                        'noopener' => true,
+                       'tag' => true,
                ]);
                $config->set('Attr.AllowedFrameTargets', [
                        '_blank' => true,
                ]);
 
-               /* Uncomment to debug HTMLPurifier behavior
-               $config->set('Core.CollectErrors', true);
-               $config->set('Core.MaintainLineNumbers', true);
-               */
+               $config->set('AutoFormat.RemoveEmpty.Predicate', [
+                       'colgroup' => [],        // |
+                       'th'       => [],        // |
+                       'td'       => [],        // |
+                       'iframe'   => ['src'],   // ↳ Default HTMLPurify values
+                       'i'        => ['class'], // Allows forkawesome icons
+               ]);
+
+               // Uncomment to debug HTMLPurifier behavior
+               //$config->set('Core.CollectErrors', true);
+               //$config->set('Core.MaintainLineNumbers', true);
 
                $HTMLPurifier = new \HTMLPurifier($config);
 
                $text = $HTMLPurifier->purify($text);
 
                /** @var \HTMLPurifier_ErrorCollector $errorCollector */
-               /* Uncomment to debug HTML Purifier behavior
-               $errorCollector = $HTMLPurifier->context->get('ErrorCollector');
-               var_dump($errorCollector->getRaw());
-               */
+               // Uncomment to debug HTML Purifier behavior
+               //$errorCollector = $HTMLPurifier->context->get('ErrorCollector');
+               //var_dump($errorCollector->getRaw());
 
                return $text;
        }