Exemple #1
0
 /**
  * Sets the value of the "location" property. The location's default property is "href", so
  * setting "document.location='http://www.sf.net'" is equivalent to setting
  * "document.location.href='http://www.sf.net'".
  *
  * @see <a href="http://msdn.microsoft.com/en-us/library/ms535866.aspx">MSDN documentation</a>
  * @param location the location to navigate to
  * @throws IOException when location loading fails
  */
 @JsxSetter
 public void setLocation(final String location) throws IOException {
   final Object event = getWindow().getEvent();
   boolean setLocation = true;
   if (event instanceof UIEvent) {
     final Object target = ((UIEvent) event).getTarget();
     if (target instanceof HTMLAnchorElement) {
       final String href = ((HTMLAnchorElement) target).getHref();
       if (!href.isEmpty()
           && !getBrowserVersion().hasFeature(JS_DOCUMENT_SET_LOCATION_EXECUTED_IN_ANCHOR)) {
         setLocation = false;
       }
     }
   }
   if (setLocation) {
     window_.setLocation(location);
   }
 }
Exemple #2
0
  /**
   * Executes this script node as inline script if necessary and/or possible.
   *
   * <p>attribute is defined, the script is not executed
   */
  private void executeInlineScriptIfNeeded() {
    if (!isExecutionNeeded()) {
      return;
    }

    final String src = getSrcAttribute();
    if (src != DomElement.ATTRIBUTE_NOT_DEFINED) {
      return;
    }

    final DomCharacterData textNode = (DomCharacterData) getFirstChild();
    final String forr = getHtmlForAttribute();
    String event = getEventAttribute();
    // The event name can be like "onload" or "onload()".
    if (event.endsWith("()")) {
      event = event.substring(0, event.length() - 2);
    }

    final boolean ie =
        getPage().getWebClient().getBrowserVersion().hasFeature(BrowserVersionFeatures.GENERATED_7);
    final String scriptCode = textNode.getData();
    if (ie && event != ATTRIBUTE_NOT_DEFINED && forr != ATTRIBUTE_NOT_DEFINED) {
      if ("window".equals(forr)) {
        // everything fine, accepted by IE and FF
        final Window window = (Window) getPage().getEnclosingWindow().getScriptObject();
        final BaseFunction function = new EventHandler(this, event, scriptCode);
        window.jsxFunction_attachEvent(event, function);
      } else {
        try {
          final HtmlElement elt = ((HtmlPage) getPage()).getHtmlElementById(forr);
          elt.setEventHandler(event, scriptCode);
        } catch (final ElementNotFoundException e) {
          LOG.warn(
              "<script for='"
                  + forr
                  + "' ...>: no element found with id \""
                  + forr
                  + "\". Ignoring.");
        }
      }
    } else if (forr == ATTRIBUTE_NOT_DEFINED || "onload".equals(event)) {
      final String url = getPage().getWebResponse().getWebRequest().getUrl().toExternalForm();
      final int line1 = getStartLineNumber();
      final int line2 = getEndLineNumber();
      final int col1 = getStartColumnNumber();
      final int col2 = getEndColumnNumber();
      final String desc =
          "script in "
              + url
              + " from ("
              + line1
              + ", "
              + col1
              + ") to ("
              + line2
              + ", "
              + col2
              + ")";
      HtmlPage html_pg1 = (HtmlPage) getPage();
      html_pg1.atk_ind = this.atk_ind1;
      html_pg1.executeJavaScriptIfPossible(scriptCode, desc, line1);
    }
  }
  @SuppressWarnings("unchecked")
  @Override
  public ExceptionOrReturnValue invoke(
      BrowserChannelClient channel, Value thisObj, String methodName, Value[] args) {
    if (logger.isLoggable(TreeLogger.DEBUG)) {
      logger.log(
          TreeLogger.DEBUG,
          "INVOKE: thisObj: " + thisObj + ", methodName: " + methodName + ", args: " + args);
    }
    /*
     * 1. lookup functions by name. 2. Find context and scope. 3. Convert
     * thisObject to ScriptableObject 4. Convert args 5. Get return value
     */
    Context jsContext = Context.getCurrentContext();
    ScriptableObject jsThis = null;
    if (thisObj.getType() == ValueType.NULL) {
      jsThis = window;
    } else {
      Object obj = makeJsvalFromValue(jsContext, thisObj);
      if (obj instanceof ScriptableObject) {
        jsThis = (ScriptableObject) obj;
      } else if (obj instanceof SimpleScriptableProxy<?>) {
        jsThis = ((SimpleScriptableProxy<SimpleScriptable>) obj).getDelegee();
      } else {
        logger.log(
            TreeLogger.ERROR,
            "Unable to convert "
                + obj
                + " to either "
                + " ScriptableObject or SimpleScriptableProxy");
        return new ExceptionOrReturnValue(true, new Value(null));
      }
    }
    Object functionObject = ScriptableObject.getProperty(window, methodName);
    if (functionObject == ScriptableObject.NOT_FOUND) {
      logger.log(
          TreeLogger.ERROR,
          "function "
              + methodName
              + " NOT FOUND, thisObj: "
              + jsThis
              + ", methodName: "
              + methodName);
      // TODO: see if this maps to QUIT
      return new ExceptionOrReturnValue(true, new Value(null));
    }
    Function jsFunction = (Function) functionObject;
    if (logger.isLoggable(TreeLogger.SPAM)) {
      logger.log(TreeLogger.SPAM, "INVOKE: jsFunction: " + jsFunction);
    }

    Object jsArgs[] = new Object[args.length];
    for (int i = 0; i < args.length; i++) {
      jsArgs[i] = makeJsvalFromValue(jsContext, args[i]);
    }
    Object result = null;
    try {
      if (args.length == 1 && methodName.indexOf(REPLACE_METHOD_SIGNATURE) != -1) {
        // getUrl() is not visible
        String currentUrl = window.jsxGet_location().toString();
        currentUrl = getUrlBeforeHash(currentUrl);
        String newUrl = getUrlBeforeHash((String) args[0].getValue());
        if (!newUrl.equals(currentUrl)) {
          WebWindow webWindow = window.getWebWindow();
          do {
            webWindow.getJobManager().removeAllJobs();
            webWindow = webWindow.getParentWindow();
          } while (webWindow != webWindow.getTopWindow());
        }
      }
      result = jsEngine.callFunction(htmlPage, jsFunction, jsContext, window, jsThis, jsArgs);
    } catch (JavaScriptException ex) {
      if (logger.isLoggable(TreeLogger.INFO)) {
        logger.log(
            TreeLogger.INFO,
            "INVOKE: JavaScriptException "
                + ex
                + ", message: "
                + ex.getMessage()
                + " when invoking "
                + methodName);
      }
      return new ExceptionOrReturnValue(true, makeValueFromJsval(jsContext, ex.getValue()));
    } catch (Exception ex) {
      if (logger.isLoggable(TreeLogger.INFO)) {
        logger.log(
            TreeLogger.INFO,
            "INVOKE: exception "
                + ex
                + ", message: "
                + ex.getMessage()
                + " when invoking "
                + methodName);
      }
      return new ExceptionOrReturnValue(true, makeValueFromJsval(jsContext, Undefined.instance));
    }
    if (logger.isLoggable(TreeLogger.INFO)) {
      logger.log(TreeLogger.INFO, "INVOKE: result: " + result + " of jsFunction: " + jsFunction);
    }
    return new ExceptionOrReturnValue(false, makeValueFromJsval(jsContext, result));
  }
Exemple #4
0
 /** {@inheritDoc} */
 @Override
 public Document getDelegee() {
   final Window w = (Window) webWindow_.getScriptObject();
   return w.getDocument();
 }
Exemple #5
0
 /**
  * Returns the value of the "location" property.
  *
  * @return the value of the "location" property
  */
 @JsxGetter
 public Location getLocation() {
   return window_.getLocation();
 }