Commit c27cc8d8 authored by Stephen Sigwart's avatar Stephen Sigwart Committed by Commit Bot

Don't allow trailing dot in number inputs to be considered valid if

there's already a dot.

Bug: 1107145
Change-Id: Idda3fc05e148dc8623221bff280c2de965bb1d4f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2358491Reviewed-by: default avatarKent Tamura <tkent@chromium.org>
Commit-Queue: Stephen Sigwart <ssigwart@gmail.com>
Cr-Commit-Position: refs/heads/master@{#799942}
parent 9c6d43dd
...@@ -431,21 +431,27 @@ String Locale::ConvertFromLocalizedNumber(const String& localized) { ...@@ -431,21 +431,27 @@ String Locale::ConvertFromLocalizedNumber(const String& localized) {
builder.ReserveCapacity(input.length()); builder.ReserveCapacity(input.length());
if (is_negative) if (is_negative)
builder.Append('-'); builder.Append('-');
unsigned num_decimal_separators = 0;
for (unsigned i = start_index; i < end_index;) { for (unsigned i = start_index; i < end_index;) {
unsigned symbol_index = MatchedDecimalSymbolIndex(input, i); unsigned symbol_index = MatchedDecimalSymbolIndex(input, i);
if (symbol_index >= kDecimalSymbolsSize) if (symbol_index >= kDecimalSymbolsSize)
return input; return input;
if (symbol_index == kDecimalSeparatorIndex) if (symbol_index == kDecimalSeparatorIndex) {
num_decimal_separators++;
builder.Append('.'); builder.Append('.');
else if (symbol_index == kGroupSeparatorIndex) } else if (symbol_index == kGroupSeparatorIndex) {
return input; return input;
else } else {
builder.Append(static_cast<UChar>('0' + symbol_index)); builder.Append(static_cast<UChar>('0' + symbol_index));
}
} }
String converted = builder.ToString(); String converted = builder.ToString();
// Ignore trailing '.', but will reject '.'-only string later. // Ignore trailing '.', but will reject '.'-only string later.
if (converted.length() >= 2 && converted[converted.length() - 1] == '.') if (converted.length() >= 2 && converted[converted.length() - 1] == '.') {
converted = converted.Left(converted.length() - 1); // Leave it if there are two decimal separators since that's invalid.
if (num_decimal_separators < 2)
converted = converted.Left(converted.length() - 1);
}
return converted; return converted;
} }
......
...@@ -47,6 +47,12 @@ PASS number.value is "1" ...@@ -47,6 +47,12 @@ PASS number.value is "1"
' +1. ' is not a badInput. ' +1. ' is not a badInput.
PASS number.validity.badInput is false PASS number.validity.badInput is false
PASS number.value is "1" PASS number.value is "1"
Arabic number is not a badInput.
PASS number_ar.validity.badInput is false
PASS number_ar.value is "1.1"
Arabic number with two decimal separators is a badInput.
PASS number_ar.validity.badInput is true
PASS number_ar.value is ""
PASS successfullyParsed is true PASS successfullyParsed is true
TEST COMPLETE TEST COMPLETE
......
...@@ -2,6 +2,12 @@ ...@@ -2,6 +2,12 @@
<html> <html>
<head> <head>
<script src="../../../resources/js-test.js"></script> <script src="../../../resources/js-test.js"></script>
<script>
if (window.internals)
internals.settings.setLangAttributeAwareFormControlUIEnabled(true);
else
debug('Require testRunner.');
</script>
<style> <style>
:invalid { :invalid {
background-color: #ff0000; background-color: #ff0000;
...@@ -12,6 +18,7 @@ ...@@ -12,6 +18,7 @@
<div id=parent> <div id=parent>
<input type=number id=number> <input type=number id=number>
<input id=another> <input id=another>
<input type="number" lang="ar-eg" id="number_ar">
</div> </div>
<script> <script>
description('A number input fields with a bad input string should make validity.badInput true and have :invalid style.'); description('A number input fields with a bad input string should make validity.badInput true and have :invalid style.');
...@@ -94,6 +101,26 @@ document.execCommand('InsertText', false, ' '); ...@@ -94,6 +101,26 @@ document.execCommand('InsertText', false, ' ');
shouldBeFalse('number.validity.badInput'); shouldBeFalse('number.validity.badInput');
shouldBeEqualToString('number.value', '1'); shouldBeEqualToString('number.value', '1');
var number_ar = document.getElementById('number_ar');
number_ar.focus();
debug("Arabic number is not a badInput.");
var arabicNum = Number(1.1).toLocaleString('ar-eg');
document.execCommand('InsertText', false, arabicNum);
// Windows seems to have issues with Arabic decimal separator, so use dot.
var isWindows = (number_ar.value == '11');
if (isWindows)
{
arabicNum = arabicNum.replace(arabicNum[1], '.');
number_ar.value = "";
document.execCommand('InsertText', false, arabicNum);
}
shouldBeFalse('number_ar.validity.badInput');
shouldBeEqualToString('number_ar.value', '1.1');
debug("Arabic number with two decimal separators is a badInput.");
document.execCommand('InsertText', false, arabicNum[1]);
shouldBeTrue('number_ar.validity.badInput');
shouldBeEqualToString('number_ar.value', '');
document.getElementById('parent').innerHTML = ''; document.getElementById('parent').innerHTML = '';
</script> </script>
</body> </body>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment