xref: /OK3568_Linux_fs/yocto/poky/bitbake/lib/bs4/testing.py (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun"""Helper classes for tests."""
2*4882a593Smuzhiyun
3*4882a593Smuzhiyun__license__ = "MIT"
4*4882a593Smuzhiyun
5*4882a593Smuzhiyunimport pickle
6*4882a593Smuzhiyunimport copy
7*4882a593Smuzhiyunimport unittest
8*4882a593Smuzhiyunfrom unittest import TestCase
9*4882a593Smuzhiyunfrom bs4 import BeautifulSoup
10*4882a593Smuzhiyunfrom bs4.element import (
11*4882a593Smuzhiyun    CharsetMetaAttributeValue,
12*4882a593Smuzhiyun    Comment,
13*4882a593Smuzhiyun    ContentMetaAttributeValue,
14*4882a593Smuzhiyun    Doctype,
15*4882a593Smuzhiyun    SoupStrainer,
16*4882a593Smuzhiyun)
17*4882a593Smuzhiyun
18*4882a593Smuzhiyunfrom bs4.builder._htmlparser import HTMLParserTreeBuilder
19*4882a593Smuzhiyundefault_builder = HTMLParserTreeBuilder
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun
22*4882a593Smuzhiyunclass SoupTest(unittest.TestCase):
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun    @property
25*4882a593Smuzhiyun    def default_builder(self):
26*4882a593Smuzhiyun        return default_builder()
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun    def soup(self, markup, **kwargs):
29*4882a593Smuzhiyun        """Build a Beautiful Soup object from markup."""
30*4882a593Smuzhiyun        builder = kwargs.pop('builder', self.default_builder)
31*4882a593Smuzhiyun        return BeautifulSoup(markup, builder=builder, **kwargs)
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun    def document_for(self, markup):
34*4882a593Smuzhiyun        """Turn an HTML fragment into a document.
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun        The details depend on the builder.
37*4882a593Smuzhiyun        """
38*4882a593Smuzhiyun        return self.default_builder.test_fragment_to_document(markup)
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun    def assertSoupEquals(self, to_parse, compare_parsed_to=None):
41*4882a593Smuzhiyun        builder = self.default_builder
42*4882a593Smuzhiyun        obj = BeautifulSoup(to_parse, builder=builder)
43*4882a593Smuzhiyun        if compare_parsed_to is None:
44*4882a593Smuzhiyun            compare_parsed_to = to_parse
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun        self.assertEqual(obj.decode(), self.document_for(compare_parsed_to))
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun    def assertConnectedness(self, element):
49*4882a593Smuzhiyun        """Ensure that next_element and previous_element are properly
50*4882a593Smuzhiyun        set for all descendants of the given element.
51*4882a593Smuzhiyun        """
52*4882a593Smuzhiyun        earlier = None
53*4882a593Smuzhiyun        for e in element.descendants:
54*4882a593Smuzhiyun            if earlier:
55*4882a593Smuzhiyun                self.assertEqual(e, earlier.next_element)
56*4882a593Smuzhiyun                self.assertEqual(earlier, e.previous_element)
57*4882a593Smuzhiyun            earlier = e
58*4882a593Smuzhiyun
59*4882a593Smuzhiyunclass HTMLTreeBuilderSmokeTest(SoupTest):
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun    """A basic test of a treebuilder's competence.
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun    Any HTML treebuilder, present or future, should be able to pass
64*4882a593Smuzhiyun    these tests. With invalid markup, there's room for interpretation,
65*4882a593Smuzhiyun    and different parsers can handle it differently. But with the
66*4882a593Smuzhiyun    markup in these tests, there's not much room for interpretation.
67*4882a593Smuzhiyun    """
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun    def test_pickle_and_unpickle_identity(self):
70*4882a593Smuzhiyun        # Pickling a tree, then unpickling it, yields a tree identical
71*4882a593Smuzhiyun        # to the original.
72*4882a593Smuzhiyun        tree = self.soup("<a><b>foo</a>")
73*4882a593Smuzhiyun        dumped = pickle.dumps(tree, 2)
74*4882a593Smuzhiyun        loaded = pickle.loads(dumped)
75*4882a593Smuzhiyun        self.assertEqual(loaded.__class__, BeautifulSoup)
76*4882a593Smuzhiyun        self.assertEqual(loaded.decode(), tree.decode())
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun    def assertDoctypeHandled(self, doctype_fragment):
79*4882a593Smuzhiyun        """Assert that a given doctype string is handled correctly."""
80*4882a593Smuzhiyun        doctype_str, soup = self._document_with_doctype(doctype_fragment)
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun        # Make sure a Doctype object was created.
83*4882a593Smuzhiyun        doctype = soup.contents[0]
84*4882a593Smuzhiyun        self.assertEqual(doctype.__class__, Doctype)
85*4882a593Smuzhiyun        self.assertEqual(doctype, doctype_fragment)
86*4882a593Smuzhiyun        self.assertEqual(str(soup)[:len(doctype_str)], doctype_str)
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun        # Make sure that the doctype was correctly associated with the
89*4882a593Smuzhiyun        # parse tree and that the rest of the document parsed.
90*4882a593Smuzhiyun        self.assertEqual(soup.p.contents[0], 'foo')
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun    def _document_with_doctype(self, doctype_fragment):
93*4882a593Smuzhiyun        """Generate and parse a document with the given doctype."""
94*4882a593Smuzhiyun        doctype = '<!DOCTYPE %s>' % doctype_fragment
95*4882a593Smuzhiyun        markup = doctype + '\n<p>foo</p>'
96*4882a593Smuzhiyun        soup = self.soup(markup)
97*4882a593Smuzhiyun        return doctype, soup
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun    def test_normal_doctypes(self):
100*4882a593Smuzhiyun        """Make sure normal, everyday HTML doctypes are handled correctly."""
101*4882a593Smuzhiyun        self.assertDoctypeHandled("html")
102*4882a593Smuzhiyun        self.assertDoctypeHandled(
103*4882a593Smuzhiyun            'html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"')
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun    def test_empty_doctype(self):
106*4882a593Smuzhiyun        soup = self.soup("<!DOCTYPE>")
107*4882a593Smuzhiyun        doctype = soup.contents[0]
108*4882a593Smuzhiyun        self.assertEqual("", doctype.strip())
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun    def test_public_doctype_with_url(self):
111*4882a593Smuzhiyun        doctype = 'html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"'
112*4882a593Smuzhiyun        self.assertDoctypeHandled(doctype)
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun    def test_system_doctype(self):
115*4882a593Smuzhiyun        self.assertDoctypeHandled('foo SYSTEM "http://www.example.com/"')
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun    def test_namespaced_system_doctype(self):
118*4882a593Smuzhiyun        # We can handle a namespaced doctype with a system ID.
119*4882a593Smuzhiyun        self.assertDoctypeHandled('xsl:stylesheet SYSTEM "htmlent.dtd"')
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun    def test_namespaced_public_doctype(self):
122*4882a593Smuzhiyun        # Test a namespaced doctype with a public id.
123*4882a593Smuzhiyun        self.assertDoctypeHandled('xsl:stylesheet PUBLIC "htmlent.dtd"')
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun    def test_real_xhtml_document(self):
126*4882a593Smuzhiyun        """A real XHTML document should come out more or less the same as it went in."""
127*4882a593Smuzhiyun        markup = b"""<?xml version="1.0" encoding="utf-8"?>
128*4882a593Smuzhiyun<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
129*4882a593Smuzhiyun<html xmlns="http://www.w3.org/1999/xhtml">
130*4882a593Smuzhiyun<head><title>Hello.</title></head>
131*4882a593Smuzhiyun<body>Goodbye.</body>
132*4882a593Smuzhiyun</html>"""
133*4882a593Smuzhiyun        soup = self.soup(markup)
134*4882a593Smuzhiyun        self.assertEqual(
135*4882a593Smuzhiyun            soup.encode("utf-8").replace(b"\n", b""),
136*4882a593Smuzhiyun            markup.replace(b"\n", b""))
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun    def test_processing_instruction(self):
139*4882a593Smuzhiyun        markup = b"""<?PITarget PIContent?>"""
140*4882a593Smuzhiyun        soup = self.soup(markup)
141*4882a593Smuzhiyun        self.assertEqual(markup, soup.encode("utf8"))
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun    def test_deepcopy(self):
144*4882a593Smuzhiyun        """Make sure you can copy the tree builder.
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun        This is important because the builder is part of a
147*4882a593Smuzhiyun        BeautifulSoup object, and we want to be able to copy that.
148*4882a593Smuzhiyun        """
149*4882a593Smuzhiyun        copy.deepcopy(self.default_builder)
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun    def test_p_tag_is_never_empty_element(self):
152*4882a593Smuzhiyun        """A <p> tag is never designated as an empty-element tag.
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun        Even if the markup shows it as an empty-element tag, it
155*4882a593Smuzhiyun        shouldn't be presented that way.
156*4882a593Smuzhiyun        """
157*4882a593Smuzhiyun        soup = self.soup("<p/>")
158*4882a593Smuzhiyun        self.assertFalse(soup.p.is_empty_element)
159*4882a593Smuzhiyun        self.assertEqual(str(soup.p), "<p></p>")
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun    def test_unclosed_tags_get_closed(self):
162*4882a593Smuzhiyun        """A tag that's not closed by the end of the document should be closed.
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun        This applies to all tags except empty-element tags.
165*4882a593Smuzhiyun        """
166*4882a593Smuzhiyun        self.assertSoupEquals("<p>", "<p></p>")
167*4882a593Smuzhiyun        self.assertSoupEquals("<b>", "<b></b>")
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun        self.assertSoupEquals("<br>", "<br/>")
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun    def test_br_is_always_empty_element_tag(self):
172*4882a593Smuzhiyun        """A <br> tag is designated as an empty-element tag.
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun        Some parsers treat <br></br> as one <br/> tag, some parsers as
175*4882a593Smuzhiyun        two tags, but it should always be an empty-element tag.
176*4882a593Smuzhiyun        """
177*4882a593Smuzhiyun        soup = self.soup("<br></br>")
178*4882a593Smuzhiyun        self.assertTrue(soup.br.is_empty_element)
179*4882a593Smuzhiyun        self.assertEqual(str(soup.br), "<br/>")
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun    def test_nested_formatting_elements(self):
182*4882a593Smuzhiyun        self.assertSoupEquals("<em><em></em></em>")
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun    def test_double_head(self):
185*4882a593Smuzhiyun        html = '''<!DOCTYPE html>
186*4882a593Smuzhiyun<html>
187*4882a593Smuzhiyun<head>
188*4882a593Smuzhiyun<title>Ordinary HEAD element test</title>
189*4882a593Smuzhiyun</head>
190*4882a593Smuzhiyun<script type="text/javascript">
191*4882a593Smuzhiyunalert("Help!");
192*4882a593Smuzhiyun</script>
193*4882a593Smuzhiyun<body>
194*4882a593SmuzhiyunHello, world!
195*4882a593Smuzhiyun</body>
196*4882a593Smuzhiyun</html>
197*4882a593Smuzhiyun'''
198*4882a593Smuzhiyun        soup = self.soup(html)
199*4882a593Smuzhiyun        self.assertEqual("text/javascript", soup.find('script')['type'])
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun    def test_comment(self):
202*4882a593Smuzhiyun        # Comments are represented as Comment objects.
203*4882a593Smuzhiyun        markup = "<p>foo<!--foobar-->baz</p>"
204*4882a593Smuzhiyun        self.assertSoupEquals(markup)
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun        soup = self.soup(markup)
207*4882a593Smuzhiyun        comment = soup.find(text="foobar")
208*4882a593Smuzhiyun        self.assertEqual(comment.__class__, Comment)
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun        # The comment is properly integrated into the tree.
211*4882a593Smuzhiyun        foo = soup.find(text="foo")
212*4882a593Smuzhiyun        self.assertEqual(comment, foo.next_element)
213*4882a593Smuzhiyun        baz = soup.find(text="baz")
214*4882a593Smuzhiyun        self.assertEqual(comment, baz.previous_element)
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun    def test_preserved_whitespace_in_pre_and_textarea(self):
217*4882a593Smuzhiyun        """Whitespace must be preserved in <pre> and <textarea> tags."""
218*4882a593Smuzhiyun        self.assertSoupEquals("<pre>   </pre>")
219*4882a593Smuzhiyun        self.assertSoupEquals("<textarea> woo  </textarea>")
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun    def test_nested_inline_elements(self):
222*4882a593Smuzhiyun        """Inline elements can be nested indefinitely."""
223*4882a593Smuzhiyun        b_tag = "<b>Inside a B tag</b>"
224*4882a593Smuzhiyun        self.assertSoupEquals(b_tag)
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun        nested_b_tag = "<p>A <i>nested <b>tag</b></i></p>"
227*4882a593Smuzhiyun        self.assertSoupEquals(nested_b_tag)
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun        double_nested_b_tag = "<p>A <a>doubly <i>nested <b>tag</b></i></a></p>"
230*4882a593Smuzhiyun        self.assertSoupEquals(nested_b_tag)
231*4882a593Smuzhiyun
232*4882a593Smuzhiyun    def test_nested_block_level_elements(self):
233*4882a593Smuzhiyun        """Block elements can be nested."""
234*4882a593Smuzhiyun        soup = self.soup('<blockquote><p><b>Foo</b></p></blockquote>')
235*4882a593Smuzhiyun        blockquote = soup.blockquote
236*4882a593Smuzhiyun        self.assertEqual(blockquote.p.b.string, 'Foo')
237*4882a593Smuzhiyun        self.assertEqual(blockquote.b.string, 'Foo')
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun    def test_correctly_nested_tables(self):
240*4882a593Smuzhiyun        """One table can go inside another one."""
241*4882a593Smuzhiyun        markup = ('<table id="1">'
242*4882a593Smuzhiyun                  '<tr>'
243*4882a593Smuzhiyun                  "<td>Here's another table:"
244*4882a593Smuzhiyun                  '<table id="2">'
245*4882a593Smuzhiyun                  '<tr><td>foo</td></tr>'
246*4882a593Smuzhiyun                  '</table></td>')
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun        self.assertSoupEquals(
249*4882a593Smuzhiyun            markup,
250*4882a593Smuzhiyun            '<table id="1"><tr><td>Here\'s another table:'
251*4882a593Smuzhiyun            '<table id="2"><tr><td>foo</td></tr></table>'
252*4882a593Smuzhiyun            '</td></tr></table>')
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun        self.assertSoupEquals(
255*4882a593Smuzhiyun            "<table><thead><tr><td>Foo</td></tr></thead>"
256*4882a593Smuzhiyun            "<tbody><tr><td>Bar</td></tr></tbody>"
257*4882a593Smuzhiyun            "<tfoot><tr><td>Baz</td></tr></tfoot></table>")
258*4882a593Smuzhiyun
259*4882a593Smuzhiyun    def test_deeply_nested_multivalued_attribute(self):
260*4882a593Smuzhiyun        # html5lib can set the attributes of the same tag many times
261*4882a593Smuzhiyun        # as it rearranges the tree. This has caused problems with
262*4882a593Smuzhiyun        # multivalued attributes.
263*4882a593Smuzhiyun        markup = '<table><div><div class="css"></div></div></table>'
264*4882a593Smuzhiyun        soup = self.soup(markup)
265*4882a593Smuzhiyun        self.assertEqual(["css"], soup.div.div['class'])
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun    def test_multivalued_attribute_on_html(self):
268*4882a593Smuzhiyun        # html5lib uses a different API to set the attributes ot the
269*4882a593Smuzhiyun        # <html> tag. This has caused problems with multivalued
270*4882a593Smuzhiyun        # attributes.
271*4882a593Smuzhiyun        markup = '<html class="a b"></html>'
272*4882a593Smuzhiyun        soup = self.soup(markup)
273*4882a593Smuzhiyun        self.assertEqual(["a", "b"], soup.html['class'])
274*4882a593Smuzhiyun
275*4882a593Smuzhiyun    def test_angle_brackets_in_attribute_values_are_escaped(self):
276*4882a593Smuzhiyun        self.assertSoupEquals('<a b="<a>"></a>', '<a b="&lt;a&gt;"></a>')
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun    def test_entities_in_attributes_converted_to_unicode(self):
279*4882a593Smuzhiyun        expect = '<p id="pi\N{LATIN SMALL LETTER N WITH TILDE}ata"></p>'
280*4882a593Smuzhiyun        self.assertSoupEquals('<p id="pi&#241;ata"></p>', expect)
281*4882a593Smuzhiyun        self.assertSoupEquals('<p id="pi&#xf1;ata"></p>', expect)
282*4882a593Smuzhiyun        self.assertSoupEquals('<p id="pi&#Xf1;ata"></p>', expect)
283*4882a593Smuzhiyun        self.assertSoupEquals('<p id="pi&ntilde;ata"></p>', expect)
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun    def test_entities_in_text_converted_to_unicode(self):
286*4882a593Smuzhiyun        expect = '<p>pi\N{LATIN SMALL LETTER N WITH TILDE}ata</p>'
287*4882a593Smuzhiyun        self.assertSoupEquals("<p>pi&#241;ata</p>", expect)
288*4882a593Smuzhiyun        self.assertSoupEquals("<p>pi&#xf1;ata</p>", expect)
289*4882a593Smuzhiyun        self.assertSoupEquals("<p>pi&#Xf1;ata</p>", expect)
290*4882a593Smuzhiyun        self.assertSoupEquals("<p>pi&ntilde;ata</p>", expect)
291*4882a593Smuzhiyun
292*4882a593Smuzhiyun    def test_quot_entity_converted_to_quotation_mark(self):
293*4882a593Smuzhiyun        self.assertSoupEquals("<p>I said &quot;good day!&quot;</p>",
294*4882a593Smuzhiyun                              '<p>I said "good day!"</p>')
295*4882a593Smuzhiyun
296*4882a593Smuzhiyun    def test_out_of_range_entity(self):
297*4882a593Smuzhiyun        expect = "\N{REPLACEMENT CHARACTER}"
298*4882a593Smuzhiyun        self.assertSoupEquals("&#10000000000000;", expect)
299*4882a593Smuzhiyun        self.assertSoupEquals("&#x10000000000000;", expect)
300*4882a593Smuzhiyun        self.assertSoupEquals("&#1000000000;", expect)
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun    def test_multipart_strings(self):
303*4882a593Smuzhiyun        "Mostly to prevent a recurrence of a bug in the html5lib treebuilder."
304*4882a593Smuzhiyun        soup = self.soup("<html><h2>\nfoo</h2><p></p></html>")
305*4882a593Smuzhiyun        self.assertEqual("p", soup.h2.string.next_element.name)
306*4882a593Smuzhiyun        self.assertEqual("p", soup.p.name)
307*4882a593Smuzhiyun        self.assertConnectedness(soup)
308*4882a593Smuzhiyun
309*4882a593Smuzhiyun    def test_head_tag_between_head_and_body(self):
310*4882a593Smuzhiyun        "Prevent recurrence of a bug in the html5lib treebuilder."
311*4882a593Smuzhiyun        content = """<html><head></head>
312*4882a593Smuzhiyun  <link></link>
313*4882a593Smuzhiyun  <body>foo</body>
314*4882a593Smuzhiyun</html>
315*4882a593Smuzhiyun"""
316*4882a593Smuzhiyun        soup = self.soup(content)
317*4882a593Smuzhiyun        self.assertNotEqual(None, soup.html.body)
318*4882a593Smuzhiyun        self.assertConnectedness(soup)
319*4882a593Smuzhiyun
320*4882a593Smuzhiyun    def test_multiple_copies_of_a_tag(self):
321*4882a593Smuzhiyun        "Prevent recurrence of a bug in the html5lib treebuilder."
322*4882a593Smuzhiyun        content = """<!DOCTYPE html>
323*4882a593Smuzhiyun<html>
324*4882a593Smuzhiyun <body>
325*4882a593Smuzhiyun   <article id="a" >
326*4882a593Smuzhiyun   <div><a href="1"></div>
327*4882a593Smuzhiyun   <footer>
328*4882a593Smuzhiyun     <a href="2"></a>
329*4882a593Smuzhiyun   </footer>
330*4882a593Smuzhiyun  </article>
331*4882a593Smuzhiyun  </body>
332*4882a593Smuzhiyun</html>
333*4882a593Smuzhiyun"""
334*4882a593Smuzhiyun        soup = self.soup(content)
335*4882a593Smuzhiyun        self.assertConnectedness(soup.article)
336*4882a593Smuzhiyun
337*4882a593Smuzhiyun    def test_basic_namespaces(self):
338*4882a593Smuzhiyun        """Parsers don't need to *understand* namespaces, but at the
339*4882a593Smuzhiyun        very least they should not choke on namespaces or lose
340*4882a593Smuzhiyun        data."""
341*4882a593Smuzhiyun
342*4882a593Smuzhiyun        markup = b'<html xmlns="http://www.w3.org/1999/xhtml" xmlns:mathml="http://www.w3.org/1998/Math/MathML" xmlns:svg="http://www.w3.org/2000/svg"><head></head><body><mathml:msqrt>4</mathml:msqrt><b svg:fill="red"></b></body></html>'
343*4882a593Smuzhiyun        soup = self.soup(markup)
344*4882a593Smuzhiyun        self.assertEqual(markup, soup.encode())
345*4882a593Smuzhiyun        html = soup.html
346*4882a593Smuzhiyun        self.assertEqual('http://www.w3.org/1999/xhtml', soup.html['xmlns'])
347*4882a593Smuzhiyun        self.assertEqual(
348*4882a593Smuzhiyun            'http://www.w3.org/1998/Math/MathML', soup.html['xmlns:mathml'])
349*4882a593Smuzhiyun        self.assertEqual(
350*4882a593Smuzhiyun            'http://www.w3.org/2000/svg', soup.html['xmlns:svg'])
351*4882a593Smuzhiyun
352*4882a593Smuzhiyun    def test_multivalued_attribute_value_becomes_list(self):
353*4882a593Smuzhiyun        markup = b'<a class="foo bar">'
354*4882a593Smuzhiyun        soup = self.soup(markup)
355*4882a593Smuzhiyun        self.assertEqual(['foo', 'bar'], soup.a['class'])
356*4882a593Smuzhiyun
357*4882a593Smuzhiyun    #
358*4882a593Smuzhiyun    # Generally speaking, tests below this point are more tests of
359*4882a593Smuzhiyun    # Beautiful Soup than tests of the tree builders. But parsers are
360*4882a593Smuzhiyun    # weird, so we run these tests separately for every tree builder
361*4882a593Smuzhiyun    # to detect any differences between them.
362*4882a593Smuzhiyun    #
363*4882a593Smuzhiyun
364*4882a593Smuzhiyun    def test_can_parse_unicode_document(self):
365*4882a593Smuzhiyun        # A seemingly innocuous document... but it's in Unicode! And
366*4882a593Smuzhiyun        # it contains characters that can't be represented in the
367*4882a593Smuzhiyun        # encoding found in the  declaration! The horror!
368*4882a593Smuzhiyun        markup = '<html><head><meta encoding="euc-jp"></head><body>Sacr\N{LATIN SMALL LETTER E WITH ACUTE} bleu!</body>'
369*4882a593Smuzhiyun        soup = self.soup(markup)
370*4882a593Smuzhiyun        self.assertEqual('Sacr\xe9 bleu!', soup.body.string)
371*4882a593Smuzhiyun
372*4882a593Smuzhiyun    def test_soupstrainer(self):
373*4882a593Smuzhiyun        """Parsers should be able to work with SoupStrainers."""
374*4882a593Smuzhiyun        strainer = SoupStrainer("b")
375*4882a593Smuzhiyun        soup = self.soup("A <b>bold</b> <meta/> <i>statement</i>",
376*4882a593Smuzhiyun                         parse_only=strainer)
377*4882a593Smuzhiyun        self.assertEqual(soup.decode(), "<b>bold</b>")
378*4882a593Smuzhiyun
379*4882a593Smuzhiyun    def test_single_quote_attribute_values_become_double_quotes(self):
380*4882a593Smuzhiyun        self.assertSoupEquals("<foo attr='bar'></foo>",
381*4882a593Smuzhiyun                              '<foo attr="bar"></foo>')
382*4882a593Smuzhiyun
383*4882a593Smuzhiyun    def test_attribute_values_with_nested_quotes_are_left_alone(self):
384*4882a593Smuzhiyun        text = """<foo attr='bar "brawls" happen'>a</foo>"""
385*4882a593Smuzhiyun        self.assertSoupEquals(text)
386*4882a593Smuzhiyun
387*4882a593Smuzhiyun    def test_attribute_values_with_double_nested_quotes_get_quoted(self):
388*4882a593Smuzhiyun        text = """<foo attr='bar "brawls" happen'>a</foo>"""
389*4882a593Smuzhiyun        soup = self.soup(text)
390*4882a593Smuzhiyun        soup.foo['attr'] = 'Brawls happen at "Bob\'s Bar"'
391*4882a593Smuzhiyun        self.assertSoupEquals(
392*4882a593Smuzhiyun            soup.foo.decode(),
393*4882a593Smuzhiyun            """<foo attr="Brawls happen at &quot;Bob\'s Bar&quot;">a</foo>""")
394*4882a593Smuzhiyun
395*4882a593Smuzhiyun    def test_ampersand_in_attribute_value_gets_escaped(self):
396*4882a593Smuzhiyun        self.assertSoupEquals('<this is="really messed up & stuff"></this>',
397*4882a593Smuzhiyun                              '<this is="really messed up &amp; stuff"></this>')
398*4882a593Smuzhiyun
399*4882a593Smuzhiyun        self.assertSoupEquals(
400*4882a593Smuzhiyun            '<a href="http://example.org?a=1&b=2;3">foo</a>',
401*4882a593Smuzhiyun            '<a href="http://example.org?a=1&amp;b=2;3">foo</a>')
402*4882a593Smuzhiyun
403*4882a593Smuzhiyun    def test_escaped_ampersand_in_attribute_value_is_left_alone(self):
404*4882a593Smuzhiyun        self.assertSoupEquals('<a href="http://example.org?a=1&amp;b=2;3"></a>')
405*4882a593Smuzhiyun
406*4882a593Smuzhiyun    def test_entities_in_strings_converted_during_parsing(self):
407*4882a593Smuzhiyun        # Both XML and HTML entities are converted to Unicode characters
408*4882a593Smuzhiyun        # during parsing.
409*4882a593Smuzhiyun        text = "<p>&lt;&lt;sacr&eacute;&#32;bleu!&gt;&gt;</p>"
410*4882a593Smuzhiyun        expected = "<p>&lt;&lt;sacr\N{LATIN SMALL LETTER E WITH ACUTE} bleu!&gt;&gt;</p>"
411*4882a593Smuzhiyun        self.assertSoupEquals(text, expected)
412*4882a593Smuzhiyun
413*4882a593Smuzhiyun    def test_smart_quotes_converted_on_the_way_in(self):
414*4882a593Smuzhiyun        # Microsoft smart quotes are converted to Unicode characters during
415*4882a593Smuzhiyun        # parsing.
416*4882a593Smuzhiyun        quote = b"<p>\x91Foo\x92</p>"
417*4882a593Smuzhiyun        soup = self.soup(quote)
418*4882a593Smuzhiyun        self.assertEqual(
419*4882a593Smuzhiyun            soup.p.string,
420*4882a593Smuzhiyun            "\N{LEFT SINGLE QUOTATION MARK}Foo\N{RIGHT SINGLE QUOTATION MARK}")
421*4882a593Smuzhiyun
422*4882a593Smuzhiyun    def test_non_breaking_spaces_converted_on_the_way_in(self):
423*4882a593Smuzhiyun        soup = self.soup("<a>&nbsp;&nbsp;</a>")
424*4882a593Smuzhiyun        self.assertEqual(soup.a.string, "\N{NO-BREAK SPACE}" * 2)
425*4882a593Smuzhiyun
426*4882a593Smuzhiyun    def test_entities_converted_on_the_way_out(self):
427*4882a593Smuzhiyun        text = "<p>&lt;&lt;sacr&eacute;&#32;bleu!&gt;&gt;</p>"
428*4882a593Smuzhiyun        expected = "<p>&lt;&lt;sacr\N{LATIN SMALL LETTER E WITH ACUTE} bleu!&gt;&gt;</p>".encode("utf-8")
429*4882a593Smuzhiyun        soup = self.soup(text)
430*4882a593Smuzhiyun        self.assertEqual(soup.p.encode("utf-8"), expected)
431*4882a593Smuzhiyun
432*4882a593Smuzhiyun    def test_real_iso_latin_document(self):
433*4882a593Smuzhiyun        # Smoke test of interrelated functionality, using an
434*4882a593Smuzhiyun        # easy-to-understand document.
435*4882a593Smuzhiyun
436*4882a593Smuzhiyun        # Here it is in Unicode. Note that it claims to be in ISO-Latin-1.
437*4882a593Smuzhiyun        unicode_html = '<html><head><meta content="text/html; charset=ISO-Latin-1" http-equiv="Content-type"/></head><body><p>Sacr\N{LATIN SMALL LETTER E WITH ACUTE} bleu!</p></body></html>'
438*4882a593Smuzhiyun
439*4882a593Smuzhiyun        # That's because we're going to encode it into ISO-Latin-1, and use
440*4882a593Smuzhiyun        # that to test.
441*4882a593Smuzhiyun        iso_latin_html = unicode_html.encode("iso-8859-1")
442*4882a593Smuzhiyun
443*4882a593Smuzhiyun        # Parse the ISO-Latin-1 HTML.
444*4882a593Smuzhiyun        soup = self.soup(iso_latin_html)
445*4882a593Smuzhiyun        # Encode it to UTF-8.
446*4882a593Smuzhiyun        result = soup.encode("utf-8")
447*4882a593Smuzhiyun
448*4882a593Smuzhiyun        # What do we expect the result to look like? Well, it would
449*4882a593Smuzhiyun        # look like unicode_html, except that the META tag would say
450*4882a593Smuzhiyun        # UTF-8 instead of ISO-Latin-1.
451*4882a593Smuzhiyun        expected = unicode_html.replace("ISO-Latin-1", "utf-8")
452*4882a593Smuzhiyun
453*4882a593Smuzhiyun        # And, of course, it would be in UTF-8, not Unicode.
454*4882a593Smuzhiyun        expected = expected.encode("utf-8")
455*4882a593Smuzhiyun
456*4882a593Smuzhiyun        # Ta-da!
457*4882a593Smuzhiyun        self.assertEqual(result, expected)
458*4882a593Smuzhiyun
459*4882a593Smuzhiyun    def test_real_shift_jis_document(self):
460*4882a593Smuzhiyun        # Smoke test to make sure the parser can handle a document in
461*4882a593Smuzhiyun        # Shift-JIS encoding, without choking.
462*4882a593Smuzhiyun        shift_jis_html = (
463*4882a593Smuzhiyun            b'<html><head></head><body><pre>'
464*4882a593Smuzhiyun            b'\x82\xb1\x82\xea\x82\xcdShift-JIS\x82\xc5\x83R\x81[\x83f'
465*4882a593Smuzhiyun            b'\x83B\x83\x93\x83O\x82\xb3\x82\xea\x82\xbd\x93\xfa\x96{\x8c'
466*4882a593Smuzhiyun            b'\xea\x82\xcc\x83t\x83@\x83C\x83\x8b\x82\xc5\x82\xb7\x81B'
467*4882a593Smuzhiyun            b'</pre></body></html>')
468*4882a593Smuzhiyun        unicode_html = shift_jis_html.decode("shift-jis")
469*4882a593Smuzhiyun        soup = self.soup(unicode_html)
470*4882a593Smuzhiyun
471*4882a593Smuzhiyun        # Make sure the parse tree is correctly encoded to various
472*4882a593Smuzhiyun        # encodings.
473*4882a593Smuzhiyun        self.assertEqual(soup.encode("utf-8"), unicode_html.encode("utf-8"))
474*4882a593Smuzhiyun        self.assertEqual(soup.encode("euc_jp"), unicode_html.encode("euc_jp"))
475*4882a593Smuzhiyun
476*4882a593Smuzhiyun    def test_real_hebrew_document(self):
477*4882a593Smuzhiyun        # A real-world test to make sure we can convert ISO-8859-9 (a
478*4882a593Smuzhiyun        # Hebrew encoding) to UTF-8.
479*4882a593Smuzhiyun        hebrew_document = b'<html><head><title>Hebrew (ISO 8859-8) in Visual Directionality</title></head><body><h1>Hebrew (ISO 8859-8) in Visual Directionality</h1>\xed\xe5\xec\xf9</body></html>'
480*4882a593Smuzhiyun        soup = self.soup(
481*4882a593Smuzhiyun            hebrew_document, from_encoding="iso8859-8")
482*4882a593Smuzhiyun        self.assertEqual(soup.original_encoding, 'iso8859-8')
483*4882a593Smuzhiyun        self.assertEqual(
484*4882a593Smuzhiyun            soup.encode('utf-8'),
485*4882a593Smuzhiyun            hebrew_document.decode("iso8859-8").encode("utf-8"))
486*4882a593Smuzhiyun
487*4882a593Smuzhiyun    def test_meta_tag_reflects_current_encoding(self):
488*4882a593Smuzhiyun        # Here's the <meta> tag saying that a document is
489*4882a593Smuzhiyun        # encoded in Shift-JIS.
490*4882a593Smuzhiyun        meta_tag = ('<meta content="text/html; charset=x-sjis" '
491*4882a593Smuzhiyun                    'http-equiv="Content-type"/>')
492*4882a593Smuzhiyun
493*4882a593Smuzhiyun        # Here's a document incorporating that meta tag.
494*4882a593Smuzhiyun        shift_jis_html = (
495*4882a593Smuzhiyun            '<html><head>\n%s\n'
496*4882a593Smuzhiyun            '<meta http-equiv="Content-language" content="ja"/>'
497*4882a593Smuzhiyun            '</head><body>Shift-JIS markup goes here.') % meta_tag
498*4882a593Smuzhiyun        soup = self.soup(shift_jis_html)
499*4882a593Smuzhiyun
500*4882a593Smuzhiyun        # Parse the document, and the charset is seemingly unaffected.
501*4882a593Smuzhiyun        parsed_meta = soup.find('meta', {'http-equiv': 'Content-type'})
502*4882a593Smuzhiyun        content = parsed_meta['content']
503*4882a593Smuzhiyun        self.assertEqual('text/html; charset=x-sjis', content)
504*4882a593Smuzhiyun
505*4882a593Smuzhiyun        # But that value is actually a ContentMetaAttributeValue object.
506*4882a593Smuzhiyun        self.assertTrue(isinstance(content, ContentMetaAttributeValue))
507*4882a593Smuzhiyun
508*4882a593Smuzhiyun        # And it will take on a value that reflects its current
509*4882a593Smuzhiyun        # encoding.
510*4882a593Smuzhiyun        self.assertEqual('text/html; charset=utf8', content.encode("utf8"))
511*4882a593Smuzhiyun
512*4882a593Smuzhiyun        # For the rest of the story, see TestSubstitutions in
513*4882a593Smuzhiyun        # test_tree.py.
514*4882a593Smuzhiyun
515*4882a593Smuzhiyun    def test_html5_style_meta_tag_reflects_current_encoding(self):
516*4882a593Smuzhiyun        # Here's the <meta> tag saying that a document is
517*4882a593Smuzhiyun        # encoded in Shift-JIS.
518*4882a593Smuzhiyun        meta_tag = ('<meta id="encoding" charset="x-sjis" />')
519*4882a593Smuzhiyun
520*4882a593Smuzhiyun        # Here's a document incorporating that meta tag.
521*4882a593Smuzhiyun        shift_jis_html = (
522*4882a593Smuzhiyun            '<html><head>\n%s\n'
523*4882a593Smuzhiyun            '<meta http-equiv="Content-language" content="ja"/>'
524*4882a593Smuzhiyun            '</head><body>Shift-JIS markup goes here.') % meta_tag
525*4882a593Smuzhiyun        soup = self.soup(shift_jis_html)
526*4882a593Smuzhiyun
527*4882a593Smuzhiyun        # Parse the document, and the charset is seemingly unaffected.
528*4882a593Smuzhiyun        parsed_meta = soup.find('meta', id="encoding")
529*4882a593Smuzhiyun        charset = parsed_meta['charset']
530*4882a593Smuzhiyun        self.assertEqual('x-sjis', charset)
531*4882a593Smuzhiyun
532*4882a593Smuzhiyun        # But that value is actually a CharsetMetaAttributeValue object.
533*4882a593Smuzhiyun        self.assertTrue(isinstance(charset, CharsetMetaAttributeValue))
534*4882a593Smuzhiyun
535*4882a593Smuzhiyun        # And it will take on a value that reflects its current
536*4882a593Smuzhiyun        # encoding.
537*4882a593Smuzhiyun        self.assertEqual('utf8', charset.encode("utf8"))
538*4882a593Smuzhiyun
539*4882a593Smuzhiyun    def test_tag_with_no_attributes_can_have_attributes_added(self):
540*4882a593Smuzhiyun        data = self.soup("<a>text</a>")
541*4882a593Smuzhiyun        data.a['foo'] = 'bar'
542*4882a593Smuzhiyun        self.assertEqual('<a foo="bar">text</a>', data.a.decode())
543*4882a593Smuzhiyun
544*4882a593Smuzhiyunclass XMLTreeBuilderSmokeTest(SoupTest):
545*4882a593Smuzhiyun
546*4882a593Smuzhiyun    def test_pickle_and_unpickle_identity(self):
547*4882a593Smuzhiyun        # Pickling a tree, then unpickling it, yields a tree identical
548*4882a593Smuzhiyun        # to the original.
549*4882a593Smuzhiyun        tree = self.soup("<a><b>foo</a>")
550*4882a593Smuzhiyun        dumped = pickle.dumps(tree, 2)
551*4882a593Smuzhiyun        loaded = pickle.loads(dumped)
552*4882a593Smuzhiyun        self.assertEqual(loaded.__class__, BeautifulSoup)
553*4882a593Smuzhiyun        self.assertEqual(loaded.decode(), tree.decode())
554*4882a593Smuzhiyun
555*4882a593Smuzhiyun    def test_docstring_generated(self):
556*4882a593Smuzhiyun        soup = self.soup("<root/>")
557*4882a593Smuzhiyun        self.assertEqual(
558*4882a593Smuzhiyun            soup.encode(), b'<?xml version="1.0" encoding="utf-8"?>\n<root/>')
559*4882a593Smuzhiyun
560*4882a593Smuzhiyun    def test_xml_declaration(self):
561*4882a593Smuzhiyun        markup = b"""<?xml version="1.0" encoding="utf8"?>\n<foo/>"""
562*4882a593Smuzhiyun        soup = self.soup(markup)
563*4882a593Smuzhiyun        self.assertEqual(markup, soup.encode("utf8"))
564*4882a593Smuzhiyun
565*4882a593Smuzhiyun    def test_real_xhtml_document(self):
566*4882a593Smuzhiyun        """A real XHTML document should come out *exactly* the same as it went in."""
567*4882a593Smuzhiyun        markup = b"""<?xml version="1.0" encoding="utf-8"?>
568*4882a593Smuzhiyun<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
569*4882a593Smuzhiyun<html xmlns="http://www.w3.org/1999/xhtml">
570*4882a593Smuzhiyun<head><title>Hello.</title></head>
571*4882a593Smuzhiyun<body>Goodbye.</body>
572*4882a593Smuzhiyun</html>"""
573*4882a593Smuzhiyun        soup = self.soup(markup)
574*4882a593Smuzhiyun        self.assertEqual(
575*4882a593Smuzhiyun            soup.encode("utf-8"), markup)
576*4882a593Smuzhiyun
577*4882a593Smuzhiyun    def test_formatter_processes_script_tag_for_xml_documents(self):
578*4882a593Smuzhiyun        doc = """
579*4882a593Smuzhiyun  <script type="text/javascript">
580*4882a593Smuzhiyun  </script>
581*4882a593Smuzhiyun"""
582*4882a593Smuzhiyun        soup = BeautifulSoup(doc, "lxml-xml")
583*4882a593Smuzhiyun        # lxml would have stripped this while parsing, but we can add
584*4882a593Smuzhiyun        # it later.
585*4882a593Smuzhiyun        soup.script.string = 'console.log("< < hey > > ");'
586*4882a593Smuzhiyun        encoded = soup.encode()
587*4882a593Smuzhiyun        self.assertTrue(b"&lt; &lt; hey &gt; &gt;" in encoded)
588*4882a593Smuzhiyun
589*4882a593Smuzhiyun    def test_can_parse_unicode_document(self):
590*4882a593Smuzhiyun        markup = '<?xml version="1.0" encoding="euc-jp"><root>Sacr\N{LATIN SMALL LETTER E WITH ACUTE} bleu!</root>'
591*4882a593Smuzhiyun        soup = self.soup(markup)
592*4882a593Smuzhiyun        self.assertEqual('Sacr\xe9 bleu!', soup.root.string)
593*4882a593Smuzhiyun
594*4882a593Smuzhiyun    def test_popping_namespaced_tag(self):
595*4882a593Smuzhiyun        markup = '<rss xmlns:dc="foo"><dc:creator>b</dc:creator><dc:date>2012-07-02T20:33:42Z</dc:date><dc:rights>c</dc:rights><image>d</image></rss>'
596*4882a593Smuzhiyun        soup = self.soup(markup)
597*4882a593Smuzhiyun        self.assertEqual(
598*4882a593Smuzhiyun            str(soup.rss), markup)
599*4882a593Smuzhiyun
600*4882a593Smuzhiyun    def test_docstring_includes_correct_encoding(self):
601*4882a593Smuzhiyun        soup = self.soup("<root/>")
602*4882a593Smuzhiyun        self.assertEqual(
603*4882a593Smuzhiyun            soup.encode("latin1"),
604*4882a593Smuzhiyun            b'<?xml version="1.0" encoding="latin1"?>\n<root/>')
605*4882a593Smuzhiyun
606*4882a593Smuzhiyun    def test_large_xml_document(self):
607*4882a593Smuzhiyun        """A large XML document should come out the same as it went in."""
608*4882a593Smuzhiyun        markup = (b'<?xml version="1.0" encoding="utf-8"?>\n<root>'
609*4882a593Smuzhiyun                  + b'0' * (2**12)
610*4882a593Smuzhiyun                  + b'</root>')
611*4882a593Smuzhiyun        soup = self.soup(markup)
612*4882a593Smuzhiyun        self.assertEqual(soup.encode("utf-8"), markup)
613*4882a593Smuzhiyun
614*4882a593Smuzhiyun
615*4882a593Smuzhiyun    def test_tags_are_empty_element_if_and_only_if_they_are_empty(self):
616*4882a593Smuzhiyun        self.assertSoupEquals("<p>", "<p/>")
617*4882a593Smuzhiyun        self.assertSoupEquals("<p>foo</p>")
618*4882a593Smuzhiyun
619*4882a593Smuzhiyun    def test_namespaces_are_preserved(self):
620*4882a593Smuzhiyun        markup = '<root xmlns:a="http://example.com/" xmlns:b="http://example.net/"><a:foo>This tag is in the a namespace</a:foo><b:foo>This tag is in the b namespace</b:foo></root>'
621*4882a593Smuzhiyun        soup = self.soup(markup)
622*4882a593Smuzhiyun        root = soup.root
623*4882a593Smuzhiyun        self.assertEqual("http://example.com/", root['xmlns:a'])
624*4882a593Smuzhiyun        self.assertEqual("http://example.net/", root['xmlns:b'])
625*4882a593Smuzhiyun
626*4882a593Smuzhiyun    def test_closing_namespaced_tag(self):
627*4882a593Smuzhiyun        markup = '<p xmlns:dc="http://purl.org/dc/elements/1.1/"><dc:date>20010504</dc:date></p>'
628*4882a593Smuzhiyun        soup = self.soup(markup)
629*4882a593Smuzhiyun        self.assertEqual(str(soup.p), markup)
630*4882a593Smuzhiyun
631*4882a593Smuzhiyun    def test_namespaced_attributes(self):
632*4882a593Smuzhiyun        markup = '<foo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><bar xsi:schemaLocation="http://www.example.com"/></foo>'
633*4882a593Smuzhiyun        soup = self.soup(markup)
634*4882a593Smuzhiyun        self.assertEqual(str(soup.foo), markup)
635*4882a593Smuzhiyun
636*4882a593Smuzhiyun    def test_namespaced_attributes_xml_namespace(self):
637*4882a593Smuzhiyun        markup = '<foo xml:lang="fr">bar</foo>'
638*4882a593Smuzhiyun        soup = self.soup(markup)
639*4882a593Smuzhiyun        self.assertEqual(str(soup.foo), markup)
640*4882a593Smuzhiyun
641*4882a593Smuzhiyunclass HTML5TreeBuilderSmokeTest(HTMLTreeBuilderSmokeTest):
642*4882a593Smuzhiyun    """Smoke test for a tree builder that supports HTML5."""
643*4882a593Smuzhiyun
644*4882a593Smuzhiyun    def test_real_xhtml_document(self):
645*4882a593Smuzhiyun        # Since XHTML is not HTML5, HTML5 parsers are not tested to handle
646*4882a593Smuzhiyun        # XHTML documents in any particular way.
647*4882a593Smuzhiyun        pass
648*4882a593Smuzhiyun
649*4882a593Smuzhiyun    def test_html_tags_have_namespace(self):
650*4882a593Smuzhiyun        markup = "<a>"
651*4882a593Smuzhiyun        soup = self.soup(markup)
652*4882a593Smuzhiyun        self.assertEqual("http://www.w3.org/1999/xhtml", soup.a.namespace)
653*4882a593Smuzhiyun
654*4882a593Smuzhiyun    def test_svg_tags_have_namespace(self):
655*4882a593Smuzhiyun        markup = '<svg><circle/></svg>'
656*4882a593Smuzhiyun        soup = self.soup(markup)
657*4882a593Smuzhiyun        namespace = "http://www.w3.org/2000/svg"
658*4882a593Smuzhiyun        self.assertEqual(namespace, soup.svg.namespace)
659*4882a593Smuzhiyun        self.assertEqual(namespace, soup.circle.namespace)
660*4882a593Smuzhiyun
661*4882a593Smuzhiyun
662*4882a593Smuzhiyun    def test_mathml_tags_have_namespace(self):
663*4882a593Smuzhiyun        markup = '<math><msqrt>5</msqrt></math>'
664*4882a593Smuzhiyun        soup = self.soup(markup)
665*4882a593Smuzhiyun        namespace = 'http://www.w3.org/1998/Math/MathML'
666*4882a593Smuzhiyun        self.assertEqual(namespace, soup.math.namespace)
667*4882a593Smuzhiyun        self.assertEqual(namespace, soup.msqrt.namespace)
668*4882a593Smuzhiyun
669*4882a593Smuzhiyun    def test_xml_declaration_becomes_comment(self):
670*4882a593Smuzhiyun        markup = '<?xml version="1.0" encoding="utf-8"?><html></html>'
671*4882a593Smuzhiyun        soup = self.soup(markup)
672*4882a593Smuzhiyun        self.assertTrue(isinstance(soup.contents[0], Comment))
673*4882a593Smuzhiyun        self.assertEqual(soup.contents[0], '?xml version="1.0" encoding="utf-8"?')
674*4882a593Smuzhiyun        self.assertEqual("html", soup.contents[0].next_element.name)
675*4882a593Smuzhiyun
676*4882a593Smuzhiyundef skipIf(condition, reason):
677*4882a593Smuzhiyun   def nothing(test, *args, **kwargs):
678*4882a593Smuzhiyun       return None
679*4882a593Smuzhiyun
680*4882a593Smuzhiyun   def decorator(test_item):
681*4882a593Smuzhiyun       if condition:
682*4882a593Smuzhiyun           return nothing
683*4882a593Smuzhiyun       else:
684*4882a593Smuzhiyun           return test_item
685*4882a593Smuzhiyun
686*4882a593Smuzhiyun   return decorator
687