1515package org .htmlunit .javascript .host ;
1616
1717import static org .htmlunit .BrowserVersionFeatures .JS_LOCATION_RELOAD_REFERRER ;
18+ import static org .htmlunit .javascript .configuration .SupportedBrowser .IE ;
1819
1920import java .io .IOException ;
2021import java .lang .reflect .Method ;
3435import org .htmlunit .javascript .HtmlUnitScriptable ;
3536import org .htmlunit .javascript .configuration .JsxClass ;
3637import org .htmlunit .javascript .configuration .JsxConstructor ;
38+ import org .htmlunit .javascript .configuration .JsxFunction ;
39+ import org .htmlunit .javascript .configuration .JsxGetter ;
40+ import org .htmlunit .javascript .configuration .JsxSetter ;
3741import org .htmlunit .javascript .host .event .Event ;
3842import org .htmlunit .javascript .host .event .HashChangeEvent ;
3943import org .htmlunit .protocol .javascript .JavaScriptURLConnection ;
@@ -210,6 +214,7 @@ public Object getDefaultValue(final Class<?> hint) {
210214 * @throws IOException if loading the specified location fails
211215 * @see <a href="http://msdn.microsoft.com/en-us/library/ms536342.aspx">MSDN Documentation</a>
212216 */
217+ @ JsxFunction (IE )
213218 public void assign (final String url ) throws IOException {
214219 setHref (url );
215220 }
@@ -221,6 +226,7 @@ public void assign(final String url) throws IOException {
221226 * @throws IOException if there is a problem reloading the page
222227 * @see <a href="http://msdn.microsoft.com/en-us/library/ms536342.aspx">MSDN Documentation</a>
223228 */
229+ @ JsxFunction (IE )
224230 public void reload (final boolean force ) throws IOException {
225231 final WebWindow webWindow = window_ .getWebWindow ();
226232 final HtmlPage htmlPage = (HtmlPage ) webWindow .getEnclosedPage ();
@@ -239,16 +245,30 @@ public void reload(final boolean force) throws IOException {
239245 * @throws IOException if loading the specified location fails
240246 * @see <a href="http://msdn.microsoft.com/en-us/library/ms536712.aspx">MSDN Documentation</a>
241247 */
248+ @ JsxFunction (IE )
242249 public void replace (final String url ) throws IOException {
243250 window_ .getWebWindow ().getHistory ().removeCurrent ();
244251 setHref (url );
245252 }
246253
254+ /**
255+ * Returns the location URL.
256+ * @return the location URL
257+ */
258+ @ JsxFunction (functionName = "toString" , value = IE )
259+ public String jsToString () {
260+ if (window_ != null ) {
261+ return getHref ();
262+ }
263+ return "" ;
264+ }
265+
247266 /**
248267 * Returns the location URL.
249268 * @return the location URL
250269 * @see <a href="http://msdn.microsoft.com/en-us/library/ms533867.aspx">MSDN Documentation</a>
251270 */
271+ @ JsxGetter (IE )
252272 public String getHref () {
253273 final WebWindow webWindow = window_ .getWebWindow ();
254274 final Page page = webWindow .getEnclosedPage ();
@@ -283,6 +303,7 @@ public String getHref() {
283303 * @throws IOException if loading the specified location fails
284304 * @see <a href="http://msdn.microsoft.com/en-us/library/ms533867.aspx">MSDN Documentation</a>
285305 */
306+ @ JsxSetter (IE )
286307 public void setHref (final String newLocation ) throws IOException {
287308 WebWindow webWindow = getWindow (getStartingScope ()).getWebWindow ();
288309 final HtmlPage page = (HtmlPage ) webWindow .getEnclosedPage ();
@@ -315,11 +336,37 @@ public void setHref(final String newLocation) throws IOException {
315336 }
316337 }
317338
339+ /**
340+ * Returns the search portion of the location URL (the portion following the '?').
341+ * @return the search portion of the location URL
342+ * @see <a href="http://msdn.microsoft.com/en-us/library/ms534620.aspx">MSDN Documentation</a>
343+ */
344+ @ JsxGetter (IE )
345+ public String getSearch () {
346+ final String search = getUrl ().getQuery ();
347+ if (search == null ) {
348+ return "" ;
349+ }
350+ return "?" + search ;
351+ }
352+
353+ /**
354+ * Sets the search portion of the location URL (the portion following the '?').
355+ * @param search the new search portion of the location URL
356+ * @throws Exception if an error occurs
357+ * @see <a href="http://msdn.microsoft.com/en-us/library/ms534620.aspx">MSDN Documentation</a>
358+ */
359+ @ JsxSetter (IE )
360+ public void setSearch (final String search ) throws Exception {
361+ setUrl (UrlUtils .getUrlWithNewQuery (getUrl (), search ));
362+ }
363+
318364 /**
319365 * Returns the hash portion of the location URL (the portion following the '#').
320366 * @return the hash portion of the location URL
321367 * @see <a href="http://msdn.microsoft.com/en-us/library/ms533775.aspx">MSDN Documentation</a>
322368 */
369+ @ JsxGetter (IE )
323370 public String getHash () {
324371 String hash = hash_ ;
325372
@@ -353,6 +400,7 @@ private String getHash(final boolean encoded) {
353400 * @param hash the new hash portion of the location URL
354401 * @see <a href="http://msdn.microsoft.com/en-us/library/ms533775.aspx">MSDN Documentation</a>
355402 */
403+ @ JsxSetter (IE )
356404 public void setHash (final String hash ) {
357405 // IMPORTANT: This method must not call setUrl(), because
358406 // we must not hit the server just to change the hash!
@@ -401,6 +449,137 @@ private static String decodeHash(final String hash) {
401449 return UrlUtils .decode (hash );
402450 }
403451
452+ /**
453+ * Returns the hostname portion of the location URL.
454+ * @return the hostname portion of the location URL
455+ * @see <a href="http://msdn.microsoft.com/en-us/library/ms533785.aspx">MSDN Documentation</a>
456+ */
457+ @ JsxGetter (IE )
458+ public String getHostname () {
459+ return getUrl ().getHost ();
460+ }
461+
462+ /**
463+ * Sets the hostname portion of the location URL.
464+ * @param hostname the new hostname portion of the location URL
465+ * @throws Exception if an error occurs
466+ * @see <a href="http://msdn.microsoft.com/en-us/library/ms533785.aspx">MSDN Documentation</a>
467+ */
468+ @ JsxSetter (IE )
469+ public void setHostname (final String hostname ) throws Exception {
470+ setUrl (UrlUtils .getUrlWithNewHost (getUrl (), hostname ));
471+ }
472+
473+ /**
474+ * Returns the host portion of the location URL (the '[hostname]:[port]' portion).
475+ * @return the host portion of the location URL
476+ * @see <a href="http://msdn.microsoft.com/en-us/library/ms533784.aspx">MSDN Documentation</a>
477+ */
478+ @ JsxGetter (IE )
479+ public String getHost () {
480+ final URL url = getUrl ();
481+ final int port = url .getPort ();
482+ final String host = url .getHost ();
483+
484+ if (port == -1 ) {
485+ return host ;
486+ }
487+ return host + ":" + port ;
488+ }
489+
490+ /**
491+ * Sets the host portion of the location URL (the '[hostname]:[port]' portion).
492+ * @param host the new host portion of the location URL
493+ * @throws Exception if an error occurs
494+ * @see <a href="http://msdn.microsoft.com/en-us/library/ms533784.aspx">MSDN Documentation</a>
495+ */
496+ @ JsxSetter (IE )
497+ public void setHost (final String host ) throws Exception {
498+ final String hostname ;
499+ final int port ;
500+ final int index = host .indexOf (':' );
501+ if (index == -1 ) {
502+ hostname = host ;
503+ port = -1 ;
504+ }
505+ else {
506+ hostname = host .substring (0 , index );
507+ port = Integer .parseInt (host .substring (index + 1 ));
508+ }
509+ final URL url = UrlUtils .getUrlWithNewHostAndPort (getUrl (), hostname , port );
510+ setUrl (url );
511+ }
512+
513+ /**
514+ * Returns the pathname portion of the location URL.
515+ * @return the pathname portion of the location URL
516+ * @see <a href="http://msdn.microsoft.com/en-us/library/ms534332.aspx">MSDN Documentation</a>
517+ */
518+ @ JsxGetter (IE )
519+ public String getPathname () {
520+ if (UrlUtils .URL_ABOUT_BLANK == getUrl ()) {
521+ return "blank" ;
522+ }
523+ return getUrl ().getPath ();
524+ }
525+
526+ /**
527+ * Sets the pathname portion of the location URL.
528+ * @param pathname the new pathname portion of the location URL
529+ * @throws Exception if an error occurs
530+ * @see <a href="http://msdn.microsoft.com/en-us/library/ms534332.aspx">MSDN Documentation</a>
531+ */
532+ @ JsxSetter (IE )
533+ public void setPathname (final String pathname ) throws Exception {
534+ setUrl (UrlUtils .getUrlWithNewPath (getUrl (), pathname ));
535+ }
536+
537+ /**
538+ * Returns the port portion of the location URL.
539+ * @return the port portion of the location URL
540+ * @see <a href="http://msdn.microsoft.com/en-us/library/ms534342.aspx">MSDN Documentation</a>
541+ */
542+ @ JsxGetter (IE )
543+ public String getPort () {
544+ final int port = getUrl ().getPort ();
545+ if (port == -1 ) {
546+ return "" ;
547+ }
548+ return Integer .toString (port );
549+ }
550+
551+ /**
552+ * Sets the port portion of the location URL.
553+ * @param port the new port portion of the location URL
554+ * @throws Exception if an error occurs
555+ * @see <a href="http://msdn.microsoft.com/en-us/library/ms534342.aspx">MSDN Documentation</a>
556+ */
557+ @ JsxSetter (IE )
558+ public void setPort (final String port ) throws Exception {
559+ setUrl (UrlUtils .getUrlWithNewPort (getUrl (), Integer .parseInt (port )));
560+ }
561+
562+ /**
563+ * Returns the protocol portion of the location URL, including the trailing ':'.
564+ * @return the protocol portion of the location URL, including the trailing ':'
565+ * @see <a href="http://msdn.microsoft.com/en-us/library/ms534353.aspx">MSDN Documentation</a>
566+ */
567+ @ JsxGetter (IE )
568+ public String getProtocol () {
569+ return getUrl ().getProtocol () + ":" ;
570+ }
571+
572+ /**
573+ * Sets the protocol portion of the location URL.
574+ * @param protocol the new protocol portion of the location URL
575+ * @throws Exception if an error occurs
576+ * @see <a href="http://msdn.microsoft.com/en-us/library/ms534353.aspx">MSDN Documentation</a>
577+ */
578+ @ JsxSetter (IE )
579+ public void setProtocol (final String protocol ) throws Exception {
580+ setUrl (UrlUtils .getUrlWithNewProtocol (getUrl (), protocol ));
581+ }
582+
404583 /**
405584 * Returns this location's current URL.
406585 * @return this location's current URL
@@ -425,4 +604,13 @@ private void setUrl(final URL url) throws IOException {
425604
426605 webWindow .getWebClient ().getPage (webWindow , webRequest );
427606 }
607+
608+ /**
609+ * Returns the {@code origin} property.
610+ * @return the {@code origin} property
611+ */
612+ @ JsxGetter (IE )
613+ public String getOrigin () {
614+ return getUrl ().getProtocol () + "://" + getHost ();
615+ }
428616}
0 commit comments