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

add trusted pseudo-classes argument #39

Open
wants to merge 1 commit 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
22 changes: 18 additions & 4 deletions premailer/premailer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import cssutils
from lxml import etree
from lxml.cssselect import CSSSelector
from lxml.cssselect import CSSSelector, SelectorError
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What version of lxml added SelectorError?
Once we know that setup.py needs to be modified and look something like lxml>=x.y.z

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SelectorError available in lxml=>3.0 version.



__all__ = ['PremailerError', 'Premailer', 'transform']
Expand Down Expand Up @@ -86,6 +86,14 @@ def make_important(bulk):
for p in bulk.split(';'))


def is_valid_selector(selector):
try:
CSSSelector(selector)
return True
except SelectorError:
return False


_element_selector_regex = re.compile(r'(^|\s)\w')
_cdata_regex = re.compile(r'\<\!\[CDATA\[(.*?)\]\]\>', re.DOTALL)
_importants = re.compile('\s*!important')
Expand All @@ -105,11 +113,13 @@ def __init__(self, html, base_url=None,
strip_important=True,
external_styles=None,
method="html",
base_path=None):
base_path=None,
trust_pseudoclasses=False):
self.html = html
self.base_url = base_url
self.preserve_internal_links = preserve_internal_links
self.exclude_pseudoclasses = exclude_pseudoclasses
self.trust_pseudoclasses = trust_pseudoclasses
# whether to delete the <style> tag once it's been processed
self.keep_style_tags = keep_style_tags
self.remove_classes = remove_classes
Expand Down Expand Up @@ -148,7 +158,10 @@ def _parse_style_rules(self, css_body, ruleset_index):
if x.strip() and not x.strip().startswith('@')
)
for selector in selectors:
if (':' in selector and self.exclude_pseudoclasses and
trust_pseudo = (self.trust_pseudoclasses and
is_valid_selector(selector))
if (':' in selector and not trust_pseudo and
self.exclude_pseudoclasses and
':' + selector.split(':', 1)[1]
not in FILTER_PSEUDOSELECTORS):
# a pseudoclass
Expand Down Expand Up @@ -266,7 +279,8 @@ def transform(self, pretty_print=True, **kwargs):
new_selector, class_ = re.split(':', selector, 1)
class_ = ':%s' % class_
# Keep filter-type selectors untouched.
if class_ in FILTER_PSEUDOSELECTORS:
if (self.trust_pseudoclasses or
class_ in FILTER_PSEUDOSELECTORS):
class_ = ''
else:
selector = new_selector
Expand Down
80 changes: 80 additions & 0 deletions premailer/test_premailer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1441,3 +1441,83 @@ def test_external_styles_and_links():
result_html = whitespace_between_tags.sub('><', result_html).strip()

eq_(expect_html, result_html)


def test_trusted_pseudo():
html = """<html>
<head>
<style type="text/css">
div {
color: red;
}
div:not(:nth-child(1)) {
color: blue;
}
p:hover {
color: green;
}
</style>
</head>
<body>
<div>First child</div>
<p>Middle child</p>
<div>Last child</div>
</body>
</html>"""

expect_html = """<html>
<head>
</head>
<body>
<div style="color:red">First child</div>
<p>Middle child</p>
<div style="color:blue">Last child</div>
</body>
</html>"""

p = Premailer(html, trust_pseudoclasses=True)
result_html = p.transform()

whitespace_between_tags = re.compile('>\s*<',)

expect_html = whitespace_between_tags.sub('><', expect_html).strip()
result_html = whitespace_between_tags.sub('><', result_html).strip()

eq_(expect_html, result_html)

def test_trusted_pseudo_bad_selector():
html = """<html>
<head>
<style type="text/css">
div {
color: red;
}
div:not(:not(:first-child)) {
color: blue
}
</style>
</head>
<body>
<div>First child</div>
<div>Last child</div>
</body>
</html>"""

expect_html = """<html>
<head>
</head>
<body>
<div style="color:red">First child</div>
<div style="color:red">Last child</div>
</body>
</html>"""

p = Premailer(html, trust_pseudoclasses=True)
result_html = p.transform()

whitespace_between_tags = re.compile('>\s*<',)

expect_html = whitespace_between_tags.sub('><', expect_html).strip()
result_html = whitespace_between_tags.sub('><', result_html).strip()

eq_(expect_html, result_html)