Closed Bug 218185 Opened 21 years ago Closed 21 years ago

js-1.5R4.1.jar inside HtmlUnit throws org.mozilla.javascript.EvaluatorException: The undefined value has no properties.

Categories

(Rhino Graveyard :: Core, defect)

x86
Windows 2000
defect
Not set
normal

Tracking

(Not tracked)

VERIFIED INVALID

People

(Reporter: kirshin, Assigned: norrisboyd)

Details

User-Agent:       Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.0.3705)
Build Identifier: 

I use js-1.5R4.1.jar inside HtmlUnit.
I get error message "The undefined value has no properties." parsing the 
javascriptt in the following HTML page:
<html>

<head>
<script language='JavaScript'>
//<!--
function buggy(){
  var option1 = document.f1.elements['select'][0];
  option1.value = 'XXX';    // This line causes the exception
  option1.selected = true;  // This line also causes the exception
}
//-->
</script>
</head>

<body onload='buggy();'>
<form name='f1' action='xxx.html'>
  <SELECT name='select'>
    <OPTION value='A'>111</OPTION>
    <OPTION value='B'>222</OPTION>
  </SELECT>
</form>
</body>

</html>

Following is the stack trace:
org.mozilla.javascript.EvaluatorException: The undefined value has no 
properties.
	at 
com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter.runtimeError
(StrictErrorReporter.java:114)
	at org.mozilla.javascript.Context.reportRuntimeError(Context.java:591)
	at org.mozilla.javascript.Context.reportRuntimeError(Context.java:630)
	at org.mozilla.javascript.Context.reportRuntimeError0(Context.java:600)
	at org.mozilla.javascript.Undefined.reportError(Undefined.java:138)
	at org.mozilla.javascript.Undefined.getPrototype(Undefined.java:100)
	at org.mozilla.javascript.ScriptableObject.getBase
(ScriptableObject.java:1585)
	at org.mozilla.javascript.ScriptableObject.putProperty
(ScriptableObject.java:1473)
	at org.mozilla.javascript.ScriptRuntime.setProp(ScriptRuntime.java:842)
	at org.mozilla.javascript.gen.c1.call(Embedded script:2)
	at org.mozilla.javascript.optimizer.OptRuntime.callSimple
(OptRuntime.java:275)
	at org.mozilla.javascript.gen.c4.call(body.onLoad:1)
	at org.mozilla.javascript.gen.c4.exec(body.onLoad)
	at org.mozilla.javascript.Context.evaluateReader(Context.java:820)
	at org.mozilla.javascript.Context.evaluateString(Context.java:784)
	at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute
(JavaScriptEngine.java:242)
	at 
com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptIfPossible
(HtmlPage.java:889)
	at 
com.gargoylesoftware.htmlunit.html.HtmlPage.executeBodyOnLoadHandlerIfNeeded
(HtmlPage.java:1096)
	at com.gargoylesoftware.htmlunit.html.HtmlPage.initialize
(HtmlPage.java:173)
	at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:356)
	at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:276)
	at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:220)
	at ScriptTest.loadPage(ScriptTest.java:45)
	at ScriptTest.main(ScriptTest.java:31)

Reproducible: Always

Steps to Reproduce:
Run the class with HtmlUnit 1.2.3 (http://htmlunit.sourceforge.net):
------------------------ START ---------------------------------
import java.io.*;
import java.net.*;
import java.util.*;

import com.gargoylesoftware.htmlunit.*;
import com.gargoylesoftware.htmlunit.html.*;
import org.apache.commons.httpclient.HttpState;


public class ScriptTest{
  

public static void main(String[] args) throws Exception{
  String content
    = "<html>"
    + "<head>"
    + "<script language='JavaScript'>"
    + "\n//<!--\n"
    + "function buggy(){"
    + "  var option1 = document.f1.elements['select'][0];"
    + "  option1.value = 'XXX';"
    + "  option1.selected = true;"
    + "}"
    + "\n//-->\n"
    + "</script>"
    + "</head>"
    + "<body onload='buggy();'>"
    + "<form name='f1'>"
    + "  <SELECT name='select'>"
    + "    <OPTION value='A'>A</OPTION>"
    + "    <OPTION value='B'>B</OPTION>"
    + "  </SELECT>"
    + "</form>"
    + "</body></html>";
  HtmlPage page = loadPage(content);
  System.out.println(page.getWebResponse().getContentAsString());
  
  System.out.println("OK");
}


private static HtmlPage loadPage(String html) throws Exception{
  WebClient client = new WebClient();
  
  FakeWebConnection webConnection = new FakeWebConnection(client);
  webConnection.setContent(html);
  client.setWebConnection(webConnection);
  
  return (HtmlPage)client.getPage(new URL("http://www.gargoylesoftware.com"));
}


private static class FakeWebConnection extends WebConnection{
  private class ResponseEntry {
      public ResponseEntry(
              final String content,
              final int statusCode,
              final String statusMessage,
              final String contentType,
              final List responseHeaders ) {

          final Iterator iterator = responseHeaders.iterator();
          while( iterator.hasNext() ) {
              final Object object = iterator.next();
              if( object instanceof KeyValuePair == false ) {
                  throw new IllegalArgumentException(
                      "Only KeyValuePairs may be in the response header list 
but found: "
                      + object.getClass().getName());
              }
          }
          
          content_ = content;
          statusCode_ = statusCode;
          statusMessage_ = statusMessage;
          contentType_ = contentType;
          responseHeaders_ = Collections.unmodifiableList(responseHeaders);
      }

      private final int statusCode_;
      private final String statusMessage_;
      private final String content_;
      private final String contentType_;
      private final List responseHeaders_;
  }

  private final Map responseMap_ = new HashMap(89);
  private ResponseEntry defaultResponseEntry_;

  private SubmitMethod lastMethod_;
  private List lastParameters_;
  private HttpState httpState_ = new HttpState();

  public FakeWebConnection( final WebClient webClient ) {
      super( webClient );
  }

  public WebResponse getResponse(
          final URL url,
          final SubmitMethod method,
          final List parameters,
          final Map requestParameters ) {

      lastMethod_ = method;
      lastParameters_ = parameters;

      ResponseEntry entry = (ResponseEntry)responseMap_.get(url.toExternalForm
());
      if( entry == null ) {
          entry = defaultResponseEntry_;
          if( entry == null ) {
              throw new IllegalStateException(
                  "No response specified that can handle url 
["+url.toExternalForm()+"]");
          }
      }

      final ResponseEntry responseEntry = entry;
      return new WebResponse() {
          public int getStatusCode()         { return 
responseEntry.statusCode_;      }
          public String getStatusMessage()   { return 
responseEntry.statusMessage_;   }
          public String getContentType()     { return 
responseEntry.contentType_;     }
          public String getContentAsString() { return 
responseEntry.content_;         }
          public URL getUrl()                { return 
url;                           }
          public long getLoadTimeInMilliSeconds() { return 0; }

          public String getResponseHeaderValue( final String headerName ) {
              final Iterator iterator = responseEntry.responseHeaders_.iterator
();
              while( iterator.hasNext() ) {
                  final KeyValuePair pair = (KeyValuePair)iterator.next();
                  if( pair.getKey().equals( headerName ) ) {
                      return pair.getValue();
                  }
              }
              return null;
          }

          public InputStream getContentAsStream() throws IOException {
              return TextUtil.toInputStream(responseEntry.content_);
          }
          public byte[] getResponseBody() {
              try{
                  return responseEntry.content_.getBytes("ISO-8859-1");
              }
              catch( final java.io.UnsupportedEncodingException e ){
                  return null;
              }
         }
          public String getContentCharSet() { return "ISO-8859-1"; }
      };
  }


  public SubmitMethod getLastMethod() {
      return lastMethod_;
  }


  public List getLastParameters() {
      return lastParameters_;
  }


  public void setResponse(
          final URL url,
          final String content,
          final int statusCode,
          final String statusMessage,
          final String contentType,
          final List responseHeaders ) {

      final ResponseEntry responseEntry
          = new ResponseEntry(content, statusCode, statusMessage, contentType, 
responseHeaders);

      responseMap_.put( url.toExternalForm(), responseEntry );
  }


  public void setDefaultResponse(
          final String content,
          final int statusCode,
          final String statusMessage,
          final String contentType ) {

      defaultResponseEntry_
          = new ResponseEntry(content, statusCode, statusMessage,
              contentType, Collections.EMPTY_LIST);
  }


  public void setContent( final String content ) {
      setDefaultResponse(content, 200, "OK", "text/html");
  }


  public HttpState getStateForUrl( final URL url ) {
      return httpState_;
  }
}


}
------------------------- END ----------------------------------

Actual Results:  
======= EXCEPTION START ========
Exception class=[org.mozilla.javascript.EvaluatorException]
com.gargoylesoftware.htmlunit.ScriptException: The undefined value has no 
properties.
	at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute
(JavaScriptEngine.java:250)
	at 
com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptIfPossible
(HtmlPage.java:889)
	at 
com.gargoylesoftware.htmlunit.html.HtmlPage.executeBodyOnLoadHandlerIfNeeded
(HtmlPage.java:1096)
	at com.gargoylesoftware.htmlunit.html.HtmlPage.initialize
(HtmlPage.java:173)
	at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:356)
	at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:276)
	at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:220)
	at ScriptTest.loadPage(ScriptTest.java:49)
	at ScriptTest.main(ScriptTest.java:35)
Enclosed exception: 
org.mozilla.javascript.EvaluatorException: The undefined value has no 
properties.
	at 
com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter.runtimeError
(StrictErrorReporter.java:114)
	at org.mozilla.javascript.Context.reportRuntimeError(Context.java:591)
	at org.mozilla.javascript.Context.reportRuntimeError(Context.java:630)
	at org.mozilla.javascript.Context.reportRuntimeError0(Context.java:600)
	at org.mozilla.javascript.Undefined.reportError(Undefined.java:138)
	at org.mozilla.javascript.Undefined.getPrototype(Undefined.java:100)
	at org.mozilla.javascript.ScriptableObject.getBase
(ScriptableObject.java:1585)
	at org.mozilla.javascript.ScriptableObject.putProperty
(ScriptableObject.java:1473)
	at org.mozilla.javascript.ScriptRuntime.setProp(ScriptRuntime.java:842)
	at org.mozilla.javascript.gen.c1.call(Embedded script:2)
	at org.mozilla.javascript.optimizer.OptRuntime.callSimple
(OptRuntime.java:275)
	at org.mozilla.javascript.gen.c4.call(body.onLoad:1)
	at org.mozilla.javascript.gen.c4.exec(body.onLoad)
	at org.mozilla.javascript.Context.evaluateReader(Context.java:820)
	at org.mozilla.javascript.Context.evaluateString(Context.java:784)
	at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute
(JavaScriptEngine.java:242)
	at 
com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptIfPossible
(HtmlPage.java:889)
	at 
com.gargoylesoftware.htmlunit.html.HtmlPage.executeBodyOnLoadHandlerIfNeeded
(HtmlPage.java:1096)
	at com.gargoylesoftware.htmlunit.html.HtmlPage.initialize
(HtmlPage.java:173)
	at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:356)
	at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:276)
	at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:220)
	at ScriptTest.loadPage(ScriptTest.java:49)
	at ScriptTest.main(ScriptTest.java:35)
== CALLING JAVASCRIPT ==
buggy();
======= EXCEPTION END ========
Exception in thread "main"
This is not a Rhino bug, it is a problem with DOM implementation for JS in
HtmlUnit. I guess they did not implemented indexed properties for a select
element so document.f1.elements['select'][0] returns undefined. Try to use 
document.f1.elements['select'].item(0) 
as they may support DOM item method.
Status: NEW → RESOLVED
Closed: 21 years ago
Resolution: --- → INVALID
Marking Verified -
Status: RESOLVED → VERIFIED
Trageting as resolved against 1.5R5
Target Milestone: --- → 1.5R5
You need to log in before you can comment on or make changes to this bug.