private function parseRule() { $oRule = new CSSRule($this->parseIdentifier()); $this->consumeWhiteSpace(); $this->consume(':'); $this->consumeWhiteSpace(); while (!($this->comes('}') || $this->comes(';') || $this->comes('!'))) { $oRule->addValue($this->parseValue()); $this->consumeWhiteSpace(); } if ($this->comes('!')) { $this->consume('!'); $this->consumeWhiteSpace(); $sImportantMarker = $this->consume(strlen('important')); if (mb_convert_case($sImportantMarker, MB_CASE_LOWER) !== 'important') { throw new Exception("! was followed by “" . $sImportantMarker . "”. Expected “important”"); } $oRule->setIsImportant(true); } if ($this->comes(';')) { $this->consume(';'); } return $oRule; }
/** * Looks for long format CSS font properties (e.g. <tt>font-weight</tt>) and * tries to convert them into a shorthand CSS <tt>font</tt> property. * At least font-size AND font-family must be present in order to create a shorthand declaration. **/ public function createFontShorthand() { $aFontProperties = array('font-style', 'font-variant', 'font-weight', 'font-size', 'line-height', 'font-family'); $aRules = $this->getRules(); if (!isset($aRules['font-size']) || !isset($aRules['font-family'])) { return; } $oNewRule = new CSSRule('font'); foreach (array('font-style', 'font-variant', 'font-weight') as $sProperty) { if (isset($aRules[$sProperty])) { $oRule = $aRules[$sProperty]; $mRuleValue = $oRule->getValue(); $aValues = array(); if (!$mRuleValue instanceof CSSRuleValueList) { $aValues[] = $mRuleValue; } else { $aValues = $mRuleValue->getListComponents(); } if ($aValues[0] !== 'normal') { $oNewRule->addValue($aValues[0]); } } } // Get the font-size value $oRule = $aRules['font-size']; $mRuleValue = $oRule->getValue(); $aFSValues = array(); if (!$mRuleValue instanceof CSSRuleValueList) { $aFSValues[] = $mRuleValue; } else { $aFSValues = $mRuleValue->getListComponents(); } // But wait to know if we have line-height to add it if (isset($aRules['line-height'])) { $oRule = $aRules['line-height']; $mRuleValue = $oRule->getValue(); $aLHValues = array(); if (!$mRuleValue instanceof CSSRuleValueList) { $aLHValues[] = $mRuleValue; } else { $aLHValues = $mRuleValue->getListComponents(); } if ($aLHValues[0] !== 'normal') { $val = new CSSRuleValueList('/'); $val->addListComponent($aFSValues[0]); $val->addListComponent($aLHValues[0]); $oNewRule->addValue($val); } } else { $oNewRule->addValue($aFSValues[0]); } $oRule = $aRules['font-family']; $mRuleValue = $oRule->getValue(); $aFFValues = array(); if (!$mRuleValue instanceof CSSRuleValueList) { $aFFValues[] = $mRuleValue; } else { $aFFValues = $mRuleValue->getListComponents(); } $oFFValue = new CSSRuleValueList(','); $oFFValue->setListComponents($aFFValues); $oNewRule->addValue($oFFValue); $this->addRule($oNewRule); foreach ($aFontProperties as $sProperty) { $this->removeRule($sProperty); } }