When merge style, don't change the style items' order
This commit is contained in:
parent
ab28267201
commit
01f9967e85
|
@ -1,6 +1,11 @@
|
||||||
import cssutils
|
import cssutils
|
||||||
import threading
|
import threading
|
||||||
from operator import itemgetter
|
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):
|
def csstext_to_pairs(csstext):
|
||||||
|
@ -49,9 +54,9 @@ def merge_styles(
|
||||||
str: the final style
|
str: the final style
|
||||||
"""
|
"""
|
||||||
# building classes
|
# building classes
|
||||||
styles = {'': {}}
|
styles = OrderedDict([('', OrderedDict())])
|
||||||
for pc in set(classes):
|
for pc in set(classes):
|
||||||
styles[pc] = {}
|
styles[pc] = OrderedDict()
|
||||||
|
|
||||||
for i, style in enumerate(new_styles):
|
for i, style in enumerate(new_styles):
|
||||||
for k, v in style:
|
for k, v in style:
|
||||||
|
@ -71,7 +76,7 @@ def merge_styles(
|
||||||
# Remove rules that we were going to have value 'unset' because
|
# Remove rules that we were going to have value 'unset' because
|
||||||
# they effectively are the same as not saying anything about the
|
# they effectively are the same as not saying anything about the
|
||||||
# property when inlined
|
# property when inlined
|
||||||
kv = dict(
|
kv = OrderedDict(
|
||||||
(k, v) for (k, v) in kv.items() if not v.lower() == 'unset'
|
(k, v) for (k, v) in kv.items() if not v.lower() == 'unset'
|
||||||
)
|
)
|
||||||
if not kv:
|
if not kv:
|
||||||
|
@ -80,12 +85,12 @@ def merge_styles(
|
||||||
pseudo_styles.append(
|
pseudo_styles.append(
|
||||||
'%s{%s}' % (
|
'%s{%s}' % (
|
||||||
pseudoclass,
|
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:
|
else:
|
||||||
normal_styles.append('; '.join(
|
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:
|
if pseudo_styles:
|
||||||
|
|
|
@ -102,7 +102,7 @@ class Tests(unittest.TestCase):
|
||||||
def test_merge_styles_basic(self):
|
def test_merge_styles_basic(self):
|
||||||
inline_style = 'font-size:1px; color: red'
|
inline_style = 'font-size:1px; color: red'
|
||||||
new = 'font-size:2px; font-weight: bold'
|
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)], [''])
|
result = merge_styles(inline_style, [csstext_to_pairs(new)], [''])
|
||||||
for each in expect:
|
for each in expect:
|
||||||
ok_(each in result)
|
ok_(each in result)
|
||||||
|
@ -146,7 +146,7 @@ class Tests(unittest.TestCase):
|
||||||
def test_merge_styles_with_unset(self):
|
def test_merge_styles_with_unset(self):
|
||||||
inline_style = 'color: red'
|
inline_style = 'color: red'
|
||||||
new = 'font-size: 10px; font-size: unset; font-weight: bold'
|
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)
|
css_new = csstext_to_pairs(new)
|
||||||
result = merge_styles(
|
result = merge_styles(
|
||||||
inline_style,
|
inline_style,
|
||||||
|
@ -632,11 +632,11 @@ ple.com/bg.png); color:#123; font-family:Omerta">
|
||||||
'Paragraph</p>'
|
'Paragraph</p>'
|
||||||
self.fragment_in_html(e, result_html, True)
|
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)
|
self.fragment_in_html(e, result_html)
|
||||||
e = ' :visited{border:1px solid green}'
|
e = ' :visited{border:1px solid green}'
|
||||||
self.fragment_in_html(e, result_html)
|
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)
|
self.fragment_in_html(e, result_html)
|
||||||
|
|
||||||
def test_css_with_pseudoclasses_excluded(self):
|
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>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<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>
|
<p>Paragraph</p>
|
||||||
</body>
|
</body>
|
||||||
</html>"""
|
</html>"""
|
||||||
|
@ -1196,7 +1196,7 @@ ical-align:middle" bgcolor="red" valign="middle">Cell 2</td>
|
||||||
<head>
|
<head>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div id="identified" style="color:blue; font-size:22px"></div>
|
<div id="identified" style="font-size:22px; color:blue"></div>
|
||||||
</body>
|
</body>
|
||||||
</html>"""
|
</html>"""
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue