mirror of
https://github.com/yt-dlp/yt-dlp
synced 2024-12-27 21:59:17 +01:00
[core] utils: fix get_element_by_*
This commit is contained in:
parent
354cb4026c
commit
d9195b5133
2 changed files with 21 additions and 1 deletions
|
@ -1769,6 +1769,10 @@ Line 1
|
||||||
<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
|
||||||
|
|
||||||
|
@ -1780,6 +1784,10 @@ Line 1
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
|
@ -1851,6 +1859,11 @@ Line 1
|
||||||
GET_ELEMENT_BY_TAG_RES_INNERSPAN_HTML = GET_ELEMENT_BY_TAG_TEST_STRING.strip()[78:119]
|
GET_ELEMENT_BY_TAG_RES_INNERSPAN_HTML = GET_ELEMENT_BY_TAG_TEST_STRING.strip()[78:119]
|
||||||
GET_ELEMENT_BY_TAG_RES_INNERSPAN_TEXT = GET_ELEMENT_BY_TAG_RES_INNERSPAN_HTML[6:-7]
|
GET_ELEMENT_BY_TAG_RES_INNERSPAN_TEXT = GET_ELEMENT_BY_TAG_RES_INNERSPAN_HTML[6:-7]
|
||||||
|
|
||||||
|
|
||||||
|
GET_ELEMENT_BY_TAG_TEST_STRING_UPPERCASE = '''
|
||||||
|
<SPAN id="foo">nice</SPAN>
|
||||||
|
'''
|
||||||
|
|
||||||
def test_get_element_text_and_html_by_tag(self):
|
def test_get_element_text_and_html_by_tag(self):
|
||||||
html = self.GET_ELEMENT_BY_TAG_TEST_STRING
|
html = self.GET_ELEMENT_BY_TAG_TEST_STRING
|
||||||
|
|
||||||
|
@ -1860,8 +1873,15 @@ Line 1
|
||||||
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.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)
|
||||||
|
|
||||||
|
html = self.GET_ELEMENT_BY_TAG_TEST_STRING_UPPERCASE
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
get_element_text_and_html_by_tag('SPAN', html),
|
||||||
|
('nice', html.strip()), html)
|
||||||
|
|
||||||
def test_iri_to_uri(self):
|
def test_iri_to_uri(self):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
iri_to_uri('https://www.google.com/search?q=foo&ie=utf-8&oe=utf-8&client=firefox-b'),
|
iri_to_uri('https://www.google.com/search?q=foo&ie=utf-8&oe=utf-8&client=firefox-b'),
|
||||||
|
|
|
@ -442,7 +442,7 @@ def get_element_text_and_html_by_tag(tag, html):
|
||||||
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