Index: modules/system/system.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/system/system.module,v
retrieving revision 1.771
diff -u -r1.771 system.module
--- modules/system/system.module	25 Aug 2009 10:07:19 -0000	1.771
+++ modules/system/system.module	25 Aug 2009 17:13:12 -0000
@@ -939,6 +939,19 @@
     ),
   );
 
+  // Vertical Tabs.
+  $libraries['vertical-tabs'] = array(
+    'title' => 'Vertical Tabs',
+    'website' => 'http://drupal.org/node/323112',
+    'version' => '1.5',
+    'js' => array(
+      'misc/vertical-tabs.js' => array(),
+    ),
+    'css' => array(
+      'misc/vertical-tabs.css' => array(),
+    ),
+  );
+
   // Farbtastic.
   $libraries['farbtastic'] = array(
     'title' => 'Farbtastic',
Index: modules/simpletest/tests/common.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/tests/common.test,v
retrieving revision 1.68
diff -u -r1.68 common.test
--- modules/simpletest/tests/common.test	25 Aug 2009 10:27:14 -0000	1.68
+++ modules/simpletest/tests/common.test	25 Aug 2009 17:13:12 -0000
@@ -857,6 +857,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: includes/common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.973
diff -u -r1.973 common.inc
--- includes/common.inc	25 Aug 2009 15:39:13 -0000	1.973
+++ includes/common.inc	25 Aug 2009 17:13:12 -0000
@@ -3120,6 +3120,67 @@
 }
 
 /**
+ * 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 $weight
+ *   The default weight of JavaScript and CSS being added. This is only applied
+ *   to the stylesheets and JavaScript items that don't have an explicit weight
+ *   assigned to them.
+ * @param $dependency_check
+ *   When TRUE, will exit if a given library's dependencies are missing. When
+ *   set to FALSE, will continue to add the libraries, even though one of the
+ *   dependencies are missing. Defaults to FALSE.
+ * @return
+ *   Will return FALSE if there were any missing library dependencies. TRUE will
+ *   be returned if all library dependencies were met.
+ *
+ * @see drupal_add_library(), drupal_render()
+ */
+function drupal_process_attached(array $libraries = array(), array $js = array(), array $css = array(), $weight = JS_DEFAULT, $dependency_check = FALSE) {
+  // Add the libraries first.
+  $success = TRUE;
+  foreach ($libraries as $library) {
+    if (drupal_add_library($library[0], $library[1]) === FALSE) {
+      $success = FALSE;
+      // Exit if the dependency is missing.
+      if ($dependency_check) {
+        return $success;
+      }
+    }
+  }
+
+  // Add both the JavaScript and the CSS.
+  foreach (array('js' => $js, 'css' => $css) as $type => $items) {
+    foreach ($items 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;
+      }
+      // In some cases, the first parameter ($data) is an array. Arrays can't be
+      // passed as keys in PHP, so we have to get $data from the value array.
+      if (is_numeric($data)) {
+        $data = $options['data'];
+        unset($options['data']);
+      }
+      // Apply the default weight if the weight isn't explicitly given.
+      if (!isset($options['weight'])) {
+        $options['weight'] = $weight;
+      }
+      call_user_func('drupal_add_' . $type, $data, $options);
+    }
+  }
+  return $success;
+}
+
+/**
  * Adds multiple JavaScript or CSS files at the same time.
  *
  * A library defines a set of JavaScript and/or CSS files, optionally using
@@ -3145,43 +3206,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;
-      }
+  if (!isset($added[$module][$name])) {
+    if ($library = drupal_get_library($module, $name)) {
+      // Add all components within the library.
+      $added[$module][$name] = drupal_process_attached($library['dependencies'], $library['js'], $library['css'], JS_LIBRARY, TRUE);
     }
-
-    // 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);
-    }
-
-    // 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];
 }
@@ -3996,27 +4030,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.365
diff -u -r1.365 form.inc
--- includes/form.inc	24 Aug 2009 14:49:08 -0000	1.365
+++ includes/form.inc	25 Aug 2009 17:13:12 -0000
@@ -1594,7 +1594,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'] = array();
@@ -2215,7 +2215,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;
 }
@@ -2300,8 +2300,7 @@
  */
 function theme_vertical_tabs($element) {
   // Add required JavaScript and Stylesheet.
-  drupal_add_js('misc/vertical-tabs.js', array('weight' => JS_DEFAULT - 1));
-  drupal_add_css('misc/vertical-tabs.css');
+  drupal_add_library('system', 'vertical-tabs');
 
   return '<div class="vertical-tabs-panes">' . $element['#children'] . '</div>';
 }
Index: modules/color/color.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/color/color.module,v
retrieving revision 1.67
diff -u -r1.67 color.module
--- modules/color/color.module	22 Aug 2009 14:34:18 -0000	1.67
+++ modules/color/color.module	25 Aug 2009 17:13:12 -0000
@@ -154,16 +154,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.
@@ -176,6 +166,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.
