HtmlUnit / htmlunit

HtmlUnit is a "GUI-Less browser for Java programs".
https://www.htmlunit.org
Apache License 2.0
879 stars 172 forks source link

ES2015 classes are not supported #139

Open niloc132 opened 4 years ago

niloc132 commented 4 years ago
[ERROR] com.gargoylesoftware.htmlunit.ScriptException: identifier is a reserved word: class

Tested on 2.36.0, 2.37.0 in maven central. Here's a sample HTML file that demonstrates the issue - works in any modern browser, but does not work in htmlunit:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <title>ecmascript 2015 class example</title>
</head>
<body>
    <script>
        class Foo {
            constructor(bar) {
                this.bar = bar;
            }
            baz() {
                return "hello " + this.bar;
            }
        }

        console.log(new Foo("world").baz());
    </script>
</body>
</html>

Output in chrome, ff: hello world.

Is this something that needs to be explicitly done upstream in rhino? I didn't find a bug report or PR on their github repo, but I could well have missed it.

Full stack trace of the failure when it occurred:

net.sourceforge.htmlunit.corejs.javascript.EvaluatorException: identifier is a reserved word: class (script in http://localhost:39421/sample.html from (7, 10) to (18, 11)#8)
    at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory$HtmlUnitErrorReporter.error(HtmlUnitContextFactory.java:438)
    at net.sourceforge.htmlunit.corejs.javascript.Parser.addError(Parser.java:258)
    at net.sourceforge.htmlunit.corejs.javascript.Parser.reportError(Parser.java:326)
    at net.sourceforge.htmlunit.corejs.javascript.Parser.reportError(Parser.java:313)
    at net.sourceforge.htmlunit.corejs.javascript.Parser.primaryExpr(Parser.java:3186)
    at net.sourceforge.htmlunit.corejs.javascript.Parser.memberExpr(Parser.java:2743)
    at net.sourceforge.htmlunit.corejs.javascript.Parser.unaryExpr(Parser.java:2637)
    at net.sourceforge.htmlunit.corejs.javascript.Parser.mulExpr(Parser.java:2558)
    at net.sourceforge.htmlunit.corejs.javascript.Parser.addExpr(Parser.java:2542)
    at net.sourceforge.htmlunit.corejs.javascript.Parser.shiftExpr(Parser.java:2523)
    at net.sourceforge.htmlunit.corejs.javascript.Parser.relExpr(Parser.java:2498)
    at net.sourceforge.htmlunit.corejs.javascript.Parser.eqExpr(Parser.java:2470)
    at net.sourceforge.htmlunit.corejs.javascript.Parser.bitAndExpr(Parser.java:2459)
    at net.sourceforge.htmlunit.corejs.javascript.Parser.bitXorExpr(Parser.java:2448)
    at net.sourceforge.htmlunit.corejs.javascript.Parser.bitOrExpr(Parser.java:2437)
    at net.sourceforge.htmlunit.corejs.javascript.Parser.andExpr(Parser.java:2426)
    at net.sourceforge.htmlunit.corejs.javascript.Parser.orExpr(Parser.java:2415)
    at net.sourceforge.htmlunit.corejs.javascript.Parser.condExpr(Parser.java:2379)
    at net.sourceforge.htmlunit.corejs.javascript.Parser.assignExpr(Parser.java:2335)
    at net.sourceforge.htmlunit.corejs.javascript.Parser.expr(Parser.java:2314)
    at net.sourceforge.htmlunit.corejs.javascript.Parser.statementHelper(Parser.java:1273)
    at net.sourceforge.htmlunit.corejs.javascript.Parser.statement(Parser.java:1127)
    at net.sourceforge.htmlunit.corejs.javascript.Parser.parse(Parser.java:664)
    at net.sourceforge.htmlunit.corejs.javascript.Parser.parse(Parser.java:583)
    at net.sourceforge.htmlunit.corejs.javascript.Context.parse(Context.java:2637)
    at net.sourceforge.htmlunit.corejs.javascript.Context.compileImpl(Context.java:2586)
    at net.sourceforge.htmlunit.corejs.javascript.Context.compileString(Context.java:1590)
    at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory$TimeoutContext.compileString(HtmlUnitContextFactory.java:224)
    at net.sourceforge.htmlunit.corejs.javascript.Context.compileString(Context.java:1579)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$1.doRun(JavaScriptEngine.java:704)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:867)
    at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:624)
    at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:537)
    at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.callSecured(HtmlUnitContextFactory.java:354)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.compile(JavaScriptEngine.java:713)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.compile(JavaScriptEngine.java:679)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:725)
    at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScript(HtmlPage.java:924)
    at com.gargoylesoftware.htmlunit.html.HtmlScript.executeInlineScriptIfNeeded(HtmlScript.java:305)
    at com.gargoylesoftware.htmlunit.html.HtmlScript.executeScriptIfNeeded(HtmlScript.java:395)
    at com.gargoylesoftware.htmlunit.html.HtmlScript$2.execute(HtmlScript.java:234)
    at com.gargoylesoftware.htmlunit.html.HtmlScript.onAllChildrenAddedToPage(HtmlScript.java:256)
    at com.gargoylesoftware.htmlunit.html.parser.neko.HtmlUnitNekoDOMBuilder.endElement(HtmlUnitNekoDOMBuilder.java:559)
    at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source)
    at com.gargoylesoftware.htmlunit.html.parser.neko.HtmlUnitNekoDOMBuilder.endElement(HtmlUnitNekoDOMBuilder.java:513)
    at net.sourceforge.htmlunit.cyberneko.HTMLTagBalancer.callEndElement(HTMLTagBalancer.java:1192)
    at net.sourceforge.htmlunit.cyberneko.HTMLTagBalancer.endElement(HTMLTagBalancer.java:1132)
    at net.sourceforge.htmlunit.cyberneko.filters.DefaultFilter.endElement(DefaultFilter.java:219)
    at net.sourceforge.htmlunit.cyberneko.filters.NamespaceBinder.endElement(NamespaceBinder.java:312)
    at net.sourceforge.htmlunit.cyberneko.HTMLScanner$ContentScanner.scanEndElement(HTMLScanner.java:3185)
    at net.sourceforge.htmlunit.cyberneko.HTMLScanner$ContentScanner.scan(HTMLScanner.java:2110)
    at net.sourceforge.htmlunit.cyberneko.HTMLScanner.scanDocument(HTMLScanner.java:937)
    at net.sourceforge.htmlunit.cyberneko.HTMLConfiguration.parse(HTMLConfiguration.java:443)
    at net.sourceforge.htmlunit.cyberneko.HTMLConfiguration.parse(HTMLConfiguration.java:394)
    at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
    at com.gargoylesoftware.htmlunit.html.parser.neko.HtmlUnitNekoDOMBuilder.parse(HtmlUnitNekoDOMBuilder.java:758)
    at com.gargoylesoftware.htmlunit.html.parser.neko.HtmlUnitNekoHtmlParser.parse(HtmlUnitNekoHtmlParser.java:236)
    at com.gargoylesoftware.htmlunit.html.parser.neko.HtmlUnitNekoHtmlParser.parseHtml(HtmlUnitNekoHtmlParser.java:179)
    at com.gargoylesoftware.htmlunit.DefaultPageCreator.createHtmlPage(DefaultPageCreator.java:280)
    at com.gargoylesoftware.htmlunit.DefaultPageCreator.createPage(DefaultPageCreator.java:163)
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:553)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:419)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:336)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.get(HtmlUnitDriver.java:715)
black-snow commented 4 years ago

Apart from the issue that "newer" ES features might not be supported, ES6 and HTML 4.1 seems like a weird combination. Also, previous to HTML5 there should be a type attribute in your script tag :)

niloc132 commented 4 years ago

Whoops, yeah, copied the wrong doctype string. Doctype should not affect this though, but I'll double check to be sure (and my experience always was that it is "required" in theory, but rarely in practice).

black-snow commented 4 years ago

@niloc132 did you check if Rhino supports these features and if htmlunit-core-js is up to date?

rbri commented 4 years ago

regarding core-js - please trust me this is up to data. Have spend a lot of time during the last year to backport some of our changes to rhino and improving rhino at various places. Have a look at commit list - i did most of the changes for the last releases. But Rhino still lacks some js features. Any help is welcome.

black-snow commented 3 years ago

Just stumbled over this again. Here's a (mostly red) es2015 support chart: https://mozilla.github.io/rhino/compat/engines.html @rbri is there a list of things that work with your improvements upon Rhino?

rbri commented 3 years ago

have merged back nearly all improvements into rhino and rhino makes some good progress at the moment. Let's cross our fingers

rbri commented 8 months ago

see #755