zmc
2023-08-08 e792e9a60d958b93aef96050644f369feb25d61b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
U
¡ý°dCã@svddlZddlZddlmZddlmZmZmZddlm    Z    m
Z
e
rPddl m Z ej je
ddGd    d
„d
e    ƒƒZdS) éN)Ú    MagicMock)ÚCSSÚ BeautifulSoupÚ    ResultSeté)ÚSoupTestÚSOUP_SIEVE_PRESENT)ÚSelectorSyntaxErrorzSoup Sieve not installed)Úreasonc@seZdZdZdZdd„Zdd„ZeZdd„Zd    d
„Z    d d „Z
d d„Z dd„Z dd„Z dd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd „Zd!d"„Zd#d$„Zd%d&„Zd'd(„Zd)d*„Zd+d,„Zd-d.„Zd/d0„Zd1d2„Zd3d4„Zd5d6„Zd7d8„Z d9d:„Z!d;d<„Z"d=d>„Z#d?d@„Z$dAdB„Z%dCdD„Z&dEdF„Z'dGdH„Z(dIdJ„Z)dKdL„Z*dMdN„Z+dOdP„Z,dQdR„Z-dSdT„Z.dUdV„Z/dWdX„Z0dYdZ„Z1d[d\„Z2d]d^„Z3d_d`„Z4dadb„Z5dcdd„Z6dedf„Z7dgdh„Z8didj„Z9dkdl„Z:dmdn„Z;dodp„Z<dqdr„Z=dsdt„Z>dudv„Z?dwdx„Z@dydz„ZAd{d|„ZBd}d~„ZCdd€„ZDdS)‚ÚTestCSSSelectorszÞTest basic CSS selector functionality.
 
    This functionality is implemented in soupsieve, which has a much
    more comprehensive test suite, so this is basically an extra check
    that soupsieve works as expected.
    aþ
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>The title</title>
<link rel="stylesheet" href="blah.css" type="text/css" id="l1">
</head>
<body>
<custom-dashed-tag class="dashed" id="dash1">Hello there.</custom-dashed-tag>
<div id="main" class="fancy">
<div id="inner">
<h1 id="header1">An H1</h1>
<p>Some text</p>
<p class="onep" id="p1">Some more text</p>
<h2 id="header2">An H2</h2>
<p class="class1 class2 class3" id="pmulti">Another</p>
<a href="http://bob.example.org/" rel="friend met" id="bob">Bob</a>
<h2 id="header3">Another H2</h2>
<a id="me" href="http://simonwillison.net/" rel="me">me</a>
<span class="s1">
<a href="#" id="s1a1">span1a1</a>
<a href="#" id="s1a2">span1a2 <span id="s1a2s1">test</span></a>
<span class="span2">
<a href="#" id="s2a1">span2a1</a>
</span>
<span class="span3"></span>
<custom-dashed-tag class="dashed" id="dash2"/>
<div data-tag="dashedvalue" id="data1"/>
</span>
</div>
<x id="xid">
<z id="zida"/>
<z id="zidab"/>
<z id="zidac"/>
</x>
<y id="yid">
<z id="zidb"/>
</y>
<p lang="en" id="lang-en">English</p>
<p lang="en-gb" id="lang-en-gb">English UK</p>
<p lang="en-us" id="lang-en-us">English US</p>
<p lang="fr" id="lang-fr">French</p>
</div>
 
<div id="footer">
</div>
cCst|jdƒ|_dS)Nú html.parser)rÚHTMLÚsoup©Úself©rúId:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\bs4/tests/test_css.pyÚ setup_methodNszTestCSSSelectors.setup_methodcKsf|jj|f|Ž}t|tƒst‚dd„|Dƒ}| ¡| ¡||ksbtd|d |¡d |¡fƒ‚dS)NcSsg|] }|d‘qS©Úidr)Ú.0ÚelrrrÚ
<listcomp>Tsz3TestCSSSelectors.assert_selects.<locals>.<listcomp>z$Selector %s, expected [%s], got [%s]z, )rÚselectÚ
isinstancerÚAssertionErrorÚsortÚjoin)rÚselectorÚ expected_idsÚkwargsÚresultsZel_idsrrrÚassert_selectsQs ÿzTestCSSSelectors.assert_selectscGs|D]\}}| ||¡qdS)N©r")rÚtestsrrrrrÚassert_select_multiple]s z'TestCSSSelectors.assert_select_multiplecCsb|jj d¡}|j |¡}t|ƒdks*t‚|D]}|jdks.t‚q.|j |¡}d|dks^t‚dS©NÚdivéÚmainr)rÚcssÚcompilerÚlenrÚnameÚ
select_one)rÚselÚelsr'rrrrÚtest_precompiledas  z!TestCSSSelectors.test_precompiledcCsF|j d¡}t|ƒdkst‚|djdks.t‚|djdgksBt‚dS)NÚtitlerrz    The title)rrr,rr-Úcontents©rr0rrrÚtest_one_tag_onels z!TestCSSSelectors.test_one_tag_onecCsT|j d¡}t|ƒdkst‚|D]}|jdks t‚q |j d¡}d|dksPt‚dSr&)rrr,rr-r.)rr0r'rrrrÚtest_one_tag_manyrs   z"TestCSSSelectors.test_one_tag_manycCs|j d¡}d|kst‚dS)NZnonexistenttag)rr.r)rÚmatchrrrÚ(test_select_one_returns_none_if_no_match{s z9TestCSSSelectors.test_select_one_returns_none_if_no_matchcCs |j d¡}| dddg¡dS)Nzdiv divÚinnerÚdata1)rrr"r4rrrÚtest_tag_in_tag_one€s z$TestCSSSelectors.test_tag_in_tag_onecCs"dD]}| |ddddg¡qdS)N)úhtml divú html body divúbody divr:r)r9Úfooterr#©rrrrrÚtest_tag_in_tag_many„sz%TestCSSSelectors.test_tag_in_tag_manycCsB|jddgdd|jdddgdd|jdd    ddd
gd ddS) Nr<r)r)Úlimitr=r9ér>r:r?é
r#rrrrÚ
test_limit‰s
ÿzTestCSSSelectors.test_limitcCst|j d¡ƒdkst‚dS)NÚdelr©r,rrrrrrrÚtest_tag_no_matchsz"TestCSSSelectors.test_tag_no_matchc    Cs&t t¡|j d¡W5QRXdS)Nztag%t©ÚpytestÚraisesr    rrrrrrÚtest_invalid_tag’s z!TestCSSSelectors.test_invalid_tagcCs| dddg¡dS)Núcustom-dashed-tagÚdash1Údash2r#rrrrÚtest_select_dashed_tag_ids–sz+TestCSSSelectors.test_select_dashed_tag_idscCs6|j d¡}|djdkst‚|dddks2t‚dS)Nzcustom-dashed-tag[id="dash2"]rrMrrO)rrr-r)rZdashedrrrÚtest_select_dashed_by_id™s z)TestCSSSelectors.test_select_dashed_by_idcCs|j d¡djdkst‚dS)Nzbody > custom-dashed-tagrz Hello there.)rrÚtextrrrrrÚtest_dashed_tag_textžsz%TestCSSSelectors.test_dashed_tag_textcCs |j d¡|j d¡kst‚dS)NrM)rrÚfind_allrrrrrÚ#test_select_dashed_matches_find_all¡sz4TestCSSSelectors.test_select_dashed_matches_find_allcCs| ddgfdddgf¡dS)NÚh1Úheader1Úh2Úheader2Úheader3©r%rrrrÚtest_header_tags¤s
þz!TestCSSSelectors.test_header_tagscCsRdD]H}|j |¡}t|ƒdks$t‚|djdks6t‚|dddgkst‚qdS)N)z.onepzp.onepz html p.oneprrÚpÚclassÚonep)rrr,rr-)rrr0rrrÚtest_class_oneªs
 zTestCSSSelectors.test_class_onecCs |j d¡}t|ƒdkst‚dS)Nzdiv.onepr©rrr,rr4rrrÚtest_class_mismatched_tag±s z*TestCSSSelectors.test_class_mismatched_tagcCsdD]}| |dg¡qdS)N)z    div#innerz#innerz div div#innerr9r#r@rrrÚ test_one_idµszTestCSSSelectors.test_one_idcCs |j d¡}t|ƒdkst‚dS)Nz #doesnotexistrrar4rrrÚ test_bad_id¹s zTestCSSSelectors.test_bad_idcCs`|j d¡}t|ƒdkst‚|D]}|jdks t‚q |dddgksJt‚|d d¡r\t‚dS)Nz div#inner pér]rr^r_r)rrr,rr-Zhas_attr)rr0rrrrÚtest_items_in_id½s  z!TestCSSSelectors.test_items_in_idcCs&dD]}t|j |¡ƒdkst‚qdS)N)z div#main delzdiv#main div.oopsz div div#mainrrGr@rrrÚtest_a_bunch_of_emptysÅsz'TestCSSSelectors.test_a_bunch_of_emptyscCsdD]}| |dg¡qdS)N)z.class1zp.class1z.class2zp.class2z.class3zp.class3z html p.class2zdiv#inner .class2Úpmultir#r@rrrÚtest_multi_class_supportÉsz)TestCSSSelectors.test_multi_class_supportcCsdD]}| |dg¡qdS)N)z.class1.class3z.class3.class2z.class1.class2.class3rhr#r@rrrÚtest_multi_class_selectionÎsz+TestCSSSelectors.test_multi_class_selectioncCs"| dddg¡| ddg¡dS)Nz.s1 > aÚs1a1Ús1a2z .s1 > a spanÚs1a2s1r#rrrrÚtest_child_selectorÓsz$TestCSSSelectors.test_child_selectorcCs| ddg¡dS)Nz.s1 > a#s1a2 spanrmr#rrrrÚtest_child_selector_id×sz'TestCSSSelectors.test_child_selector_idcCst| ddgfddgfddgfddgfddgfddgfd    dgfd
gfd dgfd dgfd dgfdgfdgfdgf¡dS)Nzp[class="onep"]Úp1z
p[id="p1"]z[class="onep"]z    [id="p1"]zlink[rel="stylesheet"]Úl1zlink[type="text/css"]zlink[href="blah.css"]zlink[href="no-blah.css"]z[rel="stylesheet"]z[type="text/css"]z[href="blah.css"]z[href="no-blah.css"]zp[href="no-blah.css"]r[rrrrÚtest_attribute_equalsÚs òz&TestCSSSelectors.test_attribute_equalsc Cs\| ddgfddgfddgfddgfddgfddgfdd    gfd
d    gfd d    gfd d    gf¡
dS) Nzp[class~="class1"]rhzp[class~="class2"]zp[class~="class3"]z[class~="class1"]z[class~="class2"]z[class~="class3"]za[rel~="friend"]Úbobz a[rel~="met"]z[rel~="friend"]z [rel~="met"]r[rrrrÚtest_attribute_tildeìsöz%TestCSSSelectors.test_attribute_tildecCsv| ddgfddgfdgfdgfdgfddgfdd    d
gfd d    d
gfd d dgfdd
dgfddgfdd
gfddgf¡ dS)Nz[rel^="style"]rqzlink[rel^="style"]znotlink[rel^="notstyle"]z[rel^="notstyle"]zlink[rel^="notstyle"]zlink[href^="bla"]za[href^="http://"]rsÚmez[href^="http://"]z    [id^="p"]rhrpz    [id^="m"]r)z div[id^="m"]z
a[id^="m"]zdiv[data-tag^="dashed"]r:r[rrrrÚtest_attribute_startswithús
 
 
 
óz*TestCSSSelectors.test_attribute_startswithcCsH| ddgfddgfddgfdddddd    d
d d gfd dgfdgf¡dS)Nz[href$=".css"]rqzlink[href$=".css"]z link[id$="1"]z    [id$="1"]r:rprWrkÚs2a1rmrNz div[id$="1"]z[id$="noending"]r[rrrrÚtest_attribute_endswith súz(TestCSSSelectors.test_attribute_endswithcCsÐ| ddgfddgfdgfdgfdgfddgfdd    d
gfd d d gfddgfdd
gfddgfddgfddgfdddd ddddddg    fddgfdgfdd    d
dgfdd    d
gfd dgfd!dd"gfd#d"gfd$dgf¡dS)%Nz[rel*="style"]rqzlink[rel*="style"]znotlink[rel*="notstyle"]z[rel*="notstyle"]zlink[rel*="notstyle"]zlink[href*="bla"]z[href*="http://"]rsruz    [id*="p"]rhrpz div[id*="m"]r)z
a[id*="m"]z[href*=".css"]zlink[href*=".css"]z link[id*="1"]z    [id*="1"]r:rWrkrlrwrmrNz div[id*="1"]z[id*="noending"]z [href*="."]z a[href*="."]zlink[href*="."]z div[id*="n"]r9z div[id*="nn"]zdiv[data-tag*="edval"]r[rrrrÚtest_attribute_containss0
 
 
 
çz(TestCSSSelectors.test_attribute_containscCs2| ddddgfddddgfddgfdgf¡dS)    Nz p[lang|="en"]úlang-enú
lang-en-gbú
lang-en-usz [lang|="en"]z p[lang|="fr"]úlang-frz p[lang|="gb"]r[rrrrÚtest_attribute_exact_or_hypen2s   üz.TestCSSSelectors.test_attribute_exact_or_hypenc CsV| ddddgfddgfdddgfddd    d
d gfd d dgfdgfdgfddgf¡dS)Nz[rel]rqrsruz    link[rel]za[rel]z[lang]rzr{r|r}zp[class]rprhz[blah]zp[blah]z div[data-tag]r:r[rrrrÚtest_attribute_exists:s 
 
øz&TestCSSSelectors.test_attribute_existscCs,d}t|dƒ}| d¡\}d|jks(t‚dS)Nz]<div style="display: wrong">nope</div>
        <div style="display: right">yes</div>
        r zdiv[style="display: right"]Úyes)rrÚstringr)rÚhtmlrZchosenrrrÚ"test_quoted_space_in_selector_nameFs
 z3TestCSSSelectors.test_quoted_space_in_selector_namec    CsHt t¡|j d¡W5QRXt t¡|j d¡W5QRXdS)Nza:no-such-pseudoclassza:nth-of-type(a))rJrKÚNotImplementedErrorrrr    rrrrÚtest_unsupported_pseudoclassNs  z-TestCSSSelectors.test_unsupported_pseudoclasscCs˜|j d¡}t|ƒdkst‚|djdks.t‚|j d¡}t|ƒdksJt‚|djdks\t‚|j d¡}t|ƒdksxt‚|j d¡}t|ƒdks”t‚dS)    Nzdiv#inner p:nth-of-type(1)rrú    Some textzdiv#inner p:nth-of-type(3)ZAnotherzdiv#inner p:nth-of-type(4)zdiv p:nth-of-type(0)©rrr,rrr4rrrÚtest_nth_of_typeUs    z!TestCSSSelectors.test_nth_of_typecCs2|j d¡}t|ƒdkst‚|djdks.t‚dS)Nzdiv#inner > p:nth-of-type(1)rrr†r‡r4rrrÚ"test_nth_of_type_direct_descendanths z3TestCSSSelectors.test_nth_of_type_direct_descendantcCs| ddg¡dS)Nz#inner > p:nth-of-type(2)rpr#rrrrÚ"test_id_child_selector_nth_of_typemsz3TestCSSSelectors.test_id_child_selector_nth_of_typecCs.|jjddd}| d¡}| |ddg¡dS)Nr'r)rr9r:)rÚfindrZassert_selects_ids)rr9ÚselectedrrrÚtest_select_on_elementps
z'TestCSSSelectors.test_select_on_elementcCs| ddg¡| dg¡dS)Nz .fancy #innerr9z.normal #innerr#rrrrÚtest_overspecified_child_idysz,TestCSSSelectors.test_overspecified_child_idcCsB| ddg¡| ddg¡| ddg¡g|j d¡ks>t‚dS)Nz#p1 + h2rYz #p1 + h2 + prhz#p1 + #header2 + .class1z#p1 + p©r"rrrrrrrÚtest_adjacent_sibling_selector}sz/TestCSSSelectors.test_adjacent_sibling_selectorcCsR| dddg¡| ddg¡| ddg¡| ddg¡g|j d¡ksNt‚dS)    Nz#p1 ~ h2rYrZz#p1 ~ #header2z #p1 ~ h2 + aruz#p1 ~ h2 + [rel="me"]z #inner ~ h2rrrrrÚtest_general_sibling_selectorƒs
z.TestCSSSelectors.test_general_sibling_selectorc    Cs&t t¡|j d¡W5QRXdS)Nzh1 >rIrrrrÚtest_dangling_combinatorŠs z)TestCSSSelectors.test_dangling_combinatorcCs| ddddg¡dS)Nz p[lang] ~ pr{r|r}r#rrrrÚ2test_sibling_combinator_wont_select_same_tag_twiceŽszCTestCSSSelectors.test_sibling_combinator_wont_select_same_tag_twicecCs| dddg¡dS)Nzx, yÚxidÚyidr#rrrrÚtest_multiple_select’sz%TestCSSSelectors.test_multiple_selectcCs| dddg¡dS)Nzx,yr”r•r#rrrrÚ"test_multiple_select_with_no_space•sz3TestCSSSelectors.test_multiple_select_with_no_spacecCs| dddg¡dS)Nzx,    yr”r•r#rrrrÚ$test_multiple_select_with_more_space˜sz5TestCSSSelectors.test_multiple_select_with_more_spacecCs| ddg¡dS)Nzx, xr”r#rrrrÚtest_multiple_select_duplicated›sz0TestCSSSelectors.test_multiple_select_duplicatedcCs| dddg¡dS)Nzx, y ~ p[lang=fr]r”r}r#rrrrÚtest_multiple_select_siblingžsz-TestCSSSelectors.test_multiple_select_siblingcCs| dddg¡dS)Nzx, y > zr”Úzidbr#rrrrÚ.test_multiple_select_tag_and_direct_descendant¡sz?TestCSSSelectors.test_multiple_select_tag_and_direct_descendantc    Cs| dddddddg¡dS)Nz div > x, y, zr”r•Úzidar›ÚzidabÚzidacr#rrrrÚ/test_multiple_select_direct_descendant_and_tags¤sz@TestCSSSelectors.test_multiple_select_direct_descendant_and_tagsc    Cs| dddddddg¡dS)Nz div x,y,  zr”r•rr›ržrŸr#rrrrÚ(test_multiple_select_indirect_descendant§sz9TestCSSSelectors.test_multiple_select_indirect_descendantc    CsHt t¡|j d¡W5QRXt t¡|j d¡W5QRXdS)Nz,x, yzx,,yrIrrrrÚtest_invalid_multiple_selectªs  z-TestCSSSelectors.test_invalid_multiple_selectcCs| dddg¡dS)Nzp[lang=en], p[lang=en-gb]rzr{r#rrrrÚtest_multiple_select_attrs°sz+TestCSSSelectors.test_multiple_select_attrscCs| ddddg¡dS)Nz*x, y > z[id=zida], z[id=zidab], z[id=zidb]r”r›ržr#rrrrÚtest_multiple_select_ids³sz)TestCSSSelectors.test_multiple_select_idscCs| dddg¡dS)Nzbody > div > x, y > zr”r›r#rrrrÚtest_multiple_select_nested¶sz,TestCSSSelectors.test_multiple_select_nestedcCsNd}t|dƒ}| d¡}dt|ƒks(t‚|jddgdD]}||ks8t‚q8dS)Nz3<div class="c1"/><div class="c2"/><div class="c1"/>r z.c1, .c2reZc1Úc2)Úclass_)rrr,rrT)rZmarkuprrŒÚelementrrrÚtest_select_duplicate_elements¹s 
 
z/TestCSSSelectors.test_select_duplicate_elementscCs8|jjddd}|j d¡}||jjdddks4t‚dS)Nr'r9rú div[id=main]r))rr‹r*Úclosestr)rr9r«rrrÚ test_closestÇs zTestCSSSelectors.test_closestcCsL|jjddd}|jjddd}|j d¡dks4t‚|j d¡dksHt‚dS)Nr'r9rr)rªFT)rr‹r*r7r)rr9r)rrrÚ
test_matchÌszTestCSSSelectors.test_matchcCsJ|jj d¡}t|tjƒst‚|\}}|ddks6t‚|ddksFt‚dS)NrXrrYrZ)rr*ZiselectrÚtypesÚ GeneratorTyper)rÚgenrYrZrrrÚ test_iselectÒs
zTestCSSSelectors.test_iselectcCsh|jjddd}|j d¡}t|j d¡ƒdks4t‚|j d¡}t|tƒsNt‚|\}|ddksdt‚dS)    Nr'r9rrXrCzh2[id=header3]rrZ)rr‹r*Úfilterr,rrr)rr9r!ÚresultrrrÚ test_filterÙs  zTestCSSSelectors.test_filtercCsH|jjj}|dƒdkst‚|dƒdks*t‚|dƒ|jj d¡ksDt‚dS)Nz.foo#barz
\.foo\#barz()[]{}z \(\)\[\]\{\}z.foo)rr*Úescaper)rÚmrrrÚ test_escapeãs
zTestCSSSelectors.test_escapeN)EÚ__name__Ú
__module__Ú __qualname__Ú__doc__r rr"Z assertSelectr%r1r5r6r8r;rArErHrLrPrQrSrUr\r`rbrcrdrfrgrirjrnrorrrtrvrxryr~rrƒr…rˆr‰rŠrrŽrr‘r’r“r–r—r˜r™ršrœr r¡r¢r£r¤r¥r©r¬r­r±r´r·rrrrr s„1
     
     
r )rJr®Z unittest.mockrZbs4rrrÚrrZ    soupsiever    ÚmarkZskipifr rrrrÚ<module>s