From eae85064b67ce62a9ea3562fa1d8f6ece9aee471 Mon Sep 17 00:00:00 2001
From: Dave Reid <dave@davereid.net>
Date: Tue, 31 May 2011 18:22:56 -0500
Subject: [PATCH] Issue #1174628: Added the HTML5 search form element.

---
 includes/common.inc                         |    3 ++
 includes/form.inc                           |   46 ++++++++++++++++++++++++--
 modules/locale/locale.admin.inc             |    2 +-
 modules/path/path.admin.inc                 |    2 +-
 modules/search/search.module                |    5 +--
 modules/simpletest/drupal_web_test_case.php |    1 +
 modules/system/system.module                |    9 +++++
 7 files changed, 59 insertions(+), 9 deletions(-)

diff --git a/includes/common.inc b/includes/common.inc
index fbad974..c700643 100644
--- a/includes/common.inc
+++ b/includes/common.inc
@@ -6558,6 +6558,9 @@ function drupal_common_theme() {
     'textfield' => array(
       'render element' => 'element',
     ),
+    'search' => array(
+      'render element' => 'element',
+    ),
     'form' => array(
       'render element' => 'element',
     ),
diff --git a/includes/form.inc b/includes/form.inc
index c0e2ec7..b3dac19 100644
--- a/includes/form.inc
+++ b/includes/form.inc
@@ -3635,8 +3635,48 @@ function theme_textfield($variables) {
   element_set_attributes($element, array('id', 'name', 'value', 'size', 'maxlength'));
   _form_set_class($element, array('form-text'));
 
+  $extra = form_add_autocomplete($element);
+  $output = '<input' . drupal_attributes($element['#attributes']) . ' />';
+
+  return $output . $extra;
+}
+
+/**
+ * Returns HTML for a search form element.
+ *
+ * @param $variables
+ *   An associative array containing:
+ *   - element: An associative array containing the properties of the element.
+ *     Properties used: #title, #value, #description, #size, #maxlength,
+ *     #placeholder, #required, #attributes, #autocomplete_path.
+ *
+ * @ingroup themeable
+ */
+function theme_search($variables) {
+  $element = $variables['element'];
+  element_set_attributes($element, array('type', 'id', 'name', 'value', 'size', 'maxlength', 'placeholder'));
+  _form_set_class($element, array('form-search', 'form-text'));
+
+  $extra = form_add_autocomplete($element);
+  $output = '<input' . drupal_attributes($element['#attributes']) . ' />';
+
+  return $output . $extra;
+}
+
+/**
+ * Return the autocompletion HTML for a form element.
+ *
+ * @param $element
+ *   The renderable element to process for autocompletion.
+ *
+ * @return
+ *   The rendered autocompletion element HTML, or an empty string if the field
+ *   has no autocompletion enabled.
+ */
+function form_add_autocomplete(&$element) {
   $extra = '';
-  if ($element['#autocomplete_path'] && drupal_valid_path($element['#autocomplete_path'])) {
+
+  if (!empty($element['#autocomplete_path']) && drupal_valid_path($element['#autocomplete_path'])) {
     drupal_add_library('system', 'drupal.autocomplete');
     $element['#attributes']['class'][] = 'form-autocomplete';
 
@@ -3649,9 +3689,7 @@ function theme_textfield($variables) {
     $extra = '<input' . drupal_attributes($attributes) . ' />';
   }
 
-  $output = '<input' . drupal_attributes($element['#attributes']) . ' />';
-
-  return $output . $extra;
+  return $extra;
 }
 
 /**
diff --git a/modules/locale/locale.admin.inc b/modules/locale/locale.admin.inc
index dff6537..2fb7d10 100644
--- a/modules/locale/locale.admin.inc
+++ b/modules/locale/locale.admin.inc
@@ -844,7 +844,7 @@ function locale_translation_filter_form() {
     // Special case for 'string' filter.
     if ($key == 'string') {
       $form['filters']['status']['string'] = array(
-        '#type' => 'textfield',
+        '#type' => 'search',
         '#title' => $filter['title'],
         '#description' => $filter['description'],
       );
diff --git a/modules/path/path.admin.inc b/modules/path/path.admin.inc
index f10142b..16bd716 100644
--- a/modules/path/path.admin.inc
+++ b/modules/path/path.admin.inc
@@ -246,7 +246,7 @@ function path_admin_filter_form($form, &$form_state, $keys = '') {
     '#attributes' => array('class' => array('container-inline')),
   );
   $form['basic']['filter'] = array(
-    '#type' => 'textfield',
+    '#type' => 'search',
     '#title' => 'Path alias',
     '#title_display' => 'invisible',
     '#default_value' => $keys,
diff --git a/modules/search/search.module b/modules/search/search.module
index 518272a..3cb39f1 100644
--- a/modules/search/search.module
+++ b/modules/search/search.module
@@ -985,7 +985,7 @@ function search_form($form, &$form_state, $action = '', $keys = '', $module = NU
   $form['module'] = array('#type' => 'value', '#value' => $module);
   $form['basic'] = array('#type' => 'container', '#attributes' => array('class' => array('container-inline')));
   $form['basic']['keys'] = array(
-    '#type' => 'textfield',
+    '#type' => 'search',
     '#title' => $prompt,
     '#default_value' => $keys,
     '#size' => $prompt ? 40 : 20,
@@ -1008,7 +1008,7 @@ function search_form($form, &$form_state, $action = '', $keys = '', $module = NU
  */
 function search_box($form, &$form_state, $form_id) {
   $form[$form_id] = array(
-    '#type' => 'textfield',
+    '#type' => 'search',
     '#title' => t('Search'),
     '#title_display' => 'invisible',
     '#size' => 15,
@@ -1348,4 +1348,3 @@ function search_forms() {
   );
   return $forms;
 }
-
diff --git a/modules/simpletest/drupal_web_test_case.php b/modules/simpletest/drupal_web_test_case.php
index b60c682..e2b2d35 100644
--- a/modules/simpletest/drupal_web_test_case.php
+++ b/modules/simpletest/drupal_web_test_case.php
@@ -2144,6 +2144,7 @@ class DrupalWebTestCase extends DrupalTestCase {
           case 'textarea':
           case 'hidden':
           case 'password':
+          case 'search':
             $post[$name] = $edit[$name];
             unset($edit[$name]);
             break;
diff --git a/modules/system/system.module b/modules/system/system.module
index 3ebc657..9dc93de 100644
--- a/modules/system/system.module
+++ b/modules/system/system.module
@@ -355,6 +355,15 @@ function system_element_info() {
     '#theme' => 'textfield',
     '#theme_wrappers' => array('form_element'),
   );
+  $types['search'] = array(
+    '#input' => TRUE,
+    '#size' => 60,
+    '#maxlength' => 128,
+    '#autocomplete_path' => FALSE,
+    '#process' => array('ajax_process_form'),
+    '#theme' => 'search',
+    '#theme_wrappers' => array('form_element'),
+  );
   $types['machine_name'] = array(
     '#input' => TRUE,
     '#default_value' => NULL,
-- 
1.7.3.1

