Menu

#1133 Function.toString behavior does not match browsers

open
nobody
None
5
2012-10-21
2010-07-13
No

The implementation of Function.toString() in HtmlUnit (and Rhino) does not match that of the web browsers that HtmlUnit is supposed to mimic.

HtmlUnit's implementation imperfectly mimics Firefox's behavior, even when it is configured to behave like Internet Explorer.

The implementation deviates from Firefox's behavior in that that HtmlUnit inserts newlines before and after the source code, while Firefox does not. Additionally, Firefox sometimes optimizes out certain expressions, but HtmlUnit does not.

The implementation deviates from Internet Explorer's behavior in that while HtmlUnit/Rhino and Firefox normalize and pretty-print a function's source code, IE returns the original source code of the function verbatim, with no normalization or pretty-printing. Additionally, if an anonymous function definition is surrounded in parentheses, IE will include the parentheses (even multiple layers of parentheses) in the source code that is returned, e.g.,
(((function() {})))

See the attached file for an example of using Function.toString.

Incidentally, Google Chrome has the same behavior as IE, and I believe Opera does as well.

Discussion

  • Richard Eggert

    Richard Eggert - 2010-07-13

    HTML containing script that calls Function.toString, whose output depends on the browser used

     
  • Richard Eggert

    Richard Eggert - 2010-07-13

    Note that the optimizing behavior of Firefox is likely to be difficult to mimic, especially since it's not consistent even among different builds of Firefox (e.g., the Windows build of Firefox differs from the Linux build in the level of optimization).

     
  • Richard Eggert

    Richard Eggert - 2010-07-13

    Some notes on the parentheses:
    If there is white space included between parentheses wrapping an anonymous function definition, IE will include the white space as well as the parentheses, exactly as they appeared in the source.

    Chrome does not including the parentheses, but otherwise returns the source code verbatim.

     
  • Richard Eggert

    Richard Eggert - 2010-07-13

    Another deviation: HtmlUnit pretty-prints nested anonymous function defintions, but Firefox does not. For example,

    function foo(){var x = function(abc){return 1;}; x();}

    In this example, foo.toString() will be rendered by Firefox with the "var x = function(abc){return 1;};" part all on one line, but HtmlUnit will insert spaces and newlines before and after the "return 1;" statement.

     
  • Marc Guillemot

    Marc Guillemot - 2010-07-14

    This is a bug, no discussion but it won't be easy to fix.

    Just curious: do you have some JS code that rely on the Function.toString and therefore causes problems to you with HtmlUnit?

     
  • Richard Eggert

    Richard Eggert - 2010-07-14

    I don't personally (that would be bad coding practice), but I've used HtmlUnit to access pages that do.

     
  • Marc Guillemot

    Marc Guillemot - 2011-05-26

    FYI: I've just committed some changes fixing many of the points mentioned in this issue (but not all):
    - representation of native functions now match the one of the simulated browsers
    - no new lines are inserted anymore before and after the source code

     

Log in to post a comment.