Index: includes/form.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/form.inc,v retrieving revision 1.225 diff -u -p -r1.225 form.inc --- includes/form.inc 14 Sep 2007 10:40:55 -0000 1.225 +++ includes/form.inc 16 Sep 2007 01:32:15 -0000 @@ -812,7 +812,9 @@ function _form_builder_handle_input_elem array_unshift($form['#parents'], $name); } if (!isset($form['#id'])) { - $form['#id'] = form_clean_id('edit-'. implode('-', $form['#parents'])); + // Add the form id into the element ID, so we are unlikely to collide + // with elements from other forms. + $form['#id'] = form_clean_id('edit-'. $form_id .'-'. implode('-', $form['#parents'])); } unset($edit); @@ -1962,7 +1964,8 @@ function _form_set_class(&$element, $cla } /** - * Remove invalid characters from an HTML ID attribute string. + * Prepare an HTML ID attribute string by removing invalid characters and + * guaranteeing uniqueness. * * @param $id * The ID to clean. @@ -1970,8 +1973,19 @@ function _form_set_class(&$element, $cla * The cleaned ID. */ function form_clean_id($id = NULL) { + static $seen_ids = array(); //Maintain list of seen IDs. $id = str_replace(array('][', '_', ' '), '-', $id); - return $id; + //In order to guarantee uniqueness, we must check to see if we have seen + //this ID before. The first time we see an ID, we leave it alone. Subsequent + //sightings get a number appended to the end. + $unique = $id; + while(isset($seen_ids[$unique])) { + $seen_ids[$id]++; + $unique = $id.'-'.$seen_ids[$id]; + } + //Track every single ID we return, so we're never tricked into issuing dupes. + $seen_ids[$unique] = 0; + return $unique; } /**