public function addRule(Rule $oRule) { $sRule = $oRule->getRule(); if (!isset($this->aRules[$sRule])) { $this->aRules[$sRule] = array(); } $this->aRules[$sRule][] = $oRule; }
public function addRule(Rule $oRule, Rule $oSibling = null) { $sRule = $oRule->getRule(); if (!isset($this->aRules[$sRule])) { $this->aRules[$sRule] = array(); } $iPosition = count($this->aRules[$sRule]); if ($oSibling !== null) { $iSiblingPos = array_search($oSibling, $this->aRules[$sRule], true); if ($iSiblingPos !== false) { $iPosition = $iSiblingPos; } } array_splice($this->aRules[$sRule], $iPosition, 0, array($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->getRulesAssoc(); if (!isset($aRules['font-size']) || !isset($aRules['font-family'])) { return; } $oNewRule = new Rule('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 RuleValueList) { $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 RuleValueList) { $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 RuleValueList) { $aLHValues[] = $mRuleValue; } else { $aLHValues = $mRuleValue->getListComponents(); } if ($aLHValues[0] !== 'normal') { $val = new RuleValueList('/'); $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 RuleValueList) { $aFFValues[] = $mRuleValue; } else { $aFFValues = $mRuleValue->getListComponents(); } $oFFValue = new RuleValueList(','); $oFFValue->setListComponents($aFFValues); $oNewRule->addValue($oFFValue); $this->addRule($oNewRule); foreach ($aFontProperties as $sProperty) { $this->removeRule($sProperty); } }
private function parseRule() { $oRule = new Rule($this->parseIdentifier()); $this->consumeWhiteSpace(); $this->consume(':'); $oValue = $this->parseValue(self::listDelimiterForRule($oRule->getRule())); $oRule->setValue($oValue); if ($this->comes('!')) { $this->consume('!'); $this->consumeWhiteSpace(); $this->consume('important'); $oRule->setIsImportant(true); } while ($this->comes(';')) { $this->consume(';'); $this->consumeWhiteSpace(); } return $oRule; }
public function testRuleInsertion() { $sCss = '.wrapper { left: 10px; text-align: left; }'; $oParser = new Parser($sCss); $oDoc = $oParser->parse(); $aContents = $oDoc->getContents(); $oWrapper = $aContents[0]; $oFirst = $oWrapper->getRules('left'); $this->assertCount(1, $oFirst); $oFirst = $oFirst[0]; $oSecond = $oWrapper->getRules('text-'); $this->assertCount(1, $oSecond); $oSecond = $oSecond[0]; $oBefore = new Rule('left'); $oBefore->setValue(new Size(16, 'em')); $oMiddle = new Rule('text-align'); $oMiddle->setValue(new Size(1)); $oAfter = new Rule('border-bottom-width'); $oAfter->setValue(new Size(1, 'px')); $oWrapper->addRule($oAfter); $oWrapper->addRule($oBefore, $oFirst); $oWrapper->addRule($oMiddle, $oSecond); $aRules = $oWrapper->getRules(); $this->assertSame($oBefore, $aRules[0]); $this->assertSame($oFirst, $aRules[1]); $this->assertSame($oMiddle, $aRules[2]); $this->assertSame($oSecond, $aRules[3]); $this->assertSame($oAfter, $aRules[4]); $this->assertSame('.wrapper {left: 16em;left: 10px;text-align: 1;text-align: left;border-bottom-width: 1px;}', $oDoc->render()); }
private function parseRule() { $aComments = $this->consumeWhiteSpace(); $oRule = new Rule($this->parseIdentifier(), $this->iLineNo); $oRule->setComments($aComments); $oRule->addComments($this->consumeWhiteSpace()); $this->consume(':'); $oValue = $this->parseValue(self::listDelimiterForRule($oRule->getRule())); $oRule->setValue($oValue); if ($this->oParserSettings->bLenientParsing) { while ($this->comes('\\')) { $this->consume('\\'); $oRule->addIeHack($this->consume()); $this->consumeWhiteSpace(); } } if ($this->comes('!')) { $this->consume('!'); $this->consumeWhiteSpace(); $this->consume('important'); $oRule->setIsImportant(true); } while ($this->comes(';')) { $this->consume(';'); } return $oRule; }