protected static function _set_basefont_size($node, $value) { // FIXME: ? we don't actually set the font size of anything here, just // the base size for later modification by <font> tags. self::$_last_basefont_size = $value; return null; }
/** * applies all current styles to a particular document tree * * apply_styles() applies all currently loaded styles to the provided * {@link FrameParser_Tree}. Aside from parsing CSS, this is the main purpose * of this class. * * @param FrameParser_Tree $tree */ function apply_styles(FrameParser_Tree $tree) { // Use XPath to select nodes. This would be easier if we could attach // FrameParser objects directly to DOMNodes using the setUserData() method, but // we can't do that just yet. Instead, we set a _node attribute_ in // FrameParser->set_id() and use that as a handle on the FrameParser object via // FrameParser_Tree::$_registry. // We create a scratch array of styles indexed by frame id. Once all // styles have been assigned, we order the cached styles by specificity // and create a final style object to assign to the frame. // FIXME: this is not particularly robust... $styles = array(); $xp = new DOMXPath($tree->get_dom()); // Add generated content foreach ($this->_styles as $selector => $style) { if (strpos($selector, ":before") === false && strpos($selector, ":after") === false) { continue; } $query = $this->_css_selector_to_xpath($selector, true); // Retrieve the nodes $nodes = @$xp->query($query["query"]); if ($nodes == null) { record_warnings_parser(E_USER_WARNING, "The CSS selector '{$selector}' is not valid", __FILE__, __LINE__); continue; } foreach ($nodes as $i => $node) { foreach ($query["pseudo_elements"] as $pos) { if (($src = $this->_image($style->content)) !== "none") { $new_node = $node->ownerDocument->createElement("img_generated"); $new_node->setAttribute("src", $src); } else { $new_node = $node->ownerDocument->createElement("dompdf_generated"); } $new_node->setAttribute($pos, $pos); $tree->insert_node($node, $new_node, $pos); } } } // Apply all styles in stylesheet foreach ($this->_styles as $selector => $style) { $query = $this->_css_selector_to_xpath($selector); // Retrieve the nodes $nodes = @$xp->query($query["query"]); if ($nodes == null) { record_warnings_parser(E_USER_WARNING, "The CSS selector '{$selector}' is not valid", __FILE__, __LINE__); continue; } foreach ($nodes as $node) { // Retrieve the node id if ($node->nodeType != XML_ELEMENT_NODE) { // Only DOMElements get styles continue; } $id = $node->getAttribute("frame_id"); // Assign the current style to the scratch array $spec = $this->_specificity($selector); $styles[$id][$spec][] = $style; } } // Now create the styles and assign them to the appropriate frames. (We // iterate over the tree using an implicit FrameParser_Tree iterator.) $root_flg = false; foreach ($tree->get_frames() as $frame) { // pre_r($frame->get_node()->nodeName . ":"); if (!$root_flg && $this->_page_style) { $style = $this->_page_style; $root_flg = true; } else { $style = $this->create_style(); } // Find nearest DOMElement parent $p = $frame; while ($p = $p->get_parent()) { if ($p->get_node()->nodeType == XML_ELEMENT_NODE) { break; } } // StyleParsers can only be applied directly to DOMElements; anonymous // frames inherit from their parent if ($frame->get_node()->nodeType != XML_ELEMENT_NODE) { if ($p) { $style->inherit($p->get_style()); } $frame->set_style($style); continue; } $id = $frame->get_id(); // Handle HTML 4.0 attributes Attribute_Translator_Parser::translate_attributes($frame); // Locate any additional style attributes if (($str = $frame->get_node()->getAttribute("style")) !== "") { // Destroy CSS comments $str = preg_replace("'/\\*.*?\\*/'si", "", $str); $spec = $this->_specificity("!style attribute"); $styles[$id][$spec][] = $this->_parse_properties($str); } // Grab the applicable styles if (isset($styles[$id])) { $applied_styles = $styles[$frame->get_id()]; // Sort by specificity ksort($applied_styles); // Merge the new styles with the inherited styles foreach ($applied_styles as $arr) { foreach ($arr as $s) { $style->merge($s); } } } // Inherit parent's styles if required if ($p) { $style->inherit($p->get_style()); } $frame->set_style($style); } // We're done! Clean out the registry of all styles since we // won't be needing this later. foreach (array_keys($this->_styles) as $key) { $this->_styles[$key] = null; unset($this->_styles[$key]); } }