/usr/share/php/Horde/Css/Parser/vendor/sabberworm/php-css-parser/lib/Sabberworm/CSS/RuleSet/RuleSet.php is in php-horde-css-parser 1.0.8-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | <?php
namespace Sabberworm\CSS\RuleSet;
use Sabberworm\CSS\Rule\Rule;
use Sabberworm\CSS\Renderable;
/**
* RuleSet is a generic superclass denoting rules. The typical example for rule sets are declaration block.
* However, unknown At-Rules (like @font-face) are also rule sets.
*/
abstract class RuleSet implements Renderable {
private $aRules;
public function __construct() {
$this->aRules = array();
}
public function addRule(Rule $oRule) {
$sRule = $oRule->getRule();
if(!isset($this->aRules[$sRule])) {
$this->aRules[$sRule] = array();
}
$this->aRules[$sRule][] = $oRule;
}
/**
* Returns all rules matching the given rule name
* @param (null|string|Rule) $mRule pattern to search for. If null, returns all rules. if the pattern ends with a dash, all rules starting with the pattern are returned as well as one matching the pattern with the dash excluded. passing a Rule behaves like calling getRules($mRule->getRule()).
* @example $oRuleSet->getRules('font-') //returns an array of all rules either beginning with font- or matching font.
* @example $oRuleSet->getRules('font') //returns array(0 => $oRule, …) or array().
*/
public function getRules($mRule = null) {
if ($mRule instanceof Rule) {
$mRule = $mRule->getRule();
}
$aResult = array();
foreach($this->aRules as $sName => $aRules) {
// Either no search rule is given or the search rule matches the found rule exactly or the search rule ends in “-” and the found rule starts with the search rule.
if(!$mRule || $sName === $mRule || (strrpos($mRule, '-') === strlen($mRule) - strlen('-') && (strpos($sName, $mRule) === 0 || $sName === substr($mRule, 0, -1)))) {
$aResult = array_merge($aResult, $aRules);
}
}
return $aResult;
}
/**
* Returns all rules matching the given pattern and returns them in an associative array with the rule’s name as keys. This method exists mainly for backwards-compatibility and is really only partially useful.
* @param (string) $mRule pattern to search for. If null, returns all rules. if the pattern ends with a dash, all rules starting with the pattern are returned as well as one matching the pattern with the dash excluded. passing a Rule behaves like calling getRules($mRule->getRule()).
* Note: This method loses some information: Calling this (with an argument of 'background-') on a declaration block like { background-color: green; background-color; rgba(0, 127, 0, 0.7); } will only yield an associative array containing the rgba-valued rule while @link{getRules()} would yield an indexed array containing both.
*/
public function getRulesAssoc($mRule = null) {
$aResult = array();
foreach($this->getRules($mRule) as $oRule) {
$aResult[$oRule->getRule()] = $oRule;
}
return $aResult;
}
/**
* Remove a rule from this RuleSet. This accepts all the possible values that @link{getRules()} accepts. If given a Rule, it will only remove this particular rule (by identity). If given a name, it will remove all rules by that name. Note: this is different from pre-v.2.0 behaviour of PHP-CSS-Parser, where passing a Rule instance would remove all rules with the same name. To get the old behvaiour, use removeRule($oRule->getRule()).
* @param (null|string|Rule) $mRule pattern to remove. If $mRule is null, all rules are removed. If the pattern ends in a dash, all rules starting with the pattern are removed as well as one matching the pattern with the dash excluded. Passing a Rule behaves matches by identity.
*/
public function removeRule($mRule) {
if($mRule instanceof Rule) {
$sRule = $mRule->getRule();
if(!isset($this->aRules[$sRule])) {
return;
}
foreach($this->aRules[$sRule] as $iKey => $oRule) {
if($oRule === $mRule) {
unset($this->aRules[$sRule][$iKey]);
}
}
} else {
foreach($this->aRules as $sName => $aRules) {
// Either no search rule is given or the search rule matches the found rule exactly or the search rule ends in “-” and the found rule starts with the search rule or equals it (without the trailing dash).
if(!$mRule || $sName === $mRule || (strrpos($mRule, '-') === strlen($mRule) - strlen('-') && (strpos($sName, $mRule) === 0 || $sName === substr($mRule, 0, -1)))) {
unset($this->aRules[$sName]);
}
}
}
}
public function __toString() {
return $this->render(new \Sabberworm\CSS\OutputFormat());
}
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
$sResult = '';
$bIsFirst = true;
foreach ($this->aRules as $aRules) {
foreach($aRules as $oRule) {
$sRendered = $oOutputFormat->safely(function() use ($oRule, $oOutputFormat) {
return $oRule->render($oOutputFormat->nextLevel());
});
if($sRendered === null) {
continue;
}
if($bIsFirst) {
$bIsFirst = false;
$sResult .= $oOutputFormat->nextLevel()->spaceBeforeRules();
} else {
$sResult .= $oOutputFormat->nextLevel()->spaceBetweenRules();
}
$sResult .= $sRendered;
}
}
if(!$bIsFirst) {
// Had some output
$sResult .= $oOutputFormat->spaceAfterRules();
}
return $oOutputFormat->removeLastSemicolon($sResult);
}
}
|