Skip to content

Commit

Permalink
re-add support for older lxml versions
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-genson committed Apr 4, 2024
1 parent 699c858 commit 3a00899
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
19 changes: 16 additions & 3 deletions extruct/xmldom.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@
from lxml.etree import ElementBase, XPath, _ElementUnicodeResult, tostring
from lxml.html import HtmlElementClassLookup, HTMLParser

try:
from lxml.etree import _ElementStringResult
except ImportError:

class _ElementStringResult(bytes): # type: ignore[no-redef]
"""
_ElementStringResult is removed in lxml >= 5.1.0,
so we define it here for compatibility.
"""

def getparent(self):
return self._parent # type: ignore[attr-defined]


class DomElementUnicodeResult:
CDATA_SECTION_NODE = Node.CDATA_SECTION_NODE
Expand Down Expand Up @@ -41,7 +54,7 @@ def lxmlDomNodeType(node):
if isinstance(node, ElementBase):
return Node.ELEMENT_NODE

elif isinstance(node, _ElementUnicodeResult):
elif isinstance(node, (_ElementStringResult, _ElementUnicodeResult)):
if node.is_attribute:
return Node.ATTRIBUTE_NODE
else:
Expand Down Expand Up @@ -110,7 +123,7 @@ def childNodes_xpath(self):
if isinstance(n, ElementBase):
yield n

elif isinstance(n, _ElementUnicodeResult):
elif isinstance(n, (_ElementStringResult, _ElementUnicodeResult)):

if isinstance(n, _ElementUnicodeResult):
n = DomElementUnicodeResult(n)
Expand All @@ -136,7 +149,7 @@ def getElementById(self, i):

@property
def data(self):
if isinstance(self, _ElementUnicodeResult):
if isinstance(self, (_ElementStringResult, _ElementUnicodeResult)):
return self
else:
raise RuntimeError
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# project requirements, install them using following command:
# pip install -r requirements.txt
lxml[html_clean]>=5.2.0
lxml[html_clean]
requests
rdflib>=6.0.0; python_version>="3.7"
rdflib<6.0.0; python_version<"3.7"
Expand Down

0 comments on commit 3a00899

Please sign in to comment.