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	4 Jul 2009 20:36:50 -0000
@@ -3808,24 +3808,31 @@
     }
   }
   
-  // Add additional CSS and JavaScript files associated with this element.
-  foreach (array('css', 'js') as $kind) {
+  // Add additional libraries, CSS and JavaScript associated with this element.
+  foreach (array('library', '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;
+        if ($kind == 'library') {
+          // Adding libraries is different than adding Javascript or CSS. Due to
+          // this, we will use call_user_func_array() rather than call_user_func
+          call_user_func_array('drupal_add_library', $options);
         }
-        // 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']);
+        else {
+          // 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);
         }
-        call_user_func('drupal_add_' . $kind, $data, $options);
       }
     }
   }
Index: includes/form.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/form.inc,v
retrieving revision 1.346
diff -u -r1.346 form.inc
--- includes/form.inc	4 Jul 2009 18:26:42 -0000	1.346
+++ includes/form.inc	4 Jul 2009 20:36:59 -0000
@@ -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;
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	4 Jul 2009 20:37:04 -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	4 Jul 2009 20:37:01 -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(),
+      array(
+        'data' => array('color' => array(
+          'reference' => color_get_palette($theme, TRUE),
+        )),
+        'type' => 'setting',
+      ),
+    ),
   );
 
   // Add palette fields.
