Merge pull request #138 from lavr/merge-styles-refactoring

Refactor csstext_to_pairs. Fixes #133
This commit is contained in:
Peter Bengtsson 2015-09-27 21:41:56 -07:00
commit b65fa6ebbe
2 changed files with 12 additions and 10 deletions

View File

@ -1,5 +1,6 @@
import cssutils
import threading
from operator import itemgetter
def csstext_to_pairs(csstext):
@ -10,11 +11,10 @@ def csstext_to_pairs(csstext):
# The lock is required to avoid ``cssutils`` concurrency
# issues documented in issue #65
with csstext_to_pairs._lock:
parsed = cssutils.css.CSSVariablesDeclaration(csstext)
return [
(key.strip(), parsed.getVariableValue(key).strip())
for key in sorted(parsed)
]
return sorted([(prop.name.strip(), prop.propertyValue.cssText.strip())
for prop in cssutils.parseStyle(csstext)],
key=itemgetter(0))
csstext_to_pairs._lock = threading.RLock()

View File

@ -1,7 +1,5 @@
from __future__ import absolute_import, unicode_literals
import unittest
import xml
from nose.tools import raises
from premailer.merge_style import csstext_to_pairs, merge_styles
@ -14,9 +12,13 @@ class TestMergeStyle(unittest.TestCase):
parsed_csstext = csstext_to_pairs(csstext)
self.assertEqual(('font-size', '1px'), parsed_csstext[0])
@raises(xml.dom.SyntaxErr)
def test_inline_invalid_syntax(self):
# inline shouldn't have those as I understand
# but keep the behaviour
# Invalid syntax does not raise
inline = '{color:pink} :hover{color:purple} :active{color:red}'
merge_styles(inline, [], [])
def test_important_rule(self):
# No exception after #133
csstext = 'font-size:1px !important'
parsed_csstext = csstext_to_pairs(csstext)
self.assertEqual(('font-size', '1px'), parsed_csstext[0])