src/MarkupFixer.php
public function fix($markup, $topLevel = 1, $depth = 6)
    {
        if ( ! $this->isFullHtmlDocument($markup)) {
            $partialID = uniqid('toc_generator_');
            $markup = sprintf("<body id='%s'>%s</body>", $partialID, $markup);
        }

        $domDocument = $this->htmlParser->loadHTML($markup);
        $domDocument->preserveWhiteSpace = true; // do not clobber whitespace

        $sluggifier = new UniqueSluggifier();

        /** @var \DOMElement $node */
        foreach ($this->traverseHeaderTags($domDocument, $topLevel, $depth) as $node) {
            if ($node->getAttribute('id')) {
                continue;
            }

            $node->setAttribute('id', $sluggifier->slugify($node->getAttribute('title') ?: $node->textContent));
        }

        return $this->htmlParser->saveHTML((isset($partialID)) ? $domDocument->getElementById($partialID)->childNodes : $domDocument);
    }
public function fix($markup, $topLevel = 1, $depth = 6)
    {
        if ( ! $this->isFullHtmlDocument($markup)) {
            $partialID = uniqid('toc_generator_');
            $markup = sprintf("<body id='%s'>%s</body>", $partialID, $markup);
        }

        $domDocument = $this->htmlParser->loadHTML($markup);
        $domDocument->preserveWhiteSpace = true; // do not clobber whitespace

        $sluggifier = new UniqueSluggifier();

        /** @var \DOMElement $node */
        foreach ($this->traverseHeaderTags($domDocument, $topLevel, $depth) as $node) {
            if ($node->getAttribute('id')) {
                continue;
            }

            $node->setAttribute('id', $sluggifier->slugify($node->getAttribute('title') ?: $node->textContent));
        }

        return $this->htmlParser->saveHTML((isset($partialID)) ? $domDocument->getElementById($partialID)->childNodes : $domDocument);
    }
public function fix ( $ markup , $ topLevel = 1 , $ depth = 6 ) { if ( ! $ this -> isFullHtmlDocument ( $ markup ) ) { $ partialID = uniqid ( 'toc_generator_' ) ; $ markup = sprintf ( "<body id='%s'>%s</body>" , $ partialID , $ markup ) ; } $ domDocument = $ this -> htmlParser -> loadHTML ( $ markup ) ; $ domDocument -> preserveWhiteSpace = true ; // do not clobber whitespace $ sluggifier = new UniqueSluggifier ( ) ; /** @var \DOMElement $node */ foreach ( $ this -> traverseHeaderTags ( $ domDocument , $ topLevel , $ depth ) as $ node ) { if ( $ node -> getAttribute ( 'id' ) ) { continue ; } $ node -> setAttribute ( 'id' , $ sluggifier -> slugify ( $ node -> getAttribute ( 'title' ) ? : $ node -> textContent ) ) ; } return $ this -> htmlParser -> saveHTML ( ( isset ( $ partialID ) ) ? $ domDocument -> getElementById ( $ partialID ) -> childNodes : $ domDocument ) ; }
Fix markup

@param string $markup
@param int    $topLevel
@param int    $depth
@return string Markup with added IDs
@throws RuntimeException
Fix markup