createDomFragment() публичный статический Метод

Helper function to create a DOM fragment with given markup.
Автор: Adam Schmalhofer
public static createDomFragment ( DOMDocument $dom, string $markup ) : DOMNode
$dom DOMDocument
$markup string
Результат DOMNode fragment in a node.
Пример #1
0
 /**
  * Process source tags in a DOMDocument.
  * $externalReferences will determine what will happen to these images, and the img src will be rewritten accordingly.
  *
  * @param DOMDocument &$xmlDoc            (referenced)
  * @param int         $externalReferences How to handle external references, EPub::EXTERNAL_REF_IGNORE, EPub::EXTERNAL_REF_ADD or EPub::EXTERNAL_REF_REMOVE_IMAGES? Default is EPub::EXTERNAL_REF_ADD.
  * @param string      $baseDir            Default is "", meaning it is pointing to the document root.
  * @param string      $htmlDir            The path to the parent HTML file's directory from the root of the archive.
  * @param string      $backPath           The path to get back to the root of the archive from $htmlDir.
  *
  * @return bool  FALSE if uncuccessful (book is finalized or $externalReferences == EXTERNAL_REF_IGNORE).
  */
 protected function processChapterSources(&$xmlDoc, $externalReferences = EPub::EXTERNAL_REF_ADD, $baseDir = "", $htmlDir = "", $backPath = "")
 {
     if ($this->isFinalized || $externalReferences === EPub::EXTERNAL_REF_IGNORE) {
         return false;
     }
     if ($this->bookVersion !== EPub::BOOK_VERSION_EPUB3) {
         // ePub 2 does not support multimedia formats, and they must be removed.
         $externalReferences = EPub::EXTERNAL_REF_REMOVE_IMAGES;
     }
     $postProcDomElememts = array();
     $images = $xmlDoc->getElementsByTagName("source");
     $itemCount = $images->length;
     for ($idx = 0; $idx < $itemCount; $idx++) {
         /** @var $img \DOMElement */
         $img = $images->item($idx);
         if ($externalReferences === EPub::EXTERNAL_REF_REMOVE_IMAGES) {
             $postProcDomElememts[] = $img;
         } else {
             if ($externalReferences === EPub::EXTERNAL_REF_REPLACE_IMAGES) {
                 $altNode = $img->attributes->getNamedItem("alt");
                 $alt = "image";
                 if ($altNode !== null && strlen($altNode->nodeValue) > 0) {
                     $alt = $altNode->nodeValue;
                 }
                 $postProcDomElememts[] = array($img, StringHelper::createDomFragment($xmlDoc, "[" . $alt . "]"));
             } else {
                 $source = $img->attributes->getNamedItem("src")->nodeValue;
                 $parsedSource = parse_url($source);
                 $internalSrc = FileHelper::sanitizeFileName(urldecode(pathinfo($parsedSource['path'], PATHINFO_BASENAME)));
                 $internalPath = "";
                 $isSourceExternal = false;
                 if ($this->resolveMedia($source, $internalPath, $internalSrc, $isSourceExternal, $baseDir, $htmlDir)) {
                     $img->setAttribute("src", $backPath . $internalPath);
                 } else {
                     if ($isSourceExternal) {
                         $postProcDomElememts[] = $img;
                         // External image is missing
                     }
                 }
                 // else do nothing, if the image is local, and missing, assume it's been generated.
             }
         }
     }
     return true;
 }