From 3e8861682a4ae6b2bd1129473104be37b63695ac Mon Sep 17 00:00:00 2001 From: catch <catch@35733.no-reply.drupal.org> Date: Tue, 8 May 2012 11:53:23 +0900 Subject: [PATCH] Issue #1540174 by Niklas Fiekas, attiks: Fixed Some attributes not allowed for the new HTML5 input elements. --- core/includes/form.inc | 40 +++++++-- .../field/modules/number/number.module | 5 -- core/modules/field/modules/text/text.module | 1 - core/modules/field_ui/field_ui.api.php | 1 - core/modules/filter/filter.module | 2 - core/modules/image/image.admin.inc | 4 - core/modules/image/image.field.inc | 8 -- core/modules/poll/poll.module | 2 - core/modules/search/search.admin.inc | 3 +- core/modules/system/image.gd.inc | 2 - core/modules/system/system.module | 4 - core/modules/system/tests/form.test | 14 +++ .../tests/modules/form_test/form_test.module | 86 +++++++++++++++++++ core/modules/user/user.admin.inc | 2 - 14 files changed, 136 insertions(+), 38 deletions(-) diff --git a/core/includes/form.inc b/core/includes/form.inc index c387384b2a3d..6d577806d191 100644 --- a/core/includes/form.inc +++ b/core/includes/form.inc @@ -3951,8 +3951,8 @@ function theme_tel($variables) { * @param $variables * An associative array containing: * - element: An associative array containing the properties of the element. - * Properties used: #title, #value, #description, #size, #min, #max, - * #placeholder, #required, #attributes, #step. + * Properties used: #title, #value, #description, #min, #max, #placeholder, + * #required, #attributes, #step. * * @ingroup themeable */ @@ -3960,7 +3960,7 @@ function theme_number($variables) { $element = $variables['element']; $element['#attributes']['type'] = 'number'; - element_set_attributes($element, array('id', 'name', 'value', 'size', 'step', 'min', 'max', 'maxlength', 'placeholder')); + element_set_attributes($element, array('id', 'name', 'value', 'step', 'min', 'max', 'placeholder')); _form_set_class($element, array('form-number')); $output = '<input' . drupal_attributes($element['#attributes']) . ' />'; @@ -3974,8 +3974,8 @@ function theme_number($variables) { * @param $variables * An associative array containing: * - element: An associative array containing the properties of the element. - * Properties used: #title, #value, #description, #size, #min, #max, - * #required, #attributes, #step. + * Properties used: #title, #value, #description, #min, #max, #attributes, + * #step. * * @ingroup themeable */ @@ -4031,6 +4031,36 @@ function form_validate_number(&$element, &$form_state) { } } +/** + * Determines the value for a range element. + * + * Make sure range elements always have a value. The 'required' attribute is not + * allowed for range elements. + * + * @param $element + * The form element whose value is being populated. + * @param $input + * The incoming input to populate the form element. If this is FALSE, the + * element's default value should be returned. + * + * @return + * The data that will appear in the $form_state['values'] collection for + * this element. Return nothing to use the default. + */ +function form_type_range_value($element, $input = FALSE) { + if ($input === '') { + $offset = ($element['#max'] - $element['#min']) / 2; + + // Round to the step. + if (strtolower($element['#step']) != 'any') { + $steps = round($offset / $element['#step']); + $offset = $element['#step'] * $steps; + } + + return $element['#min'] + $offset; + } +} + /** * Returns HTML for a url form element. * diff --git a/core/modules/field/modules/number/number.module b/core/modules/field/modules/number/number.module index 1b9a300d86d1..b3f56151d686 100644 --- a/core/modules/field/modules/number/number.module +++ b/core/modules/field/modules/number/number.module @@ -324,11 +324,6 @@ function number_field_widget_form(&$form, &$form_state, $field, $instance, $lang $element += array( '#type' => 'number', '#default_value' => $value, - // Allow a slightly larger size that the field length to allow for some - // configurations where all characters won't fit in input field. - '#size' => $field['type'] == 'number_decimal' ? $field['settings']['precision'] + 4 : 12, - // Allow two extra characters for signed values and decimal separator. - '#maxlength' => $field['type'] == 'number_decimal' ? $field['settings']['precision'] + 2 : 10, ); // Set the step for floating point and decimal numbers. diff --git a/core/modules/field/modules/text/text.module b/core/modules/field/modules/text/text.module index 985fa9b696a1..dcf4d1e7f6d5 100644 --- a/core/modules/field/modules/text/text.module +++ b/core/modules/field/modules/text/text.module @@ -225,7 +225,6 @@ function text_field_formatter_settings_form($field, $instance, $view_mode, $form $element['trim_length'] = array( '#title' => t('Trim length'), '#type' => 'number', - '#size' => 10, '#default_value' => $settings['trim_length'], '#min' => 1, '#required' => TRUE, diff --git a/core/modules/field_ui/field_ui.api.php b/core/modules/field_ui/field_ui.api.php index b6c8aeede9a3..ff85b5d957a5 100644 --- a/core/modules/field_ui/field_ui.api.php +++ b/core/modules/field_ui/field_ui.api.php @@ -158,7 +158,6 @@ function hook_field_formatter_settings_form($field, $instance, $view_mode, $form $element['trim_length'] = array( '#title' => t('Length'), '#type' => 'number', - '#size' => 20, '#default_value' => $settings['trim_length'], '#min' => 1, '#required' => TRUE, diff --git a/core/modules/filter/filter.module b/core/modules/filter/filter.module index 9f492fe3f16b..1e6c28c09366 100644 --- a/core/modules/filter/filter.module +++ b/core/modules/filter/filter.module @@ -1363,8 +1363,6 @@ function _filter_url_settings($form, &$form_state, $filter, $format, $defaults) '#type' => 'number', '#title' => t('Maximum link text length'), '#default_value' => $filter->settings['filter_url_length'], - '#size' => 5, - '#maxlength' => 4, '#min' => 1, '#field_suffix' => t('characters'), '#description' => t('URLs longer than this number of characters will be truncated to prevent long strings that break formatting. The link itself will be retained; just the text portion of the link will be truncated.'), diff --git a/core/modules/image/image.admin.inc b/core/modules/image/image.admin.inc index c6b1356c070a..f31d98ff6172 100644 --- a/core/modules/image/image.admin.inc +++ b/core/modules/image/image.admin.inc @@ -450,7 +450,6 @@ function image_resize_form($data) { '#default_value' => isset($data['width']) ? $data['width'] : '', '#field_suffix' => ' ' . t('pixels'), '#required' => TRUE, - '#size' => 10, '#min' => 1, ); $form['height'] = array( @@ -459,7 +458,6 @@ function image_resize_form($data) { '#default_value' => isset($data['height']) ? $data['height'] : '', '#field_suffix' => ' ' . t('pixels'), '#required' => TRUE, - '#size' => 10, '#min' => 1, ); return $form; @@ -547,8 +545,6 @@ function image_rotate_form($data) { '#description' => t('The number of degrees the image should be rotated. Positive numbers are clockwise, negative are counter-clockwise.'), '#field_suffix' => '°', '#required' => TRUE, - '#size' => 6, - '#maxlength' => 4, ); $form['bgcolor'] = array( '#type' => 'textfield', diff --git a/core/modules/image/image.field.inc b/core/modules/image/image.field.inc index 07dc9e390c6e..0fb657c59c28 100644 --- a/core/modules/image/image.field.inc +++ b/core/modules/image/image.field.inc @@ -91,8 +91,6 @@ function image_field_instance_settings_form($field, $instance) { '#title' => t('Maximum width'), '#title_display' => 'invisible', '#default_value' => $max_resolution[0], - '#size' => 5, - '#maxlength' => 5, '#min' => 1, '#field_suffix' => ' x ', ); @@ -101,8 +99,6 @@ function image_field_instance_settings_form($field, $instance) { '#title' => t('Maximum height'), '#title_display' => 'invisible', '#default_value' => $max_resolution[1], - '#size' => 5, - '#maxlength' => 5, '#min' => 1, '#field_suffix' => ' ' . t('pixels'), ); @@ -122,8 +118,6 @@ function image_field_instance_settings_form($field, $instance) { '#title' => t('Minimum width'), '#title_display' => 'invisible', '#default_value' => $min_resolution[0], - '#size' => 5, - '#maxlength' => 5, '#min' => 1, '#field_suffix' => ' x ', ); @@ -132,8 +126,6 @@ function image_field_instance_settings_form($field, $instance) { '#title' => t('Minimum height'), '#title_display' => 'invisible', '#default_value' => $min_resolution[1], - '#size' => 5, - '#maxlength' => 5, '#min' => 1, '#field_suffix' => ' ' . t('pixels'), ); diff --git a/core/modules/poll/poll.module b/core/modules/poll/poll.module index b62568c1a5a9..7c3a27e24f45 100644 --- a/core/modules/poll/poll.module +++ b/core/modules/poll/poll.module @@ -389,8 +389,6 @@ function _poll_choice_form($key, $chid = NULL, $value = '', $votes = 0, $weight '#title' => $value !== '' ? t('Vote count for choice @label', array('@label' => $value)) : t('Vote count for new choice'), '#title_display' => 'invisible', '#default_value' => $votes, - '#size' => 5, - '#maxlength' => 7, '#min' => 0, '#parents' => array('choice', $key, 'chvotes'), '#access' => user_access('administer nodes'), diff --git a/core/modules/search/search.admin.inc b/core/modules/search/search.admin.inc index 5355c6a49095..bef8fd90d643 100644 --- a/core/modules/search/search.admin.inc +++ b/core/modules/search/search.admin.inc @@ -93,9 +93,8 @@ function search_admin_settings($form) { '#type' => 'number', '#title' => t('Minimum word length to index'), '#default_value' => variable_get('minimum_word_size', 3), - '#size' => 5, - '#maxlength' => 3, '#min' => 1, + '#max' => 1000, '#description' => t('The number of characters a word has to be to be indexed. A lower setting means better search result ranking, but also a larger database. Each search query must contain at least one keyword that is this size (or longer).') ); $form['indexing_settings']['overlap_cjk'] = array( diff --git a/core/modules/system/image.gd.inc b/core/modules/system/image.gd.inc index 4dd5ea1903d1..b8dd8221b7c1 100644 --- a/core/modules/system/image.gd.inc +++ b/core/modules/system/image.gd.inc @@ -23,8 +23,6 @@ function image_gd_settings() { '#type' => 'number', '#title' => t('JPEG quality'), '#description' => t('Define the image quality for JPEG manipulations. Ranges from 0 to 100. Higher values mean better image quality but bigger files.'), - '#size' => 10, - '#maxlength' => 3, '#min' => 0, '#max' => 100, '#default_value' => variable_get('image_jpeg_quality', 75), diff --git a/core/modules/system/system.module b/core/modules/system/system.module index 34b6db2947eb..c8df3c5b5f83 100644 --- a/core/modules/system/system.module +++ b/core/modules/system/system.module @@ -405,9 +405,7 @@ function system_element_info() { ); $types['number'] = array( '#input' => TRUE, - '#size' => 30, '#step' => 1, - '#maxlength' => 128, '#process' => array('ajax_process_form'), '#element_validate' => array('form_validate_number'), '#theme' => 'number', @@ -415,11 +413,9 @@ function system_element_info() { ); $types['range'] = array( '#input' => TRUE, - '#size' => 30, '#step' => 1, '#min' => 0, '#max' => 100, - '#maxlength' => 128, '#process' => array('ajax_process_form'), '#element_validate' => array('form_validate_number'), '#theme' => 'range', diff --git a/core/modules/system/tests/form.test b/core/modules/system/tests/form.test index b64fdd71823a..ada1a1beb261 100644 --- a/core/modules/system/tests/form.test +++ b/core/modules/system/tests/form.test @@ -365,6 +365,20 @@ class FormsTestCase extends DrupalWebTestCase { } } + /** + * Tests default value handling of #type 'range' elements. + */ + function testRange() { + $values = json_decode($this->drupalPost('form-test/range', array(), 'Submit')); + $this->assertEqual($values->with_default_value, 18); + $this->assertEqual($values->float, 10.5); + $this->assertEqual($values->integer, 6); + $this->assertEqual($values->offset, 6.9); + + $this->drupalPost('form-test/range/invalid', array(), 'Submit'); + $this->assertFieldByXPath('//input[@type="range" and contains(@class, "error")]', NULL, 'Range element has the error class.'); + } + /** * Test handling of disabled elements. * diff --git a/core/modules/system/tests/modules/form_test/form_test.module b/core/modules/system/tests/modules/form_test/form_test.module index f42b5f5ddb42..e892f767f6e9 100644 --- a/core/modules/system/tests/modules/form_test/form_test.module +++ b/core/modules/system/tests/modules/form_test/form_test.module @@ -145,6 +145,18 @@ function form_test_menu() { 'page arguments' => array('form_test_number', 'range'), 'access callback' => TRUE, ); + $items['form-test/range']= array( + 'title' => 'Range', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('form_test_range'), + 'access callback' => TRUE, + ); + $items['form-test/range/invalid'] = array( + 'title' => 'Invalid range', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('form_test_range_invalid'), + 'access callback' => TRUE, + ); $items['form-test/checkboxes-radios'] = array( 'title' => t('Checkboxes, Radios'), 'page callback' => 'drupal_get_form', @@ -1276,6 +1288,80 @@ function form_test_number($form, &$form_state, $element = 'number') { return $form; } +/** + * Form constructor for testing #type 'range' elements. + * + * @see form_test_range_submit() + * @ingroup forms + */ +function form_test_range($form, &$form_state) { + $form['with_default_value'] = array( + '#type' => 'range', + '#title' => 'Range with default value', + '#min' => 10, + '#max' => 20, + '#step' => 2, + '#default_value' => 18, + '#description' => 'The default value is 18.', + ); + $form['float'] = array( + '#type' => 'range', + '#title' => 'Float', + '#min' => 10, + '#max' => 11, + '#step' => 'any', + '#description' => 'Floating point number between 10 and 11.', + ); + $form['integer'] = array( + '#type' => 'range', + '#title' => 'Integer', + '#min' => 2, + '#max' => 8, + '#step' => 2, + '#description' => 'Even integer between 2 and 8.', + ); + $form['offset'] = array( + '#type' => 'range', + '#title' => 'Offset', + '#min' => 2.9, + '#max' => 10.9, + '#description' => 'Value between 2.9 and 10.9.', + ); + $form['submit'] = array( + '#type' => 'submit', + '#value' => 'Submit', + ); + return $form; +} + +/** + * Form submission handler for form_test_range(). + */ +function form_test_range_submit($form, &$form_state) { + drupal_json_output($form_state['values']); + exit; +} + +/** + * Form constructor for testing invalid #type 'range' elements. + * + * @ingroup forms + */ +function form_test_range_invalid($form, &$form_state) { + $form['minmax'] = array( + '#type' => 'range', + '#min' => 10, + '#max' => 5, + '#title' => 'Invalid range', + '#description' => 'Minimum greater than maximum.', + ); + $form['submit'] = array( + '#type' => 'submit', + '#value' => 'Submit', + ); + return $form; +} + /** * Builds a form to test the placeholder attribute. */ diff --git a/core/modules/user/user.admin.inc b/core/modules/user/user.admin.inc index 8de4fb92d9d7..c65883ace6ba 100644 --- a/core/modules/user/user.admin.inc +++ b/core/modules/user/user.admin.inc @@ -404,8 +404,6 @@ function user_admin_settings() { '#type' => 'number', '#title' => t('Picture upload file size'), '#default_value' => variable_get('user_picture_file_size', '30'), - '#size' => 10, - '#maxlength' => 10, '#min' => 0, '#field_suffix' => ' ' . t('KB'), '#description' => t('Maximum allowed file size for uploaded pictures. Upload size is normally limited only by the PHP maximum post and file upload settings, and images are automatically scaled down to the dimensions specified above.'), -- GitLab