Example #1
0
    def test_document_init06(self):
        # Window: window
        # Document: SVGDOMImplementation.create_svg_document()
        impl = SVGDOMImplementation()
        doc = impl.create_svg_document(
            nsmap={'html': Element.XHTML_NAMESPACE_URI})
        self.assertEqual(9, Node.DOCUMENT_NODE)
        self.assertEqual(9, doc.node_type)
        self.assertEqual('#document', doc.node_name)
        self.assertIsNone(doc.node_value)
        self.assertIsNone(doc.text_content)
        self.assertEqual('image/svg+xml', doc.content_type)
        self.assertEqual(window, doc.default_view)
        self.assertEqual(impl, doc.implementation)
        self.assertEqual('about:blank', doc.url)
        self.assertEqual('about:blank', doc.document_uri)
        self.assertEqual('null', doc.origin)
        self.assertEqual('about:blank', doc.location.href)

        root = doc.document_element
        self.assertIsInstance(root, SVGSVGElement)
        self.assertEqual(0, len(root.keys()))

        video = doc.create_element_ns(Element.XHTML_NAMESPACE_URI, 'video')
        root.append_child(video)

        source = doc.create_element_ns(Element.XHTML_NAMESPACE_URI, 'source')
        video.append_child(source)

        expected = \
            '<svg xmlns:html="http://www.w3.org/1999/xhtml"' \
            ' xmlns="http://www.w3.org/2000/svg">' \
            '<html:video><html:source/></html:video>' \
            '</svg>'
        self.assertEqual(expected, doc.tostring().decode())
Example #2
0
    def test_document_init04(self):
        # Window: window
        # Document: SVGDOMImplementation.create_document()
        impl = SVGDOMImplementation()
        doc = impl.create_document(Element.SVG_NAMESPACE_URI, '')
        self.assertIsInstance(doc, XMLDocument)
        self.assertEqual(9, Node.DOCUMENT_NODE)
        self.assertEqual(9, doc.node_type)
        self.assertEqual('#document', doc.node_name)
        self.assertIsNone(doc.node_value)
        self.assertIsNone(doc.text_content)
        self.assertEqual('image/svg+xml', doc.content_type)
        self.assertEqual(window, doc.default_view)
        self.assertEqual(impl, doc.implementation)
        self.assertEqual('about:blank', doc.url)
        self.assertEqual('about:blank', doc.document_uri)
        self.assertEqual('null', doc.origin)
        self.assertIsNone(doc.document_element)
        self.assertIsNone(doc.parent_element)
        self.assertIsNone(doc.parent_node)
        self.assertEqual('about:blank', doc.location.href)

        root = doc.create_element('svg')
        self.assertIsInstance(root, SVGSVGElement)
        doc.append_child(root)
        self.assertEqual(root, doc.document_element)
        expected = \
            '<svg xmlns="http://www.w3.org/2000/svg"/>'
        self.assertEqual(expected, doc.tostring().decode())
Example #3
0
    def test_document_prepend(self):
        impl = SVGDOMImplementation()
        doc = impl.create_document(Element.SVG_NAMESPACE_URI)

        parser = etree.XMLParser()
        root = parser.makeelement('svg')
        self.assertNotIsInstance(root, Node)
        self.assertRaises(TypeError, lambda: doc.prepend(root))
        self.assertIsNone(doc.document_element)

        comment = doc.create_comment('demo')
        self.assertRaises(TypeError, lambda: doc.prepend(comment))
        self.assertIsNone(doc.document_element)

        pi = doc.create_processing_instruction('xml-stylesheet',
                                               'href="1.css"')
        self.assertRaises(TypeError, lambda: doc.prepend(pi))
        self.assertIsNone(doc.document_element)

        root = doc.create_element('svg')
        doc.prepend(root)
        self.assertEqual(root, doc.document_element)

        doc.prepend(comment)
        doc.prepend(pi)
        expected = \
            '<?xml-stylesheet href="1.css"?>' \
            '<!--demo-->' \
            '<svg xmlns="http://www.w3.org/2000/svg"/>'
        self.assertEqual(expected, doc.tostring().decode())

        group = doc.create_element('g')
        style = doc.create_element('style')
        comment2 = doc.create_comment('test')
        root.prepend(group)
        root.prepend(style)
        root.prepend(comment2)
        expected = \
            '<?xml-stylesheet href="1.css"?>' \
            '<!--demo-->' \
            '<svg xmlns="http://www.w3.org/2000/svg">' \
            '<!--test-->' \
            '<style/>' \
            '<g/>' \
            '</svg>'
        self.assertEqual(expected, doc.tostring().decode())
        for it in root.iter():
            self.assertEqual(doc, it.owner_document)
Example #4
0
 def test_document_init07(self):
     # Window: Window()
     # Document: Window().document
     win = Window(SVGDOMImplementation())
     doc = win.document
     self.assertIsInstance(doc, XMLDocument)
     self.assertEqual(9, Node.DOCUMENT_NODE)
     self.assertEqual(9, doc.node_type)
     self.assertEqual('#document', doc.node_name)
     self.assertIsNone(doc.node_value)
     self.assertIsNone(doc.text_content)
     self.assertEqual('image/svg+xml', doc.content_type)
     self.assertNotEqual(window, doc.default_view)
     self.assertEqual(win, doc.default_view)
     self.assertNotEqual(window.document.implementation, doc.implementation)
     self.assertEqual('about:blank', doc.url)
     self.assertEqual('about:blank', doc.document_uri)
     self.assertEqual('null', doc.origin)
     self.assertIsNotNone(doc.document_element)
     self.assertIsInstance(doc.document_element, SVGSVGElement)
     self.assertIsNone(doc.parent_element)
     self.assertIsNone(doc.parent_node)
     self.assertEqual('about:blank', doc.location.href)
Example #5
0
    def test_document_init05(self):
        # Window: window
        # Document: SVGDOMImplementation.create_document()
        impl = SVGDOMImplementation()
        doc = impl.create_document(Element.SVG_NAMESPACE_URI, 'svg')
        self.assertIsInstance(doc, XMLDocument)
        self.assertEqual(9, Node.DOCUMENT_NODE)
        self.assertEqual(9, doc.node_type)
        self.assertEqual('#document', doc.node_name)
        self.assertIsNone(doc.node_value)
        self.assertIsNone(doc.text_content)
        self.assertEqual('image/svg+xml', doc.content_type)
        self.assertEqual(window, doc.default_view)
        self.assertEqual(impl, doc.implementation)
        self.assertIsNotNone(doc.document_element)
        self.assertIsNone(doc.parent_element)
        self.assertIsNone(doc.parent_node)

        root = doc.document_element
        self.assertIsInstance(root, SVGSVGElement)
        self.assertEqual(0, len(root.keys()))
        expected = \
            '<svg xmlns="http://www.w3.org/2000/svg"/>'
        self.assertEqual(expected, doc.tostring().decode())
Example #6
0
    def test_document_append_child(self):
        impl = SVGDOMImplementation()

        doc = impl.create_document(Element.SVG_NAMESPACE_URI)
        self.assertIsNone(doc.owner_document)
        self.assertIsNone(doc.document_element)

        # parser = etree.XMLParser()
        # root = parser.makeelement('svg')
        # self.assertNotIsInstance(root, Node)
        # self.assertRaises(TypeError, lambda: doc.append_child(root))

        comment = doc.create_comment('start')
        self.assertRaises(TypeError, lambda: doc.append_child(comment))

        root = doc.create_element('svg')
        self.assertIsInstance(root, Node)
        self.assertEqual(doc, root.owner_document)
        result = doc.append_child(root)
        self.assertEqual(root, result)
        self.assertEqual(root, doc.document_element)
        self.assertEqual(doc, root.owner_document)

        comment = doc.create_comment('end')
        self.assertEqual(doc, comment.owner_document)
        result = doc.append_child(comment)
        self.assertEqual(comment, result)
        self.assertEqual(doc, comment.owner_document)

        title = doc.create_element('title')
        self.assertEqual(doc, title.owner_document)
        self.assertRaises(ValueError, lambda: comment.append_child(title))
        result = root.append_child(title)
        self.assertEqual(title, result)
        self.assertEqual(doc, title.owner_document)

        defs = doc.create_element('defs')
        self.assertEqual(doc, defs.owner_document)
        result = root.append_child(defs)
        self.assertEqual(defs, result)
        self.assertEqual(doc, defs.owner_document)

        style = doc.create_element('style')
        self.assertEqual(doc, style.owner_document)
        result = defs.append_child(style)
        self.assertEqual(style, result)
        self.assertEqual(doc, style.owner_document)

        # <title/><defs><style/></defs>
        self.assertTrue(style in defs)
        self.assertTrue(style not in doc.document_element)
        expected = \
            '<svg xmlns="http://www.w3.org/2000/svg">' \
            '<title/><defs><style/></defs>' \
            '</svg>' \
            '<!--end-->'
        self.assertEqual(expected, doc.tostring().decode())

        # <title/><defs/><style/>
        result = root.append_child(style)
        self.assertEqual(style, result)
        self.assertEqual(doc, style.owner_document)
        self.assertEqual(style, result)
        self.assertTrue(style not in defs)
        self.assertTrue(style in doc.document_element)
        expected = \
            '<svg xmlns="http://www.w3.org/2000/svg">' \
            '<title/><defs/><style/>' \
            '</svg>' \
            '<!--end-->'
        self.assertEqual(expected, doc.tostring().decode())

        # link = parser.makeelement('link')
        # self.assertNotIsInstance(link, Node)
        # self.assertRaises(TypeError, lambda: root.append_child(link))

        # <title/><defs/><style/><link/>
        link = doc.create_element('link')
        self.assertIsInstance(link, Node)
        self.assertEqual(doc, link.owner_document)
        result = root.append_child(link)
        self.assertEqual(link, result)
        self.assertEqual(doc, link.owner_document)
        expected = \
            '<svg xmlns="http://www.w3.org/2000/svg">' \
            '<title/><defs/><style/><link/>' \
            '</svg>' \
            '<!--end-->'
        self.assertEqual(expected, doc.tostring().decode())

        removed = doc.remove_child(root)
        self.assertEqual(root, removed)
        self.assertIsNone(doc.document_element)
        added = doc.append_child(removed)
        self.assertEqual(removed, added)
        self.assertEqual(root, doc.document_element)
Example #7
0
    def test_document_insert_before(self):
        impl = SVGDOMImplementation()

        doc = impl.create_document(Element.SVG_NAMESPACE_URI)
        self.assertIsNone(doc.owner_document)
        self.assertIsNone(doc.document_element)

        parser = etree.XMLParser()
        root = parser.makeelement('svg')
        self.assertNotIsInstance(root, Node)
        self.assertRaises(TypeError, lambda: doc.insert_before(root, None))

        root = doc.create_element('svg')
        self.assertIsInstance(root, Node)
        self.assertEqual(doc, root.owner_document)
        result = doc.insert_before(root, None)
        self.assertEqual(root, result)
        self.assertEqual(root, doc.document_element)
        self.assertEqual(doc, root.owner_document)

        # <defs/>
        defs = doc.create_element('defs')
        self.assertEqual(doc, defs.owner_document)
        result = root.insert_before(defs, None)
        self.assertEqual(defs, result)
        self.assertEqual(doc, defs.owner_document)

        expected = \
            '<svg xmlns="http://www.w3.org/2000/svg">' \
            '<defs/>' \
            '</svg>'
        self.assertEqual(expected, doc.tostring().decode())

        # <title/><defs/>
        title = doc.create_element('title')
        self.assertEqual(doc, title.owner_document)
        result = root.insert_before(title, defs)
        self.assertEqual(title, result)
        self.assertEqual(doc, title.owner_document)

        # <title/><defs/><style/>
        style = doc.create_element('style')
        self.assertEqual(doc, style.owner_document)
        result = root.insert_before(style, None)
        self.assertEqual(style, result)
        self.assertEqual(doc, style.owner_document)

        comment = doc.create_comment('demo')
        self.assertEqual(doc, comment.owner_document)
        result = root.insert_before(comment, title)
        self.assertEqual(comment, result)
        self.assertEqual(doc, comment.owner_document)

        expected = \
            '<svg xmlns="http://www.w3.org/2000/svg">' \
            '<!--demo-->' \
            '<title/><defs/><style/>' \
            '</svg>'
        self.assertEqual(expected, doc.tostring().decode())

        g = doc.create_element('g')
        path = doc.create_element('path')
        result = g.append_child(path)
        self.assertEqual(path, result)
        self.assertEqual(doc, g.owner_document)
        self.assertEqual(doc, path.owner_document)

        # <title/><defs/><style/><g><path/></g>
        result = root.insert_before(g, None)
        self.assertEqual(g, result)
        self.assertEqual(doc, g.owner_document)
        self.assertEqual(doc, path.owner_document)

        rect = doc.create_element('rect')
        self.assertRaises(ValueError, lambda: doc.insert_before(rect, path))
        self.assertEqual(doc, rect.owner_document)
        self.assertEqual(doc, path.owner_document)

        expected = \
            '<svg xmlns="http://www.w3.org/2000/svg">' \
            '<!--demo-->' \
            '<title/><defs/><style/><g><path/></g>' \
            '</svg>'
        self.assertEqual(expected, doc.document_element.tostring().decode())

        for it in root.iter():
            self.assertEqual(doc, it.owner_document)

        # link = parser.makeelement('link')
        # self.assertNotIsInstance(link, Node)
        # self.assertRaises(TypeError, lambda: root.insert_before(link, style))

        link = doc.create_element('link')
        self.assertIsInstance(link, Node)
        self.assertEqual(doc, link.owner_document)
        result = root.insert_before(link, style)
        self.assertEqual(link, result)
        self.assertEqual(doc, link.owner_document)

        expected = \
            '<svg xmlns="http://www.w3.org/2000/svg">' \
            '<!--demo-->' \
            '<title/><defs/><link/><style/><g><path/></g>' \
            '</svg>'
        self.assertEqual(expected, doc.tostring().decode())

        pi1 = doc.create_processing_instruction('xml-stylesheet',
                                                'href="1.css"')
        result = doc.insert_before(pi1, root)
        self.assertEqual(pi1, result)
        self.assertEqual(doc, pi1.owner_document)

        pi2 = doc.create_processing_instruction('xml-stylesheet',
                                                'href="2.css"')
        result = doc.insert_before(pi2, root)
        self.assertEqual(pi2, result)
        self.assertEqual(doc, pi2.owner_document)

        expected = \
            '<?xml-stylesheet href="1.css"?>' \
            '<?xml-stylesheet href="2.css"?>' \
            '<svg xmlns="http://www.w3.org/2000/svg">' \
            '<!--demo-->' \
            '<title/><defs/><link/><style/><g><path/></g>' \
            '</svg>'
        self.assertEqual(expected, doc.tostring().decode())
Example #8
0
    def test_document_replace_child(self):
        impl = SVGDOMImplementation()

        doc = impl.create_document(Element.SVG_NAMESPACE_URI, 'svg')
        self.assertIsNone(doc.owner_document)
        root = doc.document_element
        self.assertEqual(doc, root.owner_document)

        title = doc.create_element('title')
        root.append_child(title)

        defs = doc.create_element('defs')
        root.append_child(defs)

        comment = doc.create_comment('demo')
        doc.insert_before(comment, root)

        expected = \
            '<!--demo-->' \
            '<svg xmlns="http://www.w3.org/2000/svg">' \
            '<title/>' \
            '<defs/>' \
            '</svg>'
        self.assertEqual(expected, doc.tostring().decode())

        # <!--demo--> -> <?xml-stylesheet ?>
        pi = doc.create_processing_instruction('xml-stylesheet')
        result = doc.replace_child(pi, comment)
        self.assertEqual(pi, result)
        self.assertEqual(doc, pi.owner_document)
        self.assertEqual(doc, comment.owner_document)
        expected = \
            '<?xml-stylesheet ?>' \
            '<svg xmlns="http://www.w3.org/2000/svg">' \
            '<title/>' \
            '<defs/>' \
            '</svg>'
        self.assertEqual(expected, doc.tostring().decode())

        # <title/> -> <style/>
        style = doc.create_element('style')
        result = root.replace_child(style, title)
        self.assertEqual(style, result)
        self.assertEqual(doc, style.owner_document)
        self.assertEqual(doc, title.owner_document)
        expected = \
            '<?xml-stylesheet ?>' \
            '<svg xmlns="http://www.w3.org/2000/svg">' \
            '<style/>' \
            '<defs/>' \
            '</svg>'
        self.assertEqual(expected, doc.tostring().decode())

        # <defs/> -> <!--demo-->
        result = root.replace_child(comment, defs)
        self.assertEqual(comment, result)
        self.assertEqual(doc, comment.owner_document)
        self.assertEqual(doc, defs.owner_document)
        expected = \
            '<?xml-stylesheet ?>' \
            '<svg xmlns="http://www.w3.org/2000/svg">' \
            '<style/>' \
            '<!--demo-->' \
            '</svg>'
        self.assertEqual(expected, doc.tostring().decode())

        self.assertRaises(ValueError, lambda: doc.replace_child(defs, style))
        self.assertRaises(ValueError, lambda: root.replace_child(defs, pi))

        root2 = doc.create_element('svg')
        doc.replace_child(root2, root)
        expected = \
            '<svg xmlns="http://www.w3.org/2000/svg"/>'
        self.assertEqual(expected, doc.tostring().decode())
        for it in root.iter():
            self.assertEqual(doc, it.owner_document)
        for it in root2.iter():
            self.assertEqual(doc, it.owner_document)
Example #9
0
    def test_document_remove_child(self):
        impl = SVGDOMImplementation()

        doc = impl.create_document(Element.SVG_NAMESPACE_URI)
        self.assertIsNone(doc.owner_document)
        self.assertIsNone(doc.document_element)

        root = doc.create_element('svg')
        self.assertEqual(doc, root.owner_document)
        self.assertRaises(ValueError, lambda: doc.remove_child(root))

        # <svg/>
        doc.append_child(root)
        self.assertEqual(root, doc.document_element)
        self.assertEqual(doc, root.owner_document)

        # <svg>
        # <title/>
        # </svg>
        title = doc.create_element('title')
        root.append_child(title)

        # <svg>
        # <title/>
        # <defs/>
        # </svg>
        defs = doc.create_element('defs')
        root.append_child(defs)

        # <svg>
        # <title/>
        # <defs><style/></defs>
        # </svg>
        style = doc.create_element('style')
        defs.append_child(style)

        # <!--demo-->
        # <svg>
        # <title/>
        # <defs><style/></defs>
        # </svg>
        comment = doc.create_comment('demo')
        doc.prepend(comment)

        # <?xml-stylesheet href="1.css"?>
        # <!--demo-->
        # <svg>
        # <title/>
        # <defs><style/></defs>
        # </svg>
        pi1 = doc.create_processing_instruction('xml-stylesheet',
                                                'href="1.css"')
        doc.insert_before(pi1, comment)

        # <?xml-stylesheet href="1.css"?>
        # <?xml-stylesheet href="2.css"?>
        # <!--demo-->
        # <svg>
        # <title/>
        # <defs><style/></defs>
        # </svg>
        pi2 = doc.create_processing_instruction('xml-stylesheet',
                                                'href="2.css"')
        doc.insert_before(pi2, comment)

        expected = \
            '<?xml-stylesheet href="1.css"?>' \
            '<?xml-stylesheet href="2.css"?>' \
            '<!--demo-->' \
            '<svg xmlns="http://www.w3.org/2000/svg">' \
            '<title/><defs><style/></defs>' \
            '</svg>'
        self.assertEqual(expected, doc.tostring().decode())

        self.assertEqual(2, len(doc.document_element))
        self.assertTrue(style in defs)
        self.assertTrue(style not in doc.document_element)
        self.assertRaises(ValueError, lambda: doc.remove_child(style))
        self.assertEqual(doc, style.owner_document)

        # remove <style> element
        removed = defs.remove_child(style)
        self.assertEqual(style, removed)
        self.assertEqual(doc, style.owner_document)
        self.assertEqual('style', removed.local_name)
        self.assertEqual(2, len(doc.document_element))
        self.assertTrue(style not in defs)
        self.assertTrue(style not in doc.document_element)

        # remove a comment node
        removed = doc.remove_child(comment)
        self.assertEqual(comment, removed)
        self.assertEqual(doc, comment.owner_document)

        expected = \
            '<?xml-stylesheet href="1.css"?>' \
            '<?xml-stylesheet href="2.css"?>' \
            '<svg xmlns="http://www.w3.org/2000/svg">' \
            '<title/><defs/>' \
            '</svg>'
        self.assertEqual(expected, doc.tostring().decode())

        # remove a processing instruction node
        removed = doc.remove_child(pi2)
        self.assertEqual(pi2, removed)
        self.assertEqual(doc, pi2.owner_document)

        expected = \
            '<?xml-stylesheet href="1.css"?>' \
            '<svg xmlns="http://www.w3.org/2000/svg">' \
            '<title/><defs/>' \
            '</svg>'
        self.assertEqual(expected, doc.tostring().decode())

        # remove a root node
        removed = doc.remove_child(root)
        self.assertEqual(root, removed)
        self.assertEqual(doc, root.owner_document)
        self.assertIsNone(doc.document_element)