Skip to content

Commit

Permalink
[~]: "Refactor the storage of the computed properties" | thx @stof
Browse files Browse the repository at this point in the history
  • Loading branch information
Lars Moelleken committed Jul 25, 2016
1 parent 33d351c commit 8596bdd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
17 changes: 6 additions & 11 deletions src/CssToInlineStyles.php
Expand Up @@ -536,6 +536,7 @@ private function splitIntoProperties($styles)
*/
private function createXPath(\DOMDocument $document, array $cssRules)
{
$propertyStorage = new \SplObjectStorage();
$xPath = new \DOMXPath($document);

// any rules?
Expand Down Expand Up @@ -593,7 +594,7 @@ private function createXPath(\DOMDocument $document, array $cssRules)
}

// no styles stored?
if (null === $element->attributes->getNamedItem('data-css-to-inline-styles-original-styles')) {
if (!isset($propertyStorage[$element])) {

// init var
$originalStyle = $element->attributes->getNamedItem('style');
Expand All @@ -605,7 +606,7 @@ private function createXPath(\DOMDocument $document, array $cssRules)
}

// store original styles
$element->setAttribute('data-css-to-inline-styles-original-styles', $originalStyle);
$propertyStorage->attach($element, $originalStyle);

// clear the styles
$element->setAttribute('style', '');
Expand All @@ -619,13 +620,10 @@ private function createXPath(\DOMDocument $document, array $cssRules)
}
}

// reapply original styles (search elements)
$elements = $xPath->query('//*[@data-css-to-inline-styles-original-styles]');
//var_dump($propertyStorage);

// loop found elements
foreach ($elements as $element) {
// get the original styles
$originalStyle = $element->attributes->getNamedItem('data-css-to-inline-styles-original-styles')->value;
foreach ($propertyStorage as $element) {
$originalStyle = $propertyStorage->getInfo();

if ($originalStyle) {
$originalStyles = $this->splitIntoProperties($originalStyle);
Expand All @@ -638,9 +636,6 @@ private function createXPath(\DOMDocument $document, array $cssRules)
$element->setAttribute('style', $propertiesString);
}
}

// remove placeholder
$element->removeAttribute('data-css-to-inline-styles-original-styles');
}
}

Expand Down
6 changes: 5 additions & 1 deletion tests/CssToInlineStylesTest.php
Expand Up @@ -56,7 +56,11 @@ public function testSimpleElementSelectorWithUtf8()
#東とう京{ color: black !important}
';
$expected = '<div id="東とう京" class="ɹǝddɐɹʍ" style="display: none; color: black !important;"></div>';
$this->runHTMLToCSS($html, $css, $expected);

for ($i = 1; $i <= 2; $i++) { // keep this loop for quick&dirty performance tests
$this->runHTMLToCSS($html, $css, $expected);
}

}

/**
Expand Down

0 comments on commit 8596bdd

Please sign in to comment.