@@ -2143,6 +2143,42 @@ def testSetIdAttributeNode(self):
21432143 self .confirm (e .isSameNode (doc .getElementById ("w" ))
21442144 and a2 .isId )
21452145
2146+ def check_duplicate_id (self , doc ):
2147+ # The first element in document order is returned, and lookups
2148+ # of other IDs which continue the search do not change that.
2149+ for i in range (2 ):
2150+ self .assertEqual (doc .getElementById ("x" ).getAttribute ("n" ), "1" )
2151+ self .assertEqual (doc .getElementById ("y" ).getAttribute ("n" ), "0" )
2152+ self .assertEqual (doc .getElementById ("x" ).getAttribute ("n" ), "1" )
2153+ self .assertIsNone (doc .getElementById ("missing" ))
2154+ self .assertEqual (doc .getElementById ("x" ).getAttribute ("n" ), "1" )
2155+ self .assertEqual (doc .getElementById ("z" ).getAttribute ("n" ), "4" )
2156+ # clear the cache and start the search again
2157+ doc .documentElement .setAttribute ("a" , str (i ))
2158+
2159+ def testGetElementByIdDuplicate (self ):
2160+ body = ("<doc>"
2161+ "<e id='y' n='0'/>"
2162+ "<f><e id='x' n='1'/></f>"
2163+ "<e id='x' n='2'><e id='x' n='3'/></e>"
2164+ "<e id='z' n='4'/>"
2165+ "</doc>" )
2166+ with self .subTest ("ATTLIST" ):
2167+ doc = parseString ("<!DOCTYPE doc [<!ATTLIST e id ID #IMPLIED>]>" +
2168+ body )
2169+ self .check_duplicate_id (doc )
2170+ with self .subTest ("setIdAttribute" ):
2171+ doc = parseString (body )
2172+ for e in doc .getElementsByTagName ("e" ):
2173+ e .setIdAttribute ("id" )
2174+ self .check_duplicate_id (doc )
2175+ with self .subTest ("ATTLIST and setIdAttribute" ):
2176+ doc = parseString ("<!DOCTYPE doc [<!ATTLIST e n CDATA #IMPLIED>]>" +
2177+ body )
2178+ for e in doc .getElementsByTagName ("e" ):
2179+ e .setIdAttribute ("id" )
2180+ self .check_duplicate_id (doc )
2181+
21462182 def assert_recursive_equal (self , doc , doc2 ):
21472183 stack = [(doc , doc2 )]
21482184 while stack :
0 commit comments