Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix merge_styles for proper ordering of styles w.r.t. "constituent" styles #267

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion premailer/merge_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,13 @@ def merge_styles(inline_style, new_styles, classes, remove_unset_properties=Fals

# keep always the old inline style
if inline_style:
normal_styles_dict = styles[""]
# inline should be a declaration list as I understand
# ie property-name:property-value;...
for k, v in csstext_to_pairs(inline_style):
styles[""][k] = v
if k in normal_styles_dict:
del normal_styles_dict[k]
normal_styles_dict[k] = v

normal_styles = []
pseudo_styles = []
Expand Down
12 changes: 12 additions & 0 deletions premailer/tests/test_merge_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,15 @@ def test_inline_invalid_syntax(self):
# Invalid syntax does not raise
inline = "{color:pink} :hover{color:purple} :active{color:red}"
merge_styles(inline, [], [])

def test_constituent_styles(self):
# "constituent": `margin-bottom` is a constituent style of `margin`
new_styles = [[("margin", "5px"), ("margin-bottom", "10px")]]
classes = [""]
inline_style = "margin: 0"
csstext = merge_styles(inline_style, new_styles, classes)
self.assertEqual(
# ideally premailer could eliminate margin-bottom altogether
[("margin-bottom", "10px"), ("margin", "0")],
csstext_to_pairs(csstext),
)