mirror of
https://github.com/yt-dlp/yt-dlp.git
synced 2025-03-09 12:50:23 -05:00
Merge c49aa772cc
into 05c8023a27
This commit is contained in:
commit
c10a888325
2 changed files with 21 additions and 5 deletions
|
@ -1789,6 +1789,10 @@ def test_get_element_html_by_class(self):
|
||||||
<div itemprop="author" itemscope>foo</div>
|
<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):
|
def test_get_element_by_attribute(self):
|
||||||
html = self.GET_ELEMENT_BY_CLASS_TEST_STRING
|
html = self.GET_ELEMENT_BY_CLASS_TEST_STRING
|
||||||
|
|
||||||
|
@ -1800,6 +1804,10 @@ def test_get_element_by_attribute(self):
|
||||||
|
|
||||||
self.assertEqual(get_element_by_attribute('itemprop', 'author', html), 'foo')
|
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):
|
def test_get_element_html_by_attribute(self):
|
||||||
html = self.GET_ELEMENT_BY_CLASS_TEST_STRING
|
html = self.GET_ELEMENT_BY_CLASS_TEST_STRING
|
||||||
|
|
||||||
|
@ -1858,7 +1866,7 @@ def test_get_elements_text_and_html_by_attribute(self):
|
||||||
random text lorem ipsum</p>
|
random text lorem ipsum</p>
|
||||||
<div>
|
<div>
|
||||||
this should be returned
|
this should be returned
|
||||||
<span>this should also be returned</span>
|
<SPAN>this should also be returned</SPAN>
|
||||||
<div>
|
<div>
|
||||||
this should also be returned
|
this should also be returned
|
||||||
</div>
|
</div>
|
||||||
|
@ -1880,6 +1888,10 @@ def test_get_element_text_and_html_by_tag(self):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
get_element_text_and_html_by_tag('span', html),
|
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.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)
|
self.assertRaises(compat_HTMLParseError, get_element_text_and_html_by_tag, 'article', html)
|
||||||
|
|
||||||
def test_iri_to_uri(self):
|
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)
|
return its' content (text) and the whole element (html)
|
||||||
"""
|
"""
|
||||||
def find_or_raise(haystack, needle, exc):
|
def find_or_raise(haystack, needle, exc):
|
||||||
try:
|
with contextlib.suppress(ValueError):
|
||||||
return haystack.index(needle)
|
return haystack.index(needle)
|
||||||
except ValueError:
|
|
||||||
|
with contextlib.suppress(ValueError):
|
||||||
|
return haystack.index(needle.upper())
|
||||||
|
|
||||||
raise exc
|
raise exc
|
||||||
|
|
||||||
closing_tag = f'</{tag}>'
|
closing_tag = f'</{tag}>'
|
||||||
whole_start = find_or_raise(
|
whole_start = find_or_raise(
|
||||||
html, f'<{tag}', compat_HTMLParseError(f'opening {tag} tag not found'))
|
html, f'<{tag}', compat_HTMLParseError(f'opening {tag} tag not found'))
|
||||||
|
@ -444,7 +448,7 @@ def find_or_raise(haystack, needle, exc):
|
||||||
content_start += whole_start + 1
|
content_start += whole_start + 1
|
||||||
with HTMLBreakOnClosingTagParser() as parser:
|
with HTMLBreakOnClosingTagParser() as parser:
|
||||||
parser.feed(html[whole_start:content_start])
|
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')
|
raise compat_HTMLParseError(f'parser did not match opening {tag} tag')
|
||||||
offset = content_start
|
offset = content_start
|
||||||
while offset < len(html):
|
while offset < len(html):
|
||||||
|
|
Loading…
Reference in a new issue