Index: includes/ajax.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/ajax.inc,v
retrieving revision 1.34
diff -u -p -r1.34 ajax.inc
--- includes/ajax.inc	4 Oct 2010 17:46:00 -0000	1.34
+++ includes/ajax.inc	5 Oct 2010 01:38:29 -0000
@@ -505,7 +505,25 @@ function ajax_footer() {
 }
 
 /**
- * Add AJAX information about a form element to the page to communicate with JavaScript.
+ * Form element process callback to handle #ajax.
+ *
+ * @param $element
+ *   An associative array containing the properties of the element. See
+ *   ajax_process_element().
+ *
+ * @return
+ *   The processed element.
+ */
+function ajax_process_form($element, &$form_state) {
+  $element = ajax_pre_render_element($element);
+  if (isset($element['#ajax_processed'])) {
+    $form_state['cache'] = TRUE;
+  }
+  return $element;
+}
+
+/**
+ * Add AJAX information about an element to the page to communicate with JavaScript.
  *
  * If #ajax['path'] is set on an element, this additional JavaScript is added
  * to the page header to attach the AJAX behaviors. See ajax.js for more
@@ -521,12 +539,16 @@ function ajax_footer() {
  *   - #ajax['effect']
  *
  * @return
- *   None. Additional code is added to the header of the page using
- *   drupal_add_js().
+ *   The processed element with the necessary JavaScript attached to it.
  */
-function ajax_process_form($element, &$form_state) {
-  // Nothing to do if there is neither a callback nor a path.
-  if (!(isset($element['#ajax']['callback']) || isset($element['#ajax']['path']))) {
+function ajax_pre_render_element($element) {
+  // Skip elements already processed by Form API via ajax_process_form().
+  if (isset($element['#processed']) && $element['#processed']) {
+    return $element;
+  }
+
+  // Nothing to do if the element defines no AJAX callback/path, or link href.
+  if (!(isset($element['#ajax']['callback']) || isset($element['#ajax']['path']) || isset($element['#href']))) {
     return $element;
   }
 
@@ -557,6 +579,10 @@ function ajax_process_form($element, &$f
         $element['#ajax']['event'] = 'change';
         break;
 
+      case 'link':
+        $element['#ajax']['event'] = 'click';
+        break;
+
       default:
         return $element;
     }
@@ -569,6 +595,30 @@ function ajax_process_form($element, &$f
 
     $settings = $element['#ajax'];
 
+    // When being invoked outside of Form API context, elements of #type 'link'
+    // do not have a HTML ID, unless manually set in #options['attributes'],
+    // #attributes, or #id. Either take over one of those or generate a new one.
+    // @see drupal_pre_render_link()
+    if (isset($element['#type']) && $element['#type'] == 'link') {
+      if (isset($element['#options']['attributes']['id'])) {
+        $element['#id'] = $element['#options']['attributes']['id'];
+        $element['#attributes']['id'] = $element['#options']['attributes']['id'];
+      }
+      elseif (isset($element['#attributes']['id'])) {
+        $element['#id'] = $element['#attributes']['id'];
+        $element['#options']['attributes']['id'] = $element['#attributes']['id'];
+      }
+      elseif (isset($element['#id'])) {
+        $element['#options']['attributes']['id'] = $element['#id'];
+        $element['#attributes']['id'] = $element['#id'];
+      }
+      else {
+        $element['#id'] = drupal_html_id('ajax-link');
+        $element['#options']['attributes']['id'] = $element['#id'];
+        $element['#attributes']['id'] = $element['#id'];
+      }
+    }
+
     // Assign default settings.
     $settings += array(
       'selector' => '#' . $element['#id'],
@@ -583,37 +633,52 @@ function ajax_process_form($element, &$f
       $settings['method'] = 'replaceWith';
     }
 
-    // Change path to url.
-    $settings['url'] = isset($settings['path']) ? url($settings['path']) : url('system/ajax');
-    unset($settings['path']);
-
-    // Add special data to $settings['submit'] so that when this element
-    // triggers an AJAX submission, Drupal's form processing can determine which
-    // element triggered it.
-    // @see _form_element_triggered_scripted_submission()
-    if (isset($settings['trigger_as'])) {
-      // An element can add a 'trigger_as' key within #ajax to make the element
-      // submit as though another one (for example, a non-button can use this
-      // to submit the form as though a button were clicked). When using this,
-      // the 'name' key is always required to identify the element to trigger
-      // as. The 'value' key is optional, and only needed when multiple elements
-      // share the same name, which is commonly the case for buttons.
-      $settings['submit']['_triggering_element_name'] = $settings['trigger_as']['name'];
-      if (isset($settings['trigger_as']['value'])) {
-        $settings['submit']['_triggering_element_value'] = $settings['trigger_as']['value'];
-      }
-      unset($settings['trigger_as']);
-    }
-    else {
-      // Most of the time, elements can submit as themselves, in which case the
-      // 'trigger_as' key isn't needed, and the element's name is used.
-      $settings['submit']['_triggering_element_name'] = $element['#name'];
-      // If the element is a (non-image) button, its name may not identify it
-      // uniquely, in which case a match on value is also needed.
-      // @see _form_button_was_clicked()
-      if (isset($element['#button_type']) && empty($element['#has_garbage_value'])) {
-        $settings['submit']['_triggering_element_value'] = $element['#value'];
-      }
+    switch ($element['#type']) {
+      case 'link':
+        // If #ajax['path] was not specified, use the href as AJAX request URL.
+        if (isset($settings['path'])) {
+          $settings['url'] = url($settings['path']);
+          unset($settings['path']);
+        }
+        else {
+          $settings['url'] = url($element['#href'], isset($element['#options']) ? $element['#options'] : array());
+        }
+        break;
+
+      default:
+        // Change path to url.
+        $settings['url'] = isset($settings['path']) ? url($settings['path']) : url('system/ajax');
+        unset($settings['path']);
+
+        // Add special data to $settings['submit'] so that when this element
+        // triggers an AJAX submission, Drupal's form processing can determine which
+        // element triggered it.
+        // @see _form_element_triggered_scripted_submission()
+        if (isset($settings['trigger_as'])) {
+          // An element can add a 'trigger_as' key within #ajax to make the element
+          // submit as though another one (for example, a non-button can use this
+          // to submit the form as though a button were clicked). When using this,
+          // the 'name' key is always required to identify the element to trigger
+          // as. The 'value' key is optional, and only needed when multiple elements
+          // share the same name, which is commonly the case for buttons.
+          $settings['submit']['_triggering_element_name'] = $settings['trigger_as']['name'];
+          if (isset($settings['trigger_as']['value'])) {
+            $settings['submit']['_triggering_element_value'] = $settings['trigger_as']['value'];
+          }
+          unset($settings['trigger_as']);
+        }
+        else {
+          // Most of the time, elements can submit as themselves, in which case the
+          // 'trigger_as' key isn't needed, and the element's name is used.
+          $settings['submit']['_triggering_element_name'] = $element['#name'];
+          // If the element is a (non-image) button, its name may not identify it
+          // uniquely, in which case a match on value is also needed.
+          // @see _form_button_was_clicked()
+          if (isset($element['#button_type']) && empty($element['#has_garbage_value'])) {
+            $settings['submit']['_triggering_element_value'] = $element['#value'];
+          }
+        }
+        break;
     }
 
     // Convert a simple #ajax['progress'] string into an array.
@@ -634,8 +699,8 @@ function ajax_process_form($element, &$f
       'type' => 'setting',
       'data' => array('ajax' => array($element['#id'] => $settings)),
     );
-
-    $form_state['cache'] = TRUE;
+    // Allow others to identify whether something was successfully processed.
+    $element['#ajax_processed'] = TRUE;
   }
   return $element;
 }
Index: includes/common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.1232
diff -u -p -r1.1232 common.inc
--- includes/common.inc	4 Oct 2010 17:46:00 -0000	1.1232
+++ includes/common.inc	5 Oct 2010 01:38:29 -0000
@@ -5011,8 +5011,18 @@ function drupal_pre_render_conditional_c
  *   The passed in elements containing a rendered link in '#markup'.
  */
 function drupal_pre_render_link($elements) {
-  $options = isset($elements['#options']) ? $elements['#options'] : array();
-  $elements['#markup'] = l($elements['#title'], $elements['#href'], $options);
+  // By default, link options to pass to l() are normally set in #options.
+  if (!isset($elements['#options'])) {
+    $elements['#options'] = array();
+  }
+  // However, within the scope of renderable elements, #attributes is a valid
+  // way to specify attributes, too. Take them into account, but do not override
+  // attributes from #options.
+  if (isset($elements['#attributes'])) {
+    $elements['#options'] += array('attributes' => array());
+    $elements['#options']['attributes'] += $elements['#attributes'];
+  }
+  $elements['#markup'] = l($elements['#title'], $elements['#href'], $elements['#options']);
   return $elements;
 }
 
Index: misc/ajax.js
===================================================================
RCS file: /cvs/drupal/drupal/misc/ajax.js,v
retrieving revision 1.20
diff -u -p -r1.20 ajax.js
--- misc/ajax.js	4 Oct 2010 17:46:01 -0000	1.20
+++ misc/ajax.js	5 Oct 2010 02:27:23 -0000
@@ -35,7 +35,7 @@ Drupal.behaviors.AJAX = {
     }
 
     // Bind AJAX behaviors to all items showing the class.
-    $('.use-ajax:not(.ajax-processed)').addClass('ajax-processed').each(function () {
+    $('.use-ajax').once('ajax-processed', function () {
       var element_settings = {};
 
       // For anchor tags, these will go to the target of the anchor rather
@@ -49,7 +49,7 @@ Drupal.behaviors.AJAX = {
     });
 
     // This class means to submit the form to the action using AJAX.
-    $('.use-ajax-submit:not(.ajax-processed)').addClass('ajax-processed').each(function () {
+    $('.use-ajax-submit').once('ajax-processed', function () {
       var element_settings = {};
 
       // AJAX submits specified in this manner automatically submit to the
@@ -60,8 +60,9 @@ Drupal.behaviors.AJAX = {
       element_settings.setClick = true;
       // Form buttons use the 'click' event rather than mousedown.
       element_settings.event = 'click';
-      // Clicked form buttons look better with the throbber than the progress bar.
-      element_settings.progress = { 'type': 'throbber' };
+      // Clicked form buttons look better with the throbber than the progress
+      // bar.
+      element_settings.progress = { type: 'throbber' };
 
       var base = $(this).attr('id');
       Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings);
@@ -173,9 +174,10 @@ Drupal.ajax = function (base, element, e
           ajax.form.clk = this.element;
         }
 
-        ajax.form.ajaxSubmit(options);     
+        ajax.form.ajaxSubmit(options);
       }
       else {
+        ajax.beforeSerialize(this.element, options);
         $.ajax(options);
       }
     }
@@ -216,31 +218,33 @@ Drupal.ajax.prototype.beforeSerialize = 
     var settings = this.settings || Drupal.settings;
     Drupal.detachBehaviors(this.form, settings, 'serialize');
   }
-};
-
-/**
- * Handler for the form redirection submission.
- */
-Drupal.ajax.prototype.beforeSubmit = function (form_values, element, options) {
-  // Disable the element that received the change.
-  $(this.element).addClass('progress-disabled').attr('disabled', true);
 
   // Prevent duplicate HTML ids in the returned markup.
   // @see drupal_html_id()
+  options.data.ajax_html_ids = [];
   $('[id]').each(function () {
-    form_values.push({ name: 'ajax_html_ids[]', value: this.id });
+    options.data.ajax_html_ids.push(this.id);
   });
 
   // Allow Drupal to return new JavaScript and CSS files to load without
   // returning the ones already loaded.
-  form_values.push({ name: 'ajax_page_state[theme]', value: Drupal.settings.ajaxPageState.theme });
-  form_values.push({ name: 'ajax_page_state[theme_token]', value: Drupal.settings.ajaxPageState.themeToken });
+  options.data.ajax_page_state = { css: {}, js: {} };
+  options.data.ajax_page_state.theme = Drupal.settings.ajaxPageState.theme;
+  options.data.ajax_page_state.theme_token = Drupal.settings.ajaxPageState.themeToken;
   for (var key in Drupal.settings.ajaxPageState.css) {
-    form_values.push({ name: 'ajax_page_state[css][' + key + ']', value: 1 });
+    options.data.ajax_page_state.css[key] = 1;
   }
   for (var key in Drupal.settings.ajaxPageState.js) {
-    form_values.push({ name: 'ajax_page_state[js][' + key + ']', value: 1 });
+    options.data.ajax_page_state.js[key] = 1;
   }
+};
+
+/**
+ * Handler for the form redirection submission.
+ */
+Drupal.ajax.prototype.beforeSubmit = function (form_values, element, options) {
+  // Disable the element that received the change.
+  $(this.element).addClass('progress-disabled').attr('disabled', true);
 
   // Insert progressbar or throbber.
   if (this.progress.type == 'bar') {
@@ -279,7 +283,7 @@ Drupal.ajax.prototype.success = function
 
   Drupal.freezeHeight();
 
-  for (i in response) {
+  for (var i in response) {
     if (response[i]['command'] && this.commands[response[i]['command']]) {
       this.commands[response[i]['command']](this, response[i], status);
     }
Index: modules/system/system.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/system/system.module,v
retrieving revision 1.973
diff -u -p -r1.973 system.module
--- modules/system/system.module	5 Oct 2010 00:31:19 -0000	1.973
+++ modules/system/system.module	5 Oct 2010 01:38:29 -0000
@@ -452,7 +452,7 @@ function system_element_info() {
     '#pre_render' => array('drupal_pre_render_markup'),
   );
   $types['link'] = array(
-    '#pre_render' => array('drupal_pre_render_link', 'drupal_pre_render_markup'),
+    '#pre_render' => array('ajax_pre_render_element', 'drupal_pre_render_link', 'drupal_pre_render_markup'),
   );
   $types['fieldset'] = array(
     '#collapsible' => FALSE,
