mirror of
https://github.com/ytdl-org/youtube-dl
synced 2024-12-16 14:36:47 +00:00
[utils] Correctly match class names in get_element[s]_by_class()
* reproduce CSS .classname behaviour ("bar" matches "bar", "foo bar baz", etc) * add tests
This commit is contained in:
parent
01b80a0802
commit
13b0e81f17
@ -1603,11 +1603,14 @@ Line 1
|
||||
|
||||
def test_get_element_by_class(self):
|
||||
html = '''
|
||||
<span class="foo bar">nice</span>
|
||||
<span class="foo bar baz-bam">nice</span>
|
||||
'''
|
||||
|
||||
self.assertEqual(get_element_by_class('foo', html), 'nice')
|
||||
self.assertEqual(get_element_by_class('bar', html), 'nice')
|
||||
self.assertEqual(get_element_by_class('no-such-class', html), None)
|
||||
self.assertEqual(get_element_by_class('baz', html), None)
|
||||
self.assertEqual(get_element_by_class('bam', html), None)
|
||||
|
||||
def test_get_element_by_attribute(self):
|
||||
html = '''
|
||||
@ -1626,10 +1629,13 @@ Line 1
|
||||
|
||||
def test_get_elements_by_class(self):
|
||||
html = '''
|
||||
<span class="foo bar">nice</span><span class="foo bar">also nice</span>
|
||||
<span class="foo bar baz-bam">nice</span><span class="foo bar">also nice</span>
|
||||
'''
|
||||
|
||||
self.assertEqual(get_elements_by_class('foo', html), ['nice', 'also nice'])
|
||||
self.assertEqual(get_elements_by_class('bar', html), ['nice', 'also nice'])
|
||||
self.assertEqual(get_elements_by_class('baz', html), [])
|
||||
self.assertEqual(get_elements_by_class('bam', html), [])
|
||||
self.assertEqual(get_elements_by_class('no-such-class', html), [])
|
||||
|
||||
def test_get_elements_by_attribute(self):
|
||||
|
@ -1960,7 +1960,7 @@ def get_element_by_attribute(attribute, value, html, escape_value=True):
|
||||
def get_elements_by_class(class_name, html):
|
||||
"""Return the content of all tags with the specified class in the passed HTML document as a list"""
|
||||
return get_elements_by_attribute(
|
||||
'class', r'[^\'"]*\b%s\b[^\'"]*' % re.escape(class_name),
|
||||
'class', r'[^\'"]*(?<!-)\b%s\b(?!-)[^\'"]*' % re.escape(class_name),
|
||||
html, escape_value=False)
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user