diff --git includes/form.inc includes/form.inc index 1388351..0fbd55e 100644 --- includes/form.inc +++ includes/form.inc @@ -1350,7 +1350,7 @@ function form_type_textfield_value($form, $edit = FALSE) { if ($edit !== FALSE) { // Equate $edit to the form value to ensure it's marked for // validation. - return str_replace(array("\r", "\n"), '', $edit); + return trim(str_replace(array("\r", "\n"), '', $edit)); } } diff --git modules/comment/comment.module modules/comment/comment.module index 3bc28c0..1d39502 100644 --- modules/comment/comment.module +++ modules/comment/comment.module @@ -1780,13 +1780,13 @@ function _comment_form_submit(&$comment_values) { } // Validate the comment's subject. If not specified, extract from comment body. - if (trim($comment_values['subject']) == '') { + if ($comment_values['subject'] == '') { // The body may be in any format, so: // 1) Filter it into HTML // 2) Strip out all HTML tags // 3) Convert entities back to plain-text. // Note: format is checked by check_markup(). - $comment_values['subject'] = truncate_utf8(trim(decode_entities(strip_tags(check_markup($comment_values['comment'], $comment_values['comment_format'])))), 29, TRUE); + $comment_values['subject'] = truncate_utf8(decode_entities(strip_tags(check_markup($comment_values['comment'], $comment_values['comment_format']))), 29, TRUE); // Edge cases where the comment body is populated only by HTML tags will // require a default subject. if ($comment_values['subject'] == '') { diff --git modules/filter/filter.admin.inc modules/filter/filter.admin.inc index 3e0fc0b..41668d9 100644 --- modules/filter/filter.admin.inc +++ modules/filter/filter.admin.inc @@ -183,7 +183,7 @@ function filter_admin_format_form(&$form_state, $format) { */ function filter_admin_format_form_validate($form, &$form_state) { if (!isset($form_state['values']['format'])) { - $name = trim($form_state['values']['name']); + $name = $form_state['values']['name']; $result = db_query("SELECT format FROM {filter_format} WHERE name = :name", array(':name' => $name))->fetchField(); if ($result) { form_set_error('name', t('Text format names must be unique. A format named %name already exists.', array('%name' => $name))); @@ -197,7 +197,7 @@ function filter_admin_format_form_validate($form, &$form_state) { function filter_admin_format_form_submit($form, &$form_state) { $format = isset($form_state['values']['format']) ? $form_state['values']['format'] : NULL; $current = filter_list_format($format); - $name = trim($form_state['values']['name']); + $name = $form_state['values']['name']; $cache = TRUE; // Add a new text format. diff --git modules/menu/menu.admin.inc modules/menu/menu.admin.inc index b452e04..0b190b2 100644 --- modules/menu/menu.admin.inc +++ modules/menu/menu.admin.inc @@ -365,7 +365,7 @@ function menu_edit_item_validate($form, &$form_state) { $item['link_path'] = $parsed_link['path']; } } - if (!trim($item['link_path']) || !menu_valid_path($item)) { + if (!$item['link_path'] || !menu_valid_path($item)) { form_set_error('link_path', t("The path '@link_path' is either invalid or you do not have access to it.", array('@link_path' => $item['link_path']))); } } diff --git modules/menu/menu.module modules/menu/menu.module index 70bb609..732d8ba 100644 --- modules/menu/menu.module +++ modules/menu/menu.module @@ -301,11 +301,10 @@ function menu_node_insert($node) { if (!empty($item['delete'])) { menu_link_delete($item['mlid']); } - elseif (trim($item['link_title'])) { - $item['link_title'] = trim($item['link_title']); + elseif ($item['link_title']) { $item['link_path'] = "node/$node->nid"; if (!$item['customized']) { - $item['options']['attributes']['title'] = trim($node->title); + $item['options']['attributes']['title'] = $node->title; } if (!menu_link_save($item)) { drupal_set_message(t('There was an error saving the menu link.'), 'error'); @@ -323,11 +322,10 @@ function menu_node_update($node) { if (!empty($item['delete'])) { menu_link_delete($item['mlid']); } - elseif (trim($item['link_title'])) { - $item['link_title'] = trim($item['link_title']); + elseif ($item['link_title']) { $item['link_path'] = "node/$node->nid"; if (!$item['customized']) { - $item['options']['attributes']['title'] = trim($node->title); + $item['options']['attributes']['title'] = $node->title; } if (!menu_link_save($item)) { drupal_set_message(t('There was an error saving the menu link.'), 'error'); diff --git modules/node/content_types.inc modules/node/content_types.inc index e967193..81a9a00 100644 --- modules/node/content_types.inc +++ modules/node/content_types.inc @@ -252,11 +252,11 @@ function _node_characters($length) { */ function node_type_form_validate($form, &$form_state) { $type = new stdClass(); - $type->type = trim($form_state['values']['type']); - $type->name = trim($form_state['values']['name']); + $type->type = $form_state['values']['type']; + $type->name = $form_state['values']['name']; // Work out what the type was before the user submitted this form - $old_type = trim($form_state['values']['old_type']); + $old_type = $form_state['values']['old_type']; $types = node_type_get_names(); @@ -289,9 +289,9 @@ function node_type_form_submit($form, &$form_state) { $type = node_type_set_defaults(); - $type->type = trim($form_state['values']['type']); - $type->name = trim($form_state['values']['name']); - $type->orig_type = trim($form_state['values']['orig_type']); + $type->type = $form_state['values']['type']; + $type->name = $form_state['values']['name']; + $type->orig_type = $form_state['values']['orig_type']; $type->old_type = isset($form_state['values']['old_type']) ? $form_state['values']['old_type'] : $type->type; $type->description = $form_state['values']['description']; diff --git modules/node/node.module modules/node/node.module index 31d32ba..a7894bf 100644 --- modules/node/node.module +++ modules/node/node.module @@ -2392,7 +2392,7 @@ function node_search_validate($form, &$form_state) { $keys .= ' "' . str_replace('"', ' ', $form_state['values']['phrase']) . '"'; } if (!empty($keys)) { - form_set_value($form['basic']['inline']['processed_keys'], trim($keys), $form_state); + form_set_value($form['basic']['inline']['processed_keys'], $keys, $form_state); } } diff --git modules/simpletest/tests/form.test modules/simpletest/tests/form.test index a0973d7..389da15 100644 --- modules/simpletest/tests/form.test +++ modules/simpletest/tests/form.test @@ -346,6 +346,48 @@ class FormsFormCleanIdFunctionalTest extends DrupalWebTestCase { } /** + * Tests for form elements and their behaviors. + */ +class FormElementTest extends DrupalWebTestCase { + + public static function getInfo() { + return array( + 'name' => t('Form elements test'), + 'description' => t('Tests for form elements and their behaviors'), + 'group' => t('Form API'), + ); + } + + function setUp() { + parent::setUp('form_test'); + } + + /** + * Test that textfield values are trimmed. + */ + function testTextfieldTrim() { + $value = $this->randomName(); + // Create a form that can be processed and contains a textfield. + $form_id = $this->randomName(); + $form_state = form_state_defaults(); + $form['textfield'] = array( + '#type' => 'textfield', + ); + // Set $value with opening and trailing whitespace as input for the textfield. + $form_state['input'] = array( + 'textfield' => ' ' . $value . ' ', + 'form_id' => $form_id, + ); + // Prepare and process the form. + drupal_prepare_form($form_id, $form, $form_state); + drupal_process_form($form_id, $form, $form_state); + // Assure that the resulting form value does not contain the whitespace. + $this->assertEqual($value, $form_state['values']['textfield'], t('Textfield values are trimmed.')); + } + +} + +/** * Test using drupal_form_submit in a batch. */ class FormAPITestCase extends DrupalWebTestCase { diff --git modules/system/system.admin.inc modules/system/system.admin.inc index d0b7225..a6f5f38 100644 --- modules/system/system.admin.inc +++ modules/system/system.admin.inc @@ -1129,7 +1129,7 @@ function system_ip_blocking_form($form_state) { } function system_ip_blocking_form_validate($form, &$form_state) { - $ip = trim($form_state['values']['ip']); + $ip = $form_state['values']['ip']; if (db_query("SELECT * FROM {blocked_ips} WHERE ip = :ip", array(':ip' => $ip))->fetchField()) { form_set_error('ip', t('This IP address is already blocked.')); } @@ -1142,7 +1142,7 @@ function system_ip_blocking_form_validate($form, &$form_state) { } function system_ip_blocking_form_submit($form, &$form_state) { - $ip = trim($form_state['values']['ip']); + $ip = $form_state['values']['ip']; db_insert('blocked_ips') ->fields(array('ip' => $ip)) ->execute(); diff --git modules/taxonomy/taxonomy.module modules/taxonomy/taxonomy.module index 6bb1da3..d61933d 100644 --- modules/taxonomy/taxonomy.module +++ modules/taxonomy/taxonomy.module @@ -217,11 +217,6 @@ function taxonomy_vocabulary_save($vocabulary) { $vocabulary->nodes = array(); } - if (!empty($vocabulary->name)) { - // Prevent leading and trailing spaces in vocabulary names. - $vocabulary->name = trim($vocabulary->name); - } - if (!isset($vocabulary->module)) { $vocabulary->module = 'taxonomy'; } @@ -348,10 +343,6 @@ function taxonomy_check_vocabulary_hierarchy($vocabulary, $changed_term) { * Status constant indicating if term was inserted or updated. */ function taxonomy_term_save($term) { - if ($term->name) { - // Prevent leading and trailing spaces in term names. - $term->name = trim($term->name); - } if (!empty($term->tid) && $term->name) { $status = drupal_write_record('taxonomy_term_data', $term, 'tid'); diff --git modules/user/user.pages.inc modules/user/user.pages.inc index def9e06..8d5f173 100644 --- modules/user/user.pages.inc +++ modules/user/user.pages.inc @@ -42,7 +42,7 @@ function user_pass() { } function user_pass_validate($form, &$form_state) { - $name = trim($form_state['values']['name']); + $name = $form_state['values']['name']; // Try to load by email. $users = user_load_multiple(array(), array('mail' => $name, 'status' => '1')); $account = reset($users);