Replaces TinyMCE's emoticon images with plain-text representation

@param string $html HTML content

@return string HTML content
484697
public static function icons2text($html)
    {
        $emoticons = array(
            '8-)' => 'smiley-cool',
            ':-#' => 'smiley-foot-in-mouth',
            ':-*' => 'smiley-kiss',
            ':-X' => 'smiley-sealed',
            ':-P' => 'smiley-tongue-out',
            ':-@' => 'smiley-yell',
            ":'(" => 'smiley-cry',
            ':-(' => 'smiley-frown',
            ':-D' => 'smiley-laughing',
            ':-)' => 'smiley-smile',
            ':-S' => 'smiley-undecided',
            ':-$' => 'smiley-embarassed',
            'O:-)' => 'smiley-innocent',
            ':-|' => 'smiley-money-mouth',
            ':-O' => 'smiley-surprised',
            ';-)' => 'smiley-wink',
        );

        foreach ($emoticons as $idx => $file) {
            // <img title="Cry" src="http://.../program/js/tinymce/plugins/emoticons/img/smiley-cry.gif" border="0" alt="Cry" />
            $file      = preg_quote(self::IMG_PATH . $file . '.gif', '/');
            $search[]  = '/<img (title="[a-z ]+" )?src="[^"]+' . $file . '"[^>]+\/>/i';
            $replace[] = $idx;
        }

        return preg_replace($search, $replace, $html);
    }
286854
public function html($html = null)
    {
        if ($html === null) {
            return $this->_html;
        }
        $this->_html = $html;

        return $this;
    }
1065706
public function parseContent( $html ){
		$pattern = '~<p>ADDTEMPLATE\(([\w_:-]*)\):([\w_-]+):ETALPMETDDA<\/p>~m';
		if( preg_match_all($pattern, $html, $matches, PREG_SET_ORDER) ){
			foreach($matches as $match){
				//if a zone is specified, attach the template
				if(!empty($match[1])){
					$this->getLayout()->addTemplateTo($match[1], $match[2]);
					$html = str_replace($match[0], '', $html);
				}else{
				//otherwise inject the template inline into the wikitext
					$html = str_replace($match[0], $this->getLayout()->renderTemplate($match[2]), $html);
				}
			}
		}
		return $html;
	}
415327
protected function tidy( $html ) {

		// Make XHTML 1.1 strict using htmlLawed

		$html = \Pressbooks\Interactive\Content::init()->replaceInteractiveTags( $html );

		$config = [
			'valid_xhtml' => 1,
			'unique_ids' => 'fixme-',
			'hook' => '\Pressbooks\Sanitize\html5_to_xhtml11',
			'tidy' => -1,
		];

		return \Pressbooks\HtmLawed::filter( $html, $config );
	}
1252196
protected function buildText(string $html): string
    {
        $text = Strings::replace($html, [
            '#<(style|script|head).*</\\1>#Uis' => '',
            '#<t[dh][ >]#i' => ' $0',
            '#<a\s[^>]*href=(?|"([^"]+)"|\'([^\']+)\')[^>]*>(.*?)</a>#is' => '$2 &lt;$1&gt;',
            '#[\r\n]+#' => ' ',
            '#<(/?p|/?h\d|li|br|/tr)[ >/]#i' => "\n$0",
        ]);
        $text = \html_entity_decode(\strip_tags($text), ENT_QUOTES, 'UTF-8');
        $text = Strings::replace($text, '#[ \t]+#', ' ');

        return \trim($text);
    }
375578
protected function _convertUrls($html) {
		$fullBaseUrl = Configure::read('App.fullBaseUrl');
		if (!empty($fullBaseUrl)) {
			$html = preg_replace('~(?:src|action|href)=[\'"]\K/(?!/)[^\'"]*~', "$fullBaseUrl$0", $html);
		}

		return $html;
	}
1314865
private function minifyHTML($html)
    {
        $search = array('/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?<!\:|\\\|\'|\")\/\/.*))/', '/\n/', '/\>[^\S ]+/s', '/[^\S ]+\</s', '/(\s)+/s', '/<!--.*?-->/');
        $replace = array(' ', ' ', '>', '<', '\\1', '');

        $squeezedHTML = preg_replace($search, $replace, $html);

        return $squeezedHTML;

    }
107596
protected function removeEmptyTags($html)
    {
        if (!\is_string($html)) {
            return $html;
        }
        /*
            Toss empty tags such as label or legend
            keep empty textarea
        */
        $html = \preg_replace('#^\s*<(div|label|legend|span)\b[^>]*>\s*</\1>\n#m', '', $html);
        $html = \preg_replace('#^\s*\n#m', '', $html);  // toss empty lines
        $html = \preg_replace('#<(\w+) >#', '<$1>', $html);   // <tab > ->  <tab>
        return $html;
    }
233870
public function transform($html)
    {
        if (class_exists('HTMLPurifier')) {
            $config = HTMLPurifier_Config::createDefault();
            $purifier = new HTMLPurifier($config);
            $cleanHtml = $purifier->purify($html);
        } else {
            $cleanHtml = strip_tags($html, '<a><b><br><i><p><s><u>');
        }

        return $cleanHtml;
    }
1051858
public function processHtml($html)
    {
        if (!is_string($html)) {
            return $html;
        }

        $mediaTags = $this->extractMediaTags($html);
        foreach ($mediaTags as $tagInfo) {
            $pattern = preg_quote($tagInfo['declaration']);
            $generatedMarkup = $this->generateMediaTagMarkup($tagInfo['type'], $tagInfo['src']);
            $html = mb_ereg_replace($pattern, $generatedMarkup, $html);
        }

        return $html;
    }
700342
private function cleanupXss($html)
    {
        if (false === $this->config['xss_filter']) {
            return $html;
        }

        $this->logger->info('Filtering HTML to remove XSS');

        return htmLawed(
            $html,
            [
                'safe' => 1,
                // *+iframe: do not remove iframe elements
                'elements' => '*+iframe-meta',
                'deny_attribute' => 'style',
                'comment' => 1,
                'cdata' => 1,
            ]
        );
    }
201366
protected function minify($html)
    {
        $html = str_replace('<?xml encoding="UTF-8">', '', $html);

        $html = trim(preg_replace('/\s+/', ' ', $html));

        $html = str_replace('> <', '><', $html);

        return str_replace(array(' />', '/>'), '>', $html);
    }
418272
public static function text2icons($text)
    {
        // This is a lookbehind assertion which will exclude html entities
        // E.g. situation when ";)" in "&quot;)" shouldn't be replaced by the icon
        // It's so long because of assertion format restrictions
        $entity = '(?<!&'
            . '[a-zA-Z0-9]{2}' . '|' . '#[0-9]{2}' . '|'
            . '[a-zA-Z0-9]{3}' . '|' . '#[0-9]{3}' . '|'
            . '[a-zA-Z0-9]{4}' . '|' . '#[0-9]{4}' . '|'
            . '[a-zA-Z0-9]{5}' . '|'
            . '[a-zA-Z0-9]{6}' . '|'
            . '[a-zA-Z0-9]{7}'
            . ')';

        // map of emoticon replacements
        $map = array(
            '/(?<!mailto):D/'   => self::img_tag('smiley-laughing.gif',    ':D'    ),
            '/:-D/'             => self::img_tag('smiley-laughing.gif',    ':-D'   ),
            '/:\(/'             => self::img_tag('smiley-frown.gif',       ':('    ),
            '/:-\(/'            => self::img_tag('smiley-frown.gif',       ':-('   ),
            '/'.$entity.';\)/'  => self::img_tag('smiley-wink.gif',        ';)'    ),
            '/'.$entity.';-\)/' => self::img_tag('smiley-wink.gif',        ';-)'   ),
            '/8\)/'             => self::img_tag('smiley-cool.gif',        '8)'    ),
            '/8-\)/'            => self::img_tag('smiley-cool.gif',        '8-)'   ),
            '/(?<!mailto):O/i'  => self::img_tag('smiley-surprised.gif',   ':O'    ),
            '/(?<!mailto):-O/i' => self::img_tag('smiley-surprised.gif',   ':-O'   ),
            '/(?<!mailto):P/i'  => self::img_tag('smiley-tongue-out.gif',  ':P'    ),
            '/(?<!mailto):-P/i' => self::img_tag('smiley-tongue-out.gif',  ':-P'   ),
            '/(?<!mailto):@/i'  => self::img_tag('smiley-yell.gif',        ':@'    ),
            '/(?<!mailto):-@/i' => self::img_tag('smiley-yell.gif',        ':-@'   ),
            '/O:\)/i'           => self::img_tag('smiley-innocent.gif',    'O:)'   ),
            '/O:-\)/i'          => self::img_tag('smiley-innocent.gif',    'O:-)'  ),
            '/(?<!O):\)/'       => self::img_tag('smiley-smile.gif',       ':)'    ),
            '/(?<!O):-\)/'      => self::img_tag('smiley-smile.gif',       ':-)'   ),
            '/(?<!mailto):\$/'  => self::img_tag('smiley-embarassed.gif',  ':$'    ),
            '/(?<!mailto):-\$/' => self::img_tag('smiley-embarassed.gif',  ':-$'   ),
            '/(?<!mailto):\*/i'  => self::img_tag('smiley-kiss.gif',       ':*'    ),
            '/(?<!mailto):-\*/i' => self::img_tag('smiley-kiss.gif',       ':-*'   ),
            '/(?<!mailto):S/i'  => self::img_tag('smiley-undecided.gif',   ':S'    ),
            '/(?<!mailto):-S/i' => self::img_tag('smiley-undecided.gif',   ':-S'   ),
        );

        return preg_replace(array_keys($map), array_values($map), $text);
    }
204755
private function extractTitle(string $html): string
    {
        $result = '';

        if (preg_match('/<title[^>]*>\s*(.*?)\s*<\/title\s*>/iu', $html, $matches)) {
            $result = html_entity_decode(strip_tags($matches[1]), ENT_QUOTES);
        }

        return $result;
    }
565285
public static function compress($html)
    {
        $filters = array(
            // remove javascript comments
            '/(?:<script[^>]*>|\G(?!\A))(?:[^\'"\/<]+|"(?:[^\\"]+|\\.)*"|\'(?:[^\\\']+|\\.)*\'|\/(?!\/)|<(?!\/script))*+\K\/\/[^\n|<]*/xsu' => '',
            // remove html comments except IE conditions
            '/<!--(?!\s*(?:\[if [^\]]+]|<!|>))(?:(?!-->).)*-->/su' => '',
            // remove comments in the form /* */
            '/\/+?\s*\*[\s\S]*?\*\s*\/+/u' => '',
            // shorten multiple white spaces
            '/>\s{2,}</u' => '><',
            // shorten multiple white spaces
            '/\s{2,}/u' => ' ',
            // collapse new lines
            '/(\r?\n)/u' => '',
        );

        $output = preg_replace(array_keys($filters), array_values($filters), $html);

        return $output;
    }
935583
public function convert($html) {
    if (preg_match('/^\s*$/u', $html)) {
      return '';
    }
    $html = '<html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8"></head>' . $html . '</body></html>';
    $this->dom = new \DOMDocument();
    if (!$this->dom->loadHTML($html)) {
      return NULL;
    }
    $xp = new \DOMXPath($this->dom);
    return implode(self::BLOCK_DELIMITER, $this->getBlocks(
      $xp->query('/html/body')->item(0)->childNodes
    ));
  }
216030
public static function htmlToDomDocument($html)
    {
        if (!$html) {
            $html = '';
        }

        // Transform multibyte entities which otherwise display incorrectly.
        $html = mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8');

        // Internal errors enabled as HTML5 not fully supported.
        libxml_use_internal_errors(true);

        // Instantiate new DOMDocument object, and then load in UTF-8 HTML.
        $dom = new DOMDocument();
        $dom->encoding = 'UTF-8';
        $dom->loadHTML("<div>$html</div>");

        return $dom;
    }