pull/11736/merge
Allen 2 days ago committed by GitHub
commit 47a28a0b4c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -1787,6 +1787,10 @@ Line 1
<div itemprop="author" itemscope>foo</div>
'''
GET_ELEMENT_BY_ATTRIBUTE_TEST_STRING_UPPERCASE = '''
<DIV itemprop="author" itemscope>foo</DIV>
'''
def test_get_element_by_attribute(self):
html = self.GET_ELEMENT_BY_CLASS_TEST_STRING
@ -1798,6 +1802,10 @@ Line 1
self.assertEqual(get_element_by_attribute('itemprop', 'author', html), 'foo')
html = self.GET_ELEMENT_BY_ATTRIBUTE_TEST_STRING_UPPERCASE
self.assertEqual(get_element_by_attribute('itemprop', 'author', html), 'foo')
def test_get_element_html_by_attribute(self):
html = self.GET_ELEMENT_BY_CLASS_TEST_STRING
@ -1856,7 +1864,7 @@ Line 1
random text lorem ipsum</p>
<div>
this should be returned
<span>this should also be returned</span>
<SPAN>this should also be returned</SPAN>
<div>
this should also be returned
</div>
@ -1878,6 +1886,10 @@ Line 1
self.assertEqual(
get_element_text_and_html_by_tag('span', html),
(self.GET_ELEMENT_BY_TAG_RES_INNERSPAN_TEXT, self.GET_ELEMENT_BY_TAG_RES_INNERSPAN_HTML))
self.assertEqual(
get_element_text_and_html_by_tag('SPAN', html),
(self.GET_ELEMENT_BY_TAG_RES_INNERSPAN_TEXT, self.GET_ELEMENT_BY_TAG_RES_INNERSPAN_HTML))
self.assertRaises(compat_HTMLParseError, get_element_text_and_html_by_tag, 'article', html)
def test_iri_to_uri(self):

@ -432,10 +432,14 @@ def get_element_text_and_html_by_tag(tag, html):
return its' content (text) and the whole element (html)
"""
def find_or_raise(haystack, needle, exc):
try:
with contextlib.suppress(ValueError):
return haystack.index(needle)
except ValueError:
raise exc
with contextlib.suppress(ValueError):
return haystack.index(needle.upper())
raise exc
closing_tag = f'</{tag}>'
whole_start = find_or_raise(
html, f'<{tag}', compat_HTMLParseError(f'opening {tag} tag not found'))
@ -444,7 +448,7 @@ def get_element_text_and_html_by_tag(tag, html):
content_start += whole_start + 1
with HTMLBreakOnClosingTagParser() as parser:
parser.feed(html[whole_start:content_start])
if not parser.tagstack or parser.tagstack[0] != tag:
if not parser.tagstack or parser.tagstack[0] != tag.lower():
raise compat_HTMLParseError(f'parser did not match opening {tag} tag')
offset = content_start
while offset < len(html):

Loading…
Cancel
Save