Skip to content

Commit dcb22de

Browse files
author
Peter Bengtsson
committed
Merge pull request #39 from peterbe/accept-pseudoclass-selectors-except-mouse-related
Accept pseudoclass selectors except mouse related
2 parents 61f8915 + d075c04 commit dcb22de

File tree

4 files changed

+70
-1
lines changed

4 files changed

+70
-1
lines changed

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ script:
3939
- nosetests
4040
- mincss https://news.ycombinator.com
4141

42+
branches:
43+
only:
44+
- master
45+
4246
deploy:
4347
provider: pypi
4448
user: peterbe

mincss/processor.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
RE_NESTS = re.compile('@(-|keyframes).*?({)', re.DOTALL | re.M)
3434
RE_CLASS_DEF = re.compile('\.([\w-]+)')
3535
RE_ID_DEF = re.compile('#([\w-]+)')
36+
MOUSE_PSEUDO_CLASSES = re.compile(
37+
':(link|hover|active|focus|visited)$', re.M | re.I
38+
)
3639

3740

3841
EXCEPTIONAL_SELECTORS = (
@@ -477,7 +480,9 @@ def _found(self, bodies, selector):
477480
return r
478481

479482
def _selector_query_found(self, bodies, selector):
480-
selector = selector.split(':')[0]
483+
# If the select has something like :active or :hover
484+
if MOUSE_PSEUDO_CLASSES.findall(selector) or '::' in selector:
485+
selector = selector.split(':')[0]
481486

482487
if '}' in selector:
483488
# XXX does this ever happen any more?

tests/nth-child.html

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset='utf-8'>
5+
<title>test page</title>
6+
<style>
7+
a.nothere:hover { font-weight: bold; }
8+
a.actually:hover { font-weight: bold; }
9+
10+
a.nothere:active { font-weight: bold; }
11+
a.actually:active { font-weight: bold; }
12+
13+
a.nothere:link { font-weight: bold; }
14+
a.actually:link { font-weight: bold; }
15+
16+
a.nothere:focus { font-weight: bold; }
17+
a.actually:focus { font-weight: bold; }
18+
19+
a.nothere:visited { font-weight: bold; }
20+
a.actually:visited { font-weight: bold; }
21+
22+
div > :first-child { color: pink; }
23+
div > :last-child { color: brown; }
24+
div > :not(p) { color: blue; }
25+
div > :nth-child(2) { color: red; }
26+
</style>
27+
</head>
28+
<body>
29+
<h1>h1</h1>
30+
<a href="@" class="actually">actually</a>
31+
<div>
32+
<p>First one</p>
33+
<p>Second one</p>
34+
<p>Third one</p>
35+
<span>Fourth one</span>
36+
<p>Fifth one</p>
37+
</div>
38+
39+
</body>
40+
</html>

tests/test_mincss.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,3 +314,23 @@ def test_make_absolute_url(self):
314314
p.make_absolute_url('http://www.com/elsewhere', './style.css'),
315315
'http://www.com/style.css'
316316
)
317+
318+
def test_nth_child(self):
319+
html = os.path.join(HERE, 'nth-child.html')
320+
url = 'file://' + html
321+
p = Processor()
322+
p.process(url)
323+
# print repr(p.inlines[0].before)
324+
after = p.inlines[0].after
325+
# These mouse related one should stay, even though they're
326+
# currently NOT being acted upon with some input device.
327+
ok_('a.actually:hover { font-weight: bold; }' in after)
328+
ok_('a.actually:visited { font-weight: bold; }' in after)
329+
ok_('a.actually:link { font-weight: bold; }' in after)
330+
ok_('a.actually:focus { font-weight: bold; }' in after)
331+
ok_('a.actually:active { font-weight: bold; }' in after)
332+
# the other selectors with : in them should also stay
333+
ok_('div > :first-child { color: pink; }' in after)
334+
ok_('div > :last-child { color: brown; }' in after)
335+
ok_('div > :not(p) { color: blue; }' in after)
336+
ok_('div > :nth-child(2) { color: red; }' in after)

0 commit comments

Comments
 (0)