Index: includes/common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.930
diff -u -r1.930 common.inc
--- includes/common.inc	4 Jul 2009 18:26:42 -0000	1.930
+++ includes/common.inc	6 Jul 2009 05:34:30 -0000
@@ -2947,6 +2947,73 @@
 }
 
 /**
+ * Adds all the attached libraries, JavaScript and CSS to the page.
+ *
+ * @param $libraries
+ *   An array of depending libraries to be added.
+ * @param $js
+ *   An array of JavaScript components to add.
+ * @param $css
+ *   An array of cascading stylesheets to add.
+ * @param $jsweight
+ *   The default weight of JavaScript that is being added.
+ * @param $failsafe
+ *   When TRUE, will exit when an error occurs when adding one of the libraries.
+ *   Defaults to FALSE.
+ * @return
+ *   Will return TRUE on success, and FALSE if there were any troubles adding
+ *   the components.
+ *
+ * @see drupal_add_library
+ * @see drupal_render
+ */
+function drupal_process_attached(array $libraries, array $js, array $css, $jsweight = JS_DEFAULT, $failsafe = FALSE) {
+  // Add the libraries first.
+  foreach ($libraries as $library) {
+    if (drupal_add_library($library[0], $library[1]) === FALSE) {
+      // If any dependent library could not be added, exit if $failsafe is set.
+      if ($failsafe) {
+        return FALSE;
+      }
+    }
+  }
+
+  // Add defined JavaScript.
+  foreach ($js as $data => $options) {
+    // If the value is not an array, it's a filename and passed as first
+    // (and only) argument.
+    if (!is_array($options)) {
+      $data = $options;
+      $options = NULL;
+    }
+    // When drupal_add_js with 'type' => 'setting' is called, the first
+    // parameter ($data) is an array. Arrays can't be keys in PHP, so we
+    // have to get $data from the value array.
+    if (is_numeric($data)) {
+      $data = $options['data'];
+      unset($options['data']);
+    }
+    // JavaScript has a default weight if the weight isn't explicitly given.
+    if (!isset($options['weight'])) {
+      $options['weight'] = $jsweight;
+    }
+    drupal_add_js($data, $options);
+  }
+
+  // Add defined stylesheets.
+  foreach ($css as $data => $options) {
+    // If the value is not an array, it's a filename and passed as first
+    // (and only) argument.
+    if (!is_array($options)) {
+      $data = $options;
+      $options = NULL;
+    }
+    drupal_add_css($data, $options);
+  }
+  return TRUE;
+}
+
+/**
  * Adds multiple JavaScript or CSS files at the same time.
  *
  * A library defines a set of JavaScript and/or CSS files, optionally using
@@ -2972,43 +3039,16 @@
   $added = &drupal_static(__FUNCTION__, array());
 
   // Only process the library if it exists and it was not added already.
-  if (!isset($added[$module][$name]) && $library = drupal_get_library($module, $name)) {
-    // Prevent repeated/recursive processing.
-    $added[$module][$name] = TRUE;
-
-    // Ensure dependencies first.
-    foreach ($library['dependencies'] as $dependency) {
-      if (drupal_add_library($dependency[0], $dependency[1]) === FALSE) {
-        // If any dependent library could not be added, this library will break;
-        // stop here.
-        $added[$module][$name] = FALSE;
-        return FALSE;
-      }
-    }
-
-    // Add defined JavaScript.
-    foreach ($library['js'] as $data => $options) {
-      // For JS settings we need to transform $options['data'] into $data.
-      if (isset($options['type'], $options['data']) && $options['type'] == 'setting') {
-        $data = $options['data'];
-        unset($options['data']);
-      }
-      // If not specified, assign a default weight of JS_LIBRARY.
-      elseif (!isset($options['weight'])) {
-        $options['weight'] = JS_LIBRARY;
-      }
-      drupal_add_js($data, $options);
+  if (!isset($added[$module][$name])) {
+    if ($library = drupal_get_library($module, $name)) {
+      // Add all components within the library.
+      $added[$module][$name] = drupal_process_components($library['dependencies'], $library['js'], $library['css'], JS_LIBRARY, TRUE);
     }
-
-    // Add defined stylesheets.
-    foreach ($library['css'] as $data => $options) {
-      drupal_add_css($data, $options);
+    else {
+      // Requested library does not exist.
+      $added[$module][$name] = FALSE;
     }
   }
-  // Requested library does not exist.
-  else {
-    $added[$module][$name] = FALSE;
-  }
 
   return $added[$module][$name];
 }
@@ -3808,27 +3848,12 @@
     }
   }
   
-  // Add additional CSS and JavaScript files associated with this element.
-  foreach (array('css', 'js') as $kind) {
-    if (!empty($elements['#attached_' . $kind]) && is_array($elements['#attached_' . $kind])) {
-      foreach ($elements['#attached_' . $kind] as $data => $options) {
-        // If the value is not an array, it's a filename and passed as first
-        // (and only) argument.
-        if (!is_array($options)) {
-          $data = $options;
-          $options = NULL;
-        }
-        // When drupal_add_js with 'type' => 'setting' is called, the first
-        // parameter ($data) is an array. Arrays can't be keys in PHP, so we
-        // have to get $data from the value array.
-        if (is_numeric($data)) {
-          $data = $options['data'];
-          unset($options['data']);
-        }
-        call_user_func('drupal_add_' . $kind, $data, $options);
-      }
-    }
-  }
+  // Add additional libraries, CSS and JavaScript associated with this element.
+  drupal_process_attached(
+    isset($elements['#attached_library']) ? $elements['#attached_library'] : array(),
+    isset($elements['#attached_js']) ? $elements['#attached_js'] : array(),
+    isset($elements['#attached_css']) ? $elements['#attached_css'] : array()
+  );
 
   $prefix = isset($elements['#prefix']) ? $elements['#prefix'] : '';
   $suffix = isset($elements['#suffix']) ? $elements['#suffix'] : '';
Index: includes/form.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/form.inc,v
retrieving revision 1.347
diff -u -r1.347 form.inc
--- includes/form.inc	5 Jul 2009 18:02:08 -0000	1.347
+++ includes/form.inc	6 Jul 2009 05:34:39 -0000
@@ -1563,7 +1563,7 @@
  */
 function theme_fieldset($element) {
   if (!empty($element['#collapsible'])) {
-    drupal_add_js('misc/collapse.js');
+    $element['#attached_js']['misc/collapse.js'] = array();
 
     if (!isset($element['#attributes']['class'])) {
       $element['#attributes']['class'] = '';
@@ -2002,8 +2002,8 @@
   // Adding the same javascript settings twice will cause a recursion error,
   // we avoid the problem by checking if the javascript has already been added.
   if ((isset($element['#ahah']['callback']) || isset($element['#ahah']['path'])) && isset($element['#ahah']['event']) && !isset($js_added[$element['#id']])) {
-    drupal_add_library('system', 'form');
-    drupal_add_js('misc/ahah.js');
+    $element['#attached_library'][] = array('system', 'form');
+    $element['#attached_js']['misc/ahah.js'] = array();
 
     $ahah_binding = array(
       'url'      => isset($element['#ahah']['callback']) ? url('system/ahah') : url($element['#ahah']['path']),
@@ -2028,10 +2028,13 @@
 
     // Add progress.js if we're doing a bar display.
     if ($ahah_binding['progress']['type'] == 'bar') {
-      drupal_add_js('misc/progress.js', array('cache' => FALSE));
+      $element['#attached_js']['misc/progress.js'] = array('cache' => FALSE);
     }
 
-    drupal_add_js(array('ahah' => array($element['#id'] => $ahah_binding)), 'setting');
+    $element['#attached_js'][] = array(
+      'data' => array('ahah' => array($element['#id'] => $ahah_binding)),
+      'type' => 'setting',
+    );
 
     $js_added[$element['#id']] = TRUE;
     $element['#cache'] = TRUE;
@@ -2283,7 +2286,7 @@
   $element['#group_members'] = &$form_state['groups'][$parents];
 
   // Contains form element summary functionalities.
-  drupal_add_js('misc/form.js', array('weight' => JS_LIBRARY + 1));
+  $element['#attached_js']['misc/form.js'] = array('weight' => JS_LIBRARY + 1);
 
   return $element;
 }
Index: modules/simpletest/tests/common.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/tests/common.test,v
retrieving revision 1.51
diff -u -r1.51 common.test
--- modules/simpletest/tests/common.test	4 Jul 2009 18:26:42 -0000	1.51
+++ modules/simpletest/tests/common.test	6 Jul 2009 05:34:44 -0000
@@ -636,6 +636,20 @@
     $scripts = drupal_get_js();
     $this->assertTrue(strpos($scripts, 'unknown') === FALSE, t('Unknown library was not added to the page.'));
   }
+
+  /**
+   * Tests the addition of libraries through the #attached_library property.
+   */
+  function testAttachedLibrary() {
+    $element = array(
+      '#attached_library' => array(
+        array('system', 'farbtastic'),
+      )
+    );
+    drupal_render($element);
+    $scripts = drupal_get_js();
+    $this->assertTrue(strpos($scripts, 'misc/farbtastic/farbtastic.js'), t('The attached_library property adds the additional libraries.'));
+  }
 }
 
 /**
Index: modules/color/color.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/color/color.module,v
retrieving revision 1.60
diff -u -r1.60 color.module
--- modules/color/color.module	4 Jul 2009 18:26:42 -0000	1.60
+++ modules/color/color.module	6 Jul 2009 05:34:41 -0000
@@ -168,16 +168,6 @@
   $base = drupal_get_path('module', 'color');
   $info = color_get_info($theme);
 
-  // Add Farbtastic color picker.
-  drupal_add_library('system', 'farbtastic');
-
-  // Add custom CSS and JS.
-  drupal_add_css($base . '/color.css', array('preprocess' => FALSE));
-  drupal_add_js($base . '/color.js');
-  drupal_add_js(array('color' => array(
-    'reference' => color_get_palette($theme, TRUE)
-  )), 'setting');
-
   // See if we're using a predefined scheme.
   $current = implode(',', variable_get('color_' . $theme . '_palette', array()));
   // Note: we use the original theme when the default scheme is chosen.
@@ -190,6 +180,24 @@
     '#title' => t('Color set'),
     '#options' => $info['schemes'],
     '#default_value' => $current,
+    // Add Farbtastic color picker.
+    '#attached_library' => array(
+      array('system', 'farbtastic'),
+    ),
+    // Add custom CSS.
+    '#attached_css' => array(
+      $base . '/color.css' => array('preprocess' => FALSE),
+    ),
+    // Add custom JavaScript.
+    '#attached_js' => array(
+      $base . '/color.js',
+      array(
+        'data' => array('color' => array(
+          'reference' => color_get_palette($theme, TRUE),
+        )),
+        'type' => 'setting',
+      ),
+    ),
   );
 
   // Add palette fields.
