From: <rb...@us...> - 2017-11-29 20:13:22
|
Revision: 14982 http://sourceforge.net/p/htmlunit/code/14982 Author: rbri Date: 2017-11-29 20:13:19 +0000 (Wed, 29 Nov 2017) Log Message: ----------- fix Number.toLocaleString() to be able to support locale strings like 'en-US' Issue 1932 Modified Paths: -------------- trunk/htmlunit/src/changes/changes.xml trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/NumberCustom.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/NativeNumberTest.java Modified: trunk/htmlunit/src/changes/changes.xml =================================================================== --- trunk/htmlunit/src/changes/changes.xml 2017-11-29 18:54:53 UTC (rev 14981) +++ trunk/htmlunit/src/changes/changes.xml 2017-11-29 20:13:19 UTC (rev 14982) @@ -8,6 +8,9 @@ <body> <release version="2.29" date="xx, 2017" description=""> + <action type="fix" dev="rbri" issue="1932"> + JavaScript: fix Number.toLocaleString() to be able to support locale strings like 'en-US'. + </action> <action type="add" dev="rbri"> JavaScript: performance.navigation.toJSON() added. </action> Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/NumberCustom.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/NumberCustom.java 2017-11-29 18:54:53 UTC (rev 14981) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/NumberCustom.java 2017-11-29 20:13:19 UTC (rev 14982) @@ -15,10 +15,9 @@ package com.gargoylesoftware.htmlunit.javascript.host; import java.text.NumberFormat; +import java.util.IllformedLocaleException; import java.util.Locale; -import org.apache.commons.lang3.LocaleUtils; - import com.gargoylesoftware.htmlunit.BrowserVersion; import net.sourceforge.htmlunit.corejs.javascript.Context; @@ -49,10 +48,10 @@ if (args.length != 0 && args[0] instanceof String) { final String localeStr = (String) args[0]; try { - final Locale locale = LocaleUtils.toLocale(localeStr); + final Locale locale = new Locale.Builder().setLanguageTag(localeStr).build(); return NumberFormat.getInstance(locale).format(Double.parseDouble(thisObj.toString())); } - catch (final IllegalArgumentException e) { + catch (final IllformedLocaleException e) { throw ScriptRuntime.rangeError("Invalid language tag: " + localeStr); } } Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/NativeNumberTest.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/NativeNumberTest.java 2017-11-29 18:54:53 UTC (rev 14981) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/NativeNumberTest.java 2017-11-29 20:13:19 UTC (rev 14982) @@ -101,6 +101,19 @@ */ @Test @Alerts("12,345") + public void toLocaleStringEnUS() throws Exception { + final String html = "<html><head><script>\n" + + " alert((12345).toLocaleString('en-US'));\n" + + "</script></head><body>\n" + + "</body></html>"; + loadPageWithAlerts2(html); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("12,345") public void toLocaleStringNoParam() throws Exception { final String html = "<html><head><script>\n" + " try {\n" |