Make sure getElementById always returns the first element with the given ID in tree order.(fixes #1822)