/** * Builds the {@link Frame_Tree}, loads any CSS and applies the styles to * the {@link Frame_Tree} */ protected function _process_html() { $this->save_locale(); $this->_tree->build_tree(); $this->_css->load_css_file(Stylesheet::DEFAULT_STYLESHEET, Stylesheet::ORIG_UA); $acceptedmedia = Stylesheet::$ACCEPTED_GENERIC_MEDIA_TYPES; if (defined("DOMPDF_DEFAULT_MEDIA_TYPE")) { $acceptedmedia[] = DOMPDF_DEFAULT_MEDIA_TYPE; } else { $acceptedmedia[] = Stylesheet::$ACCEPTED_DEFAULT_MEDIA_TYPE; } // load <link rel="STYLESHEET" ... /> tags $links = $this->_xml->getElementsByTagName("link"); foreach ($links as $link) { if (mb_strtolower($link->getAttribute("rel")) === "stylesheet" || mb_strtolower($link->getAttribute("type")) === "text/css") { //Check if the css file is for an accepted media type //media not given then always valid $formedialist = preg_split("/[\\s\n,]/", $link->getAttribute("media"), -1, PREG_SPLIT_NO_EMPTY); if (count($formedialist) > 0) { $accept = false; foreach ($formedialist as $type) { if (in_array(mb_strtolower(trim($type)), $acceptedmedia)) { $accept = true; break; } } if (!$accept) { //found at least one mediatype, but none of the accepted ones //Skip this css file. continue; } } $url = $link->getAttribute("href"); $url = build_url($this->_protocol, $this->_base_host, $this->_base_path, $url); $this->_css->load_css_file($url, Stylesheet::ORIG_AUTHOR); } } // load <style> tags $styles = $this->_xml->getElementsByTagName("style"); foreach ($styles as $style) { // Accept all <style> tags by default (note this is contrary to W3C // HTML 4.0 spec: // http://www.w3.org/TR/REC-html40/present/styles.html#adef-media // which states that the default media type is 'screen' if ($style->hasAttributes() && ($media = $style->getAttribute("media")) && !in_array($media, $acceptedmedia)) { continue; } $css = ""; if ($style->hasChildNodes()) { $child = $style->firstChild; while ($child) { $css .= $child->nodeValue; // Handle <style><!-- blah --></style> $child = $child->nextSibling; } } else { $css = $style->nodeValue; } // Set the base path of the Stylesheet to that of the file being processed $this->_css->set_protocol($this->_protocol); $this->_css->set_host($this->_base_host); $this->_css->set_base_path($this->_base_path); $this->_css->load_css($css); } $this->restore_locale(); }
/** * Builds the {@link Frame_Tree}, loads any CSS and applies the styles to * the {@link Frame_Tree} */ protected function _process_html() { $this->_tree->build_tree(); $this->_css->load_css_file(Stylesheet::DEFAULT_STYLESHEET, Stylesheet::ORIG_UA); $acceptedmedia = Stylesheet::$ACCEPTED_GENERIC_MEDIA_TYPES; $acceptedmedia[] = $this->get_option("default_media_type"); // <base href="" /> $base_nodes = $this->_xml->getElementsByTagName("base"); if ($base_nodes->length && ($href = $base_nodes->item(0)->getAttribute("href"))) { list($this->_protocol, $this->_base_host, $this->_base_path) = explode_url($href); } // Set the base path of the Stylesheet to that of the file being processed $this->_css->set_protocol($this->_protocol); $this->_css->set_host($this->_base_host); $this->_css->set_base_path($this->_base_path); // Get all the stylesheets so that they are processed in document order $xpath = new DOMXPath($this->_xml); $stylesheets = $xpath->query("//*[name() = 'link' or name() = 'style']"); foreach ($stylesheets as $tag) { switch (strtolower($tag->nodeName)) { // load <link rel="STYLESHEET" ... /> tags case "link": if (mb_strtolower(stripos($tag->getAttribute("rel"), "stylesheet") !== false) || mb_strtolower($tag->getAttribute("type")) === "text/css") { //Check if the css file is for an accepted media type //media not given then always valid $formedialist = preg_split("/[\\s\n,]/", $tag->getAttribute("media"), -1, PREG_SPLIT_NO_EMPTY); if (count($formedialist) > 0) { $accept = false; foreach ($formedialist as $type) { if (in_array(mb_strtolower(trim($type)), $acceptedmedia)) { $accept = true; break; } } if (!$accept) { //found at least one mediatype, but none of the accepted ones //Skip this css file. continue; } } $url = $tag->getAttribute("href"); $url = build_url($this->_protocol, $this->_base_host, $this->_base_path, $url); $this->_css->load_css_file($url, Stylesheet::ORIG_AUTHOR); } break; // load <style> tags // load <style> tags case "style": // Accept all <style> tags by default (note this is contrary to W3C // HTML 4.0 spec: // http://www.w3.org/TR/REC-html40/present/styles.html#adef-media // which states that the default media type is 'screen' if ($tag->hasAttributes() && ($media = $tag->getAttribute("media")) && !in_array($media, $acceptedmedia)) { continue; } $css = ""; if ($tag->hasChildNodes()) { $child = $tag->firstChild; while ($child) { $css .= $child->nodeValue; // Handle <style><!-- blah --></style> $child = $child->nextSibling; } } else { $css = $tag->nodeValue; } $this->_css->load_css($css); break; } } }
/** * Builds the {@link Frame_Tree}, loads any CSS and applies the styles to * the {@link Frame_Tree} */ protected function _process_html() { $this->_tree->build_tree(); $this->_css->load_css_file(Stylesheet::DEFAULT_STYLESHEET); // load <link rel="STYLESHEET" ... /> tags $links = $this->_xml->getElementsByTagName("link"); foreach ($links as $link) { if (mb_strtolower($link->getAttribute("rel")) == "stylesheet" || mb_strtolower($link->getAttribute("type")) == "text/css") { $url = $link->getAttribute("href"); $url = build_url($this->_protocol, $this->_base_host, $this->_base_path, $url); $this->_css->load_css_file($url); } } // load <style> tags $styles = $this->_xml->getElementsByTagName("style"); foreach ($styles as $style) { // Accept all <style> tags by default (note this is contrary to W3C // HTML 4.0 spec: // http://www.w3.org/TR/REC-html40/present/styles.html#adef-media // which states that the default media type is 'screen' if ($style->hasAttributes() && ($media = $style->getAttribute("media")) && !in_array($media, Stylesheet::$ACCEPTED_MEDIA_TYPES)) { continue; } $css = ""; if ($style->hasChildNodes()) { $child = $style->firstChild; while ($child) { $css .= $child->nodeValue; // Handle <style><!-- blah --></style> $child = $child->nextSibling; } } else { $css = $style->nodeValue; } // Set the base path of the Stylesheet to that of the file being processed $this->_css->set_protocol($this->_protocol); $this->_css->set_host($this->_base_host); $this->_css->set_base_path($this->_base_path); $this->_css->load_css($css); } }