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 18 Sep 2007 00:41:42 -0000 @@ -1962,7 +1962,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 +1971,23 @@ function _form_set_class(&$element, $cla * The cleaned ID. */ function form_clean_id($id = NULL) { + static $seen_ids = array(); + $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. + while (isset($seen_ids[$id])) { + $clean_id = $id .'-'. $seen_ids[$id]; + $seen_ids[$id]++; + } + else { + $clean_id = $id; + $seen_ids[$id] = 1; + } + + return $clean_id; } /**