Menu

#485 Bad context in evaluation of the javascript.

closed
None
5
2014-08-17
2007-05-29
tonylaq
No

Hi All,
I have still not been able to pinpoint what's the problem. But when we submit a form and that we are doing javascript.
<form onsubmit="test(this)"/>
Look like the javascript is not able to interpret the this ?!? as a form ?

I have a big stackTrace error.
In JavaScripEngine --> callFunction
In the function getSourceCode:
cx.decompileFunction(function, 2);

This return :
function () {
[native code, arity=0]
}

I also join 2 unit test that failed.
One direct, one with a redirection. ( We need to make sure that both are working correctly).

FYI : This is a major issue for us. We get 25% of our test not working anymore because of that.
We get that since 1.10. ( We live with 1.9 until now but we really want to move on 1.11.)

This seem to be related to bug#1627983.
Let me know if you need more information.

thanks,
anthony

Discussion

  • tonylaq

    tonylaq - 2007-05-29

    2 Testcase that currently fail.

     
  • Marc Guillemot

    Marc Guillemot - 2007-05-29

    Logged In: YES
    user_id=402164
    Originator: NO

    You have this problem with 1.10? Can you try with the latest source from SVN? Major changes have been made in js since last release.

     
  • tonylaq

    tonylaq - 2007-05-29

    Logged In: YES
    user_id=1371534
    Originator: YES

    Hi Marc,
    I send that using reply.
    Sorry :-)

    So we get that error with 1.11 ( trunk version )
    Can you try to run the 2 units case I have send you ?

    anthony

     
  • tonylaq

    tonylaq - 2007-05-29

    Logged In: YES
    user_id=1371534
    Originator: YES

    Hi marc,
    Sorry I did make a mistake in my unit test.
    here's the new and good version (that still failed with the clean trunk )
    Thanks for your help!
    anthony

    public void testOnSubmitChangesContext1() throws Exception {
        final String[] expectedAlerts = {"elementValue", "elementValue", "elementValue"};
        final String content = 
        "<html><body>" + 
        "<form id='formtest' onsubmit='test(this)'><input id='element' value='elementValue'/>" +
        "    <input type='submit' id='button1' />" +
        " </form>" +
             "<script> \n"
            + "var docPatate = 'patate';"
            + "function test(f) {\n"
            + " alert(eval(\"document.forms['formtest'].element.value\")); \n"
            + " alert(f.element.value); \n"
            + " alert(eval('f.element.value')); \n"
            + "}\n"
            + "</script>"
            + "</body></html>";
        createTestPageForRealBrowserIfNeeded(content, expectedAlerts);
        final List collectedAlerts = new ArrayList();
        final HtmlPage page = loadPage(content, collectedAlerts);
        ((com.gargoylesoftware.htmlunit.html.ClickableElement) page.getHtmlElementById("button1")).click();
        java.util.Iterator i = collectedAlerts.iterator();
        System.out.println("Result : ");
        while(i.hasNext()){
            System.out.println("I:" + i.next());
        }
        assertEquals(expectedAlerts, collectedAlerts);
    }
    
    public void testOnSubmitChangesContext() throws Exception {
        final String[] expectedAlerts = {"elementValue", "elementValue", "elementValue","elementValue", "elementValue", "elementValue"};
        final String content = 
        "<html><body>" + 
        "<form id='formtest' onsubmit='test(this)'><input id='element' value='elementValue'/>" +
        "    <input type='submit' id='button1' />" +
        " </form>" +
             "<script> \n"
            + "var docPatate = 'patate';"
            + "function test(f) {\n"
            + " alert(eval(\"document.forms['formtest'].element.value\")); \n"
            + " alert(f.element.value); \n"
            + " alert(eval('f.element.value')); \n"
            + " test2(f); \n"
            + "}\n"
            + "function test2(f) {\n"
            + " alert(eval(\"document.forms['formtest'].element.value\")); \n"
            + " alert(f.element.value); \n"
            + " alert(eval('f.element.value')); \n"
            + "}\n"
            //+ " test();"
            + "</script>"
            + "</body></html>";
        createTestPageForRealBrowserIfNeeded(content, expectedAlerts);
        final List collectedAlerts = new ArrayList();
        final HtmlPage page = loadPage(content, collectedAlerts);
        ((com.gargoylesoftware.htmlunit.html.ClickableElement) page.getHtmlElementById("button1")).click();
        java.util.Iterator i = collectedAlerts.iterator();
        System.out.println("Result : ");
        while(i.hasNext()){
            System.out.println("I:" + i.next());
        }
        assertEquals(expectedAlerts, collectedAlerts);
    }
    
     
  • Ahmed Ashour

    Ahmed Ashour - 2007-05-29

    Logged In: YES
    user_id=950730
    Originator: NO

    The exception seems to come only from:

    alert(eval('f.element.value'));

     
  • Ahmed Ashour

    Ahmed Ashour - 2007-05-29

    Logged In: YES
    user_id=950730
    Originator: NO

    Attached is a 'SimpleEvalBug' test case.

    I am not sure if this is a Rhino bug or not.
    File Added: SimpleEvalBug.java

     
  • Ahmed Ashour

    Ahmed Ashour - 2007-05-29

    Simple test case

     
  • Marc Guillemot

    Marc Guillemot - 2007-05-30

    Logged In: YES
    user_id=402164
    Originator: NO

    seems to be related to the way event code execution is started because unit test com.gargoylesoftware.htmlunit.javascript.host.Window#testEvalScopeLocal (probably from you as it is already cooked with patate) already works.

     
  • tonylaq

    tonylaq - 2007-05-30

    Logged In: YES
    user_id=1371534
    Originator: YES

    Well, I just inspire my unit test from that test :-)

    But the fact is that this fail.
    Do you can think a way to resolve that ?
    Is it a Rhino problem or a HtmlUnit problem ?
    thanks again!

     
  • Marc Guillemot

    Marc Guillemot - 2007-05-30

    Logged In: YES
    user_id=402164
    Originator: NO

    I think that this is an HtmlUnit problem. I guess that we use the wrong scope or something like that when evaluating the handler.

     
  • Ahmed Ashour

    Ahmed Ashour - 2007-06-30

    Logged In: YES
    user_id=950730
    Originator: NO

    Well, the difference is:

    alert(f.tagName);
    uses scope of NativeCall that finds 'f'

    while:
    alert(eval('f.tagName'));
    uses Window scope (see Window.custom_eval)

    Any idea how to get the NativeCall scope in custom_eval()?

     
  • Marc Guillemot

    Marc Guillemot - 2007-07-24

    Logged In: YES
    user_id=402164
    Originator: NO

    Seems to be fixed: SimpleEvalBug.java works with current SVN state. I can imagine that this is a side effect of Daniel's last changes.

    @Ahmed:
    in such a case, please add the unit test as notYetImplemented(), this allows to directly detect when a bug gets fixed as side effect of other changes.

     

Log in to post a comment.