From: <rb...@us...> - 2017-11-15 21:29:56
|
Revision: 14960 http://sourceforge.net/p/htmlunit/code/14960 Author: rbri Date: 2017-11-15 21:29:53 +0000 (Wed, 15 Nov 2017) Log Message: ----------- URL constuctor and origin property added. Modified Paths: -------------- trunk/htmlunit/src/changes/changes.xml trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/URL.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/URLTest.java Modified: trunk/htmlunit/src/changes/changes.xml =================================================================== --- trunk/htmlunit/src/changes/changes.xml 2017-11-15 21:03:02 UTC (rev 14959) +++ trunk/htmlunit/src/changes/changes.xml 2017-11-15 21:29:53 UTC (rev 14960) @@ -10,6 +10,9 @@ <release version="2.29" date="xx, 2017" description=""> </release> <release version="2.28" date="November 12, 2017" description="Bugfixes, Chrome 62, improved Promise"> + <action type="add" dev="rbri"> + JavaScript: URL constuctor and origin property added. + </action> <action type="fix" dev="rbri" issue="1927"> The link tag now supports the onLoad and the onError handler. </action> Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/URL.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/URL.java 2017-11-15 21:03:02 UTC (rev 14959) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/URL.java 2017-11-15 21:29:53 UTC (rev 14960) @@ -18,12 +18,22 @@ import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.EDGE; import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF; +import java.net.MalformedURLException; + +import org.apache.commons.lang3.StringUtils; + import com.gargoylesoftware.htmlunit.javascript.SimpleScriptable; import com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass; import com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor; +import com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter; import com.gargoylesoftware.htmlunit.javascript.configuration.JsxStaticFunction; import com.gargoylesoftware.htmlunit.javascript.host.file.File; +import com.gargoylesoftware.htmlunit.util.UrlUtils; +import net.sourceforge.htmlunit.corejs.javascript.Context; +import net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime; +import net.sourceforge.htmlunit.corejs.javascript.Undefined; + /** * A JavaScript object for {@code URL}. * @@ -33,14 +43,44 @@ @JsxClass public class URL extends SimpleScriptable { + private java.net.URL url_; + /** * Creates an instance. */ - @JsxConstructor({CHROME, FF, EDGE}) public URL() { } /** + * Creates an instance. + * @param url a string representing an absolute or relative URL. + * If url is a relative URL, base is required, and will be used + * as the base URL. If url is an absolute URL, a given base will be ignored. + * @param base a string representing the base URL to use in case url + * is a relative URL. If not specified, it defaults to ''. + */ + @JsxConstructor({CHROME, FF, EDGE}) + public URL(final String url, final Object base) { + String baseStr = null; + if (Undefined.instance != base) { + baseStr = Context.toString(base); + } + + try { + if (StringUtils.isBlank(baseStr)) { + url_ = UrlUtils.toUrlUnsafe(url); + } + else { + final java.net.URL baseUrl = UrlUtils.toUrlUnsafe(baseStr); + url_ = new java.net.URL(baseUrl, url); + } + } + catch (final MalformedURLException e) { + throw ScriptRuntime.typeError(e.toString()); + } + } + + /** * The URL.createObjectURL() static method creates a DOMString containing a URL * representing the object given in parameter. * The URL lifetime is tied to the document in the window on which it was created. @@ -66,4 +106,34 @@ @JsxStaticFunction public static void revokeObjectURL(final Object objectURL) { } + + /** + * @return the origin + */ + @JsxGetter + public Object getOrigin() { + if (url_ == null) { + return null; + } + + return url_.getProtocol() + "://" + url_.getHost(); + } + + /** + * Calls for instance for implicit conversion to string. + * @see com.gargoylesoftware.htmlunit.javascript.SimpleScriptable#getDefaultValue(java.lang.Class) + * @param hint the type hint + * @return the default value + */ + @Override + public Object getDefaultValue(final Class<?> hint) { + if (url_ == null) { + return super.getDefaultValue(hint); + } + + if (StringUtils.isEmpty(url_.getPath())) { + return url_.toExternalForm() + "/"; + } + return url_.toExternalForm(); + } } Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/URLTest.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/URLTest.java 2017-11-15 21:03:02 UTC (rev 14959) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/URLTest.java 2017-11-15 21:29:53 UTC (rev 14960) @@ -63,6 +63,74 @@ } /** + * @throws Exception if an error occurs + */ + @Test + @Alerts(DEFAULT = { "https://developer.mozilla.org/", "https://developer.mozilla.org/", + "https://developer.mozilla.org/en-US/docs", "https://developer.mozilla.org/en-US/docs", + "https://developer.mozilla.org/en-US/docs", "https://developer.mozilla.org/en-US/docs", + "http://www.example.com/", "type error", "type error" }, + IE = {}) + // @NotYetImplemented({FF, CHROME}) + public void ctor() throws Exception { + final String html = + "<html>\n" + + "<head>\n" + + " <script>\n" + + " function test() {\n" + + " if (typeof window.URL === 'function') {\n" + + " alert(new URL('/', 'https://developer.mozilla.org'));\n" + + " var b = new URL('https://developer.mozilla.org');\n" + + " alert(b);\n" + + " alert(new URL('en-US/docs', b));\n" + + " var d = new URL('/en-US/docs', b);\n" + + " alert(d);\n" + + " alert(new URL('/en-US/docs', d));\n" + + " alert(new URL('/en-US/docs', 'https://developer.mozilla.org/fr-FR/toto'));\n" + + " alert(new URL('http://www.example.com', 'https://developers.mozilla.com'));\n" + + " try {\n" + + " new URL('/en-US/docs', '');\n" + + " } catch(e) { alert('type error'); }\n" + + " try {\n" + + " new URL('/en-US/docs');\n" + + " } catch(e) { alert('type error'); }\n" + + " }\n" + + " }\n" + + " </script>\n" + + "</head>\n" + + "<body onload='test()'>\n" + + "</body>\n" + + "</html>"; + loadPageWithAlerts2(html); + } + + /** + * @throws Exception if an error occurs + */ + @Test + @Alerts(DEFAULT = "http://developer.mozilla.org", + IE = {}) + // @NotYetImplemented({FF, CHROME}) + public void origin() throws Exception { + final String html = + "<html>\n" + + "<head>\n" + + " <script>\n" + + " function test() {\n" + + " if (typeof window.URL === 'function') {\n" + + " var u = new URL('http://developer.mozilla.org/en-US/docs');\n" + + " alert(u.origin);\n" + + " }\n" + + " }\n" + + " </script>\n" + + "</head>\n" + + "<body onload='test()'>\n" + + "</body>\n" + + "</html>"; + loadPageWithAlerts2(html); + } + + /** * @throws Exception if the test fails */ @Test |