@param string $html

@return string
184998
private function eliminateLink(string $html): string
    {
        $count = 0;
        $notLinked = preg_replace_callback('/<a\s[^>]*>.*?<\/a\s*>/ius', function () use (&$count) {
            $count++;

            return '';
        }, $html);
        $notLinked = preg_replace('/<form\s[^>]*>.*?<\/form\s *>/imsu', '', $notLinked);
        $notLinked = strip_tags($notLinked);

        if (strlen($notLinked) < 20 * $count || $this->isLinkList($html)) {
            return '';
        }

        return $notLinked;
    }
996053
public static function closeOpenTags($html)
    {
        preg_match_all("#<([a-z]+)( .*)?(?!/)>#iU", $html, $result);
        $openedTags = $result[1];
        preg_match_all("#</([a-z]+)>#iU", $html, $result);
        $closedTags = $result[1];
        $lenOpened = count($openedTags);

        if (count($closedTags) == $lenOpened) {
            return $html;
        }

        $openedTags = array_reverse($openedTags);
        for ($i = 0; $i < $lenOpened; $i++) {
            if (!in_array($openedTags[$i], $closedTags)) {
                $html .= '</' . $openedTags[$i] . '>';
            } else {
                unset($closedTags[array_search($openedTags[$i], $closedTags)]);
            }
        }

        return $html;
    }
621444
private function parseAttribute(string $html) : string
    {
        $remainingHtml = ltrim($html);

        try {
            // Will match the first entire name/value attribute pair.
            preg_match(
                "/((([a-z0-9\-_]+:)?[a-z0-9\-_]+)(\s*=\s*)?)/i",
                $remainingHtml,
                $attributeMatches
            );

            $attributeName = $attributeMatches[2];
            $remainingHtml = mb_substr(mb_strstr($remainingHtml, $attributeName), mb_strlen($attributeName));
            if ($this->isAttributeValueless($remainingHtml)) {
                $this->attributes[trim($attributeName)] = true;

                return $remainingHtml;
            }

            return $this->parseAttributeValue($html, $remainingHtml, $attributeName);
        } catch (ParseException $e) {
            if ($this->getThrowOnError()) {
                throw $e;
            }
        }

        return '';
    }
1190660
public static function htmlToPlainText($html)
    {
        // Replace &nbsp; with spaces
        $html = str_replace("\xC2\xA0", ' ', html_entity_decode($html));
        // Strip out HTML tags
        $text = strip_tags($html);
        // Replace excessive whitespace (spaces, tabs, and line breaks) with single space
        $text = preg_replace('/\s+/S', ' ', $text);

        return $text;
    }
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;
    }
538135
public function convert($html)
    {
        $html = parent::convert((string) $html);

        $html = $this->encode('--', '-', $html, false);
        $html = $this->encode('\#', '#', $html);
        $html = $this->encode('\*', '*', $html);
        $html = $this->encode('\_', '_', $html);
        $html = $this->encode('•', '*', $html);
        $html = $this->encode('…', '...', $html);

        $html = $this->encode('\[', '[', (string) $html);
        $html = $this->encode('\]', ']', (string) $html);
        $html = str_replace(array('—', '–', '\-'), '-', $html);
        $html = str_replace(array('“', '”'), '"', $html);
        $html = str_replace(array('’', '‘'), "'", $html);
        $html = str_replace("\n\n\n", "\n\n", $html);
        $html = str_replace("\n ", "\n", (string) $html);

        return str_replace('\.', '.', (string) $html);
    }
333854
private function handleHtmlResult($html)
    {
        if ($this->preserve_no_newlines) {
            $html = str_replace("\n", '', $html);
        }
        if (stripos($html, 'dqn_tmp_id=') !== false) {
            $html = preg_replace('/ dqn_tmp_id="([a-z0-9]+)"/', '', $html);
        }

        return $html;
    }
899278
public function toJson($html)
    {
        // Strip white space between tags to prevent creation of empty #text nodes
        $html = preg_replace('~>\s+<~', '><', $html);
        $document = new \DOMDocument();

        // Load UTF-8 HTML hack (from http://bit.ly/pVDyCt)
        $document->loadHTML('<?xml encoding="UTF-8">' . $html);
        $document->encoding = 'UTF-8';

        // fetch the body of the document. All html is stored in there
        $body = $document->getElementsByTagName("body")->item(0);

        $data = array();

        // loop trough the child nodes and convert them
        if ($body) {
            foreach ($body->childNodes as $node) {
                $data[] = $this->convert($node);
            }
        }

        return json_encode(array('data' => $data));
    }
1309900
private function parseForeignContents(string $tag, string $html): string
    {
        $remainingHtml = \ltrim($html);

        // Find all contents.
        $remainingHtml = $this->determineRemainingHtmlOfForeignContents(
            $tag,
            $html,
            $remainingHtml
        );

        // Handle no contents.
        if ($this->value === '') {
            return $remainingHtml;
        }

        if ($tag === 'script') {
            $text = new TextJs($this, $this->value);
        } elseif ($tag === 'style') {
            $text = new TextCss($this, $this->value);
        } else {
            $text = new Text($this, $this->value);
        }

        $this->children[] = $text;

        return $remainingHtml;
    }
1018587
protected function getHeadFrag($html)
    {
        if (!($html = (string) $html)) {
            return []; // Nothing to do.
        }
        if (preg_match('/(?P<all>(?P<open_tag>\<head(?:\s+[^>]*?)?\>)(?P<contents>.*?)(?P<closing_tag>\<\/head\>))/uis', $html, $head_frag)) {
            return $this->removeNumericKeysDeep($head_frag);
        }
        return [];
    }
660011
static function linkUrls($html)
    {
        return preg_replace_callback(self::URLS_REGEXP, function($matches) {
            // Is URL already linked?
            if (preg_match('/<a\s/i', $matches[1])) return $matches[0];
            
            $url = ($matches[2] == "www." ? "http://www." : $matches[2]) . $matches[3];
            return 
                $matches[1] . 
                '<a href="' . Html::escape($url) . '">' . Html::escape(Html::humanUrl($url)) . '</a>' . 
                $matches[4];
        }, $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,
            ]
        );
    }
1353265
public function get($html = null)
    {
        if (null === $html) {
            if (file_exists($this->_path)) {
                $html = file_get_contents($this->_path);
            }
        }

        $this->_path = null;

        return $this->parse($html);
    }
947559
public function linkUrlsInTrustedHtml($html)
	{
		$reMarkup = '{</?([a-z]+)([^"\'>]|"[^"]*"|\'[^\']*\')*>|&#?[a-zA-Z0-9]+;|$}';

		$insideAnchorTag = false;
		$position = 0;
		$result = '';

		// Iterate over every piece of markup in the HTML.
		while (true)
		{
			$match = array();
			preg_match($reMarkup, $html, $match, PREG_OFFSET_CAPTURE, $position);

			list($markup, $markupPosition) = $match[0];

			// Process text leading up to the markup.
			$text = substr($html, $position, $markupPosition - $position);

			// Link URLs unless we're inside an anchor tag.
			if ( ! $insideAnchorTag )
			{
				$text = $this->linkUrlsAndEscapeHtml($text);
			}

			$result .= $text;

			// End of HTML?
			if ( $markup === '' )
			{
				break;
			}

			// Check if markup is an anchor tag ('<a>', '</a>').
			if ( $markup[0] !== '&' && $match[1][0] === 'a' )
			{
				$insideAnchorTag = ($markup[1] !== '/');
			}

			// Pass markup through unchanged.
			$result .= $markup;

			// Continue after the markup.
			$position = $markupPosition + strlen($markup);
		}

		return $result;
	}
120255
protected function wrapElements($html)
    {
        if (isset($this->attributes['wraps']) && is_array($this->attributes['wraps'])) {
            $elements = $this->attributes['wraps'];
            $elements = array_reverse($elements);

            /** @var HtmlElement $element */
            foreach ($elements as $element) {
                $html = $element->setContent($html);
            }
        }

        return (string) $html;
    }
730593
protected function restore($html)
    {
        foreach ($this->data as $index => $item)
        {
            $key = (string) '$' . $index . '$';

            $html = str_replace($key, $item, $html);
        }

        return (string) $html;
    }
1188414
public function convertToString($html)
    {
        $externalJavaScript = $this->extractExternalJavaScript($html);

        return $this->replaceJavaScriptTags($html, $externalJavaScript);
    }