if ( $important ) { $prop = $match[1]; } $prop = trim($prop); */ if ($DEBUGCSS) print '('; $important = false; $prop = trim($prop); if (substr($prop, -9) === 'important') { $prop_tmp = rtrim(substr($prop, 0, -9)); if (substr($prop_tmp, -1) === '!') { $prop = rtrim(substr($prop_tmp, 0, -1)); $important = true; } } if ($prop === "") { if ($DEBUGCSS) print 'empty)'; continue; } $i = mb_strpos($prop, ":"); if ($i === false) { if ($DEBUGCSS) print 'novalue' . $prop . ')'; continue; } $prop_name = rtrim(mb_strtolower(mb_substr($prop, 0, $i))); $value = ltrim(mb_substr($prop, $i + 1)); if ($DEBUGCSS) print $prop_name . ':=' . $value . ($important ? '!IMPORTANT' : '') . ')'; $style->set_prop($prop_name, $value, $important, false); } if ($DEBUGCSS) print '_parse_properties]'; return $style; } /** * parse selector + rulesets * * @param string $str CSS selectors and rulesets * @param array $media_queries */ private function _parse_sections($str, $media_queries = []) { // Pre-process selectors: collapse all whitespace and strip whitespace // around '>', '.', ':', '+', '~', '#' $patterns = ["/\s+/", "/\s+([>.:+~#])\s+/"]; $replacements = [" ", "\\1"]; $DEBUGCSS = $this->_dompdf->getOptions()->getDebugCss(); $sections = explode("}", $str); if ($DEBUGCSS) print '[_parse_sections'; foreach ($sections as $sect) { $i = mb_strpos($sect, "{"); if ($i === false) { continue; } if ($DEBUGCSS) print '[section'; $selector_str = preg_replace($patterns, $replacements, mb_substr($sect, 0, $i)); $selectors = preg_split("/,(?![^\(]*\))/", $selector_str, 0, PREG_SPLIT_NO_EMPTY); $style = $this->_parse_properties(trim(mb_substr($sect, $i + 1))); // Assign it to the selected elements foreach ($selectors as $selector) { $selector = trim($selector); if ($selector === "") { if ($DEBUGCSS) print '#empty#'; continue; } if ($DEBUGCSS) print '#' . $selector . '#'; //if ($DEBUGCSS) { if (strpos($selector,'p') !== false) print '!!!p!!!#'; } //FIXME: tag the selector with a hash of the media query to separate it from non-conditional styles (?), xpath comments are probably not what we want to do here if (count($media_queries) > 0) { $style->set_media_queries($media_queries); } $this->add_style($selector, $style); } if ($DEBUGCSS) { print 'section]'; } } if ($DEBUGCSS) { print "_parse_sections]\n"; } } /** * @return string */ public function getDefaultStylesheet() { $options = $this->_dompdf->getOptions(); $rootDir = realpath($options->getRootDir()); return Helpers::build_url("file://", "", $rootDir, $rootDir . self::DEFAULT_STYLESHEET); } /** * @param FontMetrics $fontMetrics * @return $this */ public function setFontMetrics(FontMetrics $fontMetrics) { $this->fontMetrics = $fontMetrics; return $this; } /** * @return FontMetrics */ public function getFontMetrics() { return $this->fontMetrics; } /** * dumps the entire stylesheet as a string * * Generates a string of each selector and associated style in the * Stylesheet. Useful for debugging. * * @return string */ function __toString() { $str = ""; foreach ($this->_styles as $selector => $selector_styles) { foreach ($selector_styles as $style) { $str .= "$selector => " . $style->__toString() . "\n"; } } return $str; } }