diff --git a/core/misc/machine-name.js b/core/misc/machine-name.js index e76292e..d0db8d5 100644 --- a/core/misc/machine-name.js +++ b/core/misc/machine-name.js @@ -61,8 +61,13 @@ var options = data.options; var baseValue = $(e.target).val(); + // Remove punctuations and whitespaces from beginning and end of string. + var trash = '[\\u2000-\\u206F\\u2E00-\\u2E7F\\!"\'#$%&()*+,\\-.\\/:;<=>?@\\[\\]^_`{|}~]|\\s'; + var trashRE = new RegExp('^(:trash:)+|(:trash:)+$'.replace(/:trash:/g, trash), 'g'); + baseValue = baseValue.toLowerCase().replace(trashRE, ''); + var rx = new RegExp(options.replace_pattern, 'g'); - var expected = baseValue.toLowerCase().replace(rx, options.replace).substr(0, options.maxlength); + var expected = baseValue.replace(rx, options.replace).substr(0, options.maxlength); // Abort the last pending request because the label has changed and it // is no longer valid. @@ -77,7 +82,7 @@ clearTimeout(timeout); timeout = null; } - if (baseValue.toLowerCase() !== expected) { + if (baseValue !== expected) { timeout = setTimeout(function () { xhr = self.transliterate(baseValue, options).done(function (machine) { self.showMachineName(machine.substr(0, options.maxlength), data); diff --git a/core/tests/Drupal/FunctionalJavascriptTests/Core/MachineNameTest.php b/core/tests/Drupal/FunctionalJavascriptTests/Core/MachineNameTest.php index bf2643d..431bef2 100644 --- a/core/tests/Drupal/FunctionalJavascriptTests/Core/MachineNameTest.php +++ b/core/tests/Drupal/FunctionalJavascriptTests/Core/MachineNameTest.php @@ -48,13 +48,18 @@ public function testMachineName() { [ 'input' => 'Test value !0-9@', 'message' => 'A title that should be transliterated must be equal to the php generated machine name', - 'expected' => 'test_value_0_9_', + 'expected' => 'test_value_0_9', ], [ 'input' => 'Test value', 'message' => 'A title that should not be transliterated must be equal to the php generated machine name', 'expected' => 'test_value', ], + [ + 'input' => ', Neglect?! ', + 'message' => 'A title that should not be transliterated must be equal to the php generated machine name', + 'expected' => 'neglect', + ], ]; // Get page and session. @@ -110,7 +115,7 @@ public function testMachineName() { $this->assertEquals(TRUE, $machine_name_2_wrapper->hasClass('visually-hidden'), 'The ID field must not be visible'); // Validate if the element contains the correct value. - $this->assertEquals($test_values[1]['expected'], $machine_name_1_field->getValue(), 'The ID field value must be equal to the php generated machine name'); + $this->assertEquals(end($test_values)['expected'], $machine_name_1_field->getValue(), 'The ID field value must be equal to the php generated machine name'); } }