src/Graby.php
public function cleanupHtml($contentBlock, $url)
    {
        $originalContentBlock = $contentBlock;

        // if content is pure html, convert it
        if (!$contentBlock instanceof \DOMElement) {
            $this->extractor->process($contentBlock, $url);

            $contentBlock = $this->extractor->getContent();
        }

        // in case of extractor failed
        if (null === $contentBlock) {
            $this->logger->info('Cleanup html failed. Return given content (a bit cleaned)');

            return trim($this->cleanupXss($originalContentBlock));
        }

        $this->extractor->readability->clean($contentBlock, 'select');

        if ($this->config['rewrite_relative_urls']) {
            $this->makeAbsolute($url, $contentBlock);
        }

        // footnotes
        if ('footnotes' === $this->config['content_links'] && false === strpos($url, 'wikipedia.org')) {
            $this->extractor->readability->addFootnotes($contentBlock);
        }

        // normalise
        $contentBlock->normalize();

        // remove empty text nodes
        foreach ($contentBlock->childNodes as $n) {
            if (XML_TEXT_NODE === $n->nodeType && '' === trim($n->textContent)) {
                $contentBlock->removeChild($n);
            }
        }

        // remove nesting: <div><div><div><p>test</p></div></div></div> = <p>test</p>
        while (1 === $contentBlock->childNodes->length && XML_ELEMENT_NODE === $contentBlock->firstChild->nodeType) {
            // only follow these tag names
            if (!\in_array(strtolower($contentBlock->tagName), ['div', 'article', 'section', 'header', 'footer'], true)) {
                break;
            }

            $contentBlock = $contentBlock->firstChild;
        }

        // convert content block to HTML string
        // Need to preserve things like body: //img[@id='feature']
        if (\in_array(strtolower($contentBlock->tagName), ['div', 'article', 'section', 'header', 'footer', 'li', 'td'], true)) {
            $html = $contentBlock->innerHTML;
        } else {
            $html = $contentBlock->ownerDocument->saveXML($contentBlock); // essentially outerHTML
        }

        // post-processing cleanup
        $html = preg_replace('!<p>[\s\h\v]*</p>!u', '', $html);
        if ('remove' === $this->config['content_links']) {
            $html = preg_replace('!</?a[^>]*>!', '', $html);
        }

        $this->logger->debug('Body after cleanupHtml, before cleanupXss', ['html' => $html]);

        return trim($this->cleanupXss($html));
    }
public function cleanupHtml($contentBlock, $url)
    {
        $originalContentBlock = $contentBlock;

        // if content is pure html, convert it
        if (!$contentBlock instanceof \DOMElement) {
            $this->extractor->process($contentBlock, $url);

            $contentBlock = $this->extractor->getContent();
        }

        // in case of extractor failed
        if (null === $contentBlock) {
            $this->logger->info('Cleanup html failed. Return given content (a bit cleaned)');

            return trim($this->cleanupXss($originalContentBlock));
        }

        $this->extractor->readability->clean($contentBlock, 'select');

        if ($this->config['rewrite_relative_urls']) {
            $this->makeAbsolute($url, $contentBlock);
        }

        // footnotes
        if ('footnotes' === $this->config['content_links'] && false === strpos($url, 'wikipedia.org')) {
            $this->extractor->readability->addFootnotes($contentBlock);
        }

        // normalise
        $contentBlock->normalize();

        // remove empty text nodes
        foreach ($contentBlock->childNodes as $n) {
            if (XML_TEXT_NODE === $n->nodeType && '' === trim($n->textContent)) {
                $contentBlock->removeChild($n);
            }
        }

        // remove nesting: <div><div><div><p>test</p></div></div></div> = <p>test</p>
        while (1 === $contentBlock->childNodes->length && XML_ELEMENT_NODE === $contentBlock->firstChild->nodeType) {
            // only follow these tag names
            if (!\in_array(strtolower($contentBlock->tagName), ['div', 'article', 'section', 'header', 'footer'], true)) {
                break;
            }

            $contentBlock = $contentBlock->firstChild;
        }

        // convert content block to HTML string
        // Need to preserve things like body: //img[@id='feature']
        if (\in_array(strtolower($contentBlock->tagName), ['div', 'article', 'section', 'header', 'footer', 'li', 'td'], true)) {
            $html = $contentBlock->innerHTML;
        } else {
            $html = $contentBlock->ownerDocument->saveXML($contentBlock); // essentially outerHTML
        }

        // post-processing cleanup
        $html = preg_replace('!<p>[\s\h\v]*</p>!u', '', $html);
        if ('remove' === $this->config['content_links']) {
            $html = preg_replace('!</?a[^>]*>!', '', $html);
        }

        $this->logger->debug('Body after cleanupHtml, before cleanupXss', ['html' => $html]);

        return trim($this->cleanupXss($html));
    }
public function cleanupHtml ( $ contentBlock , $ url ) { $ originalContentBlock = $ contentBlock ; // if content is pure html, convert it if ( ! $ contentBlock instanceof \ DOMElement ) { $ this -> extractor -> process ( $ contentBlock , $ url ) ; $ contentBlock = $ this -> extractor -> getContent ( ) ; } // in case of extractor failed if ( null === $ contentBlock ) { $ this -> logger -> info ( 'Cleanup html failed. Return given content (a bit cleaned)' ) ; return trim ( $ this -> cleanupXss ( $ originalContentBlock ) ) ; } $ this -> extractor -> readability -> clean ( $ contentBlock , 'select' ) ; if ( $ this -> config [ 'rewrite_relative_urls' ] ) { $ this -> makeAbsolute ( $ url , $ contentBlock ) ; } // footnotes if ( 'footnotes' === $ this -> config [ 'content_links' ] && false === strpos ( $ url , 'wikipedia.org' ) ) { $ this -> extractor -> readability -> addFootnotes ( $ contentBlock ) ; } // normalise $ contentBlock -> normalize ( ) ; // remove empty text nodes foreach ( $ contentBlock -> childNodes as $ n ) { if ( XML_TEXT_NODE === $ n -> nodeType && '' === trim ( $ n -> textContent ) ) { $ contentBlock -> removeChild ( $ n ) ; } } // remove nesting: <div><div><div><p>test</p></div></div></div> = <p>test</p> while ( 1 === $ contentBlock -> childNodes -> length && XML_ELEMENT_NODE === $ contentBlock -> firstChild -> nodeType ) { // only follow these tag names if ( ! \ in_array ( strtolower ( $ contentBlock -> tagName ) , [ 'div' , 'article' , 'section' , 'header' , 'footer' ] , true ) ) { break ; } $ contentBlock = $ contentBlock -> firstChild ; } // convert content block to HTML string // Need to preserve things like body: //img[@id='feature'] if ( \ in_array ( strtolower ( $ contentBlock -> tagName ) , [ 'div' , 'article' , 'section' , 'header' , 'footer' , 'li' , 'td' ] , true ) ) { $ html = $ contentBlock -> innerHTML ; } else { $ html = $ contentBlock -> ownerDocument -> saveXML ( $ contentBlock ) ; // essentially outerHTML } // post-processing cleanup $ html = preg_replace ( '!<p>[\s\h\v]*</p>!u' , '' , $ html ) ; if ( 'remove' === $ this -> config [ 'content_links' ] ) { $ html = preg_replace ( '!</?a[^>]*>!' , '' , $ html ) ; } $ this -> logger -> debug ( 'Body after cleanupHtml, before cleanupXss' , [ 'html' => $ html ] ) ; return trim ( $ this -> cleanupXss ( $ html ) ) ; }
Cleanup HTML from a DOMElement or a string.

@param string|\DOMElement $contentBlock
@param string             $url

@return string
Cleanup HTML from a DOMElement or a string .