peterbe / premailer

Turns CSS blocks into style attributes
https://premailer.io
BSD 3-Clause "New" or "Revised" License
1.07k stars 188 forks source link

Crashing when using on Semantic with exclude_pseudoclasses=False #184

Closed jddantes closed 7 years ago

jddantes commented 7 years ago

Tried running this script:

# Just a test script for premailer

import premailer

html = """
    <html>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.9/semantic.min.css"/>
    <style>
        h1{ border: 1px solid blue}
        h1:hover {border: 1px solid green}

    </style>
    <h1>Hey</h1>
    </html>
"""

p = premailer.Premailer(html, exclude_pseudoclasses=False)
print(p.transform())

but it crashes and returns:

...
WARNING Property: Unknown Property name.
WARNING Property: Unknown Property name.
WARNING Property: Unknown Property name.
WARNING Property: Unknown Property name.
WARNING Property: Unknown Property name.
WARNING Property: Unknown Property name.
WARNING Property: Unknown Property name.
WARNING Property: Unknown Property name.
Traceback (most recent call last):
  File "prem.py", line 18, in <module>
    print(p.transform())
  File "/home/jddantes/.conda/envs/pulse/lib/python3.6/site-packages/premailer/premailer.py", line 404, in transform
    sel = CSSSelector(selector)
  File "/home/jddantes/.conda/envs/pulse/lib/python3.6/site-packages/lxml/cssselect.py", line 94, in __init__
    path = translator.css_to_xpath(css)
  File "/home/jddantes/.conda/envs/pulse/lib/python3.6/site-packages/cssselect/xpath.py", line 192, in css_to_xpath
    for selector in parse(css))
  File "/home/jddantes/.conda/envs/pulse/lib/python3.6/site-packages/cssselect/parser.py", line 355, in parse
    return list(parse_selector_group(stream))
  File "/home/jddantes/.conda/envs/pulse/lib/python3.6/site-packages/cssselect/parser.py", line 370, in parse_selector_group
    yield Selector(*parse_selector(stream))
  File "/home/jddantes/.conda/envs/pulse/lib/python3.6/site-packages/cssselect/parser.py", line 378, in parse_selector
    result, pseudo_element = parse_simple_selector(stream)
  File "/home/jddantes/.conda/envs/pulse/lib/python3.6/site-packages/cssselect/parser.py", line 480, in parse_simple_selector
    "Expected selector, got %s" % (stream.peek(),))
cssselect.parser.SelectorSyntaxError: Expected selector, got <EOF at 0>

premailer==3.0.0 lxml==3.8.0

peterbe commented 7 years ago

I managed to reproduce it with:

html = """
    <html>
    <style>
        *,:after,:before{box-sizing:inherit}
        h1{ border: 1px solid blue}
        h1:hover {border: 1px solid green}

    </style>
    <h1>Hey</h1>
    </html>
"""

What happens is that the selector in the parsed rules becomes an empty string.

jddantes commented 7 years ago

Running the same script above gives a similar error:

WARNING Property: Unknown Property name. [1:24: -ms-text-size-adjust]
WARNING Property: Unknown Property name. [1:50: -webkit-text-size-adjust]
WARNING Property: Unknown Property name. [1:197: font-smoothing]
ERROR   Property: Invalid value for "CSS Level 2.1" property: calc(2rem - 0.142857em) 0 1rem [1:89: margin]
ERROR   Property: Invalid value for "CSS Level 2.1" property: 1rem [1:1: min-height]
ERROR   Property: Invalid value for "CSS Level 2.1" property: 2rem [1:17: font-size]
Traceback (most recent call last):
  File "prem.py", line 18, in <module>
    print(p.transform())
  File "/home/jddantes/.conda/envs/pulse/lib/python3.6/site-packages/premailer/premailer.py", line 409, in transform
    sel = CSSSelector(selector)
  File "/home/jddantes/.conda/envs/pulse/lib/python3.6/site-packages/lxml/cssselect.py", line 94, in __init__
    path = translator.css_to_xpath(css)
  File "/home/jddantes/.conda/envs/pulse/lib/python3.6/site-packages/cssselect/xpath.py", line 192, in css_to_xpath
    for selector in parse(css))
  File "/home/jddantes/.conda/envs/pulse/lib/python3.6/site-packages/cssselect/parser.py", line 355, in parse
    return list(parse_selector_group(stream))
  File "/home/jddantes/.conda/envs/pulse/lib/python3.6/site-packages/cssselect/parser.py", line 370, in parse_selector_group
    yield Selector(*parse_selector(stream))
  File "/home/jddantes/.conda/envs/pulse/lib/python3.6/site-packages/cssselect/parser.py", line 378, in parse_selector
    result, pseudo_element = parse_simple_selector(stream)
  File "/home/jddantes/.conda/envs/pulse/lib/python3.6/site-packages/cssselect/parser.py", line 477, in parse_simple_selector
    "Expected selector, got %s" % (peek,))
cssselect.parser.SelectorSyntaxError: Expected selector, got <NUMBER '.500' at 6>