@@ -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" ),
@@ -2121,6 +2123,69 @@ def test_cdata_parsing(self):
21212123 dom2 = parseString (dom1 .toprettyxml ())
21222124 self .checkWholeText (dom2 .getElementsByTagName ('node' )[0 ].firstChild , '</data>' )
21232125
2126+ def testNamespaceErr (self ):
2127+ doc = parseString ("<doc/>" )
2128+ elem = doc .documentElement
2129+ XML_NS = xml .dom .XML_NAMESPACE
2130+ XMLNS_NS = xml .dom .XMLNS_NAMESPACE
2131+ for namespaceURI , qname in [
2132+ (None , "p:e" ), # a prefix without a namespace
2133+ ("" , "p:e" ),
2134+ ("http://xml.python.org/ns" , "p:p:e" ), # malformed
2135+ ("http://xml.python.org/ns" , "p:" ),
2136+ ("http://xml.python.org/ns" , "p:1e" ),
2137+ ("http://xml.python.org/ns" , "xml:e" ), # the xml prefix
2138+ ]:
2139+ with self .subTest (namespaceURI = namespaceURI , qname = qname ):
2140+ self .assertRaises (xml .dom .NamespaceErr ,
2141+ doc .createElementNS , namespaceURI , qname )
2142+ self .assertRaises (xml .dom .NamespaceErr ,
2143+ doc .createAttributeNS , namespaceURI , qname )
2144+ self .assertRaises (xml .dom .NamespaceErr ,
2145+ elem .setAttributeNS , namespaceURI , qname , "v" )
2146+
2147+ # the xmlns name and prefix are only allowed in the XMLNS namespace
2148+ for namespaceURI , qname in [
2149+ ("http://xml.python.org/ns" , "xmlns" ),
2150+ ("http://xml.python.org/ns" , "xmlns:p" ),
2151+ (None , "xmlns:p" ),
2152+ (XMLNS_NS , "p:a" ), # and it allows nothing else
2153+ (XMLNS_NS , "a" ),
2154+ ]:
2155+ with self .subTest (namespaceURI = namespaceURI , qname = qname ):
2156+ self .assertRaises (xml .dom .NamespaceErr ,
2157+ doc .createAttributeNS , namespaceURI , qname )
2158+ self .assertRaises (xml .dom .NamespaceErr ,
2159+ elem .setAttributeNS , namespaceURI , qname , "v" )
2160+
2161+ # valid combinations
2162+ doc .createElementNS (None , "e" )
2163+ doc .createElementNS ("http://xml.python.org/ns" , "p:e" )
2164+ doc .createElementNS (XML_NS , "xml:e" )
2165+ doc .createAttributeNS (None , "a" )
2166+ doc .createAttributeNS (XML_NS , "xml:lang" )
2167+ doc .createAttributeNS (XMLNS_NS , "xmlns" )
2168+ doc .createAttributeNS (XMLNS_NS , "xmlns:p" )
2169+ elem .setAttributeNS ("http://xml.python.org/ns" , "p:a" , "v" )
2170+ doc .unlink ()
2171+
2172+ def testAttrPrefix (self ):
2173+ doc = parseString ("<doc/>" )
2174+ attr = doc .createAttributeNS ("http://xml.python.org/ns" , "p:a" )
2175+ self .assertRaises (xml .dom .InvalidCharacterErr ,
2176+ setattr , attr , "prefix" , "q:r" )
2177+ self .assertRaises (xml .dom .InvalidCharacterErr ,
2178+ setattr , attr , "prefix" , "1q" )
2179+ self .assertRaises (xml .dom .NamespaceErr ,
2180+ setattr , attr , "prefix" , "xml" )
2181+ self .assertRaises (xml .dom .NamespaceErr ,
2182+ setattr , attr , "prefix" , "xmlns" )
2183+ attr .prefix = "q"
2184+ self .assertEqual (attr .name , "q:a" )
2185+ attr .prefix = None
2186+ self .assertEqual (attr .name , "a" )
2187+ doc .unlink ()
2188+
21242189 def testInvalidCharacterErr (self ):
21252190 doc = parseString ("<doc/>" )
21262191 impl = getDOMImplementation ()
0 commit comments