Skip to content

Commit

Permalink
[+]: "Removed $cssRules as property, converted to references to a loc…
Browse files Browse the repository at this point in the history
…al variable returned by processCSS." | thx @jproffitt3ge

-> jproffitt3ge@c017ab8
  • Loading branch information
Lars Moelleken committed Oct 4, 2015
1 parent 5cfdd6f commit 64d5924
Showing 1 changed file with 11 additions and 27 deletions.
38 changes: 11 additions & 27 deletions src/CssToInlineStyles.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,6 @@ class CssToInlineStyles
*/
private $css;

/**
* The processed CSS rules
*
* @var array
*/
private $cssRules = array();

/**
* Should the generated HTML be cleaned
*
Expand Down Expand Up @@ -97,12 +90,6 @@ class CssToInlineStyles
*/
private $excludeConditionalInlineStylesBlock = true;

/**
* Whether css was already processed and there's no need to do it again
* @var bool
*/
private $isCssProcessed = false;

/**
* Exclude media queries from "$this->css" and keep media queries for inline-styles blocks
*
Expand Down Expand Up @@ -147,8 +134,6 @@ public function setHTML($html)
public function setCSS($css)
{
$this->css = (string)$css;

$this->isCssProcessed = false;
}

/**
Expand Down Expand Up @@ -197,15 +182,13 @@ public function convert($outputXHTML = false)
}

// process css
if (!$this->isCssProcessed) {
$this->processCSS();
}
$cssRules = $this->processCSS();

// create new DOMDocument
$document = $this->createDOMDocument();

// create new XPath
$xPath = $this->createXPath($document);
$xPath = $this->createXPath($document, $cssRules);

// strip original style tags if we need to
if ($this->stripOriginalStyleTags === true) {
Expand Down Expand Up @@ -272,7 +255,7 @@ public function getCssFromInlineHtmlStyleBlock()
private function processCSS()
{
//reset current set of rules
$this->cssRules = array();
$cssRules = array();

// init vars
$css = (string)$this->css;
Expand Down Expand Up @@ -342,19 +325,19 @@ private function processCSS()
$ruleSet['order'] = $i;

// add into rules
$this->cssRules[] = $ruleSet;
$cssRules[] = $ruleSet;

// increment
$i++;
}
}

// sort based on specificity
if (0 !== count($this->cssRules)) {
usort($this->cssRules, array(__CLASS__, 'sortOnSpecificity'));
if (0 !== count($cssRules)) {
usort($cssRules, array(__CLASS__, 'sortOnSpecificity'));
}

$this->isCssProcessed = true;
return $cssRules;
}

/**
Expand Down Expand Up @@ -496,17 +479,18 @@ private function getEncoding()
* create XPath
*
* @param \DOMDocument $document
* @param array $cssRules
*
* @return \DOMXPath
*/
private function createXPath(\DOMDocument $document)
private function createXPath(\DOMDocument $document, Array $cssRules)
{
$xPath = new \DOMXPath($document);

// any rules?
if (0 !== count($this->cssRules)) {
if (0 !== count($cssRules)) {
// loop rules
foreach ($this->cssRules as $rule) {
foreach ($cssRules as $rule) {

try {
$query = CssSelector::toXPath($rule['selector']);
Expand Down

0 comments on commit 64d5924

Please sign in to comment.