Merge pull request #160 from machinewu/master

When merge style, don't change the style items' order
This commit is contained in:
Peter Bengtsson 2016-03-15 13:39:55 -04:00
commit 7f223891e4
2 changed files with 16 additions and 11 deletions

View File

@ -1,6 +1,11 @@
import cssutils
import threading
from operator import itemgetter
try:
from collections import OrderedDict
except ImportError: # pragma: no cover
# some old python 2.6 thing then, eh?
from ordereddict import OrderedDict
def csstext_to_pairs(csstext):
@ -49,9 +54,9 @@ def merge_styles(
str: the final style
"""
# building classes
styles = {'': {}}
styles = OrderedDict([('', OrderedDict())])
for pc in set(classes):
styles[pc] = {}
styles[pc] = OrderedDict()
for i, style in enumerate(new_styles):
for k, v in style:
@ -71,7 +76,7 @@ def merge_styles(
# Remove rules that we were going to have value 'unset' because
# they effectively are the same as not saying anything about the
# property when inlined
kv = dict(
kv = OrderedDict(
(k, v) for (k, v) in kv.items() if not v.lower() == 'unset'
)
if not kv:
@ -80,12 +85,12 @@ def merge_styles(
pseudo_styles.append(
'%s{%s}' % (
pseudoclass,
'; '.join('%s:%s' % (k, v) for k, v in sorted(kv.items()))
'; '.join('%s:%s' % (k, v) for k, v in kv.items())
)
)
else:
normal_styles.append('; '.join(
'%s:%s' % (k, v) for k, v in sorted(kv.items())
'%s:%s' % (k, v) for k, v in kv.items()
))
if pseudo_styles:

View File

@ -102,7 +102,7 @@ class Tests(unittest.TestCase):
def test_merge_styles_basic(self):
inline_style = 'font-size:1px; color: red'
new = 'font-size:2px; font-weight: bold'
expect = 'color:red;', 'font-size:1px;', 'font-weight:bold'
expect = 'font-size:1px;', 'font-weight:bold;', 'color:red'
result = merge_styles(inline_style, [csstext_to_pairs(new)], [''])
for each in expect:
ok_(each in result)
@ -146,7 +146,7 @@ class Tests(unittest.TestCase):
def test_merge_styles_with_unset(self):
inline_style = 'color: red'
new = 'font-size: 10px; font-size: unset; font-weight: bold'
expect = 'color:red;', 'font-weight:bold'
expect = 'font-weight:bold;', 'color:red'
css_new = csstext_to_pairs(new)
result = merge_styles(
inline_style,
@ -632,11 +632,11 @@ ple.com/bg.png); color:#123; font-family:Omerta">
'Paragraph</p>'
self.fragment_in_html(e, result_html, True)
e = 'style="{border:1px solid green; color:red}'
e = 'style="{color:red; border:1px solid green}'
self.fragment_in_html(e, result_html)
e = ' :visited{border:1px solid green}'
self.fragment_in_html(e, result_html)
e = ' :hover{border:1px solid green; text-decoration:none}'
e = ' :hover{text-decoration:none; border:1px solid green}'
self.fragment_in_html(e, result_html)
def test_css_with_pseudoclasses_excluded(self):
@ -666,7 +666,7 @@ a:visited {border:1px solid green}p::first-letter {float:left;font-size:300%}
</style>
</head>
<body>
<a href="#" style="border:1px solid green; color:red">Page</a>
<a href="#" style="color:red; border:1px solid green">Page</a>
<p>Paragraph</p>
</body>
</html>"""
@ -1196,7 +1196,7 @@ ical-align:middle" bgcolor="red" valign="middle">Cell 2</td>
<head>
</head>
<body>
<div id="identified" style="color:blue; font-size:22px"></div>
<div id="identified" style="font-size:22px; color:blue"></div>
</body>
</html>"""