From: <asa...@us...> - 2017-06-16 16:26:55
|
Revision: 14615 http://sourceforge.net/p/htmlunit/code/14615 Author: asashour Date: 2017-06-16 16:26:53 +0000 (Fri, 16 Jun 2017) Log Message: ----------- JavaScript: fix Number.toLocaleString(). Issue 1892 Modified Paths: -------------- trunk/htmlunit/src/changes/changes.xml trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/JavaScriptEngine.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/NativeNumberTest.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLAudioElementTest.java Added Paths: ----------- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/NumberCustom.java Modified: trunk/htmlunit/src/changes/changes.xml =================================================================== --- trunk/htmlunit/src/changes/changes.xml 2017-06-16 16:04:04 UTC (rev 14614) +++ trunk/htmlunit/src/changes/changes.xml 2017-06-16 16:26:53 UTC (rev 14615) @@ -8,6 +8,9 @@ <body> <release version="2.28" date="???" description="Bugfixes"> + <action type="fix" dev="asashour" issue="1892"> + JavaScript: fix Number.toLocaleString(). + </action> <action type="add" dev="rbri" > JavaScript: SVGMatrix operations (flipX, flipY, inverse, multiply, rotate, rotateFromVector, scale, scaleNonUniform, skewX, skewY, translate) implemented. Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/JavaScriptEngine.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/JavaScriptEngine.java 2017-06-16 16:04:04 UTC (rev 14614) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/JavaScriptEngine.java 2017-06-16 16:26:53 UTC (rev 14615) @@ -58,6 +58,7 @@ import com.gargoylesoftware.htmlunit.javascript.host.ActiveXObject; import com.gargoylesoftware.htmlunit.javascript.host.ArrayCustom; import com.gargoylesoftware.htmlunit.javascript.host.DateCustom; +import com.gargoylesoftware.htmlunit.javascript.host.NumberCustom; import com.gargoylesoftware.htmlunit.javascript.host.ObjectCustom; import com.gargoylesoftware.htmlunit.javascript.host.Reflect; import com.gargoylesoftware.htmlunit.javascript.host.StringCustom; @@ -498,6 +499,11 @@ ArrayCustom.class, ScriptableObject.DONTENUM); } + final ScriptableObject numberPrototype + = (ScriptableObject) ScriptableObject.getClassPrototype(window, "Number"); + ((ScriptableObject) numberPrototype).defineFunctionProperties(new String[] {"toLocaleString"}, + NumberCustom.class, ScriptableObject.DONTENUM); + window.setPrototypes(prototypes, prototypesPerJSName); window.initialize(webWindow); } Added: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/NumberCustom.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/NumberCustom.java (rev 0) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/NumberCustom.java 2017-06-16 16:26:53 UTC (rev 14615) @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2002-2017 Gargoyle Software Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.gargoylesoftware.htmlunit.javascript.host; + +import java.text.NumberFormat; +import java.util.Locale; + +import net.sourceforge.htmlunit.corejs.javascript.Context; +import net.sourceforge.htmlunit.corejs.javascript.Function; +import net.sourceforge.htmlunit.corejs.javascript.Scriptable; + +/** + * Contains some missing features of Rhino NativeNumber. + * + * @author Ahmed Ashour + */ +public final class NumberCustom { + + private NumberCustom() { } + + /** + * Returns a string with a language sensitive representation of this number. + * @param context the JavaScript context + * @param thisObj the scriptable + * @param args the arguments passed into the method + * @param function the function + * @return the string + */ + 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)); + } + return string; + } +} Property changes on: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/NumberCustom.java ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property 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:04:04 UTC (rev 14614) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/NativeNumberTest.java 2017-06-16 16:26:53 UTC (rev 14615) @@ -22,11 +22,10 @@ import com.gargoylesoftware.htmlunit.WebDriverTestCase; /** - * Number is a native JavaScript object and therefore provided by Rhino but some tests are needed here - * to be sure that we have the expected results (for instance EcmaScript 5 adds methods that are not - * available in FF2 or FF3). + * Number is a native JavaScript object. * * @author Marc Guillemot + * @author Ahmed Ashour */ @RunWith(BrowserRunner.class) public class NativeNumberTest extends WebDriverTestCase { @@ -69,4 +68,29 @@ + "</body></html>"; loadPageWithAlerts2(html); } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("12,345") + public void toLocaleString() throws Exception { + final String html = "<html><head><script>\n" + + " alert((12345).toLocaleString('en'));\n" + + "</script></head><body>\n" + + "</body></html>"; + loadPageWithAlerts2(html); + } + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("12.345") + public void toLocaleStringDe() throws Exception { + final String html = "<html><head><script>\n" + + " alert((12345).toLocaleString('de'));\n" + + "</script></head><body>\n" + + "</body></html>"; + loadPageWithAlerts2(html); + } } Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLAudioElementTest.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLAudioElementTest.java 2017-06-16 16:04:04 UTC (rev 14614) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLAudioElementTest.java 2017-06-16 16:26:53 UTC (rev 14615) @@ -170,4 +170,5 @@ + "</body></html>"; loadPageWithAlerts2(html); - }} + } +} |