Skip to content

Commit 59ed16b

Browse files
Dan Schaeferroot
authored andcommitted
Re-wrote a few __toString methods to account for CSSSize and CSSColor types where calling "implode" would fail
Created a few CSSDocument helper methods to return a style when given a selector
1 parent c779825 commit 59ed16b

File tree

1 file changed

+90
-2
lines changed

1 file changed

+90
-2
lines changed

CSSParser.php

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,28 @@ protected function allValues($oElement, &$aResult, $sSearchString = null) {
465465
}
466466
}
467467
}
468+
469+
/**
470+
* Does the same thing as $this->allRuleSets except it creates
471+
* an associative array with the selector as the keys. If there
472+
* are multiple selectors in the rule set, then each one will
473+
* have their own array element.
474+
*
475+
* @param array $aResult
476+
*
477+
* @author dschaefer 12/23/2010
478+
*/
479+
protected function allUniqueRuleSets(&$aResult){
480+
foreach($this->aContents as $mContent) {
481+
if($mContent instanceof CSSRuleSet) {
482+
foreach($mContent->getSelector() as $aSelector){
483+
$aResult[$aSelector] = $mContent;
484+
}
485+
} else if($mContent instanceof CSSList) {
486+
$mContent->allRuleSets($aResult);
487+
}
488+
}
489+
}
468490
}
469491

470492
class CSSDocument extends CSSList {
@@ -492,6 +514,44 @@ public function getAllValues($mElement = null) {
492514
$this->allValues($mElement, $aResult, $sSearchString);
493515
return $aResult;
494516
}
517+
518+
/**
519+
* Does the same thing as $this->getAllRuleSets except it uses
520+
* CSSList->allUniqueRuleSets. This will generate an associative
521+
* array of rule sets with the selector as the key.
522+
*
523+
* @return array
524+
*/
525+
public function getAllUniqueRuleSets(){
526+
$aResult = array();
527+
$this->allUniqueRuleSets($aResult);
528+
return $aResult;
529+
}
530+
531+
/**
532+
* Gets just the style, not the entire rule set of the given
533+
* selector
534+
* @param string $sSelector
535+
* @return string
536+
*/
537+
public function getStyle($sSelector=""){
538+
$sResult = "";
539+
if($sSelector){
540+
$aRuleSets = $this->getAllUniqueRuleSets();
541+
$aSelectors = explode(",", $sSelector);
542+
foreach($aSelectors as $sSelector){
543+
$sSelector = trim($sSelector);
544+
if(isset($aRuleSets[$sSelector])){
545+
$oRuleSet = $aRuleSets[$sSelector];
546+
$aRules = $oRuleSet->getRules();
547+
foreach($aRules as $oRule){
548+
$sResult .= $oRule->__toString()." ";
549+
}
550+
}
551+
}
552+
}
553+
return $sResult;
554+
}
495555
}
496556

497557
class CSSMediaQuery extends CSSList {
@@ -704,11 +764,24 @@ public function getIsImportant() {
704764
public function __toString() {
705765
$sResult = "{$this->sRule}: ";
706766
foreach($this->aValues as $aValues) {
707-
$sResult .= implode(', ', $aValues).' ';
767+
//Rewrote to account for CSSSize and CSSColor- dschaefer 12/22/2010
768+
reset($aValues);
769+
while($mValue = current($aValues)){
770+
if(gettype($mValue) != 'object' && gettype($mValue) != 'array'){
771+
$sResult .= $mValue." ";
772+
}else if(gettype($mValue) == 'object'){
773+
$sResult .= $mValue->__toString()." ";
774+
}
775+
if(next($aValues)){
776+
$sResult .= ', ';
777+
}
778+
}
779+
//$sResult .= implode(', ', $aValues).' ';
708780
}
709781
if($this->bIsImportant) {
710782
$sResult .= '!important';
711783
} else {
784+
//What does this accomplish? Should this remove any semi-colons instead of removing the last character?
712785
$sResult = substr($sResult, 0, -1);
713786
}
714787
$sResult .= ';';
@@ -780,7 +853,22 @@ public function getColorDescription() {
780853
}
781854

782855
public function __toString() {
783-
return $this->getColorDescription().'('.implode(', ', $this->aColor).')';
856+
//Rewrote to account for CSSSize - dschaefer 12/22/2010
857+
$sResult = $this->getColorDescription().'(';
858+
reset($this->aColor);
859+
while($mColor = current($this->aColor)){
860+
if(gettype($mColor) != "object"){
861+
$sResult .= $mColor;
862+
}else{
863+
$sResult .= $mColor->__toString();
864+
}
865+
if(next($this->aColor)){
866+
$sResult .= ', ';
867+
}
868+
}
869+
$sResult .= ')';
870+
return $sResult;
871+
//return $this->getColorDescription().'('.implode(', ', $this->aColor).')';
784872
}
785873
}
786874

0 commit comments

Comments
 (0)