Skip to content

Commit

Permalink
Merge pull request #16 from facelessuser/select-fix
Browse files Browse the repository at this point in the history
Select matches children of a given tag, not the tag itself
  • Loading branch information
facelessuser authored Dec 15, 2018
2 parents fed8c95 + b9e4710 commit 1756b28
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 6 deletions.
5 changes: 5 additions & 0 deletions docs/src/markdown/about/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 1.0.1

- **FIX**: When giving a tag to `select`, it should only return the children of that tag, never the tag itself.
- **FIX**: For informational purposes, raise a `NotImplementedError` when an unsupported pseudo class is used.

## 1.0.0

- **NEW**: Official 1.0.0 release.
Expand Down
2 changes: 1 addition & 1 deletion soupsieve/__meta__.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,5 +186,5 @@ def parse_version(ver, pre=False):
return Version(major, minor, micro, release, pre, post, dev)


__version_info__ = Version(1, 0, 0, "final")
__version_info__ = Version(1, 0, 1, "final")
__version__ = __version_info__._get_canonical()
3 changes: 0 additions & 3 deletions soupsieve/css_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,9 +496,6 @@ def __init__(self, pattern, selectors, namespaces, flags):
def _walk(self, node, capture=True, comments=False):
"""Recursively return selected tags."""

if capture and self.match(node):
yield node

# Walk children
for child in node.descendants:
if capture and isinstance(child, util.TAG) and self.match(child):
Expand Down
7 changes: 6 additions & 1 deletion soupsieve/css_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@

PAT_SPLIT = r'{ws}*?(?P<relation>[,+>~]|{ws}(?![,+>~])){ws}*'.format(ws=WS)

PAT_INVALID_PSEUDO = r':[a-z0-9-]+'

# Extra selector patterns
PAT_CONTAINS = r':contains\({ws}*{value}{ws}*\)'.format(ws=WS, value=VALUE)

Expand Down Expand Up @@ -181,6 +183,7 @@ class CSSParser:
("contains", SelectorPattern(PAT_CONTAINS)),
("pseudo_nth_child", SelectorPattern(PAT_PSEUDO_NTH_CHILD)),
("pseudo_nth_type", SelectorPattern(PAT_PSEUDO_NTH_TYPE)),
("pseudo_invalid", SelectorPattern(PAT_INVALID_PSEUDO)),
("id", SelectorPattern(PAT_ID)),
("class", SelectorPattern(PAT_CLASS)),
("tag", SelectorPattern(PAT_TAG)),
Expand Down Expand Up @@ -463,7 +466,9 @@ def parse_selectors(self, iselector, is_pseudo=False, is_not=False, is_has=False
key, m = next(iselector)

# Handle parts
if key == 'pseudo':
if key == 'pseudo_invalid':
raise NotImplementedError("'{}' pseudo class is not implemented".format(m.group(0)))
elif key == 'pseudo':
has_selector = self.parse_pseudo(sel, m, has_selector)
elif key == 'contains':
has_selector = self.parse_contains(sel, m, has_selector)
Expand Down
8 changes: 7 additions & 1 deletion tests/test_soupsieve.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def test_select(self):

span = sv.select('span[id]', soup)[0]
ids = []
for el in sv.select('span[id]', span):
for el in sv.select('span[id]:not(#some-id)', span.parent):
ids.append(el.attrs['id'])

self.assertEqual(sorted(['5']), sorted(ids))
Expand Down Expand Up @@ -364,6 +364,12 @@ def test_invalid_combination(self):
with self.assertRaises(SyntaxError):
sv.compile('div >')

def test_invalid_pseudo(self):
"""Test invalid pseudo class."""

with self.assertRaises(NotImplementedError):
sv.compile(':before')

def test_invalid_pseudo_close(self):
"""Test invalid pseudo close."""

Expand Down

0 comments on commit 1756b28

Please sign in to comment.