From: <rb...@us...> - 2017-06-16 18:30:48
|
Revision: 14617 http://sourceforge.net/p/htmlunit/code/14617 Author: rbri Date: 2017-06-16 18:30:45 +0000 (Fri, 16 Jun 2017) Log Message: ----------- more locale tests and fixes Modified Paths: -------------- 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/main/java/com/gargoylesoftware/htmlunit/javascript/host/NumberCustom.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/NumberCustom.java 2017-06-16 16:37:19 UTC (rev 14616) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/NumberCustom.java 2017-06-16 18:30:45 UTC (rev 14617) @@ -17,8 +17,13 @@ import java.text.NumberFormat; import java.util.Locale; +import org.apache.commons.lang3.LocaleUtils; + +import com.gargoylesoftware.htmlunit.BrowserVersion; + import net.sourceforge.htmlunit.corejs.javascript.Context; import net.sourceforge.htmlunit.corejs.javascript.Function; +import net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime; import net.sourceforge.htmlunit.corejs.javascript.Scriptable; /** @@ -25,6 +30,7 @@ * Contains some missing features of Rhino NativeNumber. * * @author Ahmed Ashour + * @author Ronald Brill */ public final class NumberCustom { @@ -40,11 +46,19 @@ */ public static String toLocaleString( final Context context, final Scriptable thisObj, final Object[] args, final Function function) { - String string = thisObj.toString(); if (args.length != 0 && args[0] instanceof String) { - final Locale locale = Locale.forLanguageTag((String) args[0]); - string = NumberFormat.getInstance(locale).format(Double.parseDouble(string)); + final String localeStr = (String) args[0]; + try { + final Locale locale = LocaleUtils.toLocale(localeStr); + return NumberFormat.getInstance(locale).format(Double.parseDouble(thisObj.toString())); + } + catch (final IllegalArgumentException e) { + throw ScriptRuntime.rangeError("Invalid language tag: " + localeStr); + } } - return string; + + final BrowserVersion browserVersion = ((Window) thisObj.getParentScope()).getBrowserVersion(); + final Locale locale = Locale.forLanguageTag(browserVersion.getBrowserLanguage()); + return NumberFormat.getInstance(locale).format(Double.parseDouble(thisObj.toString())); } } Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/NativeNumberTest.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/NativeNumberTest.java 2017-06-16 16:37:19 UTC (rev 14616) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/NativeNumberTest.java 2017-06-16 18:30:45 UTC (rev 14617) @@ -26,6 +26,7 @@ * * @author Marc Guillemot * @author Ahmed Ashour + * @author Ronald Brill */ @RunWith(BrowserRunner.class) public class NativeNumberTest extends WebDriverTestCase { @@ -94,4 +95,34 @@ + "</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" + + " alert((12345).toLocaleString());\n" + + " } catch(e) { alert(e); }\n" + + "</script></head><body>\n" + + "</body></html>"; + loadPageWithAlerts2(html); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("exception") + public void toLocaleStringHintertupfingen() throws Exception { + final String html = "<html><head><script>\n" + + " try {\n" + + " alert((12345).toLocaleString('Hintertupfingen'));\n" + + " } catch(e) { alert('exception'); }\n" + + "</script></head><body>\n" + + "</body></html>"; + loadPageWithAlerts2(html); + } } |