doc/lib: Fix undefined entity &nbsp and too greedy regex

This commit is contained in:
Eric Lippmann 2015-02-12 10:24:55 +01:00
parent 6a3d1c665b
commit f00f598014

View File

@ -125,9 +125,7 @@ class DocSectionRenderer extends DocRenderer
protected function highlightSearch($html, DocSearch $search) protected function highlightSearch($html, DocSearch $search)
{ {
$doc = new DOMDocument(); $doc = new DOMDocument();
$fragment = $doc->createDocumentFragment(); @$doc->loadHTML($html);
$fragment->appendXML($html);
$doc->appendChild($fragment);
$iter = new RecursiveIteratorIterator(new DomNodeIterator($doc), RecursiveIteratorIterator::SELF_FIRST); $iter = new RecursiveIteratorIterator(new DomNodeIterator($doc), RecursiveIteratorIterator::SELF_FIRST);
foreach ($iter as $node) { foreach ($iter as $node) {
if ($node->nodeType !== XML_TEXT_NODE if ($node->nodeType !== XML_TEXT_NODE
@ -152,7 +150,10 @@ class DocSectionRenderer extends DocRenderer
$fragment->appendChild($doc->createTextNode(substr($text, $offset))); $fragment->appendChild($doc->createTextNode(substr($text, $offset)));
$node->parentNode->replaceChild($fragment, $node); $node->parentNode->replaceChild($fragment, $node);
} }
return $doc->saveHTML(); // Remove <!DOCTYPE
$doc->removeChild($doc->doctype);
// Remove <html><body> and </body></html>
return substr($doc->saveHTML(), 12, -15);
} }
/** /**
@ -250,14 +251,14 @@ class DocSectionRenderer extends DocRenderer
$section->getLevel(), $section->getLevel(),
$title $title
); );
$content = $this->parsedown->text(implode('', $section->getContent())); $html = $this->parsedown->text(implode('', $section->getContent()));
if (empty($content)) { if (empty($html)) {
continue; continue;
} }
$html = preg_replace_callback( $html = preg_replace_callback(
'#<pre><code class="language-php">(.*?)</code></pre>#s', '#<pre><code class="language-php">(.*?)</code></pre>#s',
array($this, 'highlightPhp'), array($this, 'highlightPhp'),
$content $html
); );
$html = preg_replace_callback( $html = preg_replace_callback(
'/<img[^>]+>/', '/<img[^>]+>/',
@ -270,7 +271,7 @@ class DocSectionRenderer extends DocRenderer
$html $html
); );
$html = preg_replace_callback( $html = preg_replace_callback(
'/<a\s+(?P<attribs>[^>]*?\s+)?href="(?:(?!http:\/\/)[^#]*)#(?P<fragment>[^"]+)"/', '/<a\s+(?P<attribs>[^>]*?\s+)?href="(?:(?!http:\/\/)[^"#]*)#(?P<fragment>[^"]+)"/',
array($this, 'replaceLink'), array($this, 'replaceLink'),
$html $html
); );