From daca23b513f755298b166b328e97c0d830a9fcf1 Mon Sep 17 00:00:00 2001
From: Hugo
Date: Thu, 14 Dec 2017 17:13:19 +0200
Subject: [PATCH 001/108] Ignore IDE metadata
---
.gitignore | 1 +
1 file changed, 1 insertion(+)
diff --git a/.gitignore b/.gitignore
index 4c89f4c..b0ab86a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,3 +5,4 @@
/dist
/docs/_build
/.coverage
+.idea
\ No newline at end of file
From 83014a796af107b6eb934e085011ecdf85dc4c42 Mon Sep 17 00:00:00 2001
From: Hugo
Date: Thu, 14 Dec 2017 17:17:49 +0200
Subject: [PATCH 002/108] Drop support for EOL Python 2.6
---
.travis.yml | 1 -
README.rst | 4 ++--
cssselect/parser.py | 9 +--------
setup.py | 2 +-
tox.ini | 6 +-----
5 files changed, 5 insertions(+), 17 deletions(-)
diff --git a/.travis.yml b/.travis.yml
index cc709f1..61edf5a 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,6 +1,5 @@
language: python
python:
- - '2.6'
- '2.7'
- '3.3'
- '3.4'
diff --git a/README.rst b/README.rst
index 587c2d7..972b06b 100644
--- a/README.rst
+++ b/README.rst
@@ -17,9 +17,9 @@ extracted as a stand-alone project.
Quick facts:
* Free software: BSD licensed
-* Compatible with Python 2.6+ and 3.3+
+* Compatible with Python 2.7 and 3.3+
* Latest documentation `on Read the Docs `_
-* Source, issues and pull requests `on Github
+* Source, issues and pull requests `on GitHub
`_
* Releases `on PyPI `_
* Install with ``pip install cssselect``
diff --git a/cssselect/parser.py b/cssselect/parser.py
index fe5f53c..dd4709a 100644
--- a/cssselect/parser.py
+++ b/cssselect/parser.py
@@ -358,8 +358,6 @@ def parse(css):
# message = "%s at %s -> %r" % (
# e, stream.used, stream.peek())
# e.msg = message
-# if sys.version_info < (2,6):
-# e.message = message
# e.args = tuple([message])
# raise
@@ -630,12 +628,7 @@ def _compile(pattern):
_sub_newline_escape =re.compile(r'\\(?:\n|\r\n|\r|\f)').sub
# Same as r'\1', but faster on CPython
-if hasattr(operator, 'methodcaller'):
- # Python 2.6+
- _replace_simple = operator.methodcaller('group', 1)
-else:
- def _replace_simple(match):
- return match.group(1)
+_replace_simple = operator.methodcaller('group', 1)
def _replace_unicode(match):
codepoint = int(match.group(1), 16)
diff --git a/setup.py b/setup.py
index 199ffc7..032aa89 100644
--- a/setup.py
+++ b/setup.py
@@ -29,12 +29,12 @@
url='https://github.com/scrapy/cssselect',
license='BSD',
packages=['cssselect'],
+ python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*',
classifiers=[
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
'License :: OSI Approved :: BSD License',
'Programming Language :: Python :: 2',
- 'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.3',
diff --git a/tox.ini b/tox.ini
index 7a3359a..a019f4e 100644
--- a/tox.ini
+++ b/tox.ini
@@ -1,5 +1,5 @@
[tox]
-envlist = py25,py26,py27,py32,py33
+envlist = py27,py33,py34,py35,py36
[testenv]
deps=
@@ -9,7 +9,3 @@ deps=
commands =
py.test --cov-report term --cov=cssselect
-
-[testenv:py25]
-setenv =
- PIP_INSECURE = 1
From c040d86c5458547bbbf80c5fd4aa9ce771f85234 Mon Sep 17 00:00:00 2001
From: Hugo
Date: Thu, 14 Dec 2017 17:19:16 +0200
Subject: [PATCH 003/108] Use 'is' to compare with None
---
tests/test_cssselect.py | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/tests/test_cssselect.py b/tests/test_cssselect.py
index 4a0bd39..f01aa7f 100644
--- a/tests/test_cssselect.py
+++ b/tests/test_cssselect.py
@@ -288,12 +288,12 @@ def get_error(css):
"Expected string or ident, got ")
assert get_error('[href]a') == (
"Expected selector, got ")
- assert get_error('[rel=stylesheet]') == None
+ assert get_error('[rel=stylesheet]') is None
assert get_error('[rel:stylesheet]') == (
"Operator expected, got ")
assert get_error('[rel=stylesheet') == (
"Expected ']', got ")
- assert get_error(':lang(fr)') == None
+ assert get_error(':lang(fr)') is None
assert get_error(':lang(fr') == (
"Expected an argument, got ")
assert get_error(':contains("foo') == (
@@ -586,8 +586,8 @@ def series(css):
assert series('+n') == (1, 0)
assert series('-n') == (-1, 0)
assert series('5') == (0, 5)
- assert series('foo') == None
- assert series('n+') == None
+ assert series('foo') is None
+ assert series('n+') is None
def test_lang(self):
document = etree.fromstring(XMLLANG_IDS)
From 1060ca1f3f1746caad8673f0c99299a389f1bc7c Mon Sep 17 00:00:00 2001
From: Hugo
Date: Thu, 14 Dec 2017 17:20:08 +0200
Subject: [PATCH 004/108] Remove redundant parentheses
---
cssselect/parser.py | 10 +++++-----
cssselect/xpath.py | 2 +-
2 files changed, 6 insertions(+), 6 deletions(-)
diff --git a/cssselect/parser.py b/cssselect/parser.py
index dd4709a..9bb039c 100644
--- a/cssselect/parser.py
+++ b/cssselect/parser.py
@@ -552,14 +552,14 @@ def parse_series(tokens):
raise ValueError('String tokens not allowed in series.')
s = ''.join(token.value for token in tokens).strip()
if s == 'odd':
- return (2, 1)
+ return 2, 1
elif s == 'even':
- return (2, 0)
+ return 2, 0
elif s == 'n':
- return (1, 0)
+ return 1, 0
if 'n' not in s:
# Just b
- return (0, int(s))
+ return 0, int(s)
a, b = s.split('n', 1)
if not a:
a = 1
@@ -571,7 +571,7 @@ def parse_series(tokens):
b = 0
else:
b = int(b)
- return (a, b)
+ return a, b
#### Token objects
diff --git a/cssselect/xpath.py b/cssselect/xpath.py
index d0eb2cb..22cd029 100644
--- a/cssselect/xpath.py
+++ b/cssselect/xpath.py
@@ -490,7 +490,7 @@ def xpath_nth_child_function(self, xpath, function, last=False,
b_neg = (-b_min_1) % abs(a)
if b_neg != 0:
- b_neg = '+%s' % (b_neg)
+ b_neg = '+%s' % b_neg
left = '(%s %s)' % (left, b_neg)
expr.append('%s mod %s = 0' % (left, a))
From 6a53f24f3d3118d7e0ae86b2ed7521d6370608d4 Mon Sep 17 00:00:00 2001
From: Hugo
Date: Fri, 22 Dec 2017 10:55:55 +0200
Subject: [PATCH 005/108] Drop support for EOL Python 3.3
---
.travis.yml | 1 -
README.rst | 2 +-
setup.py | 3 +--
tox.ini | 2 +-
4 files changed, 3 insertions(+), 5 deletions(-)
diff --git a/.travis.yml b/.travis.yml
index 61edf5a..d86d0a8 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,7 +1,6 @@
language: python
python:
- '2.7'
- - '3.3'
- '3.4'
- '3.5'
- '3.6'
diff --git a/README.rst b/README.rst
index 972b06b..c19c6b3 100644
--- a/README.rst
+++ b/README.rst
@@ -17,7 +17,7 @@ extracted as a stand-alone project.
Quick facts:
* Free software: BSD licensed
-* Compatible with Python 2.7 and 3.3+
+* Compatible with Python 2.7 and 3.4+
* Latest documentation `on Read the Docs `_
* Source, issues and pull requests `on GitHub
`_
diff --git a/setup.py b/setup.py
index 032aa89..243927d 100644
--- a/setup.py
+++ b/setup.py
@@ -29,7 +29,7 @@
url='https://github.com/scrapy/cssselect',
license='BSD',
packages=['cssselect'],
- python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*',
+ python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*',
classifiers=[
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
@@ -37,7 +37,6 @@
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
- 'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
diff --git a/tox.ini b/tox.ini
index a019f4e..194490a 100644
--- a/tox.ini
+++ b/tox.ini
@@ -1,5 +1,5 @@
[tox]
-envlist = py27,py33,py34,py35,py36
+envlist = py27, py34, py35, py36
[testenv]
deps=
From d12b1418624faf166fdeb9db31ee95430d3c37c5 Mon Sep 17 00:00:00 2001
From: Mikhail Korobov
Date: Tue, 26 Dec 2017 17:37:14 +0500
Subject: [PATCH 006/108] badges in README
---
README.rst | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
diff --git a/README.rst b/README.rst
index c19c6b3..9bcd648 100644
--- a/README.rst
+++ b/README.rst
@@ -2,6 +2,22 @@
cssselect: CSS Selectors for Python
===================================
+.. image:: https://img.shields.io/pypi/v/cssselect.svg
+ :target: https://pypi.python.org/pypi/cssselect
+ :alt: PyPI Version
+
+.. image:: https://img.shields.io/pypi/pyversions/cssselect.svg
+ :target: https://pypi.python.org/pypi/cssselect
+ :alt: Supported Python Versions
+
+.. image:: https://img.shields.io/travis/scrapy/cssselect/master.svg
+ :target: https://travis-ci.org/scrapy/cssselect
+ :alt: Build Status
+
+.. image:: https://img.shields.io/codecov/c/github/scrapy/cssselect/master.svg
+ :target: https://codecov.io/github/scrapy/cssselect?branch=master
+ :alt: Coverage report
+
*cssselect* parses `CSS3 Selectors`_ and translate them to `XPath 1.0`_
expressions. Such expressions can be used in lxml_ or another XPath engine
to find the matching elements in an XML or HTML document.
From 73344698e95ce31433fad643598365f954488722 Mon Sep 17 00:00:00 2001
From: Mikhail Korobov
Date: Tue, 26 Dec 2017 17:42:16 +0500
Subject: [PATCH 007/108] DOC changelog
---
CHANGES | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/CHANGES b/CHANGES
index 92b0371..d8b27b6 100644
--- a/CHANGES
+++ b/CHANGES
@@ -1,6 +1,15 @@
Changelog
=========
+Version 1.0.2
+-------------
+
+Released on 2017-12-26.
+
+* Drop support for Python 2.6 and Python 3.3.
+* Fix deprecation warning in Python 3.6.
+* Minor cleanups.
+
Version 1.0.1
-------------
From c42886850a86565a3eda081ecb9eaffdfddb29e8 Mon Sep 17 00:00:00 2001
From: Mikhail Korobov
Date: Tue, 26 Dec 2017 17:44:21 +0500
Subject: [PATCH 008/108] =?UTF-8?q?Bump=20version:=201.0.1=20=E2=86=92=201?=
=?UTF-8?q?.0.2?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.bumpversion.cfg | 2 +-
cssselect/__init__.py | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/.bumpversion.cfg b/.bumpversion.cfg
index 92c7bcb..e21dbfb 100644
--- a/.bumpversion.cfg
+++ b/.bumpversion.cfg
@@ -1,5 +1,5 @@
[bumpversion]
-current_version = 1.0.1
+current_version = 1.0.2
commit = True
tag = True
diff --git a/cssselect/__init__.py b/cssselect/__init__.py
index 3b06261..9180b72 100644
--- a/cssselect/__init__.py
+++ b/cssselect/__init__.py
@@ -18,5 +18,5 @@
from cssselect.xpath import GenericTranslator, HTMLTranslator, ExpressionError
-VERSION = '1.0.1'
+VERSION = '1.0.2'
__version__ = VERSION
From 2e1234db300f4ad7f2372f15933da4f5a084b788 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Daniel=20Gra=C3=B1a?=
Date: Wed, 27 Dec 2017 12:13:36 -0300
Subject: [PATCH 009/108] Update travis->pypi credentials
---
.travis.yml | 24 ++++++++++--------------
1 file changed, 10 insertions(+), 14 deletions(-)
diff --git a/.travis.yml b/.travis.yml
index d86d0a8..b76297f 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,27 +1,23 @@
language: python
python:
- - '2.7'
- - '3.4'
- - '3.5'
- - '3.6'
-
+- '2.7'
+- '3.4'
+- '3.5'
+- '3.6'
install:
- - pip install lxml -e .
- - pip install -U codecov pytest-cov
-
+- pip install lxml -e .
+- pip install -U codecov pytest-cov
script:
- py.test --cov-report term --cov=cssselect
-
+- py.test --cov-report term --cov=cssselect
after_success:
- codecov
-
+- codecov
deploy:
provider: pypi
distributions: sdist bdist_wheel
skip_upload_docs: true
- user: redapple
+ user: scrapy
password:
- secure: T1PBD+ocIGwHMbBHPqzu7UZxpkB0w98KtEIkNzLXNQcF7JpjugZNwz4xX2xVhi8yvUQ257VtLSKpIOT2FWxrfLrgTZKbTd6Q7V5Lf3HKzLomOKUKMAd54gsOuismE27CT/SHbexskACgwVwkyG9Y3dlG6m/ZBgqoPAGaJrScjEU=
+ secure: UjCXD1ZfqgFcCs4ciPMJDaOQefV3ZOKZ8/dTZxcoaQlE1lr6CkaN6CfTdD50SX2M9uCNWvEcYnvs6U4SizgZ27MYzFWuHonED2alHKy4AtrxCEHD/+lGo9d18cNjLMPDZateX/lITjGiZ4rmYZNuA6wmA4P/bTmdazbSufcmMqY=
on:
tags: true
repo: scrapy/cssselect
From 720126ae39316dd21a4e03e56ccc0ba2c6a0fb24 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Daniel=20Gra=C3=B1a?=
Date: Wed, 27 Dec 2017 12:39:26 -0300
Subject: [PATCH 010/108] Update changelog for 1.0.3 release
---
CHANGES | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/CHANGES b/CHANGES
index d8b27b6..0a0e137 100644
--- a/CHANGES
+++ b/CHANGES
@@ -1,6 +1,13 @@
Changelog
=========
+Version 1.0.3
+-------------
+
+Released on 2017-12-27.
+
+* Fix artifact uploads to pypi
+
Version 1.0.2
-------------
From cb7a7e21de1ba9347d58a6a14b7c78b3de1f49ca Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Daniel=20Gra=C3=B1a?=
Date: Wed, 27 Dec 2017 12:39:30 -0300
Subject: [PATCH 011/108] =?UTF-8?q?Bump=20version:=201.0.2=20=E2=86=92=201?=
=?UTF-8?q?.0.3?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.bumpversion.cfg | 2 +-
cssselect/__init__.py | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/.bumpversion.cfg b/.bumpversion.cfg
index e21dbfb..acb5a66 100644
--- a/.bumpversion.cfg
+++ b/.bumpversion.cfg
@@ -1,5 +1,5 @@
[bumpversion]
-current_version = 1.0.2
+current_version = 1.0.3
commit = True
tag = True
diff --git a/cssselect/__init__.py b/cssselect/__init__.py
index 9180b72..e9f9ce1 100644
--- a/cssselect/__init__.py
+++ b/cssselect/__init__.py
@@ -18,5 +18,5 @@
from cssselect.xpath import GenericTranslator, HTMLTranslator, ExpressionError
-VERSION = '1.0.2'
+VERSION = '1.0.3'
__version__ = VERSION
From 4e90061eea44515c7c1c9e48c2b67a3a8489a692 Mon Sep 17 00:00:00 2001
From: Arthur Darcet
Date: Tue, 7 Mar 2017 15:29:46 +0100
Subject: [PATCH 012/108] add a method on the Selector class, to export back
the selector to css
---
cssselect/parser.py | 69 ++++++++++++++++++++++++++++++++++++++---
cssselect/xpath.py | 8 +++--
tests/test_cssselect.py | 31 ++++++++++++++++++
3 files changed, 101 insertions(+), 7 deletions(-)
diff --git a/cssselect/parser.py b/cssselect/parser.py
index 9bb039c..53a76bc 100644
--- a/cssselect/parser.py
+++ b/cssselect/parser.py
@@ -76,7 +76,7 @@ def __init__(self, tree, pseudo_element=None):
#: +-------------------------+----------------+--------------------------------+
#: | Invalid pseudo-class | ``li:marker`` | ``None`` |
#: +-------------------------+----------------+--------------------------------+
- #: | Functinal | ``a::foo(2)`` | ``FunctionalPseudoElement(…)`` |
+ #: | Functional | ``a::foo(2)`` | ``FunctionalPseudoElement(…)`` |
#: +-------------------------+----------------+--------------------------------+
#:
#: .. _Lists3: http://www.w3.org/TR/2011/WD-css3-lists-20110524/#marker-pseudoelement
@@ -92,6 +92,20 @@ def __repr__(self):
return '%s[%r%s]' % (
self.__class__.__name__, self.parsed_tree, pseudo_element)
+ def css(self):
+ """Return a CSS representation for this selector (a string)
+ """
+ if isinstance(self.pseudo_element, FunctionalPseudoElement):
+ pseudo_element = '::%s' % self.pseudo_element.css()
+ elif self.pseudo_element:
+ pseudo_element = '::%s' % self.pseudo_element
+ else:
+ pseudo_element = ''
+ res = '%s%s' % (self.parsed_tree.css(), pseudo_element)
+ if len(res) > 1:
+ res = res.lstrip('*')
+ return res
+
def specificity(self):
"""Return the specificity_ of this selector as a tuple of 3 integers.
@@ -116,6 +130,9 @@ def __repr__(self):
return '%s[%r.%s]' % (
self.__class__.__name__, self.selector, self.class_name)
+ def css(self):
+ return '%s.%s' % (self.selector.css(), self.class_name)
+
def specificity(self):
a, b, c = self.selector.specificity()
b += 1
@@ -151,6 +168,10 @@ def __repr__(self):
def argument_types(self):
return [token.type for token in self.arguments]
+ def css(self):
+ args = ''.join(token.css() for token in self.arguments)
+ return '%s(%s)' % (self.name, args)
+
def specificity(self):
a, b, c = self.selector.specificity()
b += 1
@@ -174,6 +195,10 @@ def __repr__(self):
def argument_types(self):
return [token.type for token in self.arguments]
+ def css(self):
+ args = ''.join(token.css() for token in self.arguments)
+ return '%s:%s(%s)' % (self.selector.css(), self.name, args)
+
def specificity(self):
a, b, c = self.selector.specificity()
b += 1
@@ -192,6 +217,9 @@ def __repr__(self):
return '%s[%r:%s]' % (
self.__class__.__name__, self.selector, self.ident)
+ def css(self):
+ return '%s:%s' % (self.selector.css(), self.ident)
+
def specificity(self):
a, b, c = self.selector.specificity()
b += 1
@@ -210,6 +238,10 @@ def __repr__(self):
return '%s[%r:not(%r)]' % (
self.__class__.__name__, self.selector, self.subselector)
+ def css(self):
+ return '%s:not(%s)' % (self.selector.css(),
+ self.subselector.css())
+
def specificity(self):
a1, b1, c1 = self.selector.specificity()
a2, b2, c2 = self.subselector.specificity()
@@ -238,7 +270,20 @@ def __repr__(self):
else:
return '%s[%r[%s %s %r]]' % (
self.__class__.__name__, self.selector, attrib,
- self.operator, self.value)
+ self.operator, self.value.value)
+
+ def css(self):
+ if self.namespace:
+ attrib = '%s|%s' % (self.namespace, self.attrib)
+ else:
+ attrib = self.attrib
+
+ if self.operator == 'exists':
+ op = attrib
+ else:
+ op = '%s%s%s' % (attrib, self.operator, self.value.css())
+
+ return '%s[%s]' % (self.selector.css(), op)
def specificity(self):
a, b, c = self.selector.specificity()
@@ -258,10 +303,13 @@ def __init__(self, namespace=None, element=None):
self.element = element
def __repr__(self):
+ return '%s[%s]' % (self.__class__.__name__, self.css())
+
+ def css(self):
element = self.element or '*'
if self.namespace:
element = '%s|%s' % (self.namespace, element)
- return '%s[%s]' % (self.__class__.__name__, element)
+ return element
def specificity(self):
if self.element:
@@ -282,6 +330,9 @@ def __repr__(self):
return '%s[%r#%s]' % (
self.__class__.__name__, self.selector, self.id)
+ def css(self):
+ return '%s#%s' % (self.selector.css(), self.id)
+
def specificity(self):
a, b, c = self.selector.specificity()
a += 1
@@ -303,6 +354,10 @@ def __repr__(self):
return '%s[%r %s %r]' % (
self.__class__.__name__, self.selector, comb, self.subselector)
+ def css(self):
+ return '%s %s %s' % (self.selector.css(),
+ self.combinator, self.subselector.css())
+
def specificity(self):
a1, b1, c1 = self.selector.specificity()
a2, b2, c2 = self.subselector.specificity()
@@ -536,7 +591,7 @@ def parse_attrib(selector, stream):
if next != ('DELIM', ']'):
raise SelectorSyntaxError(
"Expected ']', got %s" % (next,))
- return Attrib(selector, namespace, attrib, op, value.value)
+ return Attrib(selector, namespace, attrib, op, value)
def parse_series(tokens):
@@ -591,6 +646,12 @@ def is_delim(self, *values):
type = property(operator.itemgetter(0))
value = property(operator.itemgetter(1))
+ def css(self):
+ if self.type == 'STRING':
+ return repr(self.value)
+ else:
+ return self.value
+
class EOFToken(Token):
def __new__(cls, pos):
diff --git a/cssselect/xpath.py b/cssselect/xpath.py
index 22cd029..ad2ccbd 100644
--- a/cssselect/xpath.py
+++ b/cssselect/xpath.py
@@ -308,10 +308,12 @@ def xpath_attrib(self, selector):
attrib = '@' + name
else:
attrib = 'attribute::*[name() = %s]' % self.xpath_literal(name)
- if self.lower_case_attribute_values:
- value = selector.value.lower()
+ if selector.value is None:
+ value = None
+ elif self.lower_case_attribute_values:
+ value = selector.value.value.lower()
else:
- value = selector.value
+ value = selector.value.value
return method(self.xpath(selector.selector), attrib, value)
def xpath_class(self, class_selector):
diff --git a/tests/test_cssselect.py b/tests/test_cssselect.py
index f01aa7f..96e63f3 100644
--- a/tests/test_cssselect.py
+++ b/tests/test_cssselect.py
@@ -244,6 +244,37 @@ def specificity(css):
assert specificity('#lorem + foo#ipsum:first-child > bar:first-line'
) == (2, 1, 3)
+ def test_css_export(self):
+ def css2css(css, res=None):
+ selectors = parse(css)
+ assert len(selectors) == 1
+ assert selectors[0].css() == (res or css)
+
+ css2css('*')
+ css2css(' foo', 'foo')
+ css2css('Foo', 'Foo')
+ css2css(':empty ', ':empty')
+ css2css(':before', '::before')
+ css2css(':beFOre', '::before')
+ css2css('*:before', '::before')
+ css2css(':nth-child(2)')
+ css2css('.bar')
+ css2css('[baz]')
+ css2css('[baz="4"]', "[baz='4']")
+ css2css('[baz^="4"]', "[baz^='4']")
+ css2css('#lipsum')
+ css2css(':not(*)')
+ css2css(':not(foo)')
+ css2css(':not(*.foo)')
+ css2css(':not(*[foo])')
+ css2css(':not(*:empty)')
+ css2css(':not(*#foo)')
+ css2css('foo:empty')
+ css2css('foo::before')
+ css2css('foo:empty::before')
+ css2css('::name(arg + "val" - 3)', "::name(arg+'val'-3)")
+ css2css('#lorem + foo#ipsum:first-child > bar::first-line')
+
def test_parse_errors(self):
def get_error(css):
try:
From 8d0ff3e39c9c4806277e00ae517ab7da3b41d8f0 Mon Sep 17 00:00:00 2001
From: Arthur Darcet
Date: Mon, 11 Feb 2019 17:01:21 +0100
Subject: [PATCH 013/108] rename method to .canonical, and correctly strip
extra * in :not selectors (otherwise edge ignores them)
---
AUTHORS | 1 +
cssselect/parser.py | 49 +++++++++++++++++++++++------------------
tests/test_cssselect.py | 12 +++++-----
3 files changed, 35 insertions(+), 27 deletions(-)
diff --git a/AUTHORS b/AUTHORS
index 70ca409..66dcc22 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -10,3 +10,4 @@ Simon Sapin
Stefan Behnel
Thomas Grainger
Varialus
+Arthur Darcet
diff --git a/cssselect/parser.py b/cssselect/parser.py
index 53a76bc..1aed6f8 100644
--- a/cssselect/parser.py
+++ b/cssselect/parser.py
@@ -92,16 +92,16 @@ def __repr__(self):
return '%s[%r%s]' % (
self.__class__.__name__, self.parsed_tree, pseudo_element)
- def css(self):
+ def canonical(self):
"""Return a CSS representation for this selector (a string)
"""
if isinstance(self.pseudo_element, FunctionalPseudoElement):
- pseudo_element = '::%s' % self.pseudo_element.css()
+ pseudo_element = '::%s' % self.pseudo_element.canonical()
elif self.pseudo_element:
pseudo_element = '::%s' % self.pseudo_element
else:
pseudo_element = ''
- res = '%s%s' % (self.parsed_tree.css(), pseudo_element)
+ res = '%s%s' % (self.parsed_tree.canonical(), pseudo_element)
if len(res) > 1:
res = res.lstrip('*')
return res
@@ -130,8 +130,8 @@ def __repr__(self):
return '%s[%r.%s]' % (
self.__class__.__name__, self.selector, self.class_name)
- def css(self):
- return '%s.%s' % (self.selector.css(), self.class_name)
+ def canonical(self):
+ return '%s.%s' % (self.selector.canonical(), self.class_name)
def specificity(self):
a, b, c = self.selector.specificity()
@@ -168,7 +168,7 @@ def __repr__(self):
def argument_types(self):
return [token.type for token in self.arguments]
- def css(self):
+ def canonical(self):
args = ''.join(token.css() for token in self.arguments)
return '%s(%s)' % (self.name, args)
@@ -195,9 +195,9 @@ def __repr__(self):
def argument_types(self):
return [token.type for token in self.arguments]
- def css(self):
+ def canonical(self):
args = ''.join(token.css() for token in self.arguments)
- return '%s:%s(%s)' % (self.selector.css(), self.name, args)
+ return '%s:%s(%s)' % (self.selector.canonical(), self.name, args)
def specificity(self):
a, b, c = self.selector.specificity()
@@ -217,8 +217,8 @@ def __repr__(self):
return '%s[%r:%s]' % (
self.__class__.__name__, self.selector, self.ident)
- def css(self):
- return '%s:%s' % (self.selector.css(), self.ident)
+ def canonical(self):
+ return '%s:%s' % (self.selector.canonical(), self.ident)
def specificity(self):
a, b, c = self.selector.specificity()
@@ -238,9 +238,11 @@ def __repr__(self):
return '%s[%r:not(%r)]' % (
self.__class__.__name__, self.selector, self.subselector)
- def css(self):
- return '%s:not(%s)' % (self.selector.css(),
- self.subselector.css())
+ def canonical(self):
+ subsel = self.subselector.canonical()
+ if len(subsel) > 1:
+ subsel = subsel.lstrip('*')
+ return '%s:not(%s)' % (self.selector.canonical(), subsel)
def specificity(self):
a1, b1, c1 = self.selector.specificity()
@@ -272,7 +274,7 @@ def __repr__(self):
self.__class__.__name__, self.selector, attrib,
self.operator, self.value.value)
- def css(self):
+ def canonical(self):
if self.namespace:
attrib = '%s|%s' % (self.namespace, self.attrib)
else:
@@ -283,7 +285,7 @@ def css(self):
else:
op = '%s%s%s' % (attrib, self.operator, self.value.css())
- return '%s[%s]' % (self.selector.css(), op)
+ return '%s[%s]' % (self.selector.canonical(), op)
def specificity(self):
a, b, c = self.selector.specificity()
@@ -303,9 +305,9 @@ def __init__(self, namespace=None, element=None):
self.element = element
def __repr__(self):
- return '%s[%s]' % (self.__class__.__name__, self.css())
+ return '%s[%s]' % (self.__class__.__name__, self.canonical())
- def css(self):
+ def canonical(self):
element = self.element or '*'
if self.namespace:
element = '%s|%s' % (self.namespace, element)
@@ -330,8 +332,8 @@ def __repr__(self):
return '%s[%r#%s]' % (
self.__class__.__name__, self.selector, self.id)
- def css(self):
- return '%s#%s' % (self.selector.css(), self.id)
+ def canonical(self):
+ return '%s#%s' % (self.selector.canonical(), self.id)
def specificity(self):
a, b, c = self.selector.specificity()
@@ -354,9 +356,12 @@ def __repr__(self):
return '%s[%r %s %r]' % (
self.__class__.__name__, self.selector, comb, self.subselector)
- def css(self):
- return '%s %s %s' % (self.selector.css(),
- self.combinator, self.subselector.css())
+ def canonical(self):
+ subsel = self.subselector.canonical()
+ if len(subsel) > 1:
+ subsel = subsel.lstrip('*')
+ return '%s %s %s' % (
+ self.selector.canonical(), self.combinator, subsel)
def specificity(self):
a1, b1, c1 = self.selector.specificity()
diff --git a/tests/test_cssselect.py b/tests/test_cssselect.py
index 96e63f3..0819f25 100644
--- a/tests/test_cssselect.py
+++ b/tests/test_cssselect.py
@@ -248,7 +248,7 @@ def test_css_export(self):
def css2css(css, res=None):
selectors = parse(css)
assert len(selectors) == 1
- assert selectors[0].css() == (res or css)
+ assert selectors[0].canonical() == (res or css)
css2css('*')
css2css(' foo', 'foo')
@@ -262,18 +262,20 @@ def css2css(css, res=None):
css2css('[baz]')
css2css('[baz="4"]', "[baz='4']")
css2css('[baz^="4"]', "[baz^='4']")
+ css2css("[ns|attr='4']")
css2css('#lipsum')
css2css(':not(*)')
css2css(':not(foo)')
- css2css(':not(*.foo)')
- css2css(':not(*[foo])')
- css2css(':not(*:empty)')
- css2css(':not(*#foo)')
+ css2css(':not(*.foo)', ':not(.foo)')
+ css2css(':not(*[foo])', ':not([foo])')
+ css2css(':not(:empty)')
+ css2css(':not(#foo)')
css2css('foo:empty')
css2css('foo::before')
css2css('foo:empty::before')
css2css('::name(arg + "val" - 3)', "::name(arg+'val'-3)")
css2css('#lorem + foo#ipsum:first-child > bar::first-line')
+ css2css('foo > *')
def test_parse_errors(self):
def get_error(css):
From f52371a5821f6472129e4c47b4fdd54ed3a8e1f4 Mon Sep 17 00:00:00 2001
From: sortafreel
Date: Sat, 15 Jun 2019 22:55:12 +0300
Subject: [PATCH 014/108] css "^" as "." xpath symbol to use css "^ >" to get
immediate children
---
.gitignore | 4 +++-
cssselect/parser.py | 4 ++--
cssselect/xpath.py | 11 +++++++++++
3 files changed, 16 insertions(+), 3 deletions(-)
diff --git a/.gitignore b/.gitignore
index b0ab86a..4436e5d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,4 +5,6 @@
/dist
/docs/_build
/.coverage
-.idea
\ No newline at end of file
+.idea
+/venv
+*.vscode
\ No newline at end of file
diff --git a/cssselect/parser.py b/cssselect/parser.py
index 9bb039c..61358d3 100644
--- a/cssselect/parser.py
+++ b/cssselect/parser.py
@@ -400,8 +400,8 @@ def parse_simple_selector(stream, inside_negation=False):
stream.skip_whitespace()
selector_start = len(stream.used)
peek = stream.peek()
- if peek.type == 'IDENT' or peek == ('DELIM', '*'):
- if peek.type == 'IDENT':
+ if peek.type == 'IDENT' or peek == ('DELIM', '*') or peek == ('DELIM', '^'):
+ if peek.type == 'IDENT' or peek == ('DELIM', '^'):
namespace = stream.next().value
else:
stream.next()
diff --git a/cssselect/xpath.py b/cssselect/xpath.py
index 22cd029..4e5f85a 100644
--- a/cssselect/xpath.py
+++ b/cssselect/xpath.py
@@ -187,6 +187,14 @@ def css_to_xpath(self, css, prefix='descendant-or-self::'):
The equivalent XPath 1.0 expression as an Unicode string.
"""
+ # no prefix if css immediate children (example: css "^ > div" to xpath "./div")
+ child_re = r'^[ \t\r\n\f]*\^[ \t\r\n\f]*>'
+ if re.match(child_re, css):
+ prefix = ''
+ # prefix = 'child::'
+ # css = re.sub(child_re, '', css)
+ # print('*' * 50)
+ # print(css)
return ' | '.join(self.selector_to_xpath(selector, prefix,
translate_pseudo_elements=True)
for selector in parse(css))
@@ -332,6 +340,9 @@ def xpath_element(self, selector):
if not element:
element = '*'
safe = True
+ if element == '^':
+ element = '.'
+ safe = True
else:
safe = is_safe_name(element)
if self.lower_case_element_names:
From 053f2669eef8c7942346ee7ee101777f0e267cbc Mon Sep 17 00:00:00 2001
From: sortafreel
Date: Sun, 16 Jun 2019 00:27:52 +0300
Subject: [PATCH 015/108] Implement CSS immediate children
---
cssselect/parser.py | 4 ++--
cssselect/xpath.py | 25 ++++++++++++++-----------
2 files changed, 16 insertions(+), 13 deletions(-)
diff --git a/cssselect/parser.py b/cssselect/parser.py
index 61358d3..11e9ff5 100644
--- a/cssselect/parser.py
+++ b/cssselect/parser.py
@@ -400,8 +400,8 @@ def parse_simple_selector(stream, inside_negation=False):
stream.skip_whitespace()
selector_start = len(stream.used)
peek = stream.peek()
- if peek.type == 'IDENT' or peek == ('DELIM', '*') or peek == ('DELIM', '^'):
- if peek.type == 'IDENT' or peek == ('DELIM', '^'):
+ if peek.type == 'IDENT' or peek == ('DELIM', '*') or peek == ('DELIM', '<'):
+ if peek.type == 'IDENT' or peek == ('DELIM', '<'):
namespace = stream.next().value
else:
stream.next()
diff --git a/cssselect/xpath.py b/cssselect/xpath.py
index 4e5f85a..e71d21c 100644
--- a/cssselect/xpath.py
+++ b/cssselect/xpath.py
@@ -187,14 +187,6 @@ def css_to_xpath(self, css, prefix='descendant-or-self::'):
The equivalent XPath 1.0 expression as an Unicode string.
"""
- # no prefix if css immediate children (example: css "^ > div" to xpath "./div")
- child_re = r'^[ \t\r\n\f]*\^[ \t\r\n\f]*>'
- if re.match(child_re, css):
- prefix = ''
- # prefix = 'child::'
- # css = re.sub(child_re, '', css)
- # print('*' * 50)
- # print(css)
return ' | '.join(self.selector_to_xpath(selector, prefix,
translate_pseudo_elements=True)
for selector in parse(css))
@@ -228,7 +220,18 @@ def selector_to_xpath(self, selector, prefix='descendant-or-self::',
assert isinstance(xpath, self.xpathexpr_cls) # help debug a missing 'return'
if translate_pseudo_elements and selector.pseudo_element:
xpath = self.xpath_pseudo_element(xpath, selector.pseudo_element)
- return (prefix or '') + _unicode(xpath)
+
+ unicode_xpath = _unicode(xpath)
+ # CSS immediate children (CSS "<> div" to XPath "child::div" or "./div")
+ # Works only at the start of a selector
+ # Needed to get immediate children of a processed selector in Scrapy
+ # product = response.css('.product')
+ # name = product.css('<> div')
+ child_re = r'^[ \t\r\n\f]*\<[ \t\r\n\f]*\/'
+ if re.match(child_re, unicode_xpath):
+ prefix = 'child::'
+ unicode_xpath = re.sub(child_re, '', unicode_xpath)
+ return (prefix or '') + unicode_xpath
def xpath_pseudo_element(self, xpath, pseudo_element):
"""Translate a pseudo-element.
@@ -340,8 +343,8 @@ def xpath_element(self, selector):
if not element:
element = '*'
safe = True
- if element == '^':
- element = '.'
+ if element == '<':
+ element = '<'
safe = True
else:
safe = is_safe_name(element)
From 9ec22422722561060bca1d7805556c77681d7b18 Mon Sep 17 00:00:00 2001
From: sortafreel
Date: Sun, 16 Jun 2019 01:38:13 +0300
Subject: [PATCH 016/108] Add tests and errors handling.
---
cssselect/parser.py | 15 ++++++++++++++-
tests/test_cssselect.py | 31 +++++++++++++++++++++++++++++--
2 files changed, 43 insertions(+), 3 deletions(-)
diff --git a/cssselect/parser.py b/cssselect/parser.py
index 11e9ff5..5d9360c 100644
--- a/cssselect/parser.py
+++ b/cssselect/parser.py
@@ -401,8 +401,21 @@ def parse_simple_selector(stream, inside_negation=False):
selector_start = len(stream.used)
peek = stream.peek()
if peek.type == 'IDENT' or peek == ('DELIM', '*') or peek == ('DELIM', '<'):
- if peek.type == 'IDENT' or peek == ('DELIM', '<'):
+ if peek.type == 'IDENT':
namespace = stream.next().value
+ elif peek == ('DELIM', '<'):
+ if not (len(stream.used) == 0 or
+ (len(stream.used) == 1 and stream.used[0].type == 'S')):
+ raise SelectorSyntaxError(
+ 'Got immediate child pseudo-element "<>" not at the start of a selector'
+ )
+ namespace = stream.next().value
+ stream.skip_whitespace()
+ peek = stream.peek()
+ if not peek == ('DELIM', '>'):
+ raise SelectorSyntaxError(
+ 'Got incomplete immediate child pseudo-element "<>" (no ">")'
+ )
else:
stream.next()
namespace = None
diff --git a/tests/test_cssselect.py b/tests/test_cssselect.py
index f01aa7f..49bb7ba 100644
--- a/tests/test_cssselect.py
+++ b/tests/test_cssselect.py
@@ -42,7 +42,7 @@ class TestCssselect(unittest.TestCase):
def test_tokenizer(self):
tokens = [
_unicode(item) for item in tokenize(
- u(r'E\ é > f [a~="y\"x"]:nth(/* fu /]* */-3.7)'))]
+ u(r'E\ é > f [a~="y\"x"]:nth(/* fu /]* */-3.7)<'))]
assert tokens == [
u(""),
"",
@@ -61,7 +61,8 @@ def test_tokenizer(self):
"",
"",
"",
- "",
+ "<' at 42>",
+ "",
]
def test_parser(self):
@@ -146,6 +147,18 @@ def parse_many(first, *others):
'Negation[Element[div]:not(Class[Element[div].foo])]']
assert parse_many('td ~ th') == [
'CombinedSelector[Element[td] ~ Element[th]]']
+ # assert parse_many('<') == ['Element[<]']
+ # assert parse_many('<> foo') == [
+ # 'CombinedSelector[Element[<] > Element[foo]]'
+ # ]
+ # assert parse_many('<> foo bar > div') == [
+ # 'CombinedSelector[CombinedSelector[CombinedSelector[Element[<] > Element[foo]] '
+ # ' Element[bar]] > Element[div]]'
+ # ]
+ # assert parse_many('<> #foo #bar') == [
+ # 'CombinedSelector[CombinedSelector[Element[<] > Hash[Element[*]#foo]] '
+ # ' Hash[Element[*]#bar]]'
+ # ]
def test_pseudo_elements(self):
def parse_pseudo(css):
@@ -310,6 +323,12 @@ def get_error(css):
"Got pseudo-element ::before inside :not() at 12")
assert get_error(':not(:not(a))') == (
"Got nested :not()")
+ assert get_error('<> div <> header') == (
+ 'Got immediate child pseudo-element "<>" not at the start of a selector'
+ )
+ assert get_error('< div p') == (
+ 'Got incomplete immediate child pseudo-element "<>" (no ">")')
+ assert get_error('> div p') == ("Expected selector, got ' at 0>")
def test_translation(self):
def xpath(css):
@@ -483,6 +502,8 @@ def test_quoting(self):
'''descendant-or-self::*[@aval = '"']''')
assert css_to_xpath('*[aval=\'"""\']') == (
'''descendant-or-self::*[@aval = '"""']''')
+ assert css_to_xpath('<> div[dataimg=""]') == (
+ "child::div[@dataimg = '']")
def test_unicode_escapes(self):
# \22 == '"' \20 == ' '
@@ -672,6 +693,11 @@ def pcss(main, *selectors, **kwargs):
assert pcss(':lang("EN")', '*:lang(en-US)', html_only=True) == [
'second-li', 'li-div']
assert pcss(':lang("e")', html_only=True) == []
+ assert pcss('<> div') == []
+ assert pcss('<> body') == ['nil']
+ assert pcss('<> body > div') == ['outer-div', 'foobar-div']
+ assert pcss('<> head') == ['nil']
+ assert pcss('<> html') == []
# --- nth-* and nth-last-* -------------------------------------
@@ -853,6 +879,7 @@ def count(selector):
assert count('div[class|=dialog]') == 50 # ? Seems right
assert count('div[class!=madeup]') == 243 # ? Seems right
assert count('div[class~=dialog]') == 51 # ? Seems right
+ assert count('<> div') == 1
XMLLANG_IDS = '''
From 7c697daf87f1e7cea3f48a145b1cb7a5458750ad Mon Sep 17 00:00:00 2001
From: sortafreel
Date: Sun, 16 Jun 2019 01:45:25 +0300
Subject: [PATCH 017/108] Add more tests.
---
.gitignore | 2 +-
tests/test_cssselect.py | 23 +++++++++++------------
2 files changed, 12 insertions(+), 13 deletions(-)
diff --git a/.gitignore b/.gitignore
index 4436e5d..5c47adf 100644
--- a/.gitignore
+++ b/.gitignore
@@ -7,4 +7,4 @@
/.coverage
.idea
/venv
-*.vscode
\ No newline at end of file
+*.vscode
diff --git a/tests/test_cssselect.py b/tests/test_cssselect.py
index 49bb7ba..f68893b 100644
--- a/tests/test_cssselect.py
+++ b/tests/test_cssselect.py
@@ -147,18 +147,17 @@ def parse_many(first, *others):
'Negation[Element[div]:not(Class[Element[div].foo])]']
assert parse_many('td ~ th') == [
'CombinedSelector[Element[td] ~ Element[th]]']
- # assert parse_many('<') == ['Element[<]']
- # assert parse_many('<> foo') == [
- # 'CombinedSelector[Element[<] > Element[foo]]'
- # ]
- # assert parse_many('<> foo bar > div') == [
- # 'CombinedSelector[CombinedSelector[CombinedSelector[Element[<] > Element[foo]] '
- # ' Element[bar]] > Element[div]]'
- # ]
- # assert parse_many('<> #foo #bar') == [
- # 'CombinedSelector[CombinedSelector[Element[<] > Hash[Element[*]#foo]] '
- # ' Hash[Element[*]#bar]]'
- # ]
+ assert parse_many('<> foo') == [
+ 'CombinedSelector[Element[<] > Element[foo]]'
+ ]
+ assert parse_many('<> foo bar > div') == [
+ 'CombinedSelector[CombinedSelector[CombinedSelector[Element[<] > Element[foo]] '
+ ' Element[bar]] > Element[div]]'
+ ]
+ assert parse_many('<> #foo #bar') == [
+ 'CombinedSelector[CombinedSelector[Element[<] > Hash[Element[*]#foo]] '
+ ' Hash[Element[*]#bar]]'
+ ]
def test_pseudo_elements(self):
def parse_pseudo(css):
From 37b3c0ffcd1db16ca240487f1e0f8bb716a3385c Mon Sep 17 00:00:00 2001
From: sortafreel
Date: Sun, 16 Jun 2019 02:12:07 +0300
Subject: [PATCH 018/108] Code review fixes.
---
.gitignore | 4 +---
cssselect/xpath.py | 1 -
2 files changed, 1 insertion(+), 4 deletions(-)
diff --git a/.gitignore b/.gitignore
index 5c47adf..b0ab86a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,6 +5,4 @@
/dist
/docs/_build
/.coverage
-.idea
-/venv
-*.vscode
+.idea
\ No newline at end of file
diff --git a/cssselect/xpath.py b/cssselect/xpath.py
index e71d21c..e3843b5 100644
--- a/cssselect/xpath.py
+++ b/cssselect/xpath.py
@@ -344,7 +344,6 @@ def xpath_element(self, selector):
element = '*'
safe = True
if element == '<':
- element = '<'
safe = True
else:
safe = is_safe_name(element)
From 920b3d644fa62c95db40141c5322d38e98bbe8d3 Mon Sep 17 00:00:00 2001
From: sortafreel
Date: Sun, 16 Jun 2019 17:57:08 +0300
Subject: [PATCH 019/108] Change "<>" selector to ":scope"
---
cssselect/parser.py | 22 ++++++++-------------
cssselect/xpath.py | 23 +++++++++-------------
tests/test_cssselect.py | 43 ++++++++++++++++++++++-------------------
3 files changed, 40 insertions(+), 48 deletions(-)
diff --git a/cssselect/parser.py b/cssselect/parser.py
index 5d9360c..99b25a3 100644
--- a/cssselect/parser.py
+++ b/cssselect/parser.py
@@ -400,22 +400,9 @@ def parse_simple_selector(stream, inside_negation=False):
stream.skip_whitespace()
selector_start = len(stream.used)
peek = stream.peek()
- if peek.type == 'IDENT' or peek == ('DELIM', '*') or peek == ('DELIM', '<'):
+ if peek.type == 'IDENT' or peek == ('DELIM', '*'):
if peek.type == 'IDENT':
namespace = stream.next().value
- elif peek == ('DELIM', '<'):
- if not (len(stream.used) == 0 or
- (len(stream.used) == 1 and stream.used[0].type == 'S')):
- raise SelectorSyntaxError(
- 'Got immediate child pseudo-element "<>" not at the start of a selector'
- )
- namespace = stream.next().value
- stream.skip_whitespace()
- peek = stream.peek()
- if not peek == ('DELIM', '>'):
- raise SelectorSyntaxError(
- 'Got incomplete immediate child pseudo-element "<>" (no ">")'
- )
else:
stream.next()
namespace = None
@@ -465,6 +452,13 @@ def parse_simple_selector(stream, inside_negation=False):
continue
if stream.peek() != ('DELIM', '('):
result = Pseudo(result, ident)
+ if result.ident == 'scope':
+ if not (len(stream.used) == 2 or
+ (len(stream.used) == 3
+ and stream.used[0].type == 'S')):
+ raise SelectorSyntaxError(
+ 'Got immediate child pseudo-element ":scope" '
+ 'not at the start of a selector')
continue
stream.next()
stream.skip_whitespace()
diff --git a/cssselect/xpath.py b/cssselect/xpath.py
index e3843b5..d5bbf72 100644
--- a/cssselect/xpath.py
+++ b/cssselect/xpath.py
@@ -220,18 +220,7 @@ def selector_to_xpath(self, selector, prefix='descendant-or-self::',
assert isinstance(xpath, self.xpathexpr_cls) # help debug a missing 'return'
if translate_pseudo_elements and selector.pseudo_element:
xpath = self.xpath_pseudo_element(xpath, selector.pseudo_element)
-
- unicode_xpath = _unicode(xpath)
- # CSS immediate children (CSS "<> div" to XPath "child::div" or "./div")
- # Works only at the start of a selector
- # Needed to get immediate children of a processed selector in Scrapy
- # product = response.css('.product')
- # name = product.css('<> div')
- child_re = r'^[ \t\r\n\f]*\<[ \t\r\n\f]*\/'
- if re.match(child_re, unicode_xpath):
- prefix = 'child::'
- unicode_xpath = re.sub(child_re, '', unicode_xpath)
- return (prefix or '') + unicode_xpath
+ return (prefix or '') + _unicode(xpath)
def xpath_pseudo_element(self, xpath, pseudo_element):
"""Translate a pseudo-element.
@@ -343,8 +332,6 @@ def xpath_element(self, selector):
if not element:
element = '*'
safe = True
- if element == '<':
- safe = True
else:
safe = is_safe_name(element)
if self.lower_case_element_names:
@@ -554,6 +541,14 @@ def xpath_lang_function(self, xpath, function):
def xpath_root_pseudo(self, xpath):
return xpath.add_condition("not(parent::*)")
+ # CSS immediate children (CSS ":scope > div" to XPath "child::div" or "./div")
+ # Works only at the start of a selector
+ # Needed to get immediate children of a processed selector in Scrapy
+ # for product in response.css('.product'):
+ # description = product.css(':scope > div::text').get()
+ def xpath_scope_pseudo(self, xpath):
+ return xpath.add_condition("1")
+
def xpath_first_child_pseudo(self, xpath):
return xpath.add_condition('count(preceding-sibling::*) = 0')
diff --git a/tests/test_cssselect.py b/tests/test_cssselect.py
index f68893b..0f2a836 100644
--- a/tests/test_cssselect.py
+++ b/tests/test_cssselect.py
@@ -147,18 +147,19 @@ def parse_many(first, *others):
'Negation[Element[div]:not(Class[Element[div].foo])]']
assert parse_many('td ~ th') == [
'CombinedSelector[Element[td] ~ Element[th]]']
- assert parse_many('<> foo') == [
- 'CombinedSelector[Element[<] > Element[foo]]'
+ assert parse_many(':scope > foo') == [
+ 'CombinedSelector[Pseudo[Element[*]:scope] > Element[foo]]'
]
- assert parse_many('<> foo bar > div') == [
- 'CombinedSelector[CombinedSelector[CombinedSelector[Element[<] > Element[foo]] '
- ' Element[bar]] > Element[div]]'
+ assert parse_many(':scope > foo bar > div') == [
+ 'CombinedSelector[CombinedSelector[CombinedSelector[Pseudo[Element[*]:scope] > '
+ 'Element[foo]] Element[bar]] > Element[div]]'
]
- assert parse_many('<> #foo #bar') == [
- 'CombinedSelector[CombinedSelector[Element[<] > Hash[Element[*]#foo]] '
- ' Hash[Element[*]#bar]]'
+ assert parse_many(':scope > #foo #bar') == [
+ 'CombinedSelector[CombinedSelector[Pseudo[Element[*]:scope] > '
+ 'Hash[Element[*]#foo]] Hash[Element[*]#bar]]'
]
+ # TODO ADD TESTS
def test_pseudo_elements(self):
def parse_pseudo(css):
result = []
@@ -179,6 +180,7 @@ def parse_one(css):
assert parse_one('foo') == ('Element[foo]', None)
assert parse_one('*') == ('Element[*]', None)
assert parse_one(':empty') == ('Pseudo[Element[*]:empty]', None)
+ assert parse_one(':scope') == ('Pseudo[Element[*]:scope]', None)
# Special cases for CSS 2.1 pseudo-elements
assert parse_one(':BEfore') == ('Element[*]', 'before')
@@ -322,11 +324,9 @@ def get_error(css):
"Got pseudo-element ::before inside :not() at 12")
assert get_error(':not(:not(a))') == (
"Got nested :not()")
- assert get_error('<> div <> header') == (
- 'Got immediate child pseudo-element "<>" not at the start of a selector'
+ assert get_error(':scope > div :scope header') == (
+ 'Got immediate child pseudo-element ":scope" not at the start of a selector'
)
- assert get_error('< div p') == (
- 'Got incomplete immediate child pseudo-element "<>" (no ">")')
assert get_error('> div p') == ("Expected selector, got ' at 0>")
def test_translation(self):
@@ -501,8 +501,8 @@ def test_quoting(self):
'''descendant-or-self::*[@aval = '"']''')
assert css_to_xpath('*[aval=\'"""\']') == (
'''descendant-or-self::*[@aval = '"""']''')
- assert css_to_xpath('<> div[dataimg=""]') == (
- "child::div[@dataimg = '']")
+ assert css_to_xpath(':scope > div[dataimg=""]') == (
+ "descendant-or-self::*[1]/div[@dataimg = '']")
def test_unicode_escapes(self):
# \22 == '"' \20 == ' '
@@ -580,6 +580,7 @@ def xpath(css):
assert xpath('::attr-href') == "descendant-or-self::*/@href"
assert xpath('p img::attr(src)') == (
"descendant-or-self::p/descendant-or-self::*/img/@src")
+ assert xpath(':scope') == "descendant-or-self::*[1]"
def test_series(self):
def series(css):
@@ -692,11 +693,11 @@ def pcss(main, *selectors, **kwargs):
assert pcss(':lang("EN")', '*:lang(en-US)', html_only=True) == [
'second-li', 'li-div']
assert pcss(':lang("e")', html_only=True) == []
- assert pcss('<> div') == []
- assert pcss('<> body') == ['nil']
- assert pcss('<> body > div') == ['outer-div', 'foobar-div']
- assert pcss('<> head') == ['nil']
- assert pcss('<> html') == []
+ assert pcss(':scope > div') == []
+ assert pcss(':scope body') == ['nil']
+ assert pcss(':scope body > div') == ['outer-div', 'foobar-div']
+ assert pcss(':scope head') == ['nil']
+ assert pcss(':scope html') == []
# --- nth-* and nth-last-* -------------------------------------
@@ -878,7 +879,9 @@ def count(selector):
assert count('div[class|=dialog]') == 50 # ? Seems right
assert count('div[class!=madeup]') == 243 # ? Seems right
assert count('div[class~=dialog]') == 51 # ? Seems right
- assert count('<> div') == 1
+ assert count(':scope > div') == 1
+ assert count(':scope > div > div[class=dialog]') == 1
+ assert count(':scope > div div') == 242
XMLLANG_IDS = '''
From 97ab897ce8995662517d45fe417f63e1a1dcc73b Mon Sep 17 00:00:00 2001
From: sortafreel
Date: Sun, 16 Jun 2019 18:09:43 +0300
Subject: [PATCH 020/108] Add more tests.
---
tests/test_cssselect.py | 15 +++++++++++----
1 file changed, 11 insertions(+), 4 deletions(-)
diff --git a/tests/test_cssselect.py b/tests/test_cssselect.py
index 0f2a836..80dc687 100644
--- a/tests/test_cssselect.py
+++ b/tests/test_cssselect.py
@@ -150,6 +150,9 @@ def parse_many(first, *others):
assert parse_many(':scope > foo') == [
'CombinedSelector[Pseudo[Element[*]:scope] > Element[foo]]'
]
+ assert parse_many(' :scope > foo') == [
+ 'CombinedSelector[Pseudo[Element[*]:scope] > Element[foo]]'
+ ]
assert parse_many(':scope > foo bar > div') == [
'CombinedSelector[CombinedSelector[CombinedSelector[Pseudo[Element[*]:scope] > '
'Element[foo]] Element[bar]] > Element[div]]'
@@ -205,10 +208,14 @@ def parse_one(css):
'Pseudo[Attrib[Class[Hash[Element[a]#b].c][href]]:empty]]',
'selection')
- parse_pseudo('foo:before, bar, baz:after') == [
- ('Element[foo]', 'before'),
- ('Element[bar]', None),
- ('Element[baz]', 'after')]
+ assert parse_pseudo(':scope > div, foo bar') == [
+ ('CombinedSelector[Pseudo[Element[*]:scope] > Element[div]]', None),
+ ('CombinedSelector[Element[foo] Element[bar]]', None)
+ ]
+ assert parse_pseudo('foo:before, bar, baz:after') == [
+ ('Element[foo]', 'before'), ('Element[bar]', None),
+ ('Element[baz]', 'after')
+ ]
# Special cases for CSS 2.1 pseudo-elements are ignored by default
for pseudo in ('after', 'before', 'first-line', 'first-letter'):
From 8cc4a266f4851e3b2502e8e740af9a9af8771ac0 Mon Sep 17 00:00:00 2001
From: sortafreel
Date: Sun, 16 Jun 2019 18:23:00 +0300
Subject: [PATCH 021/108] Lint
---
cssselect/parser.py | 3 ++-
tests/test_cssselect.py | 9 +++++----
2 files changed, 7 insertions(+), 5 deletions(-)
diff --git a/cssselect/parser.py b/cssselect/parser.py
index 99b25a3..bcd1854 100644
--- a/cssselect/parser.py
+++ b/cssselect/parser.py
@@ -452,7 +452,8 @@ def parse_simple_selector(stream, inside_negation=False):
continue
if stream.peek() != ('DELIM', '('):
result = Pseudo(result, ident)
- if result.ident == 'scope':
+ if result.ident == 'scope' and repr(
+ result) == 'Pseudo[Element[*]:scope]':
if not (len(stream.used) == 2 or
(len(stream.used) == 3
and stream.used[0].type == 'S')):
diff --git a/tests/test_cssselect.py b/tests/test_cssselect.py
index 80dc687..5c97f30 100644
--- a/tests/test_cssselect.py
+++ b/tests/test_cssselect.py
@@ -42,7 +42,7 @@ class TestCssselect(unittest.TestCase):
def test_tokenizer(self):
tokens = [
_unicode(item) for item in tokenize(
- u(r'E\ é > f [a~="y\"x"]:nth(/* fu /]* */-3.7)<'))]
+ u(r'E\ é > f [a~="y\"x"]:nth(/* fu /]* */-3.7)'))]
assert tokens == [
u(""),
"",
@@ -61,8 +61,7 @@ def test_tokenizer(self):
"",
"",
"",
- "<' at 42>",
- "",
+ "",
]
def test_parser(self):
@@ -162,7 +161,6 @@ def parse_many(first, *others):
'Hash[Element[*]#foo]] Hash[Element[*]#bar]]'
]
- # TODO ADD TESTS
def test_pseudo_elements(self):
def parse_pseudo(css):
result = []
@@ -334,6 +332,9 @@ def get_error(css):
assert get_error(':scope > div :scope header') == (
'Got immediate child pseudo-element ":scope" not at the start of a selector'
)
+ assert get_error('div :scope header') == (
+ 'Got immediate child pseudo-element ":scope" not at the start of a selector'
+ )
assert get_error('> div p') == ("Expected selector, got ' at 0>")
def test_translation(self):
From 270f11835e81eba71441e53f4a555405df2e2a0c Mon Sep 17 00:00:00 2001
From: sortafreel
Date: Mon, 17 Jun 2019 14:53:19 +0300
Subject: [PATCH 022/108] Improve test coverage.
---
cssselect/parser.py | 3 +--
tests/test_cssselect.py | 12 +++++++++++-
2 files changed, 12 insertions(+), 3 deletions(-)
diff --git a/cssselect/parser.py b/cssselect/parser.py
index bcd1854..3be71bb 100644
--- a/cssselect/parser.py
+++ b/cssselect/parser.py
@@ -452,8 +452,7 @@ def parse_simple_selector(stream, inside_negation=False):
continue
if stream.peek() != ('DELIM', '('):
result = Pseudo(result, ident)
- if result.ident == 'scope' and repr(
- result) == 'Pseudo[Element[*]:scope]':
+ if result.__repr__() == 'Pseudo[Element[*]:scope]':
if not (len(stream.used) == 2 or
(len(stream.used) == 3
and stream.used[0].type == 'S')):
diff --git a/tests/test_cssselect.py b/tests/test_cssselect.py
index 5c97f30..8b562da 100644
--- a/tests/test_cssselect.py
+++ b/tests/test_cssselect.py
@@ -178,6 +178,12 @@ def parse_one(css):
assert len(result) == 1
return result[0]
+ def test_pseudo_repr(css):
+ result = parse(css)
+ assert len(result) == 1
+ selector = result[0]
+ return selector.parsed_tree.__repr__()
+
assert parse_one('foo') == ('Element[foo]', None)
assert parse_one('*') == ('Element[*]', None)
assert parse_one(':empty') == ('Pseudo[Element[*]:empty]', None)
@@ -205,7 +211,6 @@ def parse_one(css):
'CombinedSelector[Hash[Element[lorem]#ipsum] ~ '
'Pseudo[Attrib[Class[Hash[Element[a]#b].c][href]]:empty]]',
'selection')
-
assert parse_pseudo(':scope > div, foo bar') == [
('CombinedSelector[Pseudo[Element[*]:scope] > Element[div]]', None),
('CombinedSelector[Element[foo] Element[bar]]', None)
@@ -230,6 +235,11 @@ def parse_one(css):
self.assertRaises(ExpressionError, tr.selector_to_xpath, selector,
translate_pseudo_elements=True)
+ # Special test for the unicode symbols and ':scope' element if check
+ # Errors if use repr() instead of __repr__()
+ assert test_pseudo_repr(u':fİrst-child') == u'Pseudo[Element[*]:fİrst-child]'
+ assert test_pseudo_repr(':scope') == 'Pseudo[Element[*]:scope]'
+
def test_specificity(self):
def specificity(css):
selectors = parse(css)
From 4b966853c84f44c8fb079213337e36d4992dd7f0 Mon Sep 17 00:00:00 2001
From: sortafreel
Date: Tue, 18 Jun 2019 21:39:06 +0300
Subject: [PATCH 023/108] Edit docs.
---
docs/index.rst | 2 ++
1 file changed, 2 insertions(+)
diff --git a/docs/index.rst b/docs/index.rst
index fe473f7..c7f0c1a 100644
--- a/docs/index.rst
+++ b/docs/index.rst
@@ -108,8 +108,10 @@ in the Level 3 specification:
* ``:not()`` accepts a *sequence of simple selectors*, not just single
*simple selector*. For example, ``:not(a.important[rel])`` is allowed,
even though the negation contains 3 *simple selectors*.
+* ``:scope`` allows to access immediate children of a selector: ``product.css(':scope > div::text')``, simillar to XPath ``child::div``. Must be used at the start of a selector. Simplified version of `level 4 reference`_.
.. _an early draft: http://www.w3.org/TR/2001/CR-css3-selectors-20011113/#content-selectors
+.. _level 4 reference: https://developer.mozilla.org/en-US/docs/Web/CSS/:scope
..
The following claim was copied from lxml:
From 81c8dab8a17e389be9390260e8e22b5c0ef4df4c Mon Sep 17 00:00:00 2001
From: Simon Potter
Date: Thu, 11 Jul 2019 19:23:43 +1200
Subject: [PATCH 024/108] Parse |ident as ident. No longer an error.
---
cssselect/parser.py | 3 +++
tests/test_cssselect.py | 1 +
2 files changed, 4 insertions(+)
diff --git a/cssselect/parser.py b/cssselect/parser.py
index 3be71bb..b96d26a 100644
--- a/cssselect/parser.py
+++ b/cssselect/parser.py
@@ -430,6 +430,9 @@ def parse_simple_selector(stream, inside_negation=False):
elif peek == ('DELIM', '.'):
stream.next()
result = Class(result, stream.next_ident())
+ elif peek == ('DELIM', '|'):
+ stream.next()
+ result = Element(None, stream.next_ident())
elif peek == ('DELIM', '['):
stream.next()
result = parse_attrib(result, stream)
diff --git a/tests/test_cssselect.py b/tests/test_cssselect.py
index 8b562da..d2432ab 100644
--- a/tests/test_cssselect.py
+++ b/tests/test_cssselect.py
@@ -81,6 +81,7 @@ def parse_many(first, *others):
assert parse_many('*') == ['Element[*]']
assert parse_many('*|*') == ['Element[*]']
assert parse_many('*|foo') == ['Element[foo]']
+ assert parse_many('|foo') == ['Element[foo]']
assert parse_many('foo|*') == ['Element[foo|*]']
assert parse_many('foo|bar') == ['Element[foo|bar]']
# This will never match, but it is valid:
From 6d758551c700c784c690ff59c8ccf679c573d506 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adri=C3=A1n=20Chaves?=
Date: Fri, 9 Aug 2019 10:31:51 +0200
Subject: [PATCH 025/108] Enforce a working lxml version on the Python 3.4 CI
environment
---
.travis.yml | 3 +--
tests/requirements.txt | 5 +++++
tox.ini | 4 +---
3 files changed, 7 insertions(+), 5 deletions(-)
create mode 100644 tests/requirements.txt
diff --git a/.travis.yml b/.travis.yml
index b76297f..bfc557f 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -5,8 +5,7 @@ python:
- '3.5'
- '3.6'
install:
-- pip install lxml -e .
-- pip install -U codecov pytest-cov
+- pip install -r tests/requirements.txt -e .
script:
- py.test --cov-report term --cov=cssselect
after_success:
diff --git a/tests/requirements.txt b/tests/requirements.txt
new file mode 100644
index 0000000..5232e84
--- /dev/null
+++ b/tests/requirements.txt
@@ -0,0 +1,5 @@
+codecov
+lxml;python_version!="3.4"
+lxml<=4.3.5;python_version=="3.4"
+pytest
+pytest-cov
\ No newline at end of file
diff --git a/tox.ini b/tox.ini
index 194490a..49a1dda 100644
--- a/tox.ini
+++ b/tox.ini
@@ -3,9 +3,7 @@ envlist = py27, py34, py35, py36
[testenv]
deps=
- lxml
- pytest<3
- pytest-cov
+ -r tests/requirements.txt
commands =
py.test --cov-report term --cov=cssselect
From cff38f1f00972b9851ff64fa8380022aa0d76b9b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adri=C3=A1n=20Chaves?=
Date: Thu, 1 Aug 2019 18:30:00 +0200
Subject: [PATCH 026/108] Cover cssselect 1.1.0 in the CHANGES file
---
CHANGES | 17 +++++++++++++++++
1 file changed, 17 insertions(+)
diff --git a/CHANGES b/CHANGES
index 0a0e137..a6c5233 100644
--- a/CHANGES
+++ b/CHANGES
@@ -1,6 +1,20 @@
Changelog
=========
+Version 1.1.0
+-------------
+
+Released on 2019-08-09.
+
+* Support for the ``:scope`` selector, which allows to access immediate
+ children of a selector.
+
+* Support for the ``|E`` syntax for type selectors without a namespace.
+
+* A new selector method, ``canonical``, returns the CSS expression of the
+ selector, as a string.
+
+
Version 1.0.3
-------------
@@ -8,6 +22,7 @@ Released on 2017-12-27.
* Fix artifact uploads to pypi
+
Version 1.0.2
-------------
@@ -17,6 +32,7 @@ Released on 2017-12-26.
* Fix deprecation warning in Python 3.6.
* Minor cleanups.
+
Version 1.0.1
-------------
@@ -25,6 +41,7 @@ Released on 2017-01-10.
* Add support for Python 3.6.
* Documentation hosted `on Read the Docs `_
+
Version 1.0.0
-------------
From 518e3e1babcc3db38ab8afe948c05a4799693108 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adri=C3=A1n=20Chaves?=
Date: Fri, 9 Aug 2019 11:17:29 +0200
Subject: [PATCH 027/108] =?UTF-8?q?Bump=20version:=201.0.3=20=E2=86=92=201?=
=?UTF-8?q?.1.0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.bumpversion.cfg | 2 +-
cssselect/__init__.py | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/.bumpversion.cfg b/.bumpversion.cfg
index acb5a66..122d3d4 100644
--- a/.bumpversion.cfg
+++ b/.bumpversion.cfg
@@ -1,5 +1,5 @@
[bumpversion]
-current_version = 1.0.3
+current_version = 1.1.0
commit = True
tag = True
diff --git a/cssselect/__init__.py b/cssselect/__init__.py
index e9f9ce1..b41cef9 100644
--- a/cssselect/__init__.py
+++ b/cssselect/__init__.py
@@ -18,5 +18,5 @@
from cssselect.xpath import GenericTranslator, HTMLTranslator, ExpressionError
-VERSION = '1.0.3'
+VERSION = '1.1.0'
__version__ = VERSION
From 91822333b7a2ddbb1f11b624b304c2563be2d0ef Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adri=C3=A1n=20Chaves?=
Date: Tue, 17 Sep 2019 10:58:16 +0200
Subject: [PATCH 028/108] Package tests
Fixes #92
---
MANIFEST.in | 1 +
1 file changed, 1 insertion(+)
diff --git a/MANIFEST.in b/MANIFEST.in
index e98d213..a367dc0 100644
--- a/MANIFEST.in
+++ b/MANIFEST.in
@@ -1,3 +1,4 @@
include AUTHORS CHANGES LICENSE README.rst tox.ini .coveragerc
recursive-include docs *
+recursive-include tests *
prune docs/_build
From c909f051d0034171c0658e25aa3ee4d1b745f8a4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adri=C3=A1n=20Chaves?=
Date: Thu, 19 Sep 2019 11:50:53 +0200
Subject: [PATCH 029/108] Support :scope after a comma delimiter
---
cssselect/parser.py | 9 +++++++--
tests/test_cssselect.py | 10 +++++++++-
tox.ini | 2 +-
3 files changed, 17 insertions(+), 4 deletions(-)
diff --git a/cssselect/parser.py b/cssselect/parser.py
index 7125030..0185cb2 100644
--- a/cssselect/parser.py
+++ b/cssselect/parser.py
@@ -517,8 +517,13 @@ def parse_simple_selector(stream, inside_negation=False):
result = Pseudo(result, ident)
if result.__repr__() == 'Pseudo[Element[*]:scope]':
if not (len(stream.used) == 2 or
- (len(stream.used) == 3
- and stream.used[0].type == 'S')):
+ (len(stream.used) == 3 and
+ stream.used[0].type == 'S') or
+ (len(stream.used) >= 3 and
+ stream.used[-3].is_delim(',')) or
+ (len(stream.used) >= 4 and
+ stream.used[-3].type == 'S' and
+ stream.used[-4].is_delim(','))):
raise SelectorSyntaxError(
'Got immediate child pseudo-element ":scope" '
'not at the start of a selector')
diff --git a/tests/test_cssselect.py b/tests/test_cssselect.py
index 320736c..b81f8c5 100644
--- a/tests/test_cssselect.py
+++ b/tests/test_cssselect.py
@@ -214,7 +214,15 @@ def test_pseudo_repr(css):
'selection')
assert parse_pseudo(':scope > div, foo bar') == [
('CombinedSelector[Pseudo[Element[*]:scope] > Element[div]]', None),
- ('CombinedSelector[Element[foo] Element[bar]]', None)
+ ('CombinedSelector[Element[foo] Element[bar]]', None),
+ ]
+ assert parse_pseudo('foo bar, :scope > div') == [
+ ('CombinedSelector[Element[foo] Element[bar]]', None),
+ ('CombinedSelector[Pseudo[Element[*]:scope] > Element[div]]', None),
+ ]
+ assert parse_pseudo('foo bar,:scope > div') == [
+ ('CombinedSelector[Element[foo] Element[bar]]', None),
+ ('CombinedSelector[Pseudo[Element[*]:scope] > Element[div]]', None),
]
assert parse_pseudo('foo:before, bar, baz:after') == [
('Element[foo]', 'before'), ('Element[bar]', None),
diff --git a/tox.ini b/tox.ini
index 49a1dda..6a09b07 100644
--- a/tox.ini
+++ b/tox.ini
@@ -6,4 +6,4 @@ deps=
-r tests/requirements.txt
commands =
- py.test --cov-report term --cov=cssselect
+ py.test --cov-report term --cov=cssselect {posargs}
From 928ad922ddf3701bef5dc178a485b0d0246b784e Mon Sep 17 00:00:00 2001
From: Akshita Agarwal
Date: Wed, 16 Oct 2019 17:46:06 +0530
Subject: [PATCH 030/108] add 3.7 version after running tests
---
setup.py | 1 +
1 file changed, 1 insertion(+)
diff --git a/setup.py b/setup.py
index 243927d..de7128d 100644
--- a/setup.py
+++ b/setup.py
@@ -40,6 +40,7 @@
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
+ 'Programming Language :: Python :: 3.7'
],
**extra_kwargs
)
From 24eb0952eaf4c1f1ed86c123840e606959962953 Mon Sep 17 00:00:00 2001
From: Akshita Agarwal
Date: Wed, 16 Oct 2019 21:14:46 +0530
Subject: [PATCH 031/108] address comments
---
.travis.yml | 3 ++-
tox.ini | 2 +-
2 files changed, 3 insertions(+), 2 deletions(-)
diff --git a/.travis.yml b/.travis.yml
index bfc557f..69ecf93 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -4,6 +4,7 @@ python:
- '3.4'
- '3.5'
- '3.6'
+- '3.7'
install:
- pip install -r tests/requirements.txt -e .
script:
@@ -20,4 +21,4 @@ deploy:
on:
tags: true
repo: scrapy/cssselect
- condition: "$TRAVIS_PYTHON_VERSION == '3.6'"
+ condition: "$TRAVIS_PYTHON_VERSION == '3.7'"
diff --git a/tox.ini b/tox.ini
index 49a1dda..32136a0 100644
--- a/tox.ini
+++ b/tox.ini
@@ -1,5 +1,5 @@
[tox]
-envlist = py27, py34, py35, py36
+envlist = py27, py34, py35, py36, py37
[testenv]
deps=
From c05327240d73beda2132a1d3fcf0d33317738a58 Mon Sep 17 00:00:00 2001
From: whybin <31753349+whybin@users.noreply.github.com>
Date: Thu, 31 May 2018 16:24:45 -0700
Subject: [PATCH 032/108] Add XPath tests for operator precedence
---
tests/test_cssselect.py | 32 ++++++++++++++++++++++++++++++++
1 file changed, 32 insertions(+)
diff --git a/tests/test_cssselect.py b/tests/test_cssselect.py
index 320736c..94da2e1 100644
--- a/tests/test_cssselect.py
+++ b/tests/test_cssselect.py
@@ -622,6 +622,11 @@ def xpath_attr_href_simple_pseudo_element(self, xpath):
other = XPathExpr('@href', '', )
return xpath.join('/', other)
+ # pseudo-element:
+ # used to demonstrate operator precedence
+ def xpath_first_or_second_pseudo(self, xpath):
+ return xpath.add_condition("@id = 'first' or @id = 'second'")
+
def xpath(css):
return _unicode(CustomTranslator().css_to_xpath(css))
@@ -633,6 +638,25 @@ def xpath(css):
assert xpath('p img::attr(src)') == (
"descendant-or-self::p/descendant-or-self::*/img/@src")
assert xpath(':scope') == "descendant-or-self::*[1]"
+ assert xpath(':first-or-second[href]') == (
+ "descendant-or-self::*[(@id = 'first' or @id = 'second') "
+ "and (@href)]")
+
+ assert str(XPathExpr('', '', condition='@href')) == "[(@href)]"
+
+ document = etree.fromstring(OPERATOR_PRECEDENCE_IDS)
+ sort_key = dict(
+ (el, count) for count, el in enumerate(document.getiterator())
+ ).__getitem__
+ def operator_id(selector):
+ xpath = CustomTranslator().css_to_xpath(selector)
+ items = document.xpath(xpath)
+ items.sort(key=sort_key)
+ return [element.get('id', 'nil') for element in items]
+
+ assert operator_id(':first-or-second') == ['first', 'second']
+ assert operator_id(':first-or-second[href]') == ['second']
+ assert operator_id('[href]:first-or-second') == ['second']
def test_series(self):
def series(css):
@@ -935,6 +959,14 @@ def count(selector):
assert count(':scope > div > div[class=dialog]') == 1
assert count(':scope > div div') == 242
+OPERATOR_PRECEDENCE_IDS = '''
+
+
+
+
+
+'''
+
XMLLANG_IDS = '''
a
From 754b701bc26dcd239ae1d9813774f75f78ed2dc3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adri=C3=A1n=20Chaves?=
Date: Mon, 21 Oct 2019 16:43:10 +0200
Subject: [PATCH 033/108] Use parentheses when joining with AND
potentially-complex expressions
---
cssselect/xpath.py | 17 +++++++++++------
tests/test_cssselect.py | 12 ++++++------
2 files changed, 17 insertions(+), 12 deletions(-)
diff --git a/cssselect/xpath.py b/cssselect/xpath.py
index db50c77..14e9b80 100644
--- a/cssselect/xpath.py
+++ b/cssselect/xpath.py
@@ -56,7 +56,7 @@ def __repr__(self):
def add_condition(self, condition):
if self.condition:
- self.condition = '%s and (%s)' % (self.condition, condition)
+ self.condition = '(%s) and (%s)' % (self.condition, condition)
else:
self.condition = condition
return self
@@ -457,19 +457,19 @@ def xpath_nth_child_function(self, xpath, function, last=False,
if a == 0:
return xpath.add_condition('%s = %s' % (siblings_count, b_min_1))
- expr = []
+ expressions = []
if a > 0:
# siblings count, an+b-1, is always >= 0,
# so if a>0, and (b-1)<=0, an "n" exists to satisfy this,
# therefore, the predicate is only interesting if (b-1)>0
if b_min_1 > 0:
- expr.append('%s >= %s' % (siblings_count, b_min_1))
+ expressions.append('%s >= %s' % (siblings_count, b_min_1))
else:
# if a<0, and (b-1)<0, no "n" satisfies this,
# this is tested above as an early exist condition
# otherwise,
- expr.append('%s <= %s' % (siblings_count, b_min_1))
+ expressions.append('%s <= %s' % (siblings_count, b_min_1))
# operations modulo 1 or -1 are simpler, one only needs to verify:
#
@@ -495,9 +495,14 @@ def xpath_nth_child_function(self, xpath, function, last=False,
b_neg = '+%s' % b_neg
left = '(%s %s)' % (left, b_neg)
- expr.append('%s mod %s = 0' % (left, a))
+ expressions.append('%s mod %s = 0' % (left, a))
- xpath.add_condition(' and '.join(expr))
+ if len(expressions) > 1:
+ template = '(%s)'
+ else:
+ template = '%s'
+ xpath.add_condition(' and '.join(template % expression
+ for expression in expressions))
return xpath
def xpath_nth_last_child_function(self, xpath, function):
diff --git a/tests/test_cssselect.py b/tests/test_cssselect.py
index 94da2e1..d6969f2 100644
--- a/tests/test_cssselect.py
+++ b/tests/test_cssselect.py
@@ -428,8 +428,8 @@ def xpath(css):
"e[count(preceding-sibling::*) <= 0]")
assert xpath('e:nth-child(3n+2)') == (
- "e[count(preceding-sibling::*) >= 1 and "
- "(count(preceding-sibling::*) +2) mod 3 = 0]")
+ "e[(count(preceding-sibling::*) >= 1) and "
+ "((count(preceding-sibling::*) +2) mod 3 = 0)]")
assert xpath('e:nth-child(3n-2)') == (
"e[count(preceding-sibling::*) mod 3 = 0]")
assert xpath('e:nth-child(-n+6)') == (
@@ -442,8 +442,8 @@ def xpath(css):
assert xpath('e:nth-last-child(2n+1)') == (
"e[count(following-sibling::*) mod 2 = 0]")
assert xpath('e:nth-last-child(2n+2)') == (
- "e[count(following-sibling::*) >= 1 and "
- "(count(following-sibling::*) +1) mod 2 = 0]")
+ "e[(count(following-sibling::*) >= 1) and "
+ "((count(following-sibling::*) +1) mod 2 = 0)]")
assert xpath('e:nth-last-child(3n+1)') == (
"e[count(following-sibling::*) mod 3 = 0]")
# represents the two last e elements
@@ -497,7 +497,7 @@ def xpath(css):
assert xpath('e > f') == (
"e/f")
assert xpath('e + f') == (
- "e/following-sibling::*[name() = 'f' and (position() = 1)]")
+ "e/following-sibling::*[(name() = 'f') and (position() = 1)]")
assert xpath('e ~ f') == (
"e/following-sibling::f")
assert xpath('e ~ f:nth-child(3)') == (
@@ -642,7 +642,7 @@ def xpath(css):
"descendant-or-self::*[(@id = 'first' or @id = 'second') "
"and (@href)]")
- assert str(XPathExpr('', '', condition='@href')) == "[(@href)]"
+ assert str(XPathExpr('', '', condition='@href')) == "[@href]"
document = etree.fromstring(OPERATOR_PRECEDENCE_IDS)
sort_key = dict(
From dde3b5e68ba2e49ec4552a75a805536c7dcdc896 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adri=C3=A1n=20Chaves?=
Date: Tue, 22 Oct 2019 16:55:17 +0200
Subject: [PATCH 034/108] Enable nitpicky Sphinx warnings, fix issues and fail
on new issues
---
.travis.yml | 24 ++++++++++++++++++------
CHANGES | 4 ++--
docs/conf.py | 5 +++++
tox.ini | 10 +++++++++-
4 files changed, 34 insertions(+), 9 deletions(-)
diff --git a/.travis.yml b/.travis.yml
index 69ecf93..bd043e1 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,16 +1,28 @@
language: python
-python:
-- '2.7'
-- '3.4'
-- '3.5'
-- '3.6'
-- '3.7'
+matrix:
+ include:
+ - python: 2.7
+ env: TOXENV=py27
+ - python: 3.4
+ env: TOXENV=py34
+ - python: 3.5
+ env: TOXENV=py35
+ - python: 3.6
+ env: TOXENV=py36
+ - python: 3.7
+ env: TOXENV=py37
+ - python: 3.7
+ env: TOXENV=docs
+
install:
- pip install -r tests/requirements.txt -e .
+
script:
- py.test --cov-report term --cov=cssselect
+
after_success:
- codecov
+
deploy:
provider: pypi
distributions: sdist bdist_wheel
diff --git a/CHANGES b/CHANGES
index a6c5233..4e7185f 100644
--- a/CHANGES
+++ b/CHANGES
@@ -199,14 +199,14 @@ Version 0.3
Released on 2012-04-17.
* Fix many parsing bugs.
-* Rename the :class:`Translator` class to :class:`GenericTranslator`
+* Rename the ``Translator`` class to :class:`GenericTranslator`
* There, implement ``:target``, ``:hover``, ``:focus``, ``:active``
``:checked``, ``:enabled``, ``:disabled``, ``:link`` and ``:visited``
as never matching.
* Make a new HTML-specific ``HTMLTranslator`` subclass. There, implement
``:checked``, ``:enabled``, ``:disabled``, ``:link`` and ``:visited``
as appropriate for HTML, with all links "not visited".
-* Remove the :func:`css_to_xpath` function. The translator classes
+* Remove the ``css_to_xpath`` function. The translator classes
are the new API.
* Add support for ``:contains()`` back, but case-sensitive. lxml will
override it to be case-insensitive for backward-compatibility.
diff --git a/docs/conf.py b/docs/conf.py
index aa897ef..86898c2 100644
--- a/docs/conf.py
+++ b/docs/conf.py
@@ -248,3 +248,8 @@
# Example configuration for intersphinx: refer to the Python standard library.
intersphinx_mapping = {'http://docs.python.org/': None}
+
+
+# --- Nitpicking options ------------------------------------------------------
+
+nitpicky = True
diff --git a/tox.ini b/tox.ini
index 32136a0..1d50b69 100644
--- a/tox.ini
+++ b/tox.ini
@@ -1,5 +1,5 @@
[tox]
-envlist = py27, py34, py35, py36, py37
+envlist = py27, py34, py35, py36, py37, docs
[testenv]
deps=
@@ -7,3 +7,11 @@ deps=
commands =
py.test --cov-report term --cov=cssselect
+
+[testenv:docs]
+changedir = docs
+deps =
+ sphinx
+ sphinx_rtd_theme
+commands =
+ sphinx-build -W -b html . {envtmpdir}/html
\ No newline at end of file
From f4a04641c6ff66aec9e4247be079b02942c81b65 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adri=C3=A1n=20Chaves?=
Date: Tue, 22 Oct 2019 18:13:54 +0200
Subject: [PATCH 035/108] Remove unused FunctionalPseudoElement methods
---
cssselect/parser.py | 8 --------
1 file changed, 8 deletions(-)
diff --git a/cssselect/parser.py b/cssselect/parser.py
index 7125030..b63e3df 100644
--- a/cssselect/parser.py
+++ b/cssselect/parser.py
@@ -165,18 +165,10 @@ def __repr__(self):
self.__class__.__name__, self.name,
[token.value for token in self.arguments])
- def argument_types(self):
- return [token.type for token in self.arguments]
-
def canonical(self):
args = ''.join(token.css() for token in self.arguments)
return '%s(%s)' % (self.name, args)
- def specificity(self):
- a, b, c = self.selector.specificity()
- b += 1
- return a, b, c
-
class Function(object):
"""
From b4efd7f0e61324be6fe8d25d268628490dda82ba Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adri=C3=A1n=20Chaves?=
Date: Tue, 22 Oct 2019 18:37:35 +0200
Subject: [PATCH 036/108] Revert "Remove unused FunctionalPseudoElement
methods"
This reverts commit f4a04641c6ff66aec9e4247be079b02942c81b65.
---
cssselect/parser.py | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/cssselect/parser.py b/cssselect/parser.py
index b63e3df..7125030 100644
--- a/cssselect/parser.py
+++ b/cssselect/parser.py
@@ -165,10 +165,18 @@ def __repr__(self):
self.__class__.__name__, self.name,
[token.value for token in self.arguments])
+ def argument_types(self):
+ return [token.type for token in self.arguments]
+
def canonical(self):
args = ''.join(token.css() for token in self.arguments)
return '%s(%s)' % (self.name, args)
+ def specificity(self):
+ a, b, c = self.selector.specificity()
+ b += 1
+ return a, b, c
+
class Function(object):
"""
From 9c1fbc9c194c312077a9f82fab0b0c2e57a22e77 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adri=C3=A1n=20Chaves?=
Date: Tue, 22 Oct 2019 19:03:04 +0200
Subject: [PATCH 037/108] Cover all FunctionalPseudoElement methods with tests
---
tests/test_cssselect.py | 26 ++++++++++++++++++++++++++
1 file changed, 26 insertions(+)
diff --git a/tests/test_cssselect.py b/tests/test_cssselect.py
index 320736c..e4bd74e 100644
--- a/tests/test_cssselect.py
+++ b/tests/test_cssselect.py
@@ -258,6 +258,7 @@ def specificity(css):
assert specificity('[baz="4"]') == (0, 1, 0)
assert specificity('[baz^="4"]') == (0, 1, 0)
assert specificity('#lipsum') == (1, 0, 0)
+ assert specificity('::attr(name)') == (0, 0, 1)
assert specificity(':not(*)') == (0, 0, 0)
assert specificity(':not(foo)') == (0, 0, 1)
@@ -686,6 +687,31 @@ def langid(selector):
'first', 'second', 'third', 'fourth', 'eighth']
assert langid(':lang(es)') == []
+ def test_argument_types(self):
+
+ class CustomTranslator(GenericTranslator):
+
+ def __init__(self):
+ self.argument_types = []
+
+ def xpath_pseudo_element(self, xpath, function):
+ self.argument_types += function.argument_types()
+
+ def argument_types(css):
+ translator = CustomTranslator()
+ translator.css_to_xpath(css)
+ return translator.argument_types
+
+ mappings = (
+ ('', []),
+ ('ident', ['IDENT']),
+ ('"string"', ['STRING']),
+ ('1', ['NUMBER']),
+ )
+ for argument_string, argument_list in mappings:
+ css = '::pseudo_element({})'.format(argument_string)
+ assert argument_types(css) == argument_list
+
def test_select(self):
document = etree.fromstring(HTML_IDS)
sort_key = dict(
From 98019114d6b01f64cdcf38ad34abd5cc63e2accd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adri=C3=A1n=20Chaves?=
Date: Thu, 24 Oct 2019 13:44:49 +0200
Subject: [PATCH 038/108] Add a PyLint CI pipeline
---
.travis.yml | 23 ++++++++++++++++-------
pylintrc | 33 +++++++++++++++++++++++++++++++++
tox.ini | 8 ++++++++
3 files changed, 57 insertions(+), 7 deletions(-)
create mode 100644 pylintrc
diff --git a/.travis.yml b/.travis.yml
index 69ecf93..63d3ef2 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,10 +1,19 @@
language: python
-python:
-- '2.7'
-- '3.4'
-- '3.5'
-- '3.6'
-- '3.7'
+sudo: false
+matrix:
+ include:
+ - python: 3.7
+ env: TOXENV=pylint
+ - python: 2.7
+ env: TOXENV=py27
+ - python: 3.4
+ env: TOXENV=py34
+ - python: 3.5
+ env: TOXENV=py35
+ - python: 3.6
+ env: TOXENV=py36
+ - python: 3.7
+ env: TOXENV=py37
install:
- pip install -r tests/requirements.txt -e .
script:
@@ -21,4 +30,4 @@ deploy:
on:
tags: true
repo: scrapy/cssselect
- condition: "$TRAVIS_PYTHON_VERSION == '3.7'"
+ condition: "$TOXENV == py37"
diff --git a/pylintrc b/pylintrc
new file mode 100644
index 0000000..b6972ec
--- /dev/null
+++ b/pylintrc
@@ -0,0 +1,33 @@
+[MASTER]
+persistent=no
+
+[MESSAGES CONTROL]
+disable=assignment-from-no-return,
+ bad-continuation,
+ bad-whitespace,
+ c-extension-no-member,
+ consider-using-in,
+ fixme,
+ inconsistent-return-statements,
+ invalid-name,
+ missing-class-docstring,
+ missing-function-docstring,
+ missing-module-docstring,
+ multiple-imports,
+ no-else-return,
+ no-member,
+ no-self-use,
+ redefined-builtin,
+ redefined-outer-name,
+ too-few-public-methods,
+ too-many-arguments,
+ too-many-branches,
+ too-many-function-args,
+ too-many-lines,
+ too-many-public-methods,
+ too-many-statements,
+ undefined-variable,
+ unidiomatic-typecheck,
+ unused-argument,
+ unused-import,
+ useless-object-inheritance # Required for Python 2 support
diff --git a/tox.ini b/tox.ini
index 32136a0..430720a 100644
--- a/tox.ini
+++ b/tox.ini
@@ -7,3 +7,11 @@ deps=
commands =
py.test --cov-report term --cov=cssselect
+
+[testenv:pylint]
+basepython = python3.7
+deps =
+ {[testenv]deps}
+ pylint
+commands =
+ pylint cssselect docs setup.py tests
From cc573dfd2f83266c35014682e0fd16727b24f2fa Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adri=C3=A1n=20Chaves?=
Date: Mon, 11 Nov 2019 12:30:25 +0100
Subject: [PATCH 039/108] Also run tests from the documentation
---
docs/conftest.py | 16 ++++++++++++++++
tests/requirements.txt | 3 ++-
tox.ini | 2 +-
3 files changed, 19 insertions(+), 2 deletions(-)
create mode 100644 docs/conftest.py
diff --git a/docs/conftest.py b/docs/conftest.py
new file mode 100644
index 0000000..a98f9e5
--- /dev/null
+++ b/docs/conftest.py
@@ -0,0 +1,16 @@
+from doctest import ELLIPSIS, NORMALIZE_WHITESPACE
+
+from sybil import Sybil
+from sybil.parsers.codeblock import CodeBlockParser
+from sybil.parsers.doctest import DocTestParser
+from sybil.parsers.skip import skip
+
+
+pytest_collect_file = Sybil(
+ parsers=[
+ DocTestParser(optionflags=ELLIPSIS | NORMALIZE_WHITESPACE),
+ CodeBlockParser(future_imports=['print_function']),
+ skip,
+ ],
+ pattern='*.rst',
+).pytest()
diff --git a/tests/requirements.txt b/tests/requirements.txt
index 5232e84..00f8f94 100644
--- a/tests/requirements.txt
+++ b/tests/requirements.txt
@@ -2,4 +2,5 @@ codecov
lxml;python_version!="3.4"
lxml<=4.3.5;python_version=="3.4"
pytest
-pytest-cov
\ No newline at end of file
+pytest-cov
+sybil
\ No newline at end of file
diff --git a/tox.ini b/tox.ini
index 32136a0..ad6780d 100644
--- a/tox.ini
+++ b/tox.ini
@@ -6,4 +6,4 @@ deps=
-r tests/requirements.txt
commands =
- py.test --cov-report term --cov=cssselect
+ py.test --cov-report term --cov=cssselect docs tests
From 05c0e76dc68ac0a62ef4ba47c1e1ace855053a1d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adri=C3=A1n=20Chaves?=
Date: Thu, 14 Nov 2019 12:24:42 +0100
Subject: [PATCH 040/108] Add bandit to CI
---
.bandit.yml | 2 ++
.travis.yml | 15 +++++++++------
tox.ini | 6 ++++++
3 files changed, 17 insertions(+), 6 deletions(-)
create mode 100644 .bandit.yml
diff --git a/.bandit.yml b/.bandit.yml
new file mode 100644
index 0000000..7fcde04
--- /dev/null
+++ b/.bandit.yml
@@ -0,0 +1,2 @@
+skips:
+- B101
diff --git a/.travis.yml b/.travis.yml
index 69ecf93..dbf5885 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,10 +1,13 @@
language: python
-python:
-- '2.7'
-- '3.4'
-- '3.5'
-- '3.6'
-- '3.7'
+matrix:
+ include:
+ - env: TOXENV=security
+ python: 3.8
+ - python: 2.7
+ - python: 3.4
+ - python: 3.5
+ - python: 3.6
+ - python: 3.7
install:
- pip install -r tests/requirements.txt -e .
script:
diff --git a/tox.ini b/tox.ini
index 32136a0..4db8e7c 100644
--- a/tox.ini
+++ b/tox.ini
@@ -7,3 +7,9 @@ deps=
commands =
py.test --cov-report term --cov=cssselect
+
+[testenv:security]
+deps =
+ bandit
+commands =
+ bandit -r -c .bandit.yml {posargs:cssselect}
From b26932d66cd629dbe491b31f2ac5462d2311db14 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adri=C3=A1n=20Chaves?=
Date: Sun, 11 Oct 2020 20:12:40 +0200
Subject: [PATCH 041/108] Fix class reference (#110)
---
cssselect/xpath.py | 2 +-
tests/requirements.txt | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/cssselect/xpath.py b/cssselect/xpath.py
index db50c77..eb8be92 100644
--- a/cssselect/xpath.py
+++ b/cssselect/xpath.py
@@ -180,7 +180,7 @@ def css_to_xpath(self, css, prefix='descendant-or-self::'):
This string is prepended to the XPath expression for each selector.
The default makes selectors scoped to the context node’s subtree.
:raises:
- :class:`SelectorSyntaxError` on invalid selectors,
+ :class:`~cssselect.SelectorSyntaxError` on invalid selectors,
:class:`ExpressionError` on unknown/unsupported selectors,
including pseudo-elements.
:returns:
diff --git a/tests/requirements.txt b/tests/requirements.txt
index 5232e84..000d5f2 100644
--- a/tests/requirements.txt
+++ b/tests/requirements.txt
@@ -1,5 +1,5 @@
codecov
lxml;python_version!="3.4"
lxml<=4.3.5;python_version=="3.4"
-pytest
+pytest >=4.6, <4.7 # 4.7 drops support for Python 2.7 and 3.4
pytest-cov
\ No newline at end of file
From 163404122e5a05afe71dba59d808d7afd9726344 Mon Sep 17 00:00:00 2001
From: KOLANICH
Date: Sat, 20 Feb 2021 17:20:36 +0300
Subject: [PATCH 042/108] Added .editorconfig according to PEP 8
---
.editorconfig | 11 +++++++++++
1 file changed, 11 insertions(+)
create mode 100644 .editorconfig
diff --git a/.editorconfig b/.editorconfig
new file mode 100644
index 0000000..38558bf
--- /dev/null
+++ b/.editorconfig
@@ -0,0 +1,11 @@
+root = true
+
+[*]
+charset = utf-8
+indent_style = space
+indent_size = 4
+insert_final_newline = true
+end_of_line = lf
+
+[*.{yml,yaml}]
+indent_size = 2
From 1f643a84d651ebd3075c2f61e30f46bf90451b46 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adri=C3=A1n=20Chaves?=
Date: Fri, 5 Mar 2021 16:50:19 +0100
Subject: [PATCH 043/108] =?UTF-8?q?Travis=20CI=20=E2=86=92=20GitHub=20Acti?=
=?UTF-8?q?ons?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.github/workflows/publish.yml | 31 ++++++++++++++++++++++++++
.github/workflows/tests.yml | 41 +++++++++++++++++++++++++++++++++++
.travis.yml | 24 --------------------
tox.ini | 2 +-
4 files changed, 73 insertions(+), 25 deletions(-)
create mode 100644 .github/workflows/publish.yml
create mode 100644 .github/workflows/tests.yml
delete mode 100644 .travis.yml
diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml
new file mode 100644
index 0000000..7c0f8d0
--- /dev/null
+++ b/.github/workflows/publish.yml
@@ -0,0 +1,31 @@
+name: Publish
+on: [push]
+
+jobs:
+ publish:
+ runs-on: ubuntu-latest
+ if: startsWith(github.event.ref, 'refs/tags/')
+
+ steps:
+ - uses: actions/checkout@v2
+
+ - name: Set up Python 3.8
+ uses: actions/setup-python@v2
+ with:
+ python-version: 3
+
+ - name: Check Tag
+ id: check-release-tag
+ run: |
+ if [[ ${{ github.event.ref }} =~ ^refs/tags/[0-9]+[.][0-9]+[.][0-9]+(rc[0-9]+|[.]dev[0-9]+)?$ ]]; then
+ echo ::set-output name=release_tag::true
+ fi
+
+ - name: Publish to PyPI
+ if: steps.check-release-tag.outputs.release_tag == 'true'
+ run: |
+ pip install --upgrade setuptools wheel twine
+ python setup.py sdist bdist_wheel
+ export TWINE_USERNAME=__token__
+ export TWINE_PASSWORD=${{ secrets.PYPI_TOKEN }}
+ twine upload dist/*
diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml
new file mode 100644
index 0000000..1a0cf65
--- /dev/null
+++ b/.github/workflows/tests.yml
@@ -0,0 +1,41 @@
+name: Tests
+on: [push, pull_request]
+
+jobs:
+ tests:
+ runs-on: ubuntu-latest
+ strategy:
+ matrix:
+ include:
+ - python-version: 2.7
+ env:
+ TOXENV: py
+ - python-version: 3.4
+ env:
+ TOXENV: py
+ - python-version: 3.5
+ env:
+ TOXENV: py
+ - python-version: 3.6
+ env:
+ TOXENV: py
+ - python-version: 3.7
+ env:
+ TOXENV: py
+
+ steps:
+ - uses: actions/checkout@v2
+
+ - name: Set up Python ${{ matrix.python-version }}
+ uses: actions/setup-python@v2
+ with:
+ python-version: ${{ matrix.python-version }}
+
+ - name: Run tests
+ env: ${{ matrix.env }}
+ run: |
+ pip install -U tox
+ tox
+
+ - name: Upload coverage report
+ run: bash <(curl -s https://codecov.io/bash)
\ No newline at end of file
diff --git a/.travis.yml b/.travis.yml
deleted file mode 100644
index 69ecf93..0000000
--- a/.travis.yml
+++ /dev/null
@@ -1,24 +0,0 @@
-language: python
-python:
-- '2.7'
-- '3.4'
-- '3.5'
-- '3.6'
-- '3.7'
-install:
-- pip install -r tests/requirements.txt -e .
-script:
-- py.test --cov-report term --cov=cssselect
-after_success:
-- codecov
-deploy:
- provider: pypi
- distributions: sdist bdist_wheel
- skip_upload_docs: true
- user: scrapy
- password:
- secure: UjCXD1ZfqgFcCs4ciPMJDaOQefV3ZOKZ8/dTZxcoaQlE1lr6CkaN6CfTdD50SX2M9uCNWvEcYnvs6U4SizgZ27MYzFWuHonED2alHKy4AtrxCEHD/+lGo9d18cNjLMPDZateX/lITjGiZ4rmYZNuA6wmA4P/bTmdazbSufcmMqY=
- on:
- tags: true
- repo: scrapy/cssselect
- condition: "$TRAVIS_PYTHON_VERSION == '3.7'"
diff --git a/tox.ini b/tox.ini
index 32136a0..4fb1d7c 100644
--- a/tox.ini
+++ b/tox.ini
@@ -1,5 +1,5 @@
[tox]
-envlist = py27, py34, py35, py36, py37
+envlist = py
[testenv]
deps=
From b9506ce52a622b001d965c99e10d8deaf25e8bd0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adri=C3=A1n=20Chaves?=
Date: Fri, 5 Mar 2021 16:51:31 +0100
Subject: [PATCH 044/108] Remove end-of-life Python 3.4 from CI
---
.github/workflows/tests.yml | 3 ---
1 file changed, 3 deletions(-)
diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml
index 1a0cf65..817d824 100644
--- a/.github/workflows/tests.yml
+++ b/.github/workflows/tests.yml
@@ -10,9 +10,6 @@ jobs:
- python-version: 2.7
env:
TOXENV: py
- - python-version: 3.4
- env:
- TOXENV: py
- python-version: 3.5
env:
TOXENV: py
From a2e2894bd79457fed402b91440b63f0b28692b02 Mon Sep 17 00:00:00 2001
From: Eugenio Lacuesta
Date: Wed, 23 Jun 2021 10:02:12 -0300
Subject: [PATCH 045/108] Update CI badge
---
README.rst | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/README.rst b/README.rst
index 9bcd648..c6d387f 100644
--- a/README.rst
+++ b/README.rst
@@ -10,9 +10,9 @@ cssselect: CSS Selectors for Python
:target: https://pypi.python.org/pypi/cssselect
:alt: Supported Python Versions
-.. image:: https://img.shields.io/travis/scrapy/cssselect/master.svg
- :target: https://travis-ci.org/scrapy/cssselect
- :alt: Build Status
+.. image:: https://github.com/scrapy/cssselect/actions/workflows/tests.yml/badge.svg
+ :target: https://github.com/scrapy/cssselect/actions/workflows/tests.yml
+ :alt: Tests
.. image:: https://img.shields.io/codecov/c/github/scrapy/cssselect/master.svg
:target: https://codecov.io/github/scrapy/cssselect?branch=master
From 4bf687a167e5abd1e50f65b1749baa7634767665 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adri=C3=A1n=20Chaves?=
Date: Wed, 23 Jun 2021 15:43:46 +0200
Subject: [PATCH 046/108] Add flake8 (#105)
---
.flake8 | 15 +++++++++++++++
.github/workflows/checks.yml | 3 +++
tox.ini | 12 +++++++++---
3 files changed, 27 insertions(+), 3 deletions(-)
create mode 100644 .flake8
diff --git a/.flake8 b/.flake8
new file mode 100644
index 0000000..89e6e07
--- /dev/null
+++ b/.flake8
@@ -0,0 +1,15 @@
+[flake8]
+max-line-length = 99
+ignore = W503
+exclude =
+ .git
+ .tox
+ venv*
+
+ # pending revision
+ cssselect/__init__.py
+ cssselect/parser.py
+ cssselect/xpath.py
+ docs/conf.py
+ setup.py
+ tests/test_cssselect.py
diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml
index 99fff74..2f38d19 100644
--- a/.github/workflows/checks.yml
+++ b/.github/workflows/checks.yml
@@ -7,6 +7,9 @@ jobs:
strategy:
matrix:
include:
+ - python-version: 3
+ env:
+ TOXENV: flake8
- python-version: 3
env:
TOXENV: security
diff --git a/tox.ini b/tox.ini
index eabac24..5ae98ce 100644
--- a/tox.ini
+++ b/tox.ini
@@ -1,15 +1,21 @@
[tox]
-envlist = security,py
+envlist = flake8,security,py
[testenv]
+basepython = python3
deps=
-r tests/requirements.txt
-
commands =
py.test --cov-report term --cov=cssselect
+[testenv:flake8]
+deps =
+ flake8==3.9.2
+commands =
+ flake8 {posargs: cssselect setup.py tests docs/conf.py}
+
[testenv:security]
deps =
bandit
commands =
- bandit -r -c .bandit.yml {posargs:cssselect}
+ bandit -r -c .bandit.yml {posargs: cssselect}
From 5399d4194e14ad79247bc589cb777b5a547ac149 Mon Sep 17 00:00:00 2001
From: Eugenio Lacuesta
Date: Wed, 23 Jun 2021 11:21:22 -0300
Subject: [PATCH 047/108] Add black check
---
.github/workflows/checks.yml | 3 +++
pyproject.toml | 3 +++
setup.py | 50 +++++++++++++++++++-----------------
tox.ini | 8 +++++-
4 files changed, 39 insertions(+), 25 deletions(-)
create mode 100644 pyproject.toml
diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml
index 2f38d19..bb50590 100644
--- a/.github/workflows/checks.yml
+++ b/.github/workflows/checks.yml
@@ -7,6 +7,9 @@ jobs:
strategy:
matrix:
include:
+ - python-version: 3
+ env:
+ TOXENV: black
- python-version: 3
env:
TOXENV: flake8
diff --git a/pyproject.toml b/pyproject.toml
new file mode 100644
index 0000000..b409f47
--- /dev/null
+++ b/pyproject.toml
@@ -0,0 +1,3 @@
+[tool.black]
+line-length = 99
+exclude = 'cssselect/|tests/'
diff --git a/setup.py b/setup.py
index de7128d..3782534 100644
--- a/setup.py
+++ b/setup.py
@@ -2,45 +2,47 @@
import re
import os.path
+
try:
from setuptools import setup
- extra_kwargs = {'test_suite': 'cssselect.tests'}
+
+ extra_kwargs = {"test_suite": "cssselect.tests"}
except ImportError:
from distutils.core import setup
+
extra_kwargs = {}
ROOT = os.path.dirname(__file__)
-README = open(os.path.join(ROOT, 'README.rst')).read()
-INIT_PY = open(os.path.join(ROOT, 'cssselect', '__init__.py')).read()
+README = open(os.path.join(ROOT, "README.rst")).read()
+INIT_PY = open(os.path.join(ROOT, "cssselect", "__init__.py")).read()
VERSION = re.search("VERSION = '([^']+)'", INIT_PY).group(1)
setup(
- name='cssselect',
+ name="cssselect",
version=VERSION,
- author='Ian Bicking',
- author_email='ianb@colorstudy.com',
- maintainer='Paul Tremberth',
- maintainer_email='paul.tremberth@gmail.com',
- description=
- 'cssselect parses CSS3 Selectors and translates them to XPath 1.0',
+ author="Ian Bicking",
+ author_email="ianb@colorstudy.com",
+ maintainer="Paul Tremberth",
+ maintainer_email="paul.tremberth@gmail.com",
+ description="cssselect parses CSS3 Selectors and translates them to XPath 1.0",
long_description=README,
- url='https://github.com/scrapy/cssselect',
- license='BSD',
- packages=['cssselect'],
- python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*',
+ url="https://github.com/scrapy/cssselect",
+ license="BSD",
+ packages=["cssselect"],
+ python_requires=">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*",
classifiers=[
- 'Development Status :: 4 - Beta',
- 'Intended Audience :: Developers',
- 'License :: OSI Approved :: BSD License',
- 'Programming Language :: Python :: 2',
- 'Programming Language :: Python :: 2.7',
- 'Programming Language :: Python :: 3',
- 'Programming Language :: Python :: 3.4',
- 'Programming Language :: Python :: 3.5',
- 'Programming Language :: Python :: 3.6',
- 'Programming Language :: Python :: 3.7'
+ "Development Status :: 4 - Beta",
+ "Intended Audience :: Developers",
+ "License :: OSI Approved :: BSD License",
+ "Programming Language :: Python :: 2",
+ "Programming Language :: Python :: 2.7",
+ "Programming Language :: Python :: 3",
+ "Programming Language :: Python :: 3.4",
+ "Programming Language :: Python :: 3.5",
+ "Programming Language :: Python :: 3.6",
+ "Programming Language :: Python :: 3.7",
],
**extra_kwargs
)
diff --git a/tox.ini b/tox.ini
index 5ae98ce..1d94302 100644
--- a/tox.ini
+++ b/tox.ini
@@ -1,5 +1,5 @@
[tox]
-envlist = flake8,security,py
+envlist = black,flake8,security,py
[testenv]
basepython = python3
@@ -8,6 +8,12 @@ deps=
commands =
py.test --cov-report term --cov=cssselect
+[testenv:black]
+deps =
+ black==21.6b0
+commands =
+ black --check {posargs: cssselect setup.py tests}
+
[testenv:flake8]
deps =
flake8==3.9.2
From 79c341b15930b6c5ec811a4f7953719722148e3a Mon Sep 17 00:00:00 2001
From: Eugenio Lacuesta <1731933+elacuesta@users.noreply.github.com>
Date: Thu, 24 Jun 2021 10:42:33 -0300
Subject: [PATCH 048/108] Remove support for py2, py34, py35 (#116)
---
.github/workflows/tests.yml | 20 ++++----------------
.gitignore | 4 +++-
README.rst | 2 +-
setup.py | 10 ++++------
tests/requirements.txt | 5 -----
tox.ini | 11 +++++++----
6 files changed, 19 insertions(+), 33 deletions(-)
delete mode 100644 tests/requirements.txt
diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml
index 817d824..799f52f 100644
--- a/.github/workflows/tests.yml
+++ b/.github/workflows/tests.yml
@@ -6,19 +6,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
- include:
- - python-version: 2.7
- env:
- TOXENV: py
- - python-version: 3.5
- env:
- TOXENV: py
- - python-version: 3.6
- env:
- TOXENV: py
- - python-version: 3.7
- env:
- TOXENV: py
+ python-version: [3.6, 3.7, 3.8, 3.9]
steps:
- uses: actions/checkout@v2
@@ -29,10 +17,10 @@ jobs:
python-version: ${{ matrix.python-version }}
- name: Run tests
- env: ${{ matrix.env }}
run: |
+ pip install -U pip
pip install -U tox
- tox
+ tox -e py
- name: Upload coverage report
- run: bash <(curl -s https://codecov.io/bash)
\ No newline at end of file
+ run: bash <(curl -s https://codecov.io/bash)
diff --git a/.gitignore b/.gitignore
index b0ab86a..c276bd1 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,4 +5,6 @@
/dist
/docs/_build
/.coverage
-.idea
\ No newline at end of file
+.idea
+htmlcov/
+coverage.xml
diff --git a/README.rst b/README.rst
index c6d387f..dfeedae 100644
--- a/README.rst
+++ b/README.rst
@@ -33,7 +33,7 @@ extracted as a stand-alone project.
Quick facts:
* Free software: BSD licensed
-* Compatible with Python 2.7 and 3.4+
+* Compatible with Python 3.6+
* Latest documentation `on Read the Docs `_
* Source, issues and pull requests `on GitHub
`_
diff --git a/setup.py b/setup.py
index 3782534..bddda2e 100644
--- a/setup.py
+++ b/setup.py
@@ -31,18 +31,16 @@
url="https://github.com/scrapy/cssselect",
license="BSD",
packages=["cssselect"],
- python_requires=">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*",
+ python_requires=">=3.6",
classifiers=[
"Development Status :: 4 - Beta",
"Intended Audience :: Developers",
"License :: OSI Approved :: BSD License",
- "Programming Language :: Python :: 2",
- "Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3",
- "Programming Language :: Python :: 3.4",
- "Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
+ "Programming Language :: Python :: 3.8",
+ "Programming Language :: Python :: 3.9",
],
- **extra_kwargs
+ **extra_kwargs,
)
diff --git a/tests/requirements.txt b/tests/requirements.txt
deleted file mode 100644
index 000d5f2..0000000
--- a/tests/requirements.txt
+++ /dev/null
@@ -1,5 +0,0 @@
-codecov
-lxml;python_version!="3.4"
-lxml<=4.3.5;python_version=="3.4"
-pytest >=4.6, <4.7 # 4.7 drops support for Python 2.7 and 3.4
-pytest-cov
\ No newline at end of file
diff --git a/tox.ini b/tox.ini
index 8cbafdf..a9d39b8 100644
--- a/tox.ini
+++ b/tox.ini
@@ -2,11 +2,14 @@
envlist = black,flake8,security,py
[testenv]
-basepython = python3
-deps=
- -r tests/requirements.txt
+deps =
+ lxml>=4.4
+ pytest-cov>=2.8
+ pytest>=5.4
commands =
- py.test --cov-report term --cov=cssselect
+ pytest --cov=cssselect \
+ --cov-report=term-missing --cov-report=html --cov-report=xml \
+ --verbose {posargs: cssselect tests}
[testenv:black]
deps =
From 7bc326df9ceda7073c75f1cb636183daf38694cb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adri=C3=A1n=20Chaves?=
Date: Wed, 30 Jun 2021 14:05:18 +0200
Subject: [PATCH 049/108] Simplify the README.rst file (#103)
---
README.rst | 24 +++++++++++-------------
1 file changed, 11 insertions(+), 13 deletions(-)
diff --git a/README.rst b/README.rst
index dfeedae..9708616 100644
--- a/README.rst
+++ b/README.rst
@@ -18,24 +18,22 @@ cssselect: CSS Selectors for Python
:target: https://codecov.io/github/scrapy/cssselect?branch=master
:alt: Coverage report
-*cssselect* parses `CSS3 Selectors`_ and translate them to `XPath 1.0`_
-expressions. Such expressions can be used in lxml_ or another XPath engine
-to find the matching elements in an XML or HTML document.
+**cssselect** is a BSD-licensed Python library to parse `CSS3 selectors`_ and
+translate them to `XPath 1.0`_ expressions.
-This module used to live inside of lxml as ``lxml.cssselect`` before it was
-extracted as a stand-alone project.
-
-.. _CSS3 Selectors: https://www.w3.org/TR/css3-selectors/
-.. _XPath 1.0: https://www.w3.org/TR/xpath/
-.. _lxml: http://lxml.de/
+`XPath 1.0`_ expressions can be used in lxml_ or another XPath engine to find
+the matching elements in an XML or HTML document.
+Find the cssselect online documentation at https://cssselect.readthedocs.io.
Quick facts:
-* Free software: BSD licensed
-* Compatible with Python 3.6+
-* Latest documentation `on Read the Docs `_
* Source, issues and pull requests `on GitHub
`_
-* Releases `on PyPI `_
+* Releases `on PyPI `_
* Install with ``pip install cssselect``
+
+
+.. _CSS3 selectors: https://www.w3.org/TR/selectors-3/
+.. _XPath 1.0: https://www.w3.org/TR/xpath/all/
+.. _lxml: https://lxml.de/
From 577ca9c1c8f0286b7f34d5bee8192eed6219b677 Mon Sep 17 00:00:00 2001
From: Eugenio Lacuesta
Date: Wed, 7 Jul 2021 07:58:03 -0300
Subject: [PATCH 050/108] Add pylint to tox's envlist
---
tox.ini | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tox.ini b/tox.ini
index a9d39b8..f260626 100644
--- a/tox.ini
+++ b/tox.ini
@@ -1,5 +1,5 @@
[tox]
-envlist = black,flake8,security,py
+envlist = black,flake8,pylint,security,py
[testenv]
deps =
From 599cbb50866ba4ea4211426a1d30de32e48df5a5 Mon Sep 17 00:00:00 2001
From: Julius Kibunjia
Date: Wed, 14 Jul 2021 11:19:34 +0300
Subject: [PATCH 051/108] Add matches-any pseudo-class: ':is()' (#109)
---
cssselect/parser.py | 51 +++++++++++++++++++++++++++++++++++++++++
cssselect/xpath.py | 13 +++++++++--
tests/test_cssselect.py | 19 +++++++++++++++
3 files changed, 81 insertions(+), 2 deletions(-)
diff --git a/cssselect/parser.py b/cssselect/parser.py
index 7125030..5494bd4 100644
--- a/cssselect/parser.py
+++ b/cssselect/parser.py
@@ -250,6 +250,30 @@ def specificity(self):
return a1 + a2, b1 + b2, c1 + c2
+class Matching(object):
+ """
+ Represents selector:is(selector_list)
+ """
+ def __init__(self, selector, selector_list):
+ self.selector = selector
+ self.selector_list = selector_list
+
+ def __repr__(self):
+ return '%s[%r:is(%s)]' % (
+ self.__class__.__name__, self.selector, ", ".join(
+ map(repr, self.selector_list)))
+
+ def canonical(self):
+ selector_arguments = []
+ for s in self.selector_list:
+ selarg = s.canonical()
+ selector_arguments.append(selarg.lstrip('*'))
+ return '%s:is(%s)' % (self.selector.canonical(),
+ ", ".join(map(str, selector_arguments)))
+
+ def specificity(self):
+ return max([x.specificity() for x in self.selector_list])
+
class Attrib(object):
"""
Represents selector[namespace|attrib operator value]
@@ -432,6 +456,7 @@ def parse_selector_group(stream):
else:
break
+
def parse_selector(stream):
result, pseudo_element = parse_simple_selector(stream)
while 1:
@@ -538,6 +563,9 @@ def parse_simple_selector(stream, inside_negation=False):
if next != ('DELIM', ')'):
raise SelectorSyntaxError("Expected ')', got %s" % (next,))
result = Negation(result, argument)
+ elif ident.lower() in ('matches', 'is'):
+ selectors = parse_simple_selector_arguments(stream)
+ result = Matching(result, selectors)
else:
result = Function(result, ident, parse_arguments(stream))
else:
@@ -564,6 +592,29 @@ def parse_arguments(stream):
"Expected an argument, got %s" % (next,))
+def parse_simple_selector_arguments(stream):
+ arguments = []
+ while 1:
+ result, pseudo_element = parse_simple_selector(stream, True)
+ if pseudo_element:
+ raise SelectorSyntaxError(
+ 'Got pseudo-element ::%s inside function'
+ % (pseudo_element, ))
+ stream.skip_whitespace()
+ next = stream.next()
+ if next in (('EOF', None), ('DELIM', ',')):
+ stream.next()
+ stream.skip_whitespace()
+ arguments.append(result)
+ elif next == ('DELIM', ')'):
+ arguments.append(result)
+ break
+ else:
+ raise SelectorSyntaxError(
+ "Expected an argument, got %s" % (next,))
+ return arguments
+
+
def parse_attrib(selector, stream):
stream.skip_whitespace()
attrib = stream.next_ident_or_star()
diff --git a/cssselect/xpath.py b/cssselect/xpath.py
index a8722bb..db44d42 100644
--- a/cssselect/xpath.py
+++ b/cssselect/xpath.py
@@ -54,9 +54,9 @@ def __str__(self):
def __repr__(self):
return '%s[%s]' % (self.__class__.__name__, self)
- def add_condition(self, condition):
+ def add_condition(self, condition, conjuction='and'):
if self.condition:
- self.condition = '(%s) and (%s)' % (self.condition, condition)
+ self.condition = '(%s) %s (%s)' % (self.condition, conjuction, condition)
else:
self.condition = condition
return self
@@ -272,6 +272,15 @@ def xpath_negation(self, negation):
else:
return xpath.add_condition('0')
+ def xpath_matching(self, matching):
+ xpath = self.xpath(matching.selector)
+ exprs = [self.xpath(selector) for selector in matching.selector_list]
+ for e in exprs:
+ e.add_name_test()
+ if e.condition:
+ xpath.add_condition(e.condition, 'or')
+ return xpath
+
def xpath_function(self, function):
"""Translate a functional pseudo-class."""
method = 'xpath_%s_function' % function.name.replace('-', '_')
diff --git a/tests/test_cssselect.py b/tests/test_cssselect.py
index d6969f2..bd37875 100644
--- a/tests/test_cssselect.py
+++ b/tests/test_cssselect.py
@@ -145,6 +145,10 @@ def parse_many(first, *others):
'Hash[Element[div]#foobar]']
assert parse_many('div:not(div.foo)') == [
'Negation[Element[div]:not(Class[Element[div].foo])]']
+ assert parse_many('div:is(.foo, #bar)') == [
+ 'Matching[Element[div]:is(Class[Element[*].foo], Hash[Element[*]#bar])]']
+ assert parse_many(':is(:hover, :visited)') == [
+ 'Matching[Element[*]:is(Pseudo[Element[*]:hover], Pseudo[Element[*]:visited])]']
assert parse_many('td ~ th') == [
'CombinedSelector[Element[td] ~ Element[th]]']
assert parse_many(':scope > foo') == [
@@ -266,6 +270,9 @@ def specificity(css):
assert specificity(':not(:empty)') == (0, 1, 0)
assert specificity(':not(#foo)') == (1, 0, 0)
+ assert specificity(':is(.foo, #bar)') == (1, 0, 0)
+ assert specificity(':is(:hover, :visited)') == (0, 1, 0)
+
assert specificity('foo:empty') == (0, 1, 1)
assert specificity('foo:before') == (0, 0, 2)
assert specificity('foo::before') == (0, 0, 2)
@@ -300,6 +307,8 @@ def css2css(css, res=None):
css2css(':not(*[foo])', ':not([foo])')
css2css(':not(:empty)')
css2css(':not(#foo)')
+ css2css(':is(#bar, .foo)')
+ css2css(':is(:focused, :visited)')
css2css('foo:empty')
css2css('foo::before')
css2css('foo:empty::before')
@@ -373,6 +382,10 @@ def get_error(css):
"Got pseudo-element ::before inside :not() at 12")
assert get_error(':not(:not(a))') == (
"Got nested :not()")
+ assert get_error(':is(:before)') == (
+ "Got pseudo-element ::before inside function")
+ assert get_error(':is(a b)') == (
+ "Expected an argument, got ")
assert get_error(':scope > div :scope header') == (
'Got immediate child pseudo-element ":scope" not at the start of a selector'
)
@@ -863,6 +876,12 @@ def pcss(main, *selectors, **kwargs):
assert pcss('ol :Not(li[class])') == [
'first-li', 'second-li', 'li-div',
'fifth-li', 'sixth-li', 'seventh-li']
+ assert pcss(':is(#first-li, #second-li)') == [
+ 'first-li', 'second-li']
+ assert pcss('a:is(#name-anchor, #tag-anchor)') == [
+ 'name-anchor', 'tag-anchor']
+ assert pcss(':is(.c)') == [
+ 'first-ol', 'third-li', 'fourth-li']
assert pcss('ol.a.b.c > li.c:nth-child(3)') == ['third-li']
# Invalid characters in XPath element names, should not crash
From b06a7fcb4da29b150abd4bc7d642de2aa1d34db1 Mon Sep 17 00:00:00 2001
From: Pascal Corpet
Date: Wed, 21 Jul 2021 23:32:06 +0200
Subject: [PATCH 052/108] Update to pylint 2.9.5
---
docs/conf.py | 4 ++--
setup.py | 6 ++++--
tox.ini | 2 +-
3 files changed, 7 insertions(+), 5 deletions(-)
diff --git a/docs/conf.py b/docs/conf.py
index aa897ef..62b5202 100644
--- a/docs/conf.py
+++ b/docs/conf.py
@@ -50,8 +50,8 @@
# built documents.
#
# The full version, including alpha/beta/rc tags.
-init_py = open(os.path.join(os.path.dirname(__file__),
- '..', 'cssselect', '__init__.py')).read()
+with open(os.path.join(os.path.dirname(__file__), '..', 'cssselect', '__init__.py')) as init_file:
+ init_py = init_file.read()
release = re.search("VERSION = '([^']+)'", init_py).group(1)
# The short X.Y version.
version = release.rstrip('dev')
diff --git a/setup.py b/setup.py
index bddda2e..3a0bea0 100644
--- a/setup.py
+++ b/setup.py
@@ -14,8 +14,10 @@
ROOT = os.path.dirname(__file__)
-README = open(os.path.join(ROOT, "README.rst")).read()
-INIT_PY = open(os.path.join(ROOT, "cssselect", "__init__.py")).read()
+with open(os.path.join(ROOT, "README.rst")) as readme_file:
+ README = readme_file.read()
+with open(os.path.join(ROOT, "cssselect", "__init__.py")) as init_file:
+ INIT_PY = init_file.read()
VERSION = re.search("VERSION = '([^']+)'", INIT_PY).group(1)
diff --git a/tox.ini b/tox.ini
index f260626..372ecb9 100644
--- a/tox.ini
+++ b/tox.ini
@@ -26,7 +26,7 @@ commands =
[testenv:pylint]
deps =
{[testenv]deps}
- pylint==2.8.3
+ pylint==2.9.5
commands =
pylint {posargs: cssselect setup.py tests docs}
From 9edc6c3f5cf558bd99fa9e584c6832fabe24b942 Mon Sep 17 00:00:00 2001
From: Pascal Corpet
Date: Mon, 26 Jul 2021 15:55:37 +0200
Subject: [PATCH 053/108] Apply black formatting (#122)
---
cssselect/__init__.py | 11 +-
cssselect/parser.py | 407 ++++++------
cssselect/xpath.py | 327 +++++-----
docs/conf.py | 2 +-
pyproject.toml | 1 -
setup.py | 2 +-
tests/test_cssselect.py | 1295 +++++++++++++++++++--------------------
7 files changed, 1028 insertions(+), 1017 deletions(-)
diff --git a/cssselect/__init__.py b/cssselect/__init__.py
index b41cef9..2e4f824 100644
--- a/cssselect/__init__.py
+++ b/cssselect/__init__.py
@@ -13,10 +13,15 @@
"""
-from cssselect.parser import (parse, Selector, FunctionalPseudoElement,
- SelectorError, SelectorSyntaxError)
+from cssselect.parser import (
+ parse,
+ Selector,
+ FunctionalPseudoElement,
+ SelectorError,
+ SelectorSyntaxError,
+)
from cssselect.xpath import GenericTranslator, HTMLTranslator, ExpressionError
-VERSION = '1.1.0'
+VERSION = "1.1.0"
__version__ = VERSION
diff --git a/cssselect/parser.py b/cssselect/parser.py
index 5494bd4..a27ece5 100644
--- a/cssselect/parser.py
+++ b/cssselect/parser.py
@@ -27,7 +27,7 @@
def ascii_lower(string):
"""Lower-case, but only in the ASCII range."""
- return string.encode('utf8').lower().decode('utf8')
+ return string.encode("utf8").lower().decode("utf8")
class SelectorError(Exception):
@@ -39,12 +39,14 @@ class SelectorError(Exception):
"""
+
class SelectorSyntaxError(SelectorError, SyntaxError):
"""Parsing a selector that does not match the grammar."""
#### Parsed objects
+
class Selector(object):
"""
Represents a parsed selector.
@@ -55,10 +57,10 @@ class Selector(object):
or unsupported pseudo-elements.
"""
+
def __init__(self, tree, pseudo_element=None):
self.parsed_tree = tree
- if pseudo_element is not None and not isinstance(
- pseudo_element, FunctionalPseudoElement):
+ if pseudo_element is not None and not isinstance(pseudo_element, FunctionalPseudoElement):
pseudo_element = ascii_lower(pseudo_element)
#: A :class:`FunctionalPseudoElement`,
#: or the identifier for the pseudo-element as a string,
@@ -86,24 +88,22 @@ def __repr__(self):
if isinstance(self.pseudo_element, FunctionalPseudoElement):
pseudo_element = repr(self.pseudo_element)
elif self.pseudo_element:
- pseudo_element = '::%s' % self.pseudo_element
+ pseudo_element = "::%s" % self.pseudo_element
else:
- pseudo_element = ''
- return '%s[%r%s]' % (
- self.__class__.__name__, self.parsed_tree, pseudo_element)
+ pseudo_element = ""
+ return "%s[%r%s]" % (self.__class__.__name__, self.parsed_tree, pseudo_element)
def canonical(self):
- """Return a CSS representation for this selector (a string)
- """
+ """Return a CSS representation for this selector (a string)"""
if isinstance(self.pseudo_element, FunctionalPseudoElement):
- pseudo_element = '::%s' % self.pseudo_element.canonical()
+ pseudo_element = "::%s" % self.pseudo_element.canonical()
elif self.pseudo_element:
- pseudo_element = '::%s' % self.pseudo_element
+ pseudo_element = "::%s" % self.pseudo_element
else:
- pseudo_element = ''
- res = '%s%s' % (self.parsed_tree.canonical(), pseudo_element)
+ pseudo_element = ""
+ res = "%s%s" % (self.parsed_tree.canonical(), pseudo_element)
if len(res) > 1:
- res = res.lstrip('*')
+ res = res.lstrip("*")
return res
def specificity(self):
@@ -122,16 +122,16 @@ class Class(object):
"""
Represents selector.class_name
"""
+
def __init__(self, selector, class_name):
self.selector = selector
self.class_name = class_name
def __repr__(self):
- return '%s[%r.%s]' % (
- self.__class__.__name__, self.selector, self.class_name)
+ return "%s[%r.%s]" % (self.__class__.__name__, self.selector, self.class_name)
def canonical(self):
- return '%s.%s' % (self.selector.canonical(), self.class_name)
+ return "%s.%s" % (self.selector.canonical(), self.class_name)
def specificity(self):
a, b, c = self.selector.specificity()
@@ -156,21 +156,24 @@ class FunctionalPseudoElement(object):
Use at your own risks.
"""
+
def __init__(self, name, arguments):
self.name = ascii_lower(name)
self.arguments = arguments
def __repr__(self):
- return '%s[::%s(%r)]' % (
- self.__class__.__name__, self.name,
- [token.value for token in self.arguments])
+ return "%s[::%s(%r)]" % (
+ self.__class__.__name__,
+ self.name,
+ [token.value for token in self.arguments],
+ )
def argument_types(self):
return [token.type for token in self.arguments]
def canonical(self):
- args = ''.join(token.css() for token in self.arguments)
- return '%s(%s)' % (self.name, args)
+ args = "".join(token.css() for token in self.arguments)
+ return "%s(%s)" % (self.name, args)
def specificity(self):
a, b, c = self.selector.specificity()
@@ -182,22 +185,26 @@ class Function(object):
"""
Represents selector:name(expr)
"""
+
def __init__(self, selector, name, arguments):
self.selector = selector
self.name = ascii_lower(name)
self.arguments = arguments
def __repr__(self):
- return '%s[%r:%s(%r)]' % (
- self.__class__.__name__, self.selector, self.name,
- [token.value for token in self.arguments])
+ return "%s[%r:%s(%r)]" % (
+ self.__class__.__name__,
+ self.selector,
+ self.name,
+ [token.value for token in self.arguments],
+ )
def argument_types(self):
return [token.type for token in self.arguments]
def canonical(self):
- args = ''.join(token.css() for token in self.arguments)
- return '%s:%s(%s)' % (self.selector.canonical(), self.name, args)
+ args = "".join(token.css() for token in self.arguments)
+ return "%s:%s(%s)" % (self.selector.canonical(), self.name, args)
def specificity(self):
a, b, c = self.selector.specificity()
@@ -209,16 +216,16 @@ class Pseudo(object):
"""
Represents selector:ident
"""
+
def __init__(self, selector, ident):
self.selector = selector
self.ident = ascii_lower(ident)
def __repr__(self):
- return '%s[%r:%s]' % (
- self.__class__.__name__, self.selector, self.ident)
+ return "%s[%r:%s]" % (self.__class__.__name__, self.selector, self.ident)
def canonical(self):
- return '%s:%s' % (self.selector.canonical(), self.ident)
+ return "%s:%s" % (self.selector.canonical(), self.ident)
def specificity(self):
a, b, c = self.selector.specificity()
@@ -230,19 +237,19 @@ class Negation(object):
"""
Represents selector:not(subselector)
"""
+
def __init__(self, selector, subselector):
self.selector = selector
self.subselector = subselector
def __repr__(self):
- return '%s[%r:not(%r)]' % (
- self.__class__.__name__, self.selector, self.subselector)
+ return "%s[%r:not(%r)]" % (self.__class__.__name__, self.selector, self.subselector)
def canonical(self):
subsel = self.subselector.canonical()
if len(subsel) > 1:
- subsel = subsel.lstrip('*')
- return '%s:not(%s)' % (self.selector.canonical(), subsel)
+ subsel = subsel.lstrip("*")
+ return "%s:not(%s)" % (self.selector.canonical(), subsel)
def specificity(self):
a1, b1, c1 = self.selector.specificity()
@@ -254,30 +261,34 @@ class Matching(object):
"""
Represents selector:is(selector_list)
"""
+
def __init__(self, selector, selector_list):
self.selector = selector
self.selector_list = selector_list
def __repr__(self):
- return '%s[%r:is(%s)]' % (
- self.__class__.__name__, self.selector, ", ".join(
- map(repr, self.selector_list)))
+ return "%s[%r:is(%s)]" % (
+ self.__class__.__name__,
+ self.selector,
+ ", ".join(map(repr, self.selector_list)),
+ )
def canonical(self):
selector_arguments = []
for s in self.selector_list:
selarg = s.canonical()
- selector_arguments.append(selarg.lstrip('*'))
- return '%s:is(%s)' % (self.selector.canonical(),
- ", ".join(map(str, selector_arguments)))
+ selector_arguments.append(selarg.lstrip("*"))
+ return "%s:is(%s)" % (self.selector.canonical(), ", ".join(map(str, selector_arguments)))
def specificity(self):
return max([x.specificity() for x in self.selector_list])
+
class Attrib(object):
"""
Represents selector[namespace|attrib operator value]
"""
+
def __init__(self, selector, namespace, attrib, operator, value):
self.selector = selector
self.namespace = namespace
@@ -287,29 +298,32 @@ def __init__(self, selector, namespace, attrib, operator, value):
def __repr__(self):
if self.namespace:
- attrib = '%s|%s' % (self.namespace, self.attrib)
+ attrib = "%s|%s" % (self.namespace, self.attrib)
else:
attrib = self.attrib
- if self.operator == 'exists':
- return '%s[%r[%s]]' % (
- self.__class__.__name__, self.selector, attrib)
+ if self.operator == "exists":
+ return "%s[%r[%s]]" % (self.__class__.__name__, self.selector, attrib)
else:
- return '%s[%r[%s %s %r]]' % (
- self.__class__.__name__, self.selector, attrib,
- self.operator, self.value.value)
+ return "%s[%r[%s %s %r]]" % (
+ self.__class__.__name__,
+ self.selector,
+ attrib,
+ self.operator,
+ self.value.value,
+ )
def canonical(self):
if self.namespace:
- attrib = '%s|%s' % (self.namespace, self.attrib)
+ attrib = "%s|%s" % (self.namespace, self.attrib)
else:
attrib = self.attrib
- if self.operator == 'exists':
+ if self.operator == "exists":
op = attrib
else:
- op = '%s%s%s' % (attrib, self.operator, self.value.css())
+ op = "%s%s%s" % (attrib, self.operator, self.value.css())
- return '%s[%s]' % (self.selector.canonical(), op)
+ return "%s[%s]" % (self.selector.canonical(), op)
def specificity(self):
a, b, c = self.selector.specificity()
@@ -324,17 +338,18 @@ class Element(object):
`None` is for the universal selector '*'
"""
+
def __init__(self, namespace=None, element=None):
self.namespace = namespace
self.element = element
def __repr__(self):
- return '%s[%s]' % (self.__class__.__name__, self.canonical())
+ return "%s[%s]" % (self.__class__.__name__, self.canonical())
def canonical(self):
- element = self.element or '*'
+ element = self.element or "*"
if self.namespace:
- element = '%s|%s' % (self.namespace, element)
+ element = "%s|%s" % (self.namespace, element)
return element
def specificity(self):
@@ -348,16 +363,16 @@ class Hash(object):
"""
Represents selector#id
"""
+
def __init__(self, selector, id):
self.selector = selector
self.id = id
def __repr__(self):
- return '%s[%r#%s]' % (
- self.__class__.__name__, self.selector, self.id)
+ return "%s[%r#%s]" % (self.__class__.__name__, self.selector, self.id)
def canonical(self):
- return '%s#%s' % (self.selector.canonical(), self.id)
+ return "%s#%s" % (self.selector.canonical(), self.id)
def specificity(self):
a, b, c = self.selector.specificity()
@@ -373,19 +388,17 @@ def __init__(self, selector, combinator, subselector):
self.subselector = subselector
def __repr__(self):
- if self.combinator == ' ':
- comb = ''
+ if self.combinator == " ":
+ comb = ""
else:
comb = self.combinator
- return '%s[%r %s %r]' % (
- self.__class__.__name__, self.selector, comb, self.subselector)
+ return "%s[%r %s %r]" % (self.__class__.__name__, self.selector, comb, self.subselector)
def canonical(self):
subsel = self.subselector.canonical()
if len(subsel) > 1:
- subsel = subsel.lstrip('*')
- return '%s %s %s' % (
- self.selector.canonical(), self.combinator, subsel)
+ subsel = subsel.lstrip("*")
+ return "%s %s %s" % (self.selector.canonical(), self.combinator, subsel)
def specificity(self):
a1, b1, c1 = self.selector.specificity()
@@ -396,14 +409,13 @@ def specificity(self):
#### Parser
# foo
-_el_re = re.compile(r'^[ \t\r\n\f]*([a-zA-Z]+)[ \t\r\n\f]*$')
+_el_re = re.compile(r"^[ \t\r\n\f]*([a-zA-Z]+)[ \t\r\n\f]*$")
# foo#bar or #bar
-_id_re = re.compile(r'^[ \t\r\n\f]*([a-zA-Z]*)#([a-zA-Z0-9_-]+)[ \t\r\n\f]*$')
+_id_re = re.compile(r"^[ \t\r\n\f]*([a-zA-Z]*)#([a-zA-Z0-9_-]+)[ \t\r\n\f]*$")
# foo.bar or .bar
-_class_re = re.compile(
- r'^[ \t\r\n\f]*([a-zA-Z]*)\.([a-zA-Z][a-zA-Z0-9_-]*)[ \t\r\n\f]*$')
+_class_re = re.compile(r"^[ \t\r\n\f]*([a-zA-Z]*)\.([a-zA-Z][a-zA-Z0-9_-]*)[ \t\r\n\f]*$")
def parse(css):
@@ -427,16 +439,16 @@ def parse(css):
return [Selector(Element(element=match.group(1)))]
match = _id_re.match(css)
if match is not None:
- return [Selector(Hash(Element(element=match.group(1) or None),
- match.group(2)))]
+ return [Selector(Hash(Element(element=match.group(1) or None), match.group(2)))]
match = _class_re.match(css)
if match is not None:
- return [Selector(Class(Element(element=match.group(1) or None),
- match.group(2)))]
+ return [Selector(Class(Element(element=match.group(1) or None), match.group(2)))]
stream = TokenStream(tokenize(css))
stream.source = css
return list(parse_selector_group(stream))
+
+
# except SelectorSyntaxError:
# e = sys.exc_info()[1]
# message = "%s at %s -> %r" % (
@@ -450,7 +462,7 @@ def parse_selector_group(stream):
stream.skip_whitespace()
while 1:
yield Selector(*parse_selector(stream))
- if stream.peek() == ('DELIM', ','):
+ if stream.peek() == ("DELIM", ","):
stream.next()
stream.skip_whitespace()
else:
@@ -462,20 +474,20 @@ def parse_selector(stream):
while 1:
stream.skip_whitespace()
peek = stream.peek()
- if peek in (('EOF', None), ('DELIM', ',')):
+ if peek in (("EOF", None), ("DELIM", ",")):
break
if pseudo_element:
raise SelectorSyntaxError(
- 'Got pseudo-element ::%s not at the end of a selector'
- % pseudo_element)
- if peek.is_delim('+', '>', '~'):
+ "Got pseudo-element ::%s not at the end of a selector" % pseudo_element
+ )
+ if peek.is_delim("+", ">", "~"):
# A combinator
combinator = stream.next().value
stream.skip_whitespace()
else:
# By exclusion, the last parse_simple_selector() ended
# at peek == ' '
- combinator = ' '
+ combinator = " "
next_selector, pseudo_element = parse_simple_selector(stream)
result = CombinedSelector(result, combinator, next_selector)
return result, pseudo_element
@@ -485,13 +497,13 @@ def parse_simple_selector(stream, inside_negation=False):
stream.skip_whitespace()
selector_start = len(stream.used)
peek = stream.peek()
- if peek.type == 'IDENT' or peek == ('DELIM', '*'):
- if peek.type == 'IDENT':
+ if peek.type == "IDENT" or peek == ("DELIM", "*"):
+ if peek.type == "IDENT":
namespace = stream.next().value
else:
stream.next()
namespace = None
- if stream.peek() == ('DELIM', '|'):
+ if stream.peek() == ("DELIM", "|"):
stream.next()
element = stream.next_ident_or_star()
else:
@@ -503,77 +515,82 @@ def parse_simple_selector(stream, inside_negation=False):
pseudo_element = None
while 1:
peek = stream.peek()
- if peek.type in ('S', 'EOF') or peek.is_delim(',', '+', '>', '~') or (
- inside_negation and peek == ('DELIM', ')')):
+ if (
+ peek.type in ("S", "EOF")
+ or peek.is_delim(",", "+", ">", "~")
+ or (inside_negation and peek == ("DELIM", ")"))
+ ):
break
if pseudo_element:
raise SelectorSyntaxError(
- 'Got pseudo-element ::%s not at the end of a selector'
- % pseudo_element)
- if peek.type == 'HASH':
+ "Got pseudo-element ::%s not at the end of a selector" % pseudo_element
+ )
+ if peek.type == "HASH":
result = Hash(result, stream.next().value)
- elif peek == ('DELIM', '.'):
+ elif peek == ("DELIM", "."):
stream.next()
result = Class(result, stream.next_ident())
- elif peek == ('DELIM', '|'):
+ elif peek == ("DELIM", "|"):
stream.next()
result = Element(None, stream.next_ident())
- elif peek == ('DELIM', '['):
+ elif peek == ("DELIM", "["):
stream.next()
result = parse_attrib(result, stream)
- elif peek == ('DELIM', ':'):
+ elif peek == ("DELIM", ":"):
stream.next()
- if stream.peek() == ('DELIM', ':'):
+ if stream.peek() == ("DELIM", ":"):
stream.next()
pseudo_element = stream.next_ident()
- if stream.peek() == ('DELIM', '('):
+ if stream.peek() == ("DELIM", "("):
stream.next()
pseudo_element = FunctionalPseudoElement(
- pseudo_element, parse_arguments(stream))
+ pseudo_element, parse_arguments(stream)
+ )
continue
ident = stream.next_ident()
- if ident.lower() in ('first-line', 'first-letter',
- 'before', 'after'):
+ if ident.lower() in ("first-line", "first-letter", "before", "after"):
# Special case: CSS 2.1 pseudo-elements can have a single ':'
# Any new pseudo-element must have two.
pseudo_element = _unicode(ident)
continue
- if stream.peek() != ('DELIM', '('):
+ if stream.peek() != ("DELIM", "("):
result = Pseudo(result, ident)
- if result.__repr__() == 'Pseudo[Element[*]:scope]':
- if not (len(stream.used) == 2 or
- (len(stream.used) == 3
- and stream.used[0].type == 'S')):
+ if result.__repr__() == "Pseudo[Element[*]:scope]":
+ if not (
+ len(stream.used) == 2
+ or (len(stream.used) == 3 and stream.used[0].type == "S")
+ ):
raise SelectorSyntaxError(
'Got immediate child pseudo-element ":scope" '
- 'not at the start of a selector')
+ "not at the start of a selector"
+ )
continue
stream.next()
stream.skip_whitespace()
- if ident.lower() == 'not':
+ if ident.lower() == "not":
if inside_negation:
- raise SelectorSyntaxError('Got nested :not()')
+ raise SelectorSyntaxError("Got nested :not()")
argument, argument_pseudo_element = parse_simple_selector(
- stream, inside_negation=True)
+ stream, inside_negation=True
+ )
next = stream.next()
if argument_pseudo_element:
raise SelectorSyntaxError(
- 'Got pseudo-element ::%s inside :not() at %s'
- % (argument_pseudo_element, next.pos))
- if next != ('DELIM', ')'):
+ "Got pseudo-element ::%s inside :not() at %s"
+ % (argument_pseudo_element, next.pos)
+ )
+ if next != ("DELIM", ")"):
raise SelectorSyntaxError("Expected ')', got %s" % (next,))
result = Negation(result, argument)
- elif ident.lower() in ('matches', 'is'):
+ elif ident.lower() in ("matches", "is"):
selectors = parse_simple_selector_arguments(stream)
result = Matching(result, selectors)
else:
result = Function(result, ident, parse_arguments(stream))
else:
- raise SelectorSyntaxError(
- "Expected selector, got %s" % (peek,))
+ raise SelectorSyntaxError("Expected selector, got %s" % (peek,))
if len(stream.used) == selector_start:
- raise SelectorSyntaxError(
- "Expected selector, got %s" % (stream.peek(),))
+ raise SelectorSyntaxError("Expected selector, got %s" % (stream.peek(),))
return result, pseudo_element
@@ -582,14 +599,12 @@ def parse_arguments(stream):
while 1:
stream.skip_whitespace()
next = stream.next()
- if next.type in ('IDENT', 'STRING', 'NUMBER') or next in [
- ('DELIM', '+'), ('DELIM', '-')]:
+ if next.type in ("IDENT", "STRING", "NUMBER") or next in [("DELIM", "+"), ("DELIM", "-")]:
arguments.append(next)
- elif next == ('DELIM', ')'):
+ elif next == ("DELIM", ")"):
return arguments
else:
- raise SelectorSyntaxError(
- "Expected an argument, got %s" % (next,))
+ raise SelectorSyntaxError("Expected an argument, got %s" % (next,))
def parse_simple_selector_arguments(stream):
@@ -598,35 +613,33 @@ def parse_simple_selector_arguments(stream):
result, pseudo_element = parse_simple_selector(stream, True)
if pseudo_element:
raise SelectorSyntaxError(
- 'Got pseudo-element ::%s inside function'
- % (pseudo_element, ))
+ "Got pseudo-element ::%s inside function" % (pseudo_element,)
+ )
stream.skip_whitespace()
next = stream.next()
- if next in (('EOF', None), ('DELIM', ',')):
+ if next in (("EOF", None), ("DELIM", ",")):
stream.next()
stream.skip_whitespace()
arguments.append(result)
- elif next == ('DELIM', ')'):
+ elif next == ("DELIM", ")"):
arguments.append(result)
break
else:
- raise SelectorSyntaxError(
- "Expected an argument, got %s" % (next,))
+ raise SelectorSyntaxError("Expected an argument, got %s" % (next,))
return arguments
def parse_attrib(selector, stream):
stream.skip_whitespace()
attrib = stream.next_ident_or_star()
- if attrib is None and stream.peek() != ('DELIM', '|'):
- raise SelectorSyntaxError(
- "Expected '|', got %s" % (stream.peek(),))
- if stream.peek() == ('DELIM', '|'):
+ if attrib is None and stream.peek() != ("DELIM", "|"):
+ raise SelectorSyntaxError("Expected '|', got %s" % (stream.peek(),))
+ if stream.peek() == ("DELIM", "|"):
stream.next()
- if stream.peek() == ('DELIM', '='):
+ if stream.peek() == ("DELIM", "="):
namespace = None
stream.next()
- op = '|='
+ op = "|="
else:
namespace = attrib
attrib = stream.next_ident()
@@ -636,27 +649,23 @@ def parse_attrib(selector, stream):
if op is None:
stream.skip_whitespace()
next = stream.next()
- if next == ('DELIM', ']'):
- return Attrib(selector, namespace, attrib, 'exists', None)
- elif next == ('DELIM', '='):
- op = '='
- elif next.is_delim('^', '$', '*', '~', '|', '!') and (
- stream.peek() == ('DELIM', '=')):
- op = next.value + '='
+ if next == ("DELIM", "]"):
+ return Attrib(selector, namespace, attrib, "exists", None)
+ elif next == ("DELIM", "="):
+ op = "="
+ elif next.is_delim("^", "$", "*", "~", "|", "!") and (stream.peek() == ("DELIM", "=")):
+ op = next.value + "="
stream.next()
else:
- raise SelectorSyntaxError(
- "Operator expected, got %s" % (next,))
+ raise SelectorSyntaxError("Operator expected, got %s" % (next,))
stream.skip_whitespace()
value = stream.next()
- if value.type not in ('IDENT', 'STRING'):
- raise SelectorSyntaxError(
- "Expected string or ident, got %s" % (value,))
+ if value.type not in ("IDENT", "STRING"):
+ raise SelectorSyntaxError("Expected string or ident, got %s" % (value,))
stream.skip_whitespace()
next = stream.next()
- if next != ('DELIM', ']'):
- raise SelectorSyntaxError(
- "Expected ']', got %s" % (next,))
+ if next != ("DELIM", "]"):
+ raise SelectorSyntaxError("Expected ']', got %s" % (next,))
return Attrib(selector, namespace, attrib, op, value)
@@ -669,23 +678,23 @@ def parse_series(tokens):
"""
for token in tokens:
- if token.type == 'STRING':
- raise ValueError('String tokens not allowed in series.')
- s = ''.join(token.value for token in tokens).strip()
- if s == 'odd':
+ if token.type == "STRING":
+ raise ValueError("String tokens not allowed in series.")
+ s = "".join(token.value for token in tokens).strip()
+ if s == "odd":
return 2, 1
- elif s == 'even':
+ elif s == "even":
return 2, 0
- elif s == 'n':
+ elif s == "n":
return 1, 0
- if 'n' not in s:
+ if "n" not in s:
# Just b
return 0, int(s)
- a, b = s.split('n', 1)
+ a, b = s.split("n", 1)
if not a:
a = 1
- elif a == '-' or a == '+':
- a = int(a+'1')
+ elif a == "-" or a == "+":
+ a = int(a + "1")
else:
a = int(a)
if not b:
@@ -697,6 +706,7 @@ def parse_series(tokens):
#### Token objects
+
class Token(tuple):
def __new__(cls, type_, value, pos):
obj = tuple.__new__(cls, (type_, value))
@@ -707,13 +717,13 @@ def __repr__(self):
return "<%s '%s' at %i>" % (self.type, self.value, self.pos)
def is_delim(self, *values):
- return self.type == 'DELIM' and self.value in values
+ return self.type == "DELIM" and self.value in values
type = property(operator.itemgetter(0))
value = property(operator.itemgetter(1))
def css(self):
- if self.type == 'STRING':
+ if self.type == "STRING":
return repr(self.value)
else:
return self.value
@@ -721,41 +731,44 @@ def css(self):
class EOFToken(Token):
def __new__(cls, pos):
- return Token.__new__(cls, 'EOF', None, pos)
+ return Token.__new__(cls, "EOF", None, pos)
def __repr__(self):
- return '<%s at %i>' % (self.type, self.pos)
+ return "<%s at %i>" % (self.type, self.pos)
#### Tokenizer
class TokenMacros:
- unicode_escape = r'\\([0-9a-f]{1,6})(?:\r\n|[ \n\r\t\f])?'
- escape = unicode_escape + r'|\\[^\n\r\f0-9a-f]'
- string_escape = r'\\(?:\n|\r\n|\r|\f)|' + escape
- nonascii = r'[^\0-\177]'
- nmchar = '[_a-z0-9-]|%s|%s' % (escape, nonascii)
- nmstart = '[_a-z]|%s|%s' % (escape, nonascii)
+ unicode_escape = r"\\([0-9a-f]{1,6})(?:\r\n|[ \n\r\t\f])?"
+ escape = unicode_escape + r"|\\[^\n\r\f0-9a-f]"
+ string_escape = r"\\(?:\n|\r\n|\r|\f)|" + escape
+ nonascii = r"[^\0-\177]"
+ nmchar = "[_a-z0-9-]|%s|%s" % (escape, nonascii)
+ nmstart = "[_a-z]|%s|%s" % (escape, nonascii)
+
def _compile(pattern):
return re.compile(pattern % vars(TokenMacros), re.IGNORECASE).match
-_match_whitespace = _compile(r'[ \t\r\n\f]+')
-_match_number = _compile(r'[+-]?(?:[0-9]*\.[0-9]+|[0-9]+)')
-_match_hash = _compile('#(?:%(nmchar)s)+')
-_match_ident = _compile('-?(?:%(nmstart)s)(?:%(nmchar)s)*')
+
+_match_whitespace = _compile(r"[ \t\r\n\f]+")
+_match_number = _compile(r"[+-]?(?:[0-9]*\.[0-9]+|[0-9]+)")
+_match_hash = _compile("#(?:%(nmchar)s)+")
+_match_ident = _compile("-?(?:%(nmstart)s)(?:%(nmchar)s)*")
_match_string_by_quote = {
"'": _compile(r"([^\n\r\f\\']|%(string_escape)s)*"),
'"': _compile(r'([^\n\r\f\\"]|%(string_escape)s)*'),
}
-_sub_simple_escape = re.compile(r'\\(.)').sub
+_sub_simple_escape = re.compile(r"\\(.)").sub
_sub_unicode_escape = re.compile(TokenMacros.unicode_escape, re.I).sub
-_sub_newline_escape =re.compile(r'\\(?:\n|\r\n|\r|\f)').sub
+_sub_newline_escape = re.compile(r"\\(?:\n|\r\n|\r|\f)").sub
# Same as r'\1', but faster on CPython
-_replace_simple = operator.methodcaller('group', 1)
+_replace_simple = operator.methodcaller("group", 1)
+
def _replace_unicode(match):
codepoint = int(match.group(1), 16)
@@ -776,59 +789,62 @@ def tokenize(s):
while pos < len_s:
match = _match_whitespace(s, pos=pos)
if match:
- yield Token('S', ' ', pos)
+ yield Token("S", " ", pos)
pos = match.end()
continue
match = _match_ident(s, pos=pos)
if match:
- value = _sub_simple_escape(_replace_simple,
- _sub_unicode_escape(_replace_unicode, match.group()))
- yield Token('IDENT', value, pos)
+ value = _sub_simple_escape(
+ _replace_simple, _sub_unicode_escape(_replace_unicode, match.group())
+ )
+ yield Token("IDENT", value, pos)
pos = match.end()
continue
match = _match_hash(s, pos=pos)
if match:
- value = _sub_simple_escape(_replace_simple,
- _sub_unicode_escape(_replace_unicode, match.group()[1:]))
- yield Token('HASH', value, pos)
+ value = _sub_simple_escape(
+ _replace_simple, _sub_unicode_escape(_replace_unicode, match.group()[1:])
+ )
+ yield Token("HASH", value, pos)
pos = match.end()
continue
quote = s[pos]
if quote in _match_string_by_quote:
match = _match_string_by_quote[quote](s, pos=pos + 1)
- assert match, 'Should have found at least an empty match'
+ assert match, "Should have found at least an empty match"
end_pos = match.end()
if end_pos == len_s:
- raise SelectorSyntaxError('Unclosed string at %s' % pos)
+ raise SelectorSyntaxError("Unclosed string at %s" % pos)
if s[end_pos] != quote:
- raise SelectorSyntaxError('Invalid string at %s' % pos)
- value = _sub_simple_escape(_replace_simple,
- _sub_unicode_escape(_replace_unicode,
- _sub_newline_escape('', match.group())))
- yield Token('STRING', value, pos)
+ raise SelectorSyntaxError("Invalid string at %s" % pos)
+ value = _sub_simple_escape(
+ _replace_simple,
+ _sub_unicode_escape(_replace_unicode, _sub_newline_escape("", match.group())),
+ )
+ yield Token("STRING", value, pos)
pos = end_pos + 1
continue
match = _match_number(s, pos=pos)
if match:
value = match.group()
- yield Token('NUMBER', value, pos)
+ yield Token("NUMBER", value, pos)
pos = match.end()
continue
pos2 = pos + 2
- if s[pos:pos2] == '/*':
- pos = s.find('*/', pos2)
+ if s[pos:pos2] == "/*":
+ pos = s.find("*/", pos2)
if pos == -1:
pos = len_s
else:
pos += 2
continue
- yield Token('DELIM', s[pos], pos)
+ yield Token("DELIM", s[pos], pos)
pos += 1
assert pos == len_s
@@ -866,21 +882,20 @@ def peek(self):
def next_ident(self):
next = self.next()
- if next.type != 'IDENT':
- raise SelectorSyntaxError('Expected ident, got %s' % (next,))
+ if next.type != "IDENT":
+ raise SelectorSyntaxError("Expected ident, got %s" % (next,))
return next.value
def next_ident_or_star(self):
next = self.next()
- if next.type == 'IDENT':
+ if next.type == "IDENT":
return next.value
- elif next == ('DELIM', '*'):
+ elif next == ("DELIM", "*"):
return None
else:
- raise SelectorSyntaxError(
- "Expected ident or '*', got %s" % (next,))
+ raise SelectorSyntaxError("Expected ident or '*', got %s" % (next,))
def skip_whitespace(self):
peek = self.peek()
- if peek.type == 'S':
+ if peek.type == "S":
self.next()
diff --git a/cssselect/xpath.py b/cssselect/xpath.py
index db44d42..f80e629 100644
--- a/cssselect/xpath.py
+++ b/cssselect/xpath.py
@@ -28,7 +28,7 @@
def _unicode_safe_getattr(obj, name, default=None):
# getattr() with a non-ASCII name fails on Python 2.x
- name = name.encode('ascii', 'replace').decode('ascii')
+ name = name.encode("ascii", "replace").decode("ascii")
return getattr(obj, name, default)
@@ -38,48 +38,47 @@ class ExpressionError(SelectorError, RuntimeError):
#### XPath Helpers
-class XPathExpr(object):
- def __init__(self, path='', element='*', condition='', star_prefix=False):
+class XPathExpr(object):
+ def __init__(self, path="", element="*", condition="", star_prefix=False):
self.path = path
self.element = element
self.condition = condition
def __str__(self):
- path = _unicode(self.path) + _unicode(self.element)
+ path = _unicode(self.path) + _unicode(self.element)
if self.condition:
- path += '[%s]' % self.condition
+ path += "[%s]" % self.condition
return path
def __repr__(self):
- return '%s[%s]' % (self.__class__.__name__, self)
+ return "%s[%s]" % (self.__class__.__name__, self)
- def add_condition(self, condition, conjuction='and'):
+ def add_condition(self, condition, conjuction="and"):
if self.condition:
- self.condition = '(%s) %s (%s)' % (self.condition, conjuction, condition)
+ self.condition = "(%s) %s (%s)" % (self.condition, conjuction, condition)
else:
self.condition = condition
return self
def add_name_test(self):
- if self.element == '*':
+ if self.element == "*":
# We weren't doing a test anyway
return
- self.add_condition(
- "name() = %s" % GenericTranslator.xpath_literal(self.element))
- self.element = '*'
+ self.add_condition("name() = %s" % GenericTranslator.xpath_literal(self.element))
+ self.element = "*"
def add_star_prefix(self):
"""
Append '*/' to the path to keep the context constrained
to a single parent.
"""
- self.path += '*/'
+ self.path += "*/"
def join(self, combiner, other):
path = _unicode(self) + combiner
# Any "star prefix" is redundant when joining.
- if other.path != '*/':
+ if other.path != "*/":
path += other.path
self.path = path
self.element = other.element
@@ -92,14 +91,15 @@ def join(self, combiner, other):
# The spec is actually more permissive than that, but don’t bother.
# This is just for the fast path.
# http://www.w3.org/TR/REC-xml/#NT-NameStartChar
-is_safe_name = re.compile('^[a-zA-Z_][a-zA-Z0-9_.-]*$').match
+is_safe_name = re.compile("^[a-zA-Z_][a-zA-Z0-9_.-]*$").match
# Test that the string is not empty and does not contain whitespace
-is_non_whitespace = re.compile(r'^[^ \t\r\n\f]+$').match
+is_non_whitespace = re.compile(r"^[^ \t\r\n\f]+$").match
#### Translation
+
class GenericTranslator(object):
"""
Translator for "generic" XML documents.
@@ -122,30 +122,30 @@ class GenericTranslator(object):
####
combinator_mapping = {
- ' ': 'descendant',
- '>': 'child',
- '+': 'direct_adjacent',
- '~': 'indirect_adjacent',
+ " ": "descendant",
+ ">": "child",
+ "+": "direct_adjacent",
+ "~": "indirect_adjacent",
}
attribute_operator_mapping = {
- 'exists': 'exists',
- '=': 'equals',
- '~=': 'includes',
- '|=': 'dashmatch',
- '^=': 'prefixmatch',
- '$=': 'suffixmatch',
- '*=': 'substringmatch',
- '!=': 'different', # XXX Not in Level 3 but meh
+ "exists": "exists",
+ "=": "equals",
+ "~=": "includes",
+ "|=": "dashmatch",
+ "^=": "prefixmatch",
+ "$=": "suffixmatch",
+ "*=": "substringmatch",
+ "!=": "different", # XXX Not in Level 3 but meh
}
#: The attribute used for ID selectors depends on the document language:
#: http://www.w3.org/TR/selectors/#id-selectors
- id_attribute = 'id'
+ id_attribute = "id"
#: The attribute used for ``:lang()`` depends on the document language:
#: http://www.w3.org/TR/selectors/#lang-pseudo
- lang_attribute = 'xml:lang'
+ lang_attribute = "xml:lang"
#: The case sensitivity of document language element names,
#: attribute names, and attribute values in selectors depends
@@ -168,7 +168,7 @@ class GenericTranslator(object):
# class used to represent and xpath expression
xpathexpr_cls = XPathExpr
- def css_to_xpath(self, css, prefix='descendant-or-self::'):
+ def css_to_xpath(self, css, prefix="descendant-or-self::"):
"""Translate a *group of selectors* to XPath.
Pseudo-elements are not supported here since XPath only knows
@@ -187,12 +187,14 @@ def css_to_xpath(self, css, prefix='descendant-or-self::'):
The equivalent XPath 1.0 expression as an Unicode string.
"""
- return ' | '.join(self.selector_to_xpath(selector, prefix,
- translate_pseudo_elements=True)
- for selector in parse(css))
+ return " | ".join(
+ self.selector_to_xpath(selector, prefix, translate_pseudo_elements=True)
+ for selector in parse(css)
+ )
- def selector_to_xpath(self, selector, prefix='descendant-or-self::',
- translate_pseudo_elements=False):
+ def selector_to_xpath(
+ self, selector, prefix="descendant-or-self::", translate_pseudo_elements=False
+ ):
"""Translate a parsed selector to XPath.
@@ -213,14 +215,14 @@ def selector_to_xpath(self, selector, prefix='descendant-or-self::',
The equivalent XPath 1.0 expression as an Unicode string.
"""
- tree = getattr(selector, 'parsed_tree', None)
+ tree = getattr(selector, "parsed_tree", None)
if not tree:
- raise TypeError('Expected a parsed selector, got %r' % (selector,))
+ raise TypeError("Expected a parsed selector, got %r" % (selector,))
xpath = self.xpath(tree)
assert isinstance(xpath, self.xpathexpr_cls) # help debug a missing 'return'
if translate_pseudo_elements and selector.pseudo_element:
xpath = self.xpath_pseudo_element(xpath, selector.pseudo_element)
- return (prefix or '') + _unicode(xpath)
+ return (prefix or "") + _unicode(xpath)
def xpath_pseudo_element(self, xpath, pseudo_element):
"""Translate a pseudo-element.
@@ -229,7 +231,7 @@ def xpath_pseudo_element(self, xpath, pseudo_element):
but can be overridden by sub-classes.
"""
- raise ExpressionError('Pseudo-elements are not supported.')
+ raise ExpressionError("Pseudo-elements are not supported.")
@staticmethod
def xpath_literal(s):
@@ -239,38 +241,39 @@ def xpath_literal(s):
elif '"' not in s:
s = '"%s"' % s
else:
- s = "concat(%s)" % ','.join([
- (("'" in part) and '"%s"' or "'%s'") % part
- for part in split_at_single_quotes(s) if part
- ])
+ s = "concat(%s)" % ",".join(
+ [
+ (("'" in part) and '"%s"' or "'%s'") % part
+ for part in split_at_single_quotes(s)
+ if part
+ ]
+ )
return s
def xpath(self, parsed_selector):
"""Translate any parsed selector object."""
type_name = type(parsed_selector).__name__
- method = getattr(self, 'xpath_%s' % type_name.lower(), None)
+ method = getattr(self, "xpath_%s" % type_name.lower(), None)
if method is None:
- raise ExpressionError('%s is not supported.' % type_name)
+ raise ExpressionError("%s is not supported." % type_name)
return method(parsed_selector)
-
# Dispatched by parsed object type
def xpath_combinedselector(self, combined):
"""Translate a combined selector."""
combinator = self.combinator_mapping[combined.combinator]
- method = getattr(self, 'xpath_%s_combinator' % combinator)
- return method(self.xpath(combined.selector),
- self.xpath(combined.subselector))
+ method = getattr(self, "xpath_%s_combinator" % combinator)
+ return method(self.xpath(combined.selector), self.xpath(combined.subselector))
def xpath_negation(self, negation):
xpath = self.xpath(negation.selector)
sub_xpath = self.xpath(negation.subselector)
sub_xpath.add_name_test()
if sub_xpath.condition:
- return xpath.add_condition('not(%s)' % sub_xpath.condition)
+ return xpath.add_condition("not(%s)" % sub_xpath.condition)
else:
- return xpath.add_condition('0')
+ return xpath.add_condition("0")
def xpath_matching(self, matching):
xpath = self.xpath(matching.selector)
@@ -278,45 +281,42 @@ def xpath_matching(self, matching):
for e in exprs:
e.add_name_test()
if e.condition:
- xpath.add_condition(e.condition, 'or')
+ xpath.add_condition(e.condition, "or")
return xpath
def xpath_function(self, function):
"""Translate a functional pseudo-class."""
- method = 'xpath_%s_function' % function.name.replace('-', '_')
+ method = "xpath_%s_function" % function.name.replace("-", "_")
method = _unicode_safe_getattr(self, method, None)
if not method:
- raise ExpressionError(
- "The pseudo-class :%s() is unknown" % function.name)
+ raise ExpressionError("The pseudo-class :%s() is unknown" % function.name)
return method(self.xpath(function.selector), function)
def xpath_pseudo(self, pseudo):
"""Translate a pseudo-class."""
- method = 'xpath_%s_pseudo' % pseudo.ident.replace('-', '_')
+ method = "xpath_%s_pseudo" % pseudo.ident.replace("-", "_")
method = _unicode_safe_getattr(self, method, None)
if not method:
# TODO: better error message for pseudo-elements?
- raise ExpressionError(
- "The pseudo-class :%s is unknown" % pseudo.ident)
+ raise ExpressionError("The pseudo-class :%s is unknown" % pseudo.ident)
return method(self.xpath(pseudo.selector))
-
def xpath_attrib(self, selector):
"""Translate an attribute selector."""
operator = self.attribute_operator_mapping[selector.operator]
- method = getattr(self, 'xpath_attrib_%s' % operator)
+ method = getattr(self, "xpath_attrib_%s" % operator)
if self.lower_case_attribute_names:
name = selector.attrib.lower()
else:
name = selector.attrib
safe = is_safe_name(name)
if selector.namespace:
- name = '%s:%s' % (selector.namespace, name)
+ name = "%s:%s" % (selector.namespace, name)
safe = safe and is_safe_name(selector.namespace)
if safe:
- attrib = '@' + name
+ attrib = "@" + name
else:
- attrib = 'attribute::*[name() = %s]' % self.xpath_literal(name)
+ attrib = "attribute::*[name() = %s]" % self.xpath_literal(name)
if selector.value is None:
value = None
elif self.lower_case_attribute_values:
@@ -329,19 +329,18 @@ def xpath_class(self, class_selector):
"""Translate a class selector."""
# .foo is defined as [class~=foo] in the spec.
xpath = self.xpath(class_selector.selector)
- return self.xpath_attrib_includes(
- xpath, '@class', class_selector.class_name)
+ return self.xpath_attrib_includes(xpath, "@class", class_selector.class_name)
def xpath_hash(self, id_selector):
"""Translate an ID selector."""
xpath = self.xpath(id_selector.selector)
- return self.xpath_attrib_equals(xpath, '@id', id_selector.id)
+ return self.xpath_attrib_equals(xpath, "@id", id_selector.id)
def xpath_element(self, selector):
"""Translate a type or universal selector."""
element = selector.element
if not element:
- element = '*'
+ element = "*"
safe = True
else:
safe = is_safe_name(element)
@@ -350,39 +349,36 @@ def xpath_element(self, selector):
if selector.namespace:
# Namespace prefixes are case-sensitive.
# http://www.w3.org/TR/css3-namespace/#prefixes
- element = '%s:%s' % (selector.namespace, element)
+ element = "%s:%s" % (selector.namespace, element)
safe = safe and is_safe_name(selector.namespace)
xpath = self.xpathexpr_cls(element=element)
if not safe:
xpath.add_name_test()
return xpath
-
# CombinedSelector: dispatch by combinator
def xpath_descendant_combinator(self, left, right):
"""right is a child, grand-child or further descendant of left"""
- return left.join('/descendant-or-self::*/', right)
+ return left.join("/descendant-or-self::*/", right)
def xpath_child_combinator(self, left, right):
"""right is an immediate child of left"""
- return left.join('/', right)
+ return left.join("/", right)
def xpath_direct_adjacent_combinator(self, left, right):
"""right is a sibling immediately after left"""
- xpath = left.join('/following-sibling::', right)
+ xpath = left.join("/following-sibling::", right)
xpath.add_name_test()
- return xpath.add_condition('position() = 1')
+ return xpath.add_condition("position() = 1")
def xpath_indirect_adjacent_combinator(self, left, right):
"""right is a sibling after left, immediately or not"""
- return left.join('/following-sibling::', right)
-
+ return left.join("/following-sibling::", right)
# Function: dispatch by function/pseudo-class name
- def xpath_nth_child_function(self, xpath, function, last=False,
- add_name_test=True):
+ def xpath_nth_child_function(self, xpath, function, last=False, add_name_test=True):
try:
a, b = parse_series(function.arguments)
except ValueError:
@@ -436,35 +432,35 @@ def xpath_nth_child_function(self, xpath, function, last=False,
# for a == 1, nth-*(an+b) means n+b-1 siblings before/after,
# and since n ∈ {0, 1, 2, ...}, if b-1<=0,
# there is always an "n" matching any number of siblings (maybe none)
- if a == 1 and b_min_1 <=0:
+ if a == 1 and b_min_1 <= 0:
return xpath
# early-exit condition 2:
# ~~~~~~~~~~~~~~~~~~~~~~~
# an+b-1 siblings with a<0 and (b-1)<0 is not possible
if a < 0 and b_min_1 < 0:
- return xpath.add_condition('0')
+ return xpath.add_condition("0")
# `add_name_test` boolean is inverted and somewhat counter-intuitive:
#
# nth_of_type() calls nth_child(add_name_test=False)
if add_name_test:
- nodetest = '*'
+ nodetest = "*"
else:
- nodetest = '%s' % xpath.element
+ nodetest = "%s" % xpath.element
# count siblings before or after the element
if not last:
- siblings_count = 'count(preceding-sibling::%s)' % nodetest
+ siblings_count = "count(preceding-sibling::%s)" % nodetest
else:
- siblings_count = 'count(following-sibling::%s)' % nodetest
+ siblings_count = "count(following-sibling::%s)" % nodetest
# special case of fixed position: nth-*(0n+b)
# if a == 0:
# ~~~~~~~~~~
# count(***-sibling::***) = b-1
if a == 0:
- return xpath.add_condition('%s = %s' % (siblings_count, b_min_1))
+ return xpath.add_condition("%s = %s" % (siblings_count, b_min_1))
expressions = []
@@ -473,12 +469,12 @@ def xpath_nth_child_function(self, xpath, function, last=False,
# so if a>0, and (b-1)<=0, an "n" exists to satisfy this,
# therefore, the predicate is only interesting if (b-1)>0
if b_min_1 > 0:
- expressions.append('%s >= %s' % (siblings_count, b_min_1))
+ expressions.append("%s >= %s" % (siblings_count, b_min_1))
else:
# if a<0, and (b-1)<0, no "n" satisfies this,
# this is tested above as an early exist condition
# otherwise,
- expressions.append('%s <= %s' % (siblings_count, b_min_1))
+ expressions.append("%s <= %s" % (siblings_count, b_min_1))
# operations modulo 1 or -1 are simpler, one only needs to verify:
#
@@ -501,56 +497,48 @@ def xpath_nth_child_function(self, xpath, function, last=False,
b_neg = (-b_min_1) % abs(a)
if b_neg != 0:
- b_neg = '+%s' % b_neg
- left = '(%s %s)' % (left, b_neg)
+ b_neg = "+%s" % b_neg
+ left = "(%s %s)" % (left, b_neg)
- expressions.append('%s mod %s = 0' % (left, a))
+ expressions.append("%s mod %s = 0" % (left, a))
if len(expressions) > 1:
- template = '(%s)'
+ template = "(%s)"
else:
- template = '%s'
- xpath.add_condition(' and '.join(template % expression
- for expression in expressions))
+ template = "%s"
+ xpath.add_condition(" and ".join(template % expression for expression in expressions))
return xpath
def xpath_nth_last_child_function(self, xpath, function):
return self.xpath_nth_child_function(xpath, function, last=True)
def xpath_nth_of_type_function(self, xpath, function):
- if xpath.element == '*':
- raise ExpressionError(
- "*:nth-of-type() is not implemented")
- return self.xpath_nth_child_function(xpath, function,
- add_name_test=False)
+ if xpath.element == "*":
+ raise ExpressionError("*:nth-of-type() is not implemented")
+ return self.xpath_nth_child_function(xpath, function, add_name_test=False)
def xpath_nth_last_of_type_function(self, xpath, function):
- if xpath.element == '*':
- raise ExpressionError(
- "*:nth-of-type() is not implemented")
- return self.xpath_nth_child_function(xpath, function, last=True,
- add_name_test=False)
+ if xpath.element == "*":
+ raise ExpressionError("*:nth-of-type() is not implemented")
+ return self.xpath_nth_child_function(xpath, function, last=True, add_name_test=False)
def xpath_contains_function(self, xpath, function):
# Defined there, removed in later drafts:
# http://www.w3.org/TR/2001/CR-css3-selectors-20011113/#content-selectors
- if function.argument_types() not in (['STRING'], ['IDENT']):
+ if function.argument_types() not in (["STRING"], ["IDENT"]):
raise ExpressionError(
- "Expected a single string or ident for :contains(), got %r"
- % function.arguments)
+ "Expected a single string or ident for :contains(), got %r" % function.arguments
+ )
value = function.arguments[0].value
- return xpath.add_condition(
- 'contains(., %s)' % self.xpath_literal(value))
+ return xpath.add_condition("contains(., %s)" % self.xpath_literal(value))
def xpath_lang_function(self, xpath, function):
- if function.argument_types() not in (['STRING'], ['IDENT']):
+ if function.argument_types() not in (["STRING"], ["IDENT"]):
raise ExpressionError(
- "Expected a single string or ident for :lang(), got %r"
- % function.arguments)
+ "Expected a single string or ident for :lang(), got %r" % function.arguments
+ )
value = function.arguments[0].value
- return xpath.add_condition(
- "lang(%s)" % (self.xpath_literal(value)))
-
+ return xpath.add_condition("lang(%s)" % (self.xpath_literal(value)))
# Pseudo: dispatch by pseudo-class name
@@ -566,31 +554,28 @@ def xpath_scope_pseudo(self, xpath):
return xpath.add_condition("1")
def xpath_first_child_pseudo(self, xpath):
- return xpath.add_condition('count(preceding-sibling::*) = 0')
+ return xpath.add_condition("count(preceding-sibling::*) = 0")
def xpath_last_child_pseudo(self, xpath):
- return xpath.add_condition('count(following-sibling::*) = 0')
+ return xpath.add_condition("count(following-sibling::*) = 0")
def xpath_first_of_type_pseudo(self, xpath):
- if xpath.element == '*':
- raise ExpressionError(
- "*:first-of-type is not implemented")
- return xpath.add_condition('count(preceding-sibling::%s) = 0' % xpath.element)
+ if xpath.element == "*":
+ raise ExpressionError("*:first-of-type is not implemented")
+ return xpath.add_condition("count(preceding-sibling::%s) = 0" % xpath.element)
def xpath_last_of_type_pseudo(self, xpath):
- if xpath.element == '*':
- raise ExpressionError(
- "*:last-of-type is not implemented")
- return xpath.add_condition('count(following-sibling::%s) = 0' % xpath.element)
+ if xpath.element == "*":
+ raise ExpressionError("*:last-of-type is not implemented")
+ return xpath.add_condition("count(following-sibling::%s) = 0" % xpath.element)
def xpath_only_child_pseudo(self, xpath):
- return xpath.add_condition('count(parent::*/child::*) = 1')
+ return xpath.add_condition("count(parent::*/child::*) = 1")
def xpath_only_of_type_pseudo(self, xpath):
- if xpath.element == '*':
- raise ExpressionError(
- "*:only-of-type is not implemented")
- return xpath.add_condition('count(parent::*/child::%s) = 1' % xpath.element)
+ if xpath.element == "*":
+ raise ExpressionError("*:only-of-type is not implemented")
+ return xpath.add_condition("count(parent::*/child::%s) = 1" % xpath.element)
def xpath_empty_pseudo(self, xpath):
return xpath.add_condition("not(*) and not(string-length())")
@@ -617,61 +602,63 @@ def xpath_attrib_exists(self, xpath, name, value):
return xpath
def xpath_attrib_equals(self, xpath, name, value):
- xpath.add_condition('%s = %s' % (name, self.xpath_literal(value)))
+ xpath.add_condition("%s = %s" % (name, self.xpath_literal(value)))
return xpath
def xpath_attrib_different(self, xpath, name, value):
# FIXME: this seems like a weird hack...
if value:
- xpath.add_condition('not(%s) or %s != %s'
- % (name, name, self.xpath_literal(value)))
+ xpath.add_condition("not(%s) or %s != %s" % (name, name, self.xpath_literal(value)))
else:
- xpath.add_condition('%s != %s'
- % (name, self.xpath_literal(value)))
+ xpath.add_condition("%s != %s" % (name, self.xpath_literal(value)))
return xpath
def xpath_attrib_includes(self, xpath, name, value):
if is_non_whitespace(value):
xpath.add_condition(
"%s and contains(concat(' ', normalize-space(%s), ' '), %s)"
- % (name, name, self.xpath_literal(' '+value+' ')))
+ % (name, name, self.xpath_literal(" " + value + " "))
+ )
else:
- xpath.add_condition('0')
+ xpath.add_condition("0")
return xpath
def xpath_attrib_dashmatch(self, xpath, name, value):
# Weird, but true...
- xpath.add_condition('%s and (%s = %s or starts-with(%s, %s))' % (
- name,
- name, self.xpath_literal(value),
- name, self.xpath_literal(value + '-')))
+ xpath.add_condition(
+ "%s and (%s = %s or starts-with(%s, %s))"
+ % (name, name, self.xpath_literal(value), name, self.xpath_literal(value + "-"))
+ )
return xpath
def xpath_attrib_prefixmatch(self, xpath, name, value):
if value:
- xpath.add_condition('%s and starts-with(%s, %s)' % (
- name, name, self.xpath_literal(value)))
+ xpath.add_condition(
+ "%s and starts-with(%s, %s)" % (name, name, self.xpath_literal(value))
+ )
else:
- xpath.add_condition('0')
+ xpath.add_condition("0")
return xpath
def xpath_attrib_suffixmatch(self, xpath, name, value):
if value:
# Oddly there is a starts-with in XPath 1.0, but not ends-with
xpath.add_condition(
- '%s and substring(%s, string-length(%s)-%s) = %s'
- % (name, name, name, len(value)-1, self.xpath_literal(value)))
+ "%s and substring(%s, string-length(%s)-%s) = %s"
+ % (name, name, name, len(value) - 1, self.xpath_literal(value))
+ )
else:
- xpath.add_condition('0')
+ xpath.add_condition("0")
return xpath
def xpath_attrib_substringmatch(self, xpath, name, value):
if value:
# Attribute selectors are case sensitive
- xpath.add_condition('%s and contains(%s, %s)' % (
- name, name, self.xpath_literal(value)))
+ xpath.add_condition(
+ "%s and contains(%s, %s)" % (name, name, self.xpath_literal(value))
+ )
else:
- xpath.add_condition('0')
+ xpath.add_condition("0")
return xpath
@@ -692,7 +679,7 @@ class HTMLTranslator(GenericTranslator):
"""
- lang_attribute = 'lang'
+ lang_attribute = "lang"
def __init__(self, xhtml=False):
self.xhtml = xhtml # Might be useful for sub-classes?
@@ -706,33 +693,36 @@ def xpath_checked_pseudo(self, xpath):
return xpath.add_condition(
"(@selected and name(.) = 'option') or "
"(@checked "
- "and (name(.) = 'input' or name(.) = 'command')"
- "and (@type = 'checkbox' or @type = 'radio'))")
+ "and (name(.) = 'input' or name(.) = 'command')"
+ "and (@type = 'checkbox' or @type = 'radio'))"
+ )
def xpath_lang_function(self, xpath, function):
- if function.argument_types() not in (['STRING'], ['IDENT']):
+ if function.argument_types() not in (["STRING"], ["IDENT"]):
raise ExpressionError(
- "Expected a single string or ident for :lang(), got %r"
- % function.arguments)
+ "Expected a single string or ident for :lang(), got %r" % function.arguments
+ )
value = function.arguments[0].value
return xpath.add_condition(
"ancestor-or-self::*[@lang][1][starts-with(concat("
- # XPath 1.0 has no lower-case function...
- "translate(@%s, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', "
- "'abcdefghijklmnopqrstuvwxyz'), "
- "'-'), %s)]"
- % (self.lang_attribute, self.xpath_literal(value.lower() + '-')))
+ # XPath 1.0 has no lower-case function...
+ "translate(@%s, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', "
+ "'abcdefghijklmnopqrstuvwxyz'), "
+ "'-'), %s)]" % (self.lang_attribute, self.xpath_literal(value.lower() + "-"))
+ )
def xpath_link_pseudo(self, xpath):
- return xpath.add_condition("@href and "
- "(name(.) = 'a' or name(.) = 'link' or name(.) = 'area')")
+ return xpath.add_condition(
+ "@href and " "(name(.) = 'a' or name(.) = 'link' or name(.) = 'area')"
+ )
# Links are never visited, the implementation for :visited is the same
# as in GenericTranslator
def xpath_disabled_pseudo(self, xpath):
# http://www.w3.org/TR/html5/section-index.html#attributes-1
- return xpath.add_condition('''
+ return xpath.add_condition(
+ """
(
@disabled and
(
@@ -754,13 +744,15 @@ def xpath_disabled_pseudo(self, xpath):
)
and ancestor::fieldset[@disabled]
)
- ''')
+ """
+ )
# FIXME: in the second half, add "and is not a descendant of that
# fieldset element's first legend element child, if any."
def xpath_enabled_pseudo(self, xpath):
# http://www.w3.org/TR/html5/section-index.html#attributes-1
- return xpath.add_condition('''
+ return xpath.add_condition(
+ """
(
@href and (
name(.) = 'a' or
@@ -788,7 +780,8 @@ def xpath_enabled_pseudo(self, xpath):
@disabled or ancestor::optgroup[@disabled]
)
)
- ''')
+ """
+ )
# FIXME: ... or "li elements that are children of menu elements,
# and that have a child element that defines a command, if the first
# such element's Disabled State facet is false (not disabled)".
diff --git a/docs/conf.py b/docs/conf.py
index 62b5202..9dc2575 100644
--- a/docs/conf.py
+++ b/docs/conf.py
@@ -52,7 +52,7 @@
# The full version, including alpha/beta/rc tags.
with open(os.path.join(os.path.dirname(__file__), '..', 'cssselect', '__init__.py')) as init_file:
init_py = init_file.read()
-release = re.search("VERSION = '([^']+)'", init_py).group(1)
+release = re.search('VERSION = "([^"]+)"', init_py).group(1)
# The short X.Y version.
version = release.rstrip('dev')
diff --git a/pyproject.toml b/pyproject.toml
index b409f47..57a5583 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -1,3 +1,2 @@
[tool.black]
line-length = 99
-exclude = 'cssselect/|tests/'
diff --git a/setup.py b/setup.py
index 3a0bea0..f95721d 100644
--- a/setup.py
+++ b/setup.py
@@ -18,7 +18,7 @@
README = readme_file.read()
with open(os.path.join(ROOT, "cssselect", "__init__.py")) as init_file:
INIT_PY = init_file.read()
-VERSION = re.search("VERSION = '([^']+)'", INIT_PY).group(1)
+VERSION = re.search('VERSION = "([^"]+)"', INIT_PY).group(1)
setup(
diff --git a/tests/test_cssselect.py b/tests/test_cssselect.py
index bd37875..ba46d8a 100644
--- a/tests/test_cssselect.py
+++ b/tests/test_cssselect.py
@@ -21,17 +21,23 @@
import unittest
from lxml import etree, html
-from cssselect import (parse, GenericTranslator, HTMLTranslator,
- SelectorSyntaxError, ExpressionError)
-from cssselect.parser import (tokenize, parse_series, _unicode,
- FunctionalPseudoElement)
+from cssselect import (
+ parse,
+ GenericTranslator,
+ HTMLTranslator,
+ SelectorSyntaxError,
+ ExpressionError,
+)
+from cssselect.parser import tokenize, parse_series, _unicode, FunctionalPseudoElement
from cssselect.xpath import _unicode_safe_getattr, XPathExpr
if sys.version_info[0] < 3:
# Python 2
def u(text):
- return text.decode('utf8')
+ return text.decode("utf8")
+
+
else:
# Python 3
def u(text):
@@ -41,8 +47,8 @@ def u(text):
class TestCssselect(unittest.TestCase):
def test_tokenizer(self):
tokens = [
- _unicode(item) for item in tokenize(
- u(r'E\ é > f [a~="y\"x"]:nth(/* fu /]* */-3.7)'))]
+ _unicode(item) for item in tokenize(u(r'E\ é > f [a~="y\"x"]:nth(/* fu /]* */-3.7)'))
+ ]
assert tokens == [
u(""),
"",
@@ -69,8 +75,7 @@ def repr_parse(css):
selectors = parse(css)
for selector in selectors:
assert selector.pseudo_element is None
- return [repr(selector.parsed_tree).replace("(u'", "('")
- for selector in selectors]
+ return [repr(selector.parsed_tree).replace("(u'", "('") for selector in selectors]
def parse_many(first, *others):
result = repr_parse(first)
@@ -78,92 +83,91 @@ def parse_many(first, *others):
assert repr_parse(other) == result
return result
- assert parse_many('*') == ['Element[*]']
- assert parse_many('*|*') == ['Element[*]']
- assert parse_many('*|foo') == ['Element[foo]']
- assert parse_many('|foo') == ['Element[foo]']
- assert parse_many('foo|*') == ['Element[foo|*]']
- assert parse_many('foo|bar') == ['Element[foo|bar]']
+ assert parse_many("*") == ["Element[*]"]
+ assert parse_many("*|*") == ["Element[*]"]
+ assert parse_many("*|foo") == ["Element[foo]"]
+ assert parse_many("|foo") == ["Element[foo]"]
+ assert parse_many("foo|*") == ["Element[foo|*]"]
+ assert parse_many("foo|bar") == ["Element[foo|bar]"]
# This will never match, but it is valid:
- assert parse_many('#foo#bar') == ['Hash[Hash[Element[*]#foo]#bar]']
- assert parse_many(
- 'div>.foo',
- 'div> .foo',
- 'div >.foo',
- 'div > .foo',
- 'div \n> \t \t .foo', 'div\r>\n\n\n.foo', 'div\f>\f.foo'
- ) == ['CombinedSelector[Element[div] > Class[Element[*].foo]]']
- assert parse_many('td.foo,.bar',
- 'td.foo, .bar',
- 'td.foo\t\r\n\f ,\t\r\n\f .bar'
- ) == [
- 'Class[Element[td].foo]',
- 'Class[Element[*].bar]'
+ assert parse_many("#foo#bar") == ["Hash[Hash[Element[*]#foo]#bar]"]
+ assert (
+ parse_many(
+ "div>.foo",
+ "div> .foo",
+ "div >.foo",
+ "div > .foo",
+ "div \n> \t \t .foo",
+ "div\r>\n\n\n.foo",
+ "div\f>\f.foo",
+ )
+ == ["CombinedSelector[Element[div] > Class[Element[*].foo]]"]
+ )
+ assert parse_many("td.foo,.bar", "td.foo, .bar", "td.foo\t\r\n\f ,\t\r\n\f .bar") == [
+ "Class[Element[td].foo]",
+ "Class[Element[*].bar]",
+ ]
+ assert parse_many("div, td.foo, div.bar span") == [
+ "Element[div]",
+ "Class[Element[td].foo]",
+ "CombinedSelector[Class[Element[div].bar] Element[span]]",
+ ]
+ assert parse_many("div > p") == ["CombinedSelector[Element[div] > Element[p]]"]
+ assert parse_many("td:first") == ["Pseudo[Element[td]:first]"]
+ assert parse_many("td:first") == ["Pseudo[Element[td]:first]"]
+ assert parse_many("td :first") == [
+ "CombinedSelector[Element[td] Pseudo[Element[*]:first]]"
+ ]
+ assert parse_many("td :first") == [
+ "CombinedSelector[Element[td] Pseudo[Element[*]:first]]"
+ ]
+ assert parse_many("a[name]", "a[ name\t]") == ["Attrib[Element[a][name]]"]
+ assert parse_many("a [name]") == [
+ "CombinedSelector[Element[a] Attrib[Element[*][name]]]"
+ ]
+ assert parse_many('a[rel="include"]', "a[rel = include]") == [
+ "Attrib[Element[a][rel = 'include']]"
]
- assert parse_many('div, td.foo, div.bar span') == [
- 'Element[div]',
- 'Class[Element[td].foo]',
- 'CombinedSelector[Class[Element[div].bar] '
- ' Element[span]]']
- assert parse_many('div > p') == [
- 'CombinedSelector[Element[div] > Element[p]]']
- assert parse_many('td:first') == [
- 'Pseudo[Element[td]:first]']
- assert parse_many('td:first') == [
- 'Pseudo[Element[td]:first]']
- assert parse_many('td :first') == [
- 'CombinedSelector[Element[td] '
- ' Pseudo[Element[*]:first]]']
- assert parse_many('td :first') == [
- 'CombinedSelector[Element[td] '
- ' Pseudo[Element[*]:first]]']
- assert parse_many('a[name]', 'a[ name\t]') == [
- 'Attrib[Element[a][name]]']
- assert parse_many('a [name]') == [
- 'CombinedSelector[Element[a] Attrib[Element[*][name]]]']
- assert parse_many('a[rel="include"]', 'a[rel = include]') == [
- "Attrib[Element[a][rel = 'include']]"]
assert parse_many("a[hreflang |= 'en']", "a[hreflang|=en]") == [
- "Attrib[Element[a][hreflang |= 'en']]"]
- assert parse_many('div:nth-child(10)') == [
- "Function[Element[div]:nth-child(['10'])]"]
- assert parse_many(':nth-child(2n+2)') == [
- "Function[Element[*]:nth-child(['2', 'n', '+2'])]"]
- assert parse_many('div:nth-of-type(10)') == [
- "Function[Element[div]:nth-of-type(['10'])]"]
- assert parse_many('div div:nth-of-type(10) .aclass') == [
- 'CombinedSelector[CombinedSelector[Element[div] '
- "Function[Element[div]:nth-of-type(['10'])]] "
- ' Class[Element[*].aclass]]']
- assert parse_many('label:only') == [
- 'Pseudo[Element[label]:only]']
- assert parse_many('a:lang(fr)') == [
- "Function[Element[a]:lang(['fr'])]"]
- assert parse_many('div:contains("foo")') == [
- "Function[Element[div]:contains(['foo'])]"]
- assert parse_many('div#foobar') == [
- 'Hash[Element[div]#foobar]']
- assert parse_many('div:not(div.foo)') == [
- 'Negation[Element[div]:not(Class[Element[div].foo])]']
- assert parse_many('div:is(.foo, #bar)') == [
- 'Matching[Element[div]:is(Class[Element[*].foo], Hash[Element[*]#bar])]']
- assert parse_many(':is(:hover, :visited)') == [
- 'Matching[Element[*]:is(Pseudo[Element[*]:hover], Pseudo[Element[*]:visited])]']
- assert parse_many('td ~ th') == [
- 'CombinedSelector[Element[td] ~ Element[th]]']
- assert parse_many(':scope > foo') == [
- 'CombinedSelector[Pseudo[Element[*]:scope] > Element[foo]]'
+ "Attrib[Element[a][hreflang |= 'en']]"
+ ]
+ assert parse_many("div:nth-child(10)") == ["Function[Element[div]:nth-child(['10'])]"]
+ assert parse_many(":nth-child(2n+2)") == [
+ "Function[Element[*]:nth-child(['2', 'n', '+2'])]"
+ ]
+ assert parse_many("div:nth-of-type(10)") == ["Function[Element[div]:nth-of-type(['10'])]"]
+ assert parse_many("div div:nth-of-type(10) .aclass") == [
+ "CombinedSelector[CombinedSelector[Element[div] "
+ "Function[Element[div]:nth-of-type(['10'])]] "
+ " Class[Element[*].aclass]]"
+ ]
+ assert parse_many("label:only") == ["Pseudo[Element[label]:only]"]
+ assert parse_many("a:lang(fr)") == ["Function[Element[a]:lang(['fr'])]"]
+ assert parse_many('div:contains("foo")') == ["Function[Element[div]:contains(['foo'])]"]
+ assert parse_many("div#foobar") == ["Hash[Element[div]#foobar]"]
+ assert parse_many("div:not(div.foo)") == [
+ "Negation[Element[div]:not(Class[Element[div].foo])]"
+ ]
+ assert parse_many("div:is(.foo, #bar)") == [
+ "Matching[Element[div]:is(Class[Element[*].foo], Hash[Element[*]#bar])]"
+ ]
+ assert parse_many(":is(:hover, :visited)") == [
+ "Matching[Element[*]:is(Pseudo[Element[*]:hover], Pseudo[Element[*]:visited])]"
+ ]
+ assert parse_many("td ~ th") == ["CombinedSelector[Element[td] ~ Element[th]]"]
+ assert parse_many(":scope > foo") == [
+ "CombinedSelector[Pseudo[Element[*]:scope] > Element[foo]]"
]
- assert parse_many(' :scope > foo') == [
- 'CombinedSelector[Pseudo[Element[*]:scope] > Element[foo]]'
+ assert parse_many(" :scope > foo") == [
+ "CombinedSelector[Pseudo[Element[*]:scope] > Element[foo]]"
]
- assert parse_many(':scope > foo bar > div') == [
- 'CombinedSelector[CombinedSelector[CombinedSelector[Pseudo[Element[*]:scope] > '
- 'Element[foo]] Element[bar]] > Element[div]]'
+ assert parse_many(":scope > foo bar > div") == [
+ "CombinedSelector[CombinedSelector[CombinedSelector[Pseudo[Element[*]:scope] > "
+ "Element[foo]] Element[bar]] > Element[div]]"
]
- assert parse_many(':scope > #foo #bar') == [
- 'CombinedSelector[CombinedSelector[Pseudo[Element[*]:scope] > '
- 'Hash[Element[*]#foo]] Hash[Element[*]#bar]]'
+ assert parse_many(":scope > #foo #bar") == [
+ "CombinedSelector[CombinedSelector[Pseudo[Element[*]:scope] > "
+ "Hash[Element[*]#foo]] Hash[Element[*]#bar]]"
]
def test_pseudo_elements(self):
@@ -189,61 +193,66 @@ def test_pseudo_repr(css):
selector = result[0]
return selector.parsed_tree.__repr__()
- assert parse_one('foo') == ('Element[foo]', None)
- assert parse_one('*') == ('Element[*]', None)
- assert parse_one(':empty') == ('Pseudo[Element[*]:empty]', None)
- assert parse_one(':scope') == ('Pseudo[Element[*]:scope]', None)
+ assert parse_one("foo") == ("Element[foo]", None)
+ assert parse_one("*") == ("Element[*]", None)
+ assert parse_one(":empty") == ("Pseudo[Element[*]:empty]", None)
+ assert parse_one(":scope") == ("Pseudo[Element[*]:scope]", None)
# Special cases for CSS 2.1 pseudo-elements
- assert parse_one(':BEfore') == ('Element[*]', 'before')
- assert parse_one(':aftER') == ('Element[*]', 'after')
- assert parse_one(':First-Line') == ('Element[*]', 'first-line')
- assert parse_one(':First-Letter') == ('Element[*]', 'first-letter')
-
- assert parse_one('::befoRE') == ('Element[*]', 'before')
- assert parse_one('::AFter') == ('Element[*]', 'after')
- assert parse_one('::firsT-linE') == ('Element[*]', 'first-line')
- assert parse_one('::firsT-letteR') == ('Element[*]', 'first-letter')
-
- assert parse_one('::text-content') == ('Element[*]', 'text-content')
- assert parse_one('::attr(name)') == (
- "Element[*]", "FunctionalPseudoElement[::attr(['name'])]")
-
- assert parse_one('::Selection') == ('Element[*]', 'selection')
- assert parse_one('foo:after') == ('Element[foo]', 'after')
- assert parse_one('foo::selection') == ('Element[foo]', 'selection')
- assert parse_one('lorem#ipsum ~ a#b.c[href]:empty::selection') == (
- 'CombinedSelector[Hash[Element[lorem]#ipsum] ~ '
- 'Pseudo[Attrib[Class[Hash[Element[a]#b].c][href]]:empty]]',
- 'selection')
- assert parse_pseudo(':scope > div, foo bar') == [
- ('CombinedSelector[Pseudo[Element[*]:scope] > Element[div]]', None),
- ('CombinedSelector[Element[foo] Element[bar]]', None)
+ assert parse_one(":BEfore") == ("Element[*]", "before")
+ assert parse_one(":aftER") == ("Element[*]", "after")
+ assert parse_one(":First-Line") == ("Element[*]", "first-line")
+ assert parse_one(":First-Letter") == ("Element[*]", "first-letter")
+
+ assert parse_one("::befoRE") == ("Element[*]", "before")
+ assert parse_one("::AFter") == ("Element[*]", "after")
+ assert parse_one("::firsT-linE") == ("Element[*]", "first-line")
+ assert parse_one("::firsT-letteR") == ("Element[*]", "first-letter")
+
+ assert parse_one("::text-content") == ("Element[*]", "text-content")
+ assert parse_one("::attr(name)") == (
+ "Element[*]",
+ "FunctionalPseudoElement[::attr(['name'])]",
+ )
+
+ assert parse_one("::Selection") == ("Element[*]", "selection")
+ assert parse_one("foo:after") == ("Element[foo]", "after")
+ assert parse_one("foo::selection") == ("Element[foo]", "selection")
+ assert parse_one("lorem#ipsum ~ a#b.c[href]:empty::selection") == (
+ "CombinedSelector[Hash[Element[lorem]#ipsum] ~ "
+ "Pseudo[Attrib[Class[Hash[Element[a]#b].c][href]]:empty]]",
+ "selection",
+ )
+ assert parse_pseudo(":scope > div, foo bar") == [
+ ("CombinedSelector[Pseudo[Element[*]:scope] > Element[div]]", None),
+ ("CombinedSelector[Element[foo] Element[bar]]", None),
]
- assert parse_pseudo('foo:before, bar, baz:after') == [
- ('Element[foo]', 'before'), ('Element[bar]', None),
- ('Element[baz]', 'after')
+ assert parse_pseudo("foo:before, bar, baz:after") == [
+ ("Element[foo]", "before"),
+ ("Element[bar]", None),
+ ("Element[baz]", "after"),
]
# Special cases for CSS 2.1 pseudo-elements are ignored by default
- for pseudo in ('after', 'before', 'first-line', 'first-letter'):
- selector, = parse('e:%s' % pseudo)
+ for pseudo in ("after", "before", "first-line", "first-letter"):
+ (selector,) = parse("e:%s" % pseudo)
assert selector.pseudo_element == pseudo
- assert GenericTranslator().selector_to_xpath(selector, prefix='') == "e"
+ assert GenericTranslator().selector_to_xpath(selector, prefix="") == "e"
# Pseudo Elements are ignored by default, but if allowed they are not
# supported by GenericTranslator
tr = GenericTranslator()
- selector, = parse('e::foo')
- assert selector.pseudo_element == 'foo'
- assert tr.selector_to_xpath(selector, prefix='') == "e"
- self.assertRaises(ExpressionError, tr.selector_to_xpath, selector,
- translate_pseudo_elements=True)
+ (selector,) = parse("e::foo")
+ assert selector.pseudo_element == "foo"
+ assert tr.selector_to_xpath(selector, prefix="") == "e"
+ self.assertRaises(
+ ExpressionError, tr.selector_to_xpath, selector, translate_pseudo_elements=True
+ )
# Special test for the unicode symbols and ':scope' element if check
# Errors if use repr() instead of __repr__()
- assert test_pseudo_repr(u':fİrst-child') == u'Pseudo[Element[*]:fİrst-child]'
- assert test_pseudo_repr(':scope') == 'Pseudo[Element[*]:scope]'
+ assert test_pseudo_repr(u":fİrst-child") == u"Pseudo[Element[*]:fİrst-child]"
+ assert test_pseudo_repr(":scope") == "Pseudo[Element[*]:scope]"
def test_specificity(self):
def specificity(css):
@@ -251,35 +260,34 @@ def specificity(css):
assert len(selectors) == 1
return selectors[0].specificity()
- assert specificity('*') == (0, 0, 0)
- assert specificity(' foo') == (0, 0, 1)
- assert specificity(':empty ') == (0, 1, 0)
- assert specificity(':before') == (0, 0, 1)
- assert specificity('*:before') == (0, 0, 1)
- assert specificity(':nth-child(2)') == (0, 1, 0)
- assert specificity('.bar') == (0, 1, 0)
- assert specificity('[baz]') == (0, 1, 0)
+ assert specificity("*") == (0, 0, 0)
+ assert specificity(" foo") == (0, 0, 1)
+ assert specificity(":empty ") == (0, 1, 0)
+ assert specificity(":before") == (0, 0, 1)
+ assert specificity("*:before") == (0, 0, 1)
+ assert specificity(":nth-child(2)") == (0, 1, 0)
+ assert specificity(".bar") == (0, 1, 0)
+ assert specificity("[baz]") == (0, 1, 0)
assert specificity('[baz="4"]') == (0, 1, 0)
assert specificity('[baz^="4"]') == (0, 1, 0)
- assert specificity('#lipsum') == (1, 0, 0)
+ assert specificity("#lipsum") == (1, 0, 0)
- assert specificity(':not(*)') == (0, 0, 0)
- assert specificity(':not(foo)') == (0, 0, 1)
- assert specificity(':not(.foo)') == (0, 1, 0)
- assert specificity(':not([foo])') == (0, 1, 0)
- assert specificity(':not(:empty)') == (0, 1, 0)
- assert specificity(':not(#foo)') == (1, 0, 0)
+ assert specificity(":not(*)") == (0, 0, 0)
+ assert specificity(":not(foo)") == (0, 0, 1)
+ assert specificity(":not(.foo)") == (0, 1, 0)
+ assert specificity(":not([foo])") == (0, 1, 0)
+ assert specificity(":not(:empty)") == (0, 1, 0)
+ assert specificity(":not(#foo)") == (1, 0, 0)
- assert specificity(':is(.foo, #bar)') == (1, 0, 0)
- assert specificity(':is(:hover, :visited)') == (0, 1, 0)
+ assert specificity(":is(.foo, #bar)") == (1, 0, 0)
+ assert specificity(":is(:hover, :visited)") == (0, 1, 0)
- assert specificity('foo:empty') == (0, 1, 1)
- assert specificity('foo:before') == (0, 0, 2)
- assert specificity('foo::before') == (0, 0, 2)
- assert specificity('foo:empty::before') == (0, 1, 2)
+ assert specificity("foo:empty") == (0, 1, 1)
+ assert specificity("foo:before") == (0, 0, 2)
+ assert specificity("foo::before") == (0, 0, 2)
+ assert specificity("foo:empty::before") == (0, 1, 2)
- assert specificity('#lorem + foo#ipsum:first-child > bar:first-line'
- ) == (2, 1, 3)
+ assert specificity("#lorem + foo#ipsum:first-child > bar:first-line") == (2, 1, 3)
def test_css_export(self):
def css2css(css, res=None):
@@ -287,34 +295,34 @@ def css2css(css, res=None):
assert len(selectors) == 1
assert selectors[0].canonical() == (res or css)
- css2css('*')
- css2css(' foo', 'foo')
- css2css('Foo', 'Foo')
- css2css(':empty ', ':empty')
- css2css(':before', '::before')
- css2css(':beFOre', '::before')
- css2css('*:before', '::before')
- css2css(':nth-child(2)')
- css2css('.bar')
- css2css('[baz]')
+ css2css("*")
+ css2css(" foo", "foo")
+ css2css("Foo", "Foo")
+ css2css(":empty ", ":empty")
+ css2css(":before", "::before")
+ css2css(":beFOre", "::before")
+ css2css("*:before", "::before")
+ css2css(":nth-child(2)")
+ css2css(".bar")
+ css2css("[baz]")
css2css('[baz="4"]', "[baz='4']")
css2css('[baz^="4"]', "[baz^='4']")
css2css("[ns|attr='4']")
- css2css('#lipsum')
- css2css(':not(*)')
- css2css(':not(foo)')
- css2css(':not(*.foo)', ':not(.foo)')
- css2css(':not(*[foo])', ':not([foo])')
- css2css(':not(:empty)')
- css2css(':not(#foo)')
- css2css(':is(#bar, .foo)')
- css2css(':is(:focused, :visited)')
- css2css('foo:empty')
- css2css('foo::before')
- css2css('foo:empty::before')
+ css2css("#lipsum")
+ css2css(":not(*)")
+ css2css(":not(foo)")
+ css2css(":not(*.foo)", ":not(.foo)")
+ css2css(":not(*[foo])", ":not([foo])")
+ css2css(":not(:empty)")
+ css2css(":not(#foo)")
+ css2css(":is(#bar, .foo)")
+ css2css(":is(:focused, :visited)")
+ css2css("foo:empty")
+ css2css("foo::before")
+ css2css("foo:empty::before")
css2css('::name(arg + "val" - 3)', "::name(arg+'val'-3)")
- css2css('#lorem + foo#ipsum:first-child > bar::first-line')
- css2css('foo > *')
+ css2css("#lorem + foo#ipsum:first-child > bar::first-line")
+ css2css("foo > *")
def test_parse_errors(self):
def get_error(css):
@@ -324,283 +332,224 @@ def get_error(css):
# Py2, Py3, ...
return str(sys.exc_info()[1]).replace("(u'", "('")
- assert get_error('attributes(href)/html/body/a') == (
- "Expected selector, got ")
- assert get_error('attributes(href)') == (
- "Expected selector, got ")
- assert get_error('html/body/a') == (
- "Expected selector, got ")
- assert get_error(' ') == (
- "Expected selector, got ")
- assert get_error('div, ') == (
- "Expected selector, got ")
- assert get_error(' , div') == (
- "Expected selector, got ")
- assert get_error('p, , div') == (
- "Expected selector, got ")
- assert get_error('div > ') == (
- "Expected selector, got ")
- assert get_error(' > div') == (
- "Expected selector, got ' at 2>")
- assert get_error('foo|#bar') == (
- "Expected ident or '*', got ")
- assert get_error('#.foo') == (
- "Expected selector, got ")
- assert get_error('.#foo') == (
- "Expected ident, got ")
- assert get_error(':#foo') == (
- "Expected ident, got ")
- assert get_error('[*]') == (
- "Expected '|', got ")
- assert get_error('[foo|]') == (
- "Expected ident, got ")
- assert get_error('[#]') == (
- "Expected ident or '*', got ")
- assert get_error('[foo=#]') == (
- "Expected string or ident, got ")
- assert get_error('[href]a') == (
- "Expected selector, got ")
- assert get_error('[rel=stylesheet]') is None
- assert get_error('[rel:stylesheet]') == (
- "Operator expected, got ")
- assert get_error('[rel=stylesheet') == (
- "Expected ']', got ")
- assert get_error(':lang(fr)') is None
- assert get_error(':lang(fr') == (
- "Expected an argument, got ")
- assert get_error(':contains("foo') == (
- "Unclosed string at 10")
- assert get_error('foo!') == (
- "Expected selector, got ")
+ assert get_error("attributes(href)/html/body/a") == (
+ "Expected selector, got "
+ )
+ assert get_error("attributes(href)") == ("Expected selector, got ")
+ assert get_error("html/body/a") == ("Expected selector, got ")
+ assert get_error(" ") == ("Expected selector, got ")
+ assert get_error("div, ") == ("Expected selector, got ")
+ assert get_error(" , div") == ("Expected selector, got ")
+ assert get_error("p, , div") == ("Expected selector, got ")
+ assert get_error("div > ") == ("Expected selector, got ")
+ assert get_error(" > div") == ("Expected selector, got ' at 2>")
+ assert get_error("foo|#bar") == ("Expected ident or '*', got ")
+ assert get_error("#.foo") == ("Expected selector, got ")
+ assert get_error(".#foo") == ("Expected ident, got ")
+ assert get_error(":#foo") == ("Expected ident, got ")
+ assert get_error("[*]") == ("Expected '|', got ")
+ assert get_error("[foo|]") == ("Expected ident, got ")
+ assert get_error("[#]") == ("Expected ident or '*', got ")
+ assert get_error("[foo=#]") == ("Expected string or ident, got ")
+ assert get_error("[href]a") == ("Expected selector, got ")
+ assert get_error("[rel=stylesheet]") is None
+ assert get_error("[rel:stylesheet]") == ("Operator expected, got ")
+ assert get_error("[rel=stylesheet") == ("Expected ']', got ")
+ assert get_error(":lang(fr)") is None
+ assert get_error(":lang(fr") == ("Expected an argument, got ")
+ assert get_error(':contains("foo') == ("Unclosed string at 10")
+ assert get_error("foo!") == ("Expected selector, got ")
# Mis-placed pseudo-elements
- assert get_error('a:before:empty') == (
- "Got pseudo-element ::before not at the end of a selector")
- assert get_error('li:before a') == (
- "Got pseudo-element ::before not at the end of a selector")
- assert get_error(':not(:before)') == (
- "Got pseudo-element ::before inside :not() at 12")
- assert get_error(':not(:not(a))') == (
- "Got nested :not()")
- assert get_error(':is(:before)') == (
- "Got pseudo-element ::before inside function")
- assert get_error(':is(a b)') == (
- "Expected an argument, got ")
- assert get_error(':scope > div :scope header') == (
+ assert get_error("a:before:empty") == (
+ "Got pseudo-element ::before not at the end of a selector"
+ )
+ assert get_error("li:before a") == (
+ "Got pseudo-element ::before not at the end of a selector"
+ )
+ assert get_error(":not(:before)") == ("Got pseudo-element ::before inside :not() at 12")
+ assert get_error(":not(:not(a))") == ("Got nested :not()")
+ assert get_error(":is(:before)") == ("Got pseudo-element ::before inside function")
+ assert get_error(":is(a b)") == ("Expected an argument, got ")
+ assert get_error(":scope > div :scope header") == (
'Got immediate child pseudo-element ":scope" not at the start of a selector'
)
- assert get_error('div :scope header') == (
+ assert get_error("div :scope header") == (
'Got immediate child pseudo-element ":scope" not at the start of a selector'
)
- assert get_error('> div p') == ("Expected selector, got ' at 0>")
+ assert get_error("> div p") == ("Expected selector, got ' at 0>")
def test_translation(self):
def xpath(css):
- return _unicode(GenericTranslator().css_to_xpath(css, prefix=''))
-
- assert xpath('*') == "*"
- assert xpath('e') == "e"
- assert xpath('*|e') == "e"
- assert xpath('e|f') == "e:f"
- assert xpath('e[foo]') == "e[@foo]"
- assert xpath('e[foo|bar]') == "e[@foo:bar]"
+ return _unicode(GenericTranslator().css_to_xpath(css, prefix=""))
+
+ assert xpath("*") == "*"
+ assert xpath("e") == "e"
+ assert xpath("*|e") == "e"
+ assert xpath("e|f") == "e:f"
+ assert xpath("e[foo]") == "e[@foo]"
+ assert xpath("e[foo|bar]") == "e[@foo:bar]"
assert xpath('e[foo="bar"]') == "e[@foo = 'bar']"
assert xpath('e[foo~="bar"]') == (
- "e[@foo and contains("
- "concat(' ', normalize-space(@foo), ' '), ' bar ')]")
- assert xpath('e[foo^="bar"]') == (
- "e[@foo and starts-with(@foo, 'bar')]")
+ "e[@foo and contains(" "concat(' ', normalize-space(@foo), ' '), ' bar ')]"
+ )
+ assert xpath('e[foo^="bar"]') == ("e[@foo and starts-with(@foo, 'bar')]")
assert xpath('e[foo$="bar"]') == (
- "e[@foo and substring(@foo, string-length(@foo)-2) = 'bar']")
- assert xpath('e[foo*="bar"]') == (
- "e[@foo and contains(@foo, 'bar')]")
+ "e[@foo and substring(@foo, string-length(@foo)-2) = 'bar']"
+ )
+ assert xpath('e[foo*="bar"]') == ("e[@foo and contains(@foo, 'bar')]")
assert xpath('e[hreflang|="en"]') == (
- "e[@hreflang and ("
- "@hreflang = 'en' or starts-with(@hreflang, 'en-'))]")
+ "e[@hreflang and (" "@hreflang = 'en' or starts-with(@hreflang, 'en-'))]"
+ )
# --- nth-* and nth-last-* -------------------------------------
- assert xpath('e:nth-child(1)') == (
- "e[count(preceding-sibling::*) = 0]")
+ assert xpath("e:nth-child(1)") == ("e[count(preceding-sibling::*) = 0]")
# always true
- assert xpath('e:nth-child(n)') == (
- "e")
- assert xpath('e:nth-child(n+1)') == (
- "e")
+ assert xpath("e:nth-child(n)") == ("e")
+ assert xpath("e:nth-child(n+1)") == ("e")
# always true too
- assert xpath('e:nth-child(n-10)') == (
- "e")
+ assert xpath("e:nth-child(n-10)") == ("e")
# b=2 is the limit...
- assert xpath('e:nth-child(n+2)') == (
- "e[count(preceding-sibling::*) >= 1]")
+ assert xpath("e:nth-child(n+2)") == ("e[count(preceding-sibling::*) >= 1]")
# always false
- assert xpath('e:nth-child(-n)') == (
- "e[0]")
+ assert xpath("e:nth-child(-n)") == ("e[0]")
# equivalent to first child
- assert xpath('e:nth-child(-n+1)') == (
- "e[count(preceding-sibling::*) <= 0]")
+ assert xpath("e:nth-child(-n+1)") == ("e[count(preceding-sibling::*) <= 0]")
- assert xpath('e:nth-child(3n+2)') == (
+ assert xpath("e:nth-child(3n+2)") == (
"e[(count(preceding-sibling::*) >= 1) and "
- "((count(preceding-sibling::*) +2) mod 3 = 0)]")
- assert xpath('e:nth-child(3n-2)') == (
- "e[count(preceding-sibling::*) mod 3 = 0]")
- assert xpath('e:nth-child(-n+6)') == (
- "e[count(preceding-sibling::*) <= 5]")
-
- assert xpath('e:nth-last-child(1)') == (
- "e[count(following-sibling::*) = 0]")
- assert xpath('e:nth-last-child(2n)') == (
- "e[(count(following-sibling::*) +1) mod 2 = 0]")
- assert xpath('e:nth-last-child(2n+1)') == (
- "e[count(following-sibling::*) mod 2 = 0]")
- assert xpath('e:nth-last-child(2n+2)') == (
+ "((count(preceding-sibling::*) +2) mod 3 = 0)]"
+ )
+ assert xpath("e:nth-child(3n-2)") == ("e[count(preceding-sibling::*) mod 3 = 0]")
+ assert xpath("e:nth-child(-n+6)") == ("e[count(preceding-sibling::*) <= 5]")
+
+ assert xpath("e:nth-last-child(1)") == ("e[count(following-sibling::*) = 0]")
+ assert xpath("e:nth-last-child(2n)") == ("e[(count(following-sibling::*) +1) mod 2 = 0]")
+ assert xpath("e:nth-last-child(2n+1)") == ("e[count(following-sibling::*) mod 2 = 0]")
+ assert xpath("e:nth-last-child(2n+2)") == (
"e[(count(following-sibling::*) >= 1) and "
- "((count(following-sibling::*) +1) mod 2 = 0)]")
- assert xpath('e:nth-last-child(3n+1)') == (
- "e[count(following-sibling::*) mod 3 = 0]")
+ "((count(following-sibling::*) +1) mod 2 = 0)]"
+ )
+ assert xpath("e:nth-last-child(3n+1)") == ("e[count(following-sibling::*) mod 3 = 0]")
# represents the two last e elements
- assert xpath('e:nth-last-child(-n+2)') == (
- "e[count(following-sibling::*) <= 1]")
-
- assert xpath('e:nth-of-type(1)') == (
- "e[count(preceding-sibling::e) = 0]")
- assert xpath('e:nth-last-of-type(1)') == (
- "e[count(following-sibling::e) = 0]")
- assert xpath('div e:nth-last-of-type(1) .aclass') == (
+ assert xpath("e:nth-last-child(-n+2)") == ("e[count(following-sibling::*) <= 1]")
+
+ assert xpath("e:nth-of-type(1)") == ("e[count(preceding-sibling::e) = 0]")
+ assert xpath("e:nth-last-of-type(1)") == ("e[count(following-sibling::e) = 0]")
+ assert xpath("div e:nth-last-of-type(1) .aclass") == (
"div/descendant-or-self::*/e[count(following-sibling::e) = 0]"
- "/descendant-or-self::*/*[@class and contains("
- "concat(' ', normalize-space(@class), ' '), ' aclass ')]")
-
- assert xpath('e:first-child') == (
- "e[count(preceding-sibling::*) = 0]")
- assert xpath('e:last-child') == (
- "e[count(following-sibling::*) = 0]")
- assert xpath('e:first-of-type') == (
- "e[count(preceding-sibling::e) = 0]")
- assert xpath('e:last-of-type') == (
- "e[count(following-sibling::e) = 0]")
- assert xpath('e:only-child') == (
- "e[count(parent::*/child::*) = 1]")
- assert xpath('e:only-of-type') == (
- "e[count(parent::*/child::e) = 1]")
- assert xpath('e:empty') == (
- "e[not(*) and not(string-length())]")
- assert xpath('e:EmPTY') == (
- "e[not(*) and not(string-length())]")
- assert xpath('e:root') == (
- "e[not(parent::*)]")
- assert xpath('e:hover') == (
- "e[0]") # never matches
- assert xpath('e:contains("foo")') == (
- "e[contains(., 'foo')]")
- assert xpath('e:ConTains(foo)') == (
- "e[contains(., 'foo')]")
- assert xpath('e.warning') == (
- "e[@class and contains("
- "concat(' ', normalize-space(@class), ' '), ' warning ')]")
- assert xpath('e#myid') == (
- "e[@id = 'myid']")
- assert xpath('e:not(:nth-child(odd))') == (
- "e[not(count(preceding-sibling::*) mod 2 = 0)]")
- assert xpath('e:nOT(*)') == (
- "e[0]") # never matches
- assert xpath('e f') == (
- "e/descendant-or-self::*/f")
- assert xpath('e > f') == (
- "e/f")
- assert xpath('e + f') == (
- "e/following-sibling::*[(name() = 'f') and (position() = 1)]")
- assert xpath('e ~ f') == (
- "e/following-sibling::f")
- assert xpath('e ~ f:nth-child(3)') == (
- "e/following-sibling::f[count(preceding-sibling::*) = 2]")
- assert xpath('div#container p') == (
- "div[@id = 'container']/descendant-or-self::*/p")
+ "/descendant-or-self::*/*[@class and contains("
+ "concat(' ', normalize-space(@class), ' '), ' aclass ')]"
+ )
+
+ assert xpath("e:first-child") == ("e[count(preceding-sibling::*) = 0]")
+ assert xpath("e:last-child") == ("e[count(following-sibling::*) = 0]")
+ assert xpath("e:first-of-type") == ("e[count(preceding-sibling::e) = 0]")
+ assert xpath("e:last-of-type") == ("e[count(following-sibling::e) = 0]")
+ assert xpath("e:only-child") == ("e[count(parent::*/child::*) = 1]")
+ assert xpath("e:only-of-type") == ("e[count(parent::*/child::e) = 1]")
+ assert xpath("e:empty") == ("e[not(*) and not(string-length())]")
+ assert xpath("e:EmPTY") == ("e[not(*) and not(string-length())]")
+ assert xpath("e:root") == ("e[not(parent::*)]")
+ assert xpath("e:hover") == ("e[0]") # never matches
+ assert xpath('e:contains("foo")') == ("e[contains(., 'foo')]")
+ assert xpath("e:ConTains(foo)") == ("e[contains(., 'foo')]")
+ assert xpath("e.warning") == (
+ "e[@class and contains(" "concat(' ', normalize-space(@class), ' '), ' warning ')]"
+ )
+ assert xpath("e#myid") == ("e[@id = 'myid']")
+ assert xpath("e:not(:nth-child(odd))") == ("e[not(count(preceding-sibling::*) mod 2 = 0)]")
+ assert xpath("e:nOT(*)") == ("e[0]") # never matches
+ assert xpath("e f") == ("e/descendant-or-self::*/f")
+ assert xpath("e > f") == ("e/f")
+ assert xpath("e + f") == ("e/following-sibling::*[(name() = 'f') and (position() = 1)]")
+ assert xpath("e ~ f") == ("e/following-sibling::f")
+ assert xpath("e ~ f:nth-child(3)") == (
+ "e/following-sibling::f[count(preceding-sibling::*) = 2]"
+ )
+ assert xpath("div#container p") == ("div[@id = 'container']/descendant-or-self::*/p")
# Invalid characters in XPath element names
- assert xpath(r'di\a0 v') == (
- u("*[name() = 'di v']")) # di\xa0v
- assert xpath(r'di\[v') == (
- "*[name() = 'di[v']")
- assert xpath(r'[h\a0 ref]') == (
- u("*[attribute::*[name() = 'h ref']]")) # h\xa0ref
- assert xpath(r'[h\]ref]') == (
- "*[attribute::*[name() = 'h]ref']]")
-
- self.assertRaises(ExpressionError, xpath, u(':fİrst-child'))
- self.assertRaises(ExpressionError, xpath, ':first-of-type')
- self.assertRaises(ExpressionError, xpath, ':only-of-type')
- self.assertRaises(ExpressionError, xpath, ':last-of-type')
- self.assertRaises(ExpressionError, xpath, ':nth-of-type(1)')
- self.assertRaises(ExpressionError, xpath, ':nth-last-of-type(1)')
- self.assertRaises(ExpressionError, xpath, ':nth-child(n-)')
- self.assertRaises(ExpressionError, xpath, ':after')
- self.assertRaises(ExpressionError, xpath, ':lorem-ipsum')
- self.assertRaises(ExpressionError, xpath, ':lorem(ipsum)')
- self.assertRaises(ExpressionError, xpath, '::lorem-ipsum')
+ assert xpath(r"di\a0 v") == (u("*[name() = 'di v']")) # di\xa0v
+ assert xpath(r"di\[v") == ("*[name() = 'di[v']")
+ assert xpath(r"[h\a0 ref]") == (u("*[attribute::*[name() = 'h ref']]")) # h\xa0ref
+ assert xpath(r"[h\]ref]") == ("*[attribute::*[name() = 'h]ref']]")
+
+ self.assertRaises(ExpressionError, xpath, u(":fİrst-child"))
+ self.assertRaises(ExpressionError, xpath, ":first-of-type")
+ self.assertRaises(ExpressionError, xpath, ":only-of-type")
+ self.assertRaises(ExpressionError, xpath, ":last-of-type")
+ self.assertRaises(ExpressionError, xpath, ":nth-of-type(1)")
+ self.assertRaises(ExpressionError, xpath, ":nth-last-of-type(1)")
+ self.assertRaises(ExpressionError, xpath, ":nth-child(n-)")
+ self.assertRaises(ExpressionError, xpath, ":after")
+ self.assertRaises(ExpressionError, xpath, ":lorem-ipsum")
+ self.assertRaises(ExpressionError, xpath, ":lorem(ipsum)")
+ self.assertRaises(ExpressionError, xpath, "::lorem-ipsum")
self.assertRaises(TypeError, GenericTranslator().css_to_xpath, 4)
- self.assertRaises(TypeError, GenericTranslator().selector_to_xpath,
- 'foo')
+ self.assertRaises(TypeError, GenericTranslator().selector_to_xpath, "foo")
def test_unicode(self):
if sys.version_info[0] < 3:
- css = '.a\xc1b'.decode('ISO-8859-1')
+ css = ".a\xc1b".decode("ISO-8859-1")
else:
- css = '.a\xc1b'
+ css = ".a\xc1b"
xpath = GenericTranslator().css_to_xpath(css)
assert css[1:] in xpath
- xpath = xpath.encode('ascii', 'xmlcharrefreplace').decode('ASCII')
+ xpath = xpath.encode("ascii", "xmlcharrefreplace").decode("ASCII")
assert xpath == (
"descendant-or-self::*[@class and contains("
- "concat(' ', normalize-space(@class), ' '), ' aÁb ')]")
+ "concat(' ', normalize-space(@class), ' '), ' aÁb ')]"
+ )
def test_quoting(self):
css_to_xpath = GenericTranslator().css_to_xpath
- assert css_to_xpath('*[aval="\'"]') == (
- '''descendant-or-self::*[@aval = "'"]''')
- assert css_to_xpath('*[aval="\'\'\'"]') == (
- """descendant-or-self::*[@aval = "'''"]""")
- assert css_to_xpath('*[aval=\'"\']') == (
- '''descendant-or-self::*[@aval = '"']''')
- assert css_to_xpath('*[aval=\'"""\']') == (
- '''descendant-or-self::*[@aval = '"""']''')
+ assert css_to_xpath('*[aval="\'"]') == ("""descendant-or-self::*[@aval = "'"]""")
+ assert css_to_xpath("*[aval=\"'''\"]") == ("""descendant-or-self::*[@aval = "'''"]""")
+ assert css_to_xpath("*[aval='\"']") == ("""descendant-or-self::*[@aval = '"']""")
+ assert css_to_xpath('*[aval=\'"""\']') == ('''descendant-or-self::*[@aval = '"""']''')
assert css_to_xpath(':scope > div[dataimg=""]') == (
- "descendant-or-self::*[1]/div[@dataimg = '']")
+ "descendant-or-self::*[1]/div[@dataimg = '']"
+ )
def test_unicode_escapes(self):
# \22 == '"' \20 == ' '
css_to_xpath = GenericTranslator().css_to_xpath
assert css_to_xpath(r'*[aval="\'\22\'"]') == (
- '''descendant-or-self::*[@aval = concat("'",'"',"'")]''')
+ """descendant-or-self::*[@aval = concat("'",'"',"'")]"""
+ )
assert css_to_xpath(r'*[aval="\'\22 2\'"]') == (
- '''descendant-or-self::*[@aval = concat("'",'"2',"'")]''')
+ """descendant-or-self::*[@aval = concat("'",'"2',"'")]"""
+ )
assert css_to_xpath(r'*[aval="\'\20 \'"]') == (
- '''descendant-or-self::*[@aval = "' '"]''')
- assert css_to_xpath('*[aval="\'\\20\r\n \'"]') == (
- '''descendant-or-self::*[@aval = "' '"]''')
+ """descendant-or-self::*[@aval = "' '"]"""
+ )
+ assert css_to_xpath("*[aval=\"'\\20\r\n '\"]") == (
+ """descendant-or-self::*[@aval = "' '"]"""
+ )
def test_xpath_pseudo_elements(self):
class CustomTranslator(GenericTranslator):
def xpath_pseudo_element(self, xpath, pseudo_element):
if isinstance(pseudo_element, FunctionalPseudoElement):
- method = 'xpath_%s_functional_pseudo_element' % (
- pseudo_element.name.replace('-', '_'))
+ method = "xpath_%s_functional_pseudo_element" % (
+ pseudo_element.name.replace("-", "_")
+ )
method = _unicode_safe_getattr(self, method, None)
if not method:
raise ExpressionError(
- "The functional pseudo-element ::%s() is unknown"
- % pseudo_element.name)
+ "The functional pseudo-element ::%s() is unknown" % pseudo_element.name
+ )
xpath = method(xpath, pseudo_element.arguments)
else:
- method = 'xpath_%s_simple_pseudo_element' % (
- pseudo_element.replace('-', '_'))
+ method = "xpath_%s_simple_pseudo_element" % (pseudo_element.replace("-", "_"))
method = _unicode_safe_getattr(self, method, None)
if not method:
raise ExpressionError(
- "The pseudo-element ::%s is unknown"
- % pseudo_element)
+ "The pseudo-element ::%s is unknown" % pseudo_element
+ )
xpath = method(xpath)
return xpath
@@ -608,8 +557,7 @@ def xpath_pseudo_element(self, xpath, pseudo_element):
# elements that have a certain number of attributes
def xpath_nb_attr_function(self, xpath, function):
nb_attributes = int(function.arguments[0].value)
- return xpath.add_condition(
- "count(@*)=%d" % nb_attributes)
+ return xpath.add_condition("count(@*)=%d" % nb_attributes)
# pseudo-class:
# elements that have 5 attributes
@@ -620,20 +568,29 @@ def xpath_five_attributes_pseudo(self, xpath):
# element's attribute by name
def xpath_attr_functional_pseudo_element(self, xpath, arguments):
attribute_name = arguments[0].value
- other = XPathExpr('@%s' % attribute_name, '', )
- return xpath.join('/', other)
+ other = XPathExpr(
+ "@%s" % attribute_name,
+ "",
+ )
+ return xpath.join("/", other)
# pseudo-element:
# element's text() nodes
def xpath_text_node_simple_pseudo_element(self, xpath):
- other = XPathExpr('text()', '', )
- return xpath.join('/', other)
+ other = XPathExpr(
+ "text()",
+ "",
+ )
+ return xpath.join("/", other)
# pseudo-element:
# element's href attribute
def xpath_attr_href_simple_pseudo_element(self, xpath):
- other = XPathExpr('@href', '', )
- return xpath.join('/', other)
+ other = XPathExpr(
+ "@href",
+ "",
+ )
+ return xpath.join("/", other)
# pseudo-element:
# used to demonstrate operator precedence
@@ -643,91 +600,86 @@ def xpath_first_or_second_pseudo(self, xpath):
def xpath(css):
return _unicode(CustomTranslator().css_to_xpath(css))
- assert xpath(':five-attributes') == "descendant-or-self::*[count(@*)=5]"
- assert xpath(':nb-attr(3)') == "descendant-or-self::*[count(@*)=3]"
- assert xpath('::attr(href)') == "descendant-or-self::*/@href"
- assert xpath('::text-node') == "descendant-or-self::*/text()"
- assert xpath('::attr-href') == "descendant-or-self::*/@href"
- assert xpath('p img::attr(src)') == (
- "descendant-or-self::p/descendant-or-self::*/img/@src")
- assert xpath(':scope') == "descendant-or-self::*[1]"
- assert xpath(':first-or-second[href]') == (
- "descendant-or-self::*[(@id = 'first' or @id = 'second') "
- "and (@href)]")
+ assert xpath(":five-attributes") == "descendant-or-self::*[count(@*)=5]"
+ assert xpath(":nb-attr(3)") == "descendant-or-self::*[count(@*)=3]"
+ assert xpath("::attr(href)") == "descendant-or-self::*/@href"
+ assert xpath("::text-node") == "descendant-or-self::*/text()"
+ assert xpath("::attr-href") == "descendant-or-self::*/@href"
+ assert xpath("p img::attr(src)") == (
+ "descendant-or-self::p/descendant-or-self::*/img/@src"
+ )
+ assert xpath(":scope") == "descendant-or-self::*[1]"
+ assert xpath(":first-or-second[href]") == (
+ "descendant-or-self::*[(@id = 'first' or @id = 'second') " "and (@href)]"
+ )
- assert str(XPathExpr('', '', condition='@href')) == "[@href]"
+ assert str(XPathExpr("", "", condition="@href")) == "[@href]"
document = etree.fromstring(OPERATOR_PRECEDENCE_IDS)
- sort_key = dict(
- (el, count) for count, el in enumerate(document.getiterator())
- ).__getitem__
+ sort_key = dict((el, count) for count, el in enumerate(document.getiterator())).__getitem__
+
def operator_id(selector):
xpath = CustomTranslator().css_to_xpath(selector)
items = document.xpath(xpath)
items.sort(key=sort_key)
- return [element.get('id', 'nil') for element in items]
+ return [element.get("id", "nil") for element in items]
- assert operator_id(':first-or-second') == ['first', 'second']
- assert operator_id(':first-or-second[href]') == ['second']
- assert operator_id('[href]:first-or-second') == ['second']
+ assert operator_id(":first-or-second") == ["first", "second"]
+ assert operator_id(":first-or-second[href]") == ["second"]
+ assert operator_id("[href]:first-or-second") == ["second"]
def test_series(self):
def series(css):
- selector, = parse(':nth-child(%s)' % css)
+ (selector,) = parse(":nth-child(%s)" % css)
args = selector.parsed_tree.arguments
try:
return parse_series(args)
except ValueError:
return None
- assert series('1n+3') == (1, 3)
- assert series('1n +3') == (1, 3)
- assert series('1n + 3') == (1, 3)
- assert series('1n+ 3') == (1, 3)
- assert series('1n-3') == (1, -3)
- assert series('1n -3') == (1, -3)
- assert series('1n - 3') == (1, -3)
- assert series('1n- 3') == (1, -3)
- assert series('n-5') == (1, -5)
- assert series('odd') == (2, 1)
- assert series('even') == (2, 0)
- assert series('3n') == (3, 0)
- assert series('n') == (1, 0)
- assert series('+n') == (1, 0)
- assert series('-n') == (-1, 0)
- assert series('5') == (0, 5)
- assert series('foo') is None
- assert series('n+') is None
+ assert series("1n+3") == (1, 3)
+ assert series("1n +3") == (1, 3)
+ assert series("1n + 3") == (1, 3)
+ assert series("1n+ 3") == (1, 3)
+ assert series("1n-3") == (1, -3)
+ assert series("1n -3") == (1, -3)
+ assert series("1n - 3") == (1, -3)
+ assert series("1n- 3") == (1, -3)
+ assert series("n-5") == (1, -5)
+ assert series("odd") == (2, 1)
+ assert series("even") == (2, 0)
+ assert series("3n") == (3, 0)
+ assert series("n") == (1, 0)
+ assert series("+n") == (1, 0)
+ assert series("-n") == (-1, 0)
+ assert series("5") == (0, 5)
+ assert series("foo") is None
+ assert series("n+") is None
def test_lang(self):
document = etree.fromstring(XMLLANG_IDS)
- sort_key = dict(
- (el, count) for count, el in enumerate(document.getiterator())
- ).__getitem__
+ sort_key = dict((el, count) for count, el in enumerate(document.getiterator())).__getitem__
css_to_xpath = GenericTranslator().css_to_xpath
def langid(selector):
xpath = css_to_xpath(selector)
items = document.xpath(xpath)
items.sort(key=sort_key)
- return [element.get('id', 'nil') for element in items]
-
- assert langid(':lang("EN")') == ['first', 'second', 'third', 'fourth']
- assert langid(':lang("en-us")') == ['second', 'fourth']
- assert langid(':lang(en-nz)') == ['third']
- assert langid(':lang(fr)') == ['fifth']
- assert langid(':lang(ru)') == ['sixth']
- assert langid(":lang('ZH')") == ['eighth']
- assert langid(':lang(de) :lang(zh)') == ['eighth']
- assert langid(':lang(en), :lang(zh)') == [
- 'first', 'second', 'third', 'fourth', 'eighth']
- assert langid(':lang(es)') == []
+ return [element.get("id", "nil") for element in items]
+
+ assert langid(':lang("EN")') == ["first", "second", "third", "fourth"]
+ assert langid(':lang("en-us")') == ["second", "fourth"]
+ assert langid(":lang(en-nz)") == ["third"]
+ assert langid(":lang(fr)") == ["fifth"]
+ assert langid(":lang(ru)") == ["sixth"]
+ assert langid(":lang('ZH')") == ["eighth"]
+ assert langid(":lang(de) :lang(zh)") == ["eighth"]
+ assert langid(":lang(en), :lang(zh)") == ["first", "second", "third", "fourth", "eighth"]
+ assert langid(":lang(es)") == []
def test_select(self):
document = etree.fromstring(HTML_IDS)
- sort_key = dict(
- (el, count) for count, el in enumerate(document.getiterator())
- ).__getitem__
+ sort_key = dict((el, count) for count, el in enumerate(document.getiterator())).__getitem__
css_to_xpath = GenericTranslator().css_to_xpath
html_css_to_xpath = HTMLTranslator().css_to_xpath
@@ -739,172 +691,218 @@ def select_ids(selector, html_only):
xpath = html_css_to_xpath(selector)
items = document.xpath(xpath)
items.sort(key=sort_key)
- return [element.get('id', 'nil') for element in items]
+ return [element.get("id", "nil") for element in items]
def pcss(main, *selectors, **kwargs):
- html_only = kwargs.pop('html_only', False)
+ html_only = kwargs.pop("html_only", False)
result = select_ids(main, html_only)
for selector in selectors:
assert select_ids(selector, html_only) == result
return result
- all_ids = pcss('*')
- assert all_ids[:6] == [
- 'html', 'nil', 'link-href', 'link-nohref', 'nil', 'outer-div']
- assert all_ids[-1:] == ['foobar-span']
- assert pcss('div') == ['outer-div', 'li-div', 'foobar-div']
- assert pcss('DIV', html_only=True) == [
- 'outer-div', 'li-div', 'foobar-div'] # case-insensitive in HTML
- assert pcss('div div') == ['li-div']
- assert pcss('div, div div') == ['outer-div', 'li-div', 'foobar-div']
- assert pcss('a[name]') == ['name-anchor']
- assert pcss('a[NAme]', html_only=True) == [
- 'name-anchor'] # case-insensitive in HTML:
- assert pcss('a[rel]') == ['tag-anchor', 'nofollow-anchor']
- assert pcss('a[rel="tag"]') == ['tag-anchor']
- assert pcss('a[href*="localhost"]') == ['tag-anchor']
+ all_ids = pcss("*")
+ assert all_ids[:6] == ["html", "nil", "link-href", "link-nohref", "nil", "outer-div"]
+ assert all_ids[-1:] == ["foobar-span"]
+ assert pcss("div") == ["outer-div", "li-div", "foobar-div"]
+ assert pcss("DIV", html_only=True) == [
+ "outer-div",
+ "li-div",
+ "foobar-div",
+ ] # case-insensitive in HTML
+ assert pcss("div div") == ["li-div"]
+ assert pcss("div, div div") == ["outer-div", "li-div", "foobar-div"]
+ assert pcss("a[name]") == ["name-anchor"]
+ assert pcss("a[NAme]", html_only=True) == ["name-anchor"] # case-insensitive in HTML:
+ assert pcss("a[rel]") == ["tag-anchor", "nofollow-anchor"]
+ assert pcss('a[rel="tag"]') == ["tag-anchor"]
+ assert pcss('a[href*="localhost"]') == ["tag-anchor"]
assert pcss('a[href*=""]') == []
- assert pcss('a[href^="http"]') == ['tag-anchor', 'nofollow-anchor']
- assert pcss('a[href^="http:"]') == ['tag-anchor']
+ assert pcss('a[href^="http"]') == ["tag-anchor", "nofollow-anchor"]
+ assert pcss('a[href^="http:"]') == ["tag-anchor"]
assert pcss('a[href^=""]') == []
- assert pcss('a[href$="org"]') == ['nofollow-anchor']
+ assert pcss('a[href$="org"]') == ["nofollow-anchor"]
assert pcss('a[href$=""]') == []
- assert pcss('div[foobar~="bc"]', 'div[foobar~="cde"]') == [
- 'foobar-div']
- assert pcss('[foobar~="ab bc"]',
- '[foobar~=""]', '[foobar~=" \t"]') == []
+ assert pcss('div[foobar~="bc"]', 'div[foobar~="cde"]') == ["foobar-div"]
+ assert pcss('[foobar~="ab bc"]', '[foobar~=""]', '[foobar~=" \t"]') == []
assert pcss('div[foobar~="cd"]') == []
- assert pcss('*[lang|="En"]', '[lang|="En-us"]') == ['second-li']
+ assert pcss('*[lang|="En"]', '[lang|="En-us"]') == ["second-li"]
# Attribute values are case sensitive
assert pcss('*[lang|="en"]', '[lang|="en-US"]') == []
assert pcss('*[lang|="e"]') == []
# ... :lang() is not.
- assert pcss(':lang("EN")', '*:lang(en-US)', html_only=True) == [
- 'second-li', 'li-div']
+ assert pcss(':lang("EN")', "*:lang(en-US)", html_only=True) == ["second-li", "li-div"]
assert pcss(':lang("e")', html_only=True) == []
- assert pcss(':scope > div') == []
- assert pcss(':scope body') == ['nil']
- assert pcss(':scope body > div') == ['outer-div', 'foobar-div']
- assert pcss(':scope head') == ['nil']
- assert pcss(':scope html') == []
+ assert pcss(":scope > div") == []
+ assert pcss(":scope body") == ["nil"]
+ assert pcss(":scope body > div") == ["outer-div", "foobar-div"]
+ assert pcss(":scope head") == ["nil"]
+ assert pcss(":scope html") == []
# --- nth-* and nth-last-* -------------------------------------
# select nothing
- assert pcss('li:nth-child(-n)') == []
+ assert pcss("li:nth-child(-n)") == []
# select all children
- assert pcss('li:nth-child(n)') == [
- 'first-li', 'second-li', 'third-li', 'fourth-li',
- 'fifth-li', 'sixth-li', 'seventh-li']
-
- assert pcss('li:nth-child(3)',
- '#first-li ~ :nth-child(3)') == ['third-li']
- assert pcss('li:nth-child(10)') == []
- assert pcss('li:nth-child(2n)', 'li:nth-child(even)',
- 'li:nth-child(2n+0)') == [
- 'second-li', 'fourth-li', 'sixth-li']
- assert pcss('li:nth-child(+2n+1)', 'li:nth-child(odd)') == [
- 'first-li', 'third-li', 'fifth-li', 'seventh-li']
- assert pcss('li:nth-child(2n+4)') == ['fourth-li', 'sixth-li']
- assert pcss('li:nth-child(3n+1)') == [
- 'first-li', 'fourth-li', 'seventh-li']
- assert pcss('li:nth-child(-n+3)') == [
- 'first-li', 'second-li', 'third-li']
- assert pcss('li:nth-child(-2n+4)') == ['second-li', 'fourth-li']
- assert pcss('li:nth-last-child(0)') == []
- assert pcss('li:nth-last-child(1)') == ['seventh-li']
- assert pcss('li:nth-last-child(2n)', 'li:nth-last-child(even)') == [
- 'second-li', 'fourth-li', 'sixth-li']
- assert pcss('li:nth-last-child(2n+1)') == [
- 'first-li', 'third-li', 'fifth-li', 'seventh-li']
- assert pcss('li:nth-last-child(2n+2)') == [
- 'second-li', 'fourth-li', 'sixth-li']
- assert pcss('li:nth-last-child(3n+1)') == [
- 'first-li', 'fourth-li', 'seventh-li']
- assert pcss('ol:first-of-type') == ['first-ol']
- assert pcss('ol:nth-child(1)') == []
- assert pcss('ol:nth-of-type(2)') == ['second-ol']
- assert pcss('ol:nth-last-of-type(1)') == ['second-ol']
+ assert pcss("li:nth-child(n)") == [
+ "first-li",
+ "second-li",
+ "third-li",
+ "fourth-li",
+ "fifth-li",
+ "sixth-li",
+ "seventh-li",
+ ]
+
+ assert pcss("li:nth-child(3)", "#first-li ~ :nth-child(3)") == ["third-li"]
+ assert pcss("li:nth-child(10)") == []
+ assert pcss("li:nth-child(2n)", "li:nth-child(even)", "li:nth-child(2n+0)") == [
+ "second-li",
+ "fourth-li",
+ "sixth-li",
+ ]
+ assert pcss("li:nth-child(+2n+1)", "li:nth-child(odd)") == [
+ "first-li",
+ "third-li",
+ "fifth-li",
+ "seventh-li",
+ ]
+ assert pcss("li:nth-child(2n+4)") == ["fourth-li", "sixth-li"]
+ assert pcss("li:nth-child(3n+1)") == ["first-li", "fourth-li", "seventh-li"]
+ assert pcss("li:nth-child(-n+3)") == ["first-li", "second-li", "third-li"]
+ assert pcss("li:nth-child(-2n+4)") == ["second-li", "fourth-li"]
+ assert pcss("li:nth-last-child(0)") == []
+ assert pcss("li:nth-last-child(1)") == ["seventh-li"]
+ assert pcss("li:nth-last-child(2n)", "li:nth-last-child(even)") == [
+ "second-li",
+ "fourth-li",
+ "sixth-li",
+ ]
+ assert pcss("li:nth-last-child(2n+1)") == [
+ "first-li",
+ "third-li",
+ "fifth-li",
+ "seventh-li",
+ ]
+ assert pcss("li:nth-last-child(2n+2)") == ["second-li", "fourth-li", "sixth-li"]
+ assert pcss("li:nth-last-child(3n+1)") == ["first-li", "fourth-li", "seventh-li"]
+ assert pcss("ol:first-of-type") == ["first-ol"]
+ assert pcss("ol:nth-child(1)") == []
+ assert pcss("ol:nth-of-type(2)") == ["second-ol"]
+ assert pcss("ol:nth-last-of-type(1)") == ["second-ol"]
# "+" and "~" tests
- assert pcss('ol#first-ol li + li:nth-child(4)') == ['fourth-li']
- assert pcss('li + li:nth-child(1)') == []
- assert pcss('li ~ li:nth-child(2n+1)') == [
- 'third-li', 'fifth-li', 'seventh-li'
- ] # all but the first
- assert pcss('li ~ li:nth-last-child(2n+1)') == [
- 'third-li', 'fifth-li', 'seventh-li'
- ] # all but the first
-
- assert pcss('span:only-child') == ['foobar-span']
- assert pcss('li div:only-child') == ['li-div']
- assert pcss('div *:only-child') == ['li-div', 'foobar-span']
- self.assertRaises(ExpressionError, pcss, 'p *:only-of-type')
- assert pcss('p:only-of-type') == ['paragraph']
- assert pcss('a:empty', 'a:EMpty') == ['name-anchor']
- assert pcss('li:empty') == [
- 'third-li', 'fourth-li', 'fifth-li', 'sixth-li']
- assert pcss(':root', 'html:root') == ['html']
- assert pcss('li:root', '* :root') == []
+ assert pcss("ol#first-ol li + li:nth-child(4)") == ["fourth-li"]
+ assert pcss("li + li:nth-child(1)") == []
+ assert pcss("li ~ li:nth-child(2n+1)") == [
+ "third-li",
+ "fifth-li",
+ "seventh-li",
+ ] # all but the first
+ assert pcss("li ~ li:nth-last-child(2n+1)") == [
+ "third-li",
+ "fifth-li",
+ "seventh-li",
+ ] # all but the first
+
+ assert pcss("span:only-child") == ["foobar-span"]
+ assert pcss("li div:only-child") == ["li-div"]
+ assert pcss("div *:only-child") == ["li-div", "foobar-span"]
+ self.assertRaises(ExpressionError, pcss, "p *:only-of-type")
+ assert pcss("p:only-of-type") == ["paragraph"]
+ assert pcss("a:empty", "a:EMpty") == ["name-anchor"]
+ assert pcss("li:empty") == ["third-li", "fourth-li", "fifth-li", "sixth-li"]
+ assert pcss(":root", "html:root") == ["html"]
+ assert pcss("li:root", "* :root") == []
assert pcss('*:contains("link")', ':CONtains("link")') == [
- 'html', 'nil', 'outer-div', 'tag-anchor', 'nofollow-anchor']
+ "html",
+ "nil",
+ "outer-div",
+ "tag-anchor",
+ "nofollow-anchor",
+ ]
assert pcss('*:contains("LInk")') == [] # case sensitive
assert pcss('*:contains("e")') == [
- 'html', 'nil', 'outer-div', 'first-ol', 'first-li',
- 'paragraph', 'p-em']
+ "html",
+ "nil",
+ "outer-div",
+ "first-ol",
+ "first-li",
+ "paragraph",
+ "p-em",
+ ]
assert pcss('*:contains("E")') == [] # case-sensitive
- assert pcss('.a', '.b', '*.a', 'ol.a') == ['first-ol']
- assert pcss('.c', '*.c') == ['first-ol', 'third-li', 'fourth-li']
- assert pcss('ol *.c', 'ol li.c', 'li ~ li.c', 'ol > li.c') == [
- 'third-li', 'fourth-li']
- assert pcss('#first-li', 'li#first-li', '*#first-li') == ['first-li']
- assert pcss('li div', 'li > div', 'div div') == ['li-div']
- assert pcss('div > div') == []
- assert pcss('div>.c', 'div > .c') == ['first-ol']
- assert pcss('div + div') == ['foobar-div']
- assert pcss('a ~ a') == ['tag-anchor', 'nofollow-anchor']
- assert pcss('a[rel="tag"] ~ a') == ['nofollow-anchor']
- assert pcss('ol#first-ol li:last-child') == ['seventh-li']
- assert pcss('ol#first-ol *:last-child') == ['li-div', 'seventh-li']
- assert pcss('#outer-div:first-child') == ['outer-div']
- assert pcss('#outer-div :first-child') == [
- 'name-anchor', 'first-li', 'li-div', 'p-b',
- 'checkbox-fieldset-disabled', 'area-href']
- assert pcss('a[href]') == ['tag-anchor', 'nofollow-anchor']
- assert pcss(':not(*)') == []
- assert pcss('a:not([href])') == ['name-anchor']
- assert pcss('ol :Not(li[class])') == [
- 'first-li', 'second-li', 'li-div',
- 'fifth-li', 'sixth-li', 'seventh-li']
- assert pcss(':is(#first-li, #second-li)') == [
- 'first-li', 'second-li']
- assert pcss('a:is(#name-anchor, #tag-anchor)') == [
- 'name-anchor', 'tag-anchor']
- assert pcss(':is(.c)') == [
- 'first-ol', 'third-li', 'fourth-li']
- assert pcss('ol.a.b.c > li.c:nth-child(3)') == ['third-li']
+ assert pcss(".a", ".b", "*.a", "ol.a") == ["first-ol"]
+ assert pcss(".c", "*.c") == ["first-ol", "third-li", "fourth-li"]
+ assert pcss("ol *.c", "ol li.c", "li ~ li.c", "ol > li.c") == ["third-li", "fourth-li"]
+ assert pcss("#first-li", "li#first-li", "*#first-li") == ["first-li"]
+ assert pcss("li div", "li > div", "div div") == ["li-div"]
+ assert pcss("div > div") == []
+ assert pcss("div>.c", "div > .c") == ["first-ol"]
+ assert pcss("div + div") == ["foobar-div"]
+ assert pcss("a ~ a") == ["tag-anchor", "nofollow-anchor"]
+ assert pcss('a[rel="tag"] ~ a') == ["nofollow-anchor"]
+ assert pcss("ol#first-ol li:last-child") == ["seventh-li"]
+ assert pcss("ol#first-ol *:last-child") == ["li-div", "seventh-li"]
+ assert pcss("#outer-div:first-child") == ["outer-div"]
+ assert pcss("#outer-div :first-child") == [
+ "name-anchor",
+ "first-li",
+ "li-div",
+ "p-b",
+ "checkbox-fieldset-disabled",
+ "area-href",
+ ]
+ assert pcss("a[href]") == ["tag-anchor", "nofollow-anchor"]
+ assert pcss(":not(*)") == []
+ assert pcss("a:not([href])") == ["name-anchor"]
+ assert pcss("ol :Not(li[class])") == [
+ "first-li",
+ "second-li",
+ "li-div",
+ "fifth-li",
+ "sixth-li",
+ "seventh-li",
+ ]
+ assert pcss(":is(#first-li, #second-li)") == ["first-li", "second-li"]
+ assert pcss("a:is(#name-anchor, #tag-anchor)") == ["name-anchor", "tag-anchor"]
+ assert pcss(":is(.c)") == ["first-ol", "third-li", "fourth-li"]
+ assert pcss("ol.a.b.c > li.c:nth-child(3)") == ["third-li"]
# Invalid characters in XPath element names, should not crash
- assert pcss(r'di\a0 v', r'div\[') == []
- assert pcss(r'[h\a0 ref]', r'[h\]ref]') == []
+ assert pcss(r"di\a0 v", r"div\[") == []
+ assert pcss(r"[h\a0 ref]", r"[h\]ref]") == []
# HTML-specific
- assert pcss(':link', html_only=True) == [
- 'link-href', 'tag-anchor', 'nofollow-anchor', 'area-href']
- assert pcss(':visited', html_only=True) == []
- assert pcss(':enabled', html_only=True) == [
- 'link-href', 'tag-anchor', 'nofollow-anchor',
- 'checkbox-unchecked', 'text-checked', 'checkbox-checked',
- 'area-href']
- assert pcss(':disabled', html_only=True) == [
- 'checkbox-disabled', 'checkbox-disabled-checked', 'fieldset',
- 'checkbox-fieldset-disabled']
- assert pcss(':checked', html_only=True) == [
- 'checkbox-checked', 'checkbox-disabled-checked']
+ assert pcss(":link", html_only=True) == [
+ "link-href",
+ "tag-anchor",
+ "nofollow-anchor",
+ "area-href",
+ ]
+ assert pcss(":visited", html_only=True) == []
+ assert pcss(":enabled", html_only=True) == [
+ "link-href",
+ "tag-anchor",
+ "nofollow-anchor",
+ "checkbox-unchecked",
+ "text-checked",
+ "checkbox-checked",
+ "area-href",
+ ]
+ assert pcss(":disabled", html_only=True) == [
+ "checkbox-disabled",
+ "checkbox-disabled-checked",
+ "fieldset",
+ "checkbox-fieldset-disabled",
+ ]
+ assert pcss(":checked", html_only=True) == [
+ "checkbox-checked",
+ "checkbox-disabled-checked",
+ ]
def test_select_shakespeare(self):
document = html.document_fromstring(HTML_SHAKESPEARE)
- body = document.xpath('//body')[0]
+ body = document.xpath("//body")[0]
css_to_xpath = GenericTranslator().css_to_xpath
try:
@@ -927,66 +925,67 @@ def count(selector):
## Changed from original; probably because I'm only
## searching the body.
- #assert count('*') == 252
- assert count('*') == 246
- assert count('div:contains(CELIA)') == 26
- assert count('div:only-child') == 22 # ?
- assert count('div:nth-child(even)') == 106
- assert count('div:nth-child(2n)') == 106
- assert count('div:nth-child(odd)') == 137
- assert count('div:nth-child(2n+1)') == 137
- assert count('div:nth-child(n)') == 243
- assert count('div:last-child') == 53
- assert count('div:first-child') == 51
- assert count('div > div') == 242
- assert count('div + div') == 190
- assert count('div ~ div') == 190
- assert count('body') == 1
- assert count('body div') == 243
- assert count('div') == 243
- assert count('div div') == 242
- assert count('div div div') == 241
- assert count('div, div, div') == 243
- assert count('div, a, span') == 243
- assert count('.dialog') == 51
- assert count('div.dialog') == 51
- assert count('div .dialog') == 51
- assert count('div.character, div.dialog') == 99
- assert count('div.direction.dialog') == 0
- assert count('div.dialog.direction') == 0
- assert count('div.dialog.scene') == 1
- assert count('div.scene.scene') == 1
- assert count('div.scene .scene') == 0
- assert count('div.direction .dialog ') == 0
- assert count('div .dialog .direction') == 4
- assert count('div.dialog .dialog .direction') == 4
- assert count('#speech5') == 1
- assert count('div#speech5') == 1
- assert count('div #speech5') == 1
- assert count('div.scene div.dialog') == 49
- assert count('div#scene1 div.dialog div') == 142
- assert count('#scene1 #speech1') == 1
- assert count('div[class]') == 103
- assert count('div[class=dialog]') == 50
- assert count('div[class^=dia]') == 51
- assert count('div[class$=log]') == 50
- assert count('div[class*=sce]') == 1
- assert count('div[class|=dialog]') == 50 # ? Seems right
- assert count('div[class!=madeup]') == 243 # ? Seems right
- assert count('div[class~=dialog]') == 51 # ? Seems right
- assert count(':scope > div') == 1
- assert count(':scope > div > div[class=dialog]') == 1
- assert count(':scope > div div') == 242
-
-OPERATOR_PRECEDENCE_IDS = '''
+ # assert count('*') == 252
+ assert count("*") == 246
+ assert count("div:contains(CELIA)") == 26
+ assert count("div:only-child") == 22 # ?
+ assert count("div:nth-child(even)") == 106
+ assert count("div:nth-child(2n)") == 106
+ assert count("div:nth-child(odd)") == 137
+ assert count("div:nth-child(2n+1)") == 137
+ assert count("div:nth-child(n)") == 243
+ assert count("div:last-child") == 53
+ assert count("div:first-child") == 51
+ assert count("div > div") == 242
+ assert count("div + div") == 190
+ assert count("div ~ div") == 190
+ assert count("body") == 1
+ assert count("body div") == 243
+ assert count("div") == 243
+ assert count("div div") == 242
+ assert count("div div div") == 241
+ assert count("div, div, div") == 243
+ assert count("div, a, span") == 243
+ assert count(".dialog") == 51
+ assert count("div.dialog") == 51
+ assert count("div .dialog") == 51
+ assert count("div.character, div.dialog") == 99
+ assert count("div.direction.dialog") == 0
+ assert count("div.dialog.direction") == 0
+ assert count("div.dialog.scene") == 1
+ assert count("div.scene.scene") == 1
+ assert count("div.scene .scene") == 0
+ assert count("div.direction .dialog ") == 0
+ assert count("div .dialog .direction") == 4
+ assert count("div.dialog .dialog .direction") == 4
+ assert count("#speech5") == 1
+ assert count("div#speech5") == 1
+ assert count("div #speech5") == 1
+ assert count("div.scene div.dialog") == 49
+ assert count("div#scene1 div.dialog div") == 142
+ assert count("#scene1 #speech1") == 1
+ assert count("div[class]") == 103
+ assert count("div[class=dialog]") == 50
+ assert count("div[class^=dia]") == 51
+ assert count("div[class$=log]") == 50
+ assert count("div[class*=sce]") == 1
+ assert count("div[class|=dialog]") == 50 # ? Seems right
+ assert count("div[class!=madeup]") == 243 # ? Seems right
+ assert count("div[class~=dialog]") == 51 # ? Seems right
+ assert count(":scope > div") == 1
+ assert count(":scope > div > div[class=dialog]") == 1
+ assert count(":scope > div div") == 242
+
+
+OPERATOR_PRECEDENCE_IDS = """
-'''
+"""
-XMLLANG_IDS = '''
+XMLLANG_IDS = """
a
b
@@ -998,9 +997,9 @@ def count(selector):
-'''
+"""
-HTML_IDS = '''
+HTML_IDS = """
@@ -1049,10 +1048,10 @@ def count(selector):
-'''
+"""
-HTML_SHAKESPEARE = '''
+HTML_SHAKESPEARE = """
@@ -1361,8 +1360,8 @@ def count(selector):