@@ -365,7 +365,7 @@ def testRemoveAttrNS(self):
365365 dom = Document ()
366366 child = dom .appendChild (
367367 dom .createElementNS ("http://www.python.org" , "python:abc" ))
368- child .setAttributeNS ("http://www.w3.org" , "xmlns:python" ,
368+ child .setAttributeNS (xml . dom . XMLNS_NAMESPACE , "xmlns:python" ,
369369 "http://www.python.org" )
370370 child .setAttributeNS ("http://www.python.org" , "python:abcattr" , "foo" )
371371 # removing an absent attribute has no effect
@@ -472,11 +472,13 @@ def testGetAttributeNS(self):
472472 dom = Document ()
473473 child = dom .appendChild (
474474 dom .createElementNS ("http://www.python.org" , "python:abc" ))
475- child .setAttributeNS ("http://www.w3.org" , "xmlns:python" ,
475+ child .setAttributeNS (xml . dom . XMLNS_NAMESPACE , "xmlns:python" ,
476476 "http://www.python.org" )
477- self .assertEqual (child .getAttributeNS ("http://www.w3.org" , "python" ),
477+ self .assertEqual (
478+ child .getAttributeNS (xml .dom .XMLNS_NAMESPACE , "python" ),
478479 'http://www.python.org' )
479- self .assertEqual (child .getAttributeNS ("http://www.w3.org" , "other" ),
480+ self .assertEqual (
481+ child .getAttributeNS (xml .dom .XMLNS_NAMESPACE , "other" ),
480482 '' )
481483 child2 = child .appendChild (dom .createElement ('abc' ))
482484 self .assertEqual (child2 .getAttributeNS ("http://www.python.org" , "missing" ),
@@ -1786,6 +1788,69 @@ def test_cdata_parsing(self):
17861788 dom2 = parseString (dom1 .toprettyxml ())
17871789 self .checkWholeText (dom2 .getElementsByTagName ('node' )[0 ].firstChild , '</data>' )
17881790
1791+ def testNamespaceErr (self ):
1792+ doc = parseString ("<doc/>" )
1793+ elem = doc .documentElement
1794+ XML_NS = xml .dom .XML_NAMESPACE
1795+ XMLNS_NS = xml .dom .XMLNS_NAMESPACE
1796+ for namespaceURI , qname in [
1797+ (None , "p:e" ), # a prefix without a namespace
1798+ ("" , "p:e" ),
1799+ ("http://xml.python.org/ns" , "p:p:e" ), # malformed
1800+ ("http://xml.python.org/ns" , "p:" ),
1801+ ("http://xml.python.org/ns" , "p:1e" ),
1802+ ("http://xml.python.org/ns" , "xml:e" ), # the xml prefix
1803+ ]:
1804+ with self .subTest (namespaceURI = namespaceURI , qname = qname ):
1805+ self .assertRaises (xml .dom .NamespaceErr ,
1806+ doc .createElementNS , namespaceURI , qname )
1807+ self .assertRaises (xml .dom .NamespaceErr ,
1808+ doc .createAttributeNS , namespaceURI , qname )
1809+ self .assertRaises (xml .dom .NamespaceErr ,
1810+ elem .setAttributeNS , namespaceURI , qname , "v" )
1811+
1812+ # the xmlns name and prefix are only allowed in the XMLNS namespace
1813+ for namespaceURI , qname in [
1814+ ("http://xml.python.org/ns" , "xmlns" ),
1815+ ("http://xml.python.org/ns" , "xmlns:p" ),
1816+ (None , "xmlns:p" ),
1817+ (XMLNS_NS , "p:a" ), # and it allows nothing else
1818+ (XMLNS_NS , "a" ),
1819+ ]:
1820+ with self .subTest (namespaceURI = namespaceURI , qname = qname ):
1821+ self .assertRaises (xml .dom .NamespaceErr ,
1822+ doc .createAttributeNS , namespaceURI , qname )
1823+ self .assertRaises (xml .dom .NamespaceErr ,
1824+ elem .setAttributeNS , namespaceURI , qname , "v" )
1825+
1826+ # valid combinations
1827+ doc .createElementNS (None , "e" )
1828+ doc .createElementNS ("http://xml.python.org/ns" , "p:e" )
1829+ doc .createElementNS (XML_NS , "xml:e" )
1830+ doc .createAttributeNS (None , "a" )
1831+ doc .createAttributeNS (XML_NS , "xml:lang" )
1832+ doc .createAttributeNS (XMLNS_NS , "xmlns" )
1833+ doc .createAttributeNS (XMLNS_NS , "xmlns:p" )
1834+ elem .setAttributeNS ("http://xml.python.org/ns" , "p:a" , "v" )
1835+ doc .unlink ()
1836+
1837+ def testAttrPrefix (self ):
1838+ doc = parseString ("<doc/>" )
1839+ attr = doc .createAttributeNS ("http://xml.python.org/ns" , "p:a" )
1840+ self .assertRaises (xml .dom .InvalidCharacterErr ,
1841+ setattr , attr , "prefix" , "q:r" )
1842+ self .assertRaises (xml .dom .InvalidCharacterErr ,
1843+ setattr , attr , "prefix" , "1q" )
1844+ self .assertRaises (xml .dom .NamespaceErr ,
1845+ setattr , attr , "prefix" , "xml" )
1846+ self .assertRaises (xml .dom .NamespaceErr ,
1847+ setattr , attr , "prefix" , "xmlns" )
1848+ attr .prefix = "q"
1849+ self .assertEqual (attr .name , "q:a" )
1850+ attr .prefix = None
1851+ self .assertEqual (attr .name , "a" )
1852+ doc .unlink ()
1853+
17891854 def testInvalidCharacterErr (self ):
17901855 doc = parseString ("<doc/>" )
17911856 impl = getDOMImplementation ()
0 commit comments