Exemple #1
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);
    }
  }