Example #1
0
    def test_class_lookup_type_mismatch(self):
        class MyLookup(etree.CustomElementClassLookup):
            def lookup(self, t, d, ns, name):
                if t == 'element':
                    if name == 'root':
                        return etree.ElementBase
                    return etree.CommentBase
                elif t == 'comment':
                    return etree.PIBase
                elif t == 'PI':
                    return etree.EntityBase
                elif t == 'entity':
                    return etree.ElementBase
                else:
                    raise ValueError('got type %s' % t)

        parser = etree.XMLParser(resolve_entities=False)
        parser.set_element_class_lookup(MyLookup())

        root = etree.XML(_bytes('<root></root>'), parser)
        self.assertEqual('root', root.tag)
        self.assertEqual(etree.ElementBase, type(root))

        root = etree.XML(_bytes("<root><test/></root>"), parser)
        self.assertRaises(TypeError, root.__getitem__, 0)

        root = etree.XML(_bytes("<root><!-- test --></root>"), parser)
        self.assertRaises(TypeError, root.__getitem__, 0)

        root = etree.XML(_bytes("<root><?test?></root>"), parser)
        self.assertRaises(TypeError, root.__getitem__, 0)

        root = etree.XML(
            _bytes('<!DOCTYPE root [<!ENTITY myent "ent">]>'
                   '<root>&myent;</root>'), parser)
        self.assertRaises(TypeError, root.__getitem__, 0)

        root = etree.XML(_bytes('<root><root/></root>'), parser)
        self.assertEqual('root', root[0].tag)
Example #2
0
 def test_dtd_assertValid(self):
     root = etree.XML("<b><a/></b>")
     dtd = etree.DTD(BytesIO("<!ELEMENT b (a)><!ELEMENT a EMPTY>"))
     dtd.assertValid(root)
Example #3
0
 def test_dtd_invalid(self):
     root = etree.XML("<b><a/></b>")
     dtd = etree.DTD(BytesIO("<!ELEMENT b EMPTY>"))
     self.assertRaises(etree.DocumentInvalid, dtd.assertValid, root)
Example #4
0
 def test_declaration_apos(self):
     root = etree.XML('''<!DOCTYPE a SYSTEM "'"><a/>''')
     doc = root.getroottree()
     self.assertEqual(doc.docinfo.doctype, '''<!DOCTYPE a SYSTEM "'">''')
     self.assertEqual(etree.tostring(doc),
                      _bytes('''<!DOCTYPE a SYSTEM "'">\n<a/>'''))
Example #5
0
 def test_declaration_quote_withoutpid(self):
     root = etree.XML('''<!DOCTYPE a SYSTEM '"'><a/>''')
     doc = root.getroottree()
     self.assertEqual(doc.docinfo.doctype, '''<!DOCTYPE a SYSTEM '"'>''')
     self.assertEqual(etree.tostring(doc),
                      _bytes('''<!DOCTYPE a SYSTEM '"'>\n<a/>'''))
Example #6
0
 def test_dtd_stringio(self):
     root = etree.XML(_bytes("<b/>"))
     dtd = etree.DTD(BytesIO("<!ELEMENT b EMPTY>"))
     self.assertTrue(dtd.validate(root))
Example #7
0
    def test_xslt_document_XML_resolver(self):
        # make sure document('') works when custom resolvers are in use
        assertEquals = self.assertEquals
        called = {'count' : 0}
        class TestResolver(etree.Resolver):
            def resolve(self, url, id, context):
                assertEquals(url, 'file://ANYTHING')
                called['count'] += 1
                return self.resolve_string('<CALLED/>', context)

        parser = etree.XMLParser()
        parser.resolvers.add(TestResolver())

        xslt = etree.XSLT(etree.XML(_bytes("""\
<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:l="local">
  <xsl:template match="/">
    <test>
      <xsl:for-each select="document('')//l:data/l:entry">
        <xsl:copy-of select="document('file://ANYTHING')"/>
        <xsl:copy>
          <xsl:attribute name="value">
            <xsl:value-of select="."/>
          </xsl:attribute>
        </xsl:copy>
      </xsl:for-each>
    </test>
  </xsl:template>
  <l:data>
    <l:entry>A</l:entry>
    <l:entry>B</l:entry>
  </l:data>
</xsl:stylesheet>
"""), parser))

        self.assertEquals(called['count'], 0)
        result = xslt(etree.XML('<a/>'))
        self.assertEquals(called['count'], 1)

        root = result.getroot()
        self.assertEquals(root.tag,
                          'test')
        self.assertEquals(len(root), 4)

        self.assertEquals(root[0].tag,
                          'CALLED')
        self.assertEquals(root[1].tag,
                          '{local}entry')
        self.assertEquals(root[1].text,
                          None)
        self.assertEquals(root[1].get("value"),
                          'A')
        self.assertEquals(root[2].tag,
                          'CALLED')
        self.assertEquals(root[3].tag,
                          '{local}entry')
        self.assertEquals(root[3].text,
                          None)
        self.assertEquals(root[3].get("value"),
                          'B')
Example #8
0
 def test_xslt_document_parse_deny_all(self):
     access_control = etree.XSLTAccessControl.DENY_ALL
     xslt = etree.XSLT(etree.parse(fileInTestDir("test-document.xslt")),
                       access_control = access_control)
     self.assertRaises(etree.XSLTApplyError, xslt, etree.XML('<a/>'))
Example #9
0
 def test_rel_xpath_boolean(self):
     root = etree.XML('<a><b><c/></b></a>')
     el = root[0]
     self.assert_(el.xpath('boolean(c)'))
     self.assert_(not el.xpath('boolean(d)'))
Example #10
0
 def lookup(dummy, id):
     return etree.XML(xml_data).xpath('id(%r)' % id)
Example #11
0
 def test_unicode_xml(self):
     tree = etree.XML(_str('<p>%s</p>') % uni)
     self.assertEquals(uni, tree.text)
Example #12
0
 def test_proxy_reuse_after_gc(self):
     root = etree.XML('<a><b><c/></b></a>')
     b = root.find('b')
     self.assertTrue(self.etree.iselement(b))
     gc.collect()
     self.assertTrue(b is root[0])
Example #13
0
 def test_proxy_reuse(self):
     root = etree.XML('<a><b><c/></b></a>')
     b = root.find('b')
     self.assertTrue(b is root[0])