Skip to content

Commit b9bd23d

Browse files
author
Raphael Schweikert
committed
added the ability to filter CSSDocument->getAllValues()
all CSSSizes are now stored as floats parseColorValue stores color values as CSSSizes inside CSSColor even when using #rrggbb notation
1 parent e7ab942 commit b9bd23d

File tree

1 file changed

+40
-14
lines changed

1 file changed

+40
-14
lines changed

CSSParser.php

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ private function parseColorValue() {
288288
if(mb_strlen($sValue, $this->sCharset) === 3) {
289289
$sValue = $sValue[0].$sValue[0].$sValue[1].$sValue[1].$sValue[2].$sValue[2];
290290
}
291-
$aColor = array('r' => intval($sValue[0].$sValue[1], 16), 'g' => intval($sValue[2].$sValue[3], 16), 'b' => intval($sValue[4].$sValue[5], 16));
291+
$aColor = array('r' => new CSSSize(intval($sValue[0].$sValue[1], 16)), 'g' => new CSSSize(intval($sValue[2].$sValue[3], 16)), 'b' => new CSSSize(intval($sValue[4].$sValue[5], 16)));
292292
} else {
293293
$sColorMode = $this->parseIdentifier();
294294
$this->consumeWhiteSpace();
@@ -448,14 +448,14 @@ protected function allRuleSets(&$aResult) {
448448
}
449449
}
450450

451-
protected function allValues($oElement, &$aResult) {
451+
protected function allValues($oElement, &$aResult, $sSearchString = null) {
452452
if($oElement instanceof CSSList) {
453453
foreach($oElement->getContents() as $oContent) {
454-
$this->allValues($oContent, $aResult);
454+
$this->allValues($oContent, $aResult, $sSearchString);
455455
}
456456
} else if($oElement instanceof CSSRuleSet) {
457-
foreach($oElement->getRules() as $oRule) {
458-
$this->allValues($oRule, $aResult);
457+
foreach($oElement->getRules($sSearchString) as $oRule) {
458+
$this->allValues($oRule, $aResult, $sSearchString);
459459
}
460460
} else if($oElement instanceof CSSRule) {
461461
foreach($oElement->getValues() as $aValues) {
@@ -480,12 +480,16 @@ public function getAllRuleSets() {
480480
return $aResult;
481481
}
482482

483-
public function getAllValues($oElement = null) {
484-
if($oElement === null) {
485-
$oElement = $this;
483+
public function getAllValues($mElement = null) {
484+
$sSearchString = null;
485+
if($mElement === null) {
486+
$mElement = $this;
487+
} else if(is_string($mElement)) {
488+
$sSearchString = $mElement;
489+
$mElement = $this;
486490
}
487491
$aResult = array();
488-
$this->allValues($oElement, $aResult);
492+
$this->allValues($mElement, $aResult, $sSearchString);
489493
return $aResult;
490494
}
491495
}
@@ -567,8 +571,25 @@ public function addRule(CSSRule $oRule) {
567571
$this->aRules[$oRule->getRule()] = $oRule;
568572
}
569573

570-
public function getRules() {
571-
return $this->aRules;
574+
public function getRules($mRule = null) {
575+
if($mRule === null) {
576+
return $this->aRules;
577+
}
578+
$aResult = array();
579+
if($mRule instanceof CSSRule) {
580+
$mRule = $mRule->getRule();
581+
}
582+
if(strrpos($mRule, '-')===strlen($mRule)-strlen('-')) {
583+
$sStart = substr($mRule, 0, -1);
584+
foreach($this->aRules as $oRule) {
585+
if($oRule->getRule() === $sStart || strpos($oRule->getRule(), $mRule) === 0) {
586+
$aResult[$oRule->getRule()] = $this->aRules[$oRule->getRule()];
587+
}
588+
}
589+
} else if(isset($this->aRules[$mRule])) {
590+
$aResult[$mRule] = $this->aRules[$mRule];
591+
}
592+
return $aResult;
572593
}
573594

574595
public function removeRule($mRule) {
@@ -600,6 +621,7 @@ class CSSAtRule extends CSSRuleSet {
600621
private $sType;
601622

602623
public function __construct($sType) {
624+
parent::__construct();
603625
$this->sType = $sType;
604626
}
605627

@@ -702,8 +724,8 @@ class CSSSize extends CSSValue {
702724
private $fSize;
703725
private $sUnit;
704726

705-
public function __construct($fSize, $sUnit) {
706-
$this->fSize = $fSize;
727+
public function __construct($fSize, $sUnit = null) {
728+
$this->fSize = floatval($fSize);
707729
$this->sUnit = $sUnit;
708730
}
709731

@@ -753,8 +775,12 @@ public function getColor() {
753775
return $this->aColor;
754776
}
755777

778+
public function getColorDescription() {
779+
return implode('', array_keys($this->aColor));
780+
}
781+
756782
public function __toString() {
757-
return implode('', array_keys($this->aColor)).'('.implode(', ', $this->aColor).')';
783+
return $this->getColorDescription().'('.implode(', ', $this->aColor).')';
758784
}
759785
}
760786

0 commit comments

Comments
 (0)