protected function processChapterExternalReferences(&$doc, $externalReferences = EPub::EXTERNAL_REF_ADD, $baseDir = "", $htmlDir = "")
{
if ($this->isFinalized || $externalReferences === EPub::EXTERNAL_REF_IGNORE) {
return false;
}
$backPath = preg_replace('#[^/]+/#i', "../", $htmlDir);
$isDocAString = is_string($doc);
$xmlDoc = null;
if ($isDocAString) {
$doc = StringHelper::removeComments($doc);
$xmlDoc = new DOMDocument();
@$xmlDoc->loadHTML($doc);
} else {
$xmlDoc = $doc;
}
$this->processChapterStyles($xmlDoc, $externalReferences, $baseDir, $htmlDir);
$this->processChapterLinks($xmlDoc, $externalReferences, $baseDir, $htmlDir, $backPath);
$this->processChapterImages($xmlDoc, $externalReferences, $baseDir, $htmlDir, $backPath);
$this->processChapterSources($xmlDoc, $externalReferences, $baseDir, $htmlDir, $backPath);
if ($isDocAString) {
//$html = $xmlDoc->saveXML();
$htmlNode = $xmlDoc->getElementsByTagName("html");
$headNode = $xmlDoc->getElementsByTagName("head");
$bodyNode = $xmlDoc->getElementsByTagName("body");
$htmlNS = "";
for ($index = 0; $index < $htmlNode->item(0)->attributes->length; $index++) {
$nodeName = $htmlNode->item(0)->attributes->item($index)->nodeName;
$nodeValue = $htmlNode->item(0)->attributes->item($index)->nodeValue;
if ($nodeName != "xmlns") {
$htmlNS .= " {$nodeName}=\"{$nodeValue}\"";
}
}
$xml = new DOMDocument('1.0', "utf-8");
$xml->lookupPrefix("http://www.w3.org/1999/xhtml");
$xml->preserveWhiteSpace = false;
$xml->formatOutput = true;
$xml2Doc = new DOMDocument('1.0', "utf-8");
$xml2Doc->lookupPrefix("http://www.w3.org/1999/xhtml");
$xml2Doc->loadXML("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\"\n" . " \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n" . "<html xmlns=\"http://www.w3.org/1999/xhtml\"" . $htmlNS . ">\n</html>\n");
$html = $xml2Doc->getElementsByTagName("html")->item(0);
$html->appendChild($xml2Doc->importNode($headNode->item(0), true));
$html->appendChild($xml2Doc->importNode($bodyNode->item(0), true));
// force pretty printing and correct formatting, should not be needed, but it is.
$xml->loadXML($xml2Doc->saveXML());
$doc = $xml->saveXML();
if (!$this->isEPubVersion2()) {
$doc = preg_replace('#^\\s*<!DOCTYPE\\ .+?>\\s*#im', '', $doc);
}
}
return true;
}