Search code examples
javascriptrhino

Is it possible to support document.all() in Rhino?


I am creating my own custom DOM for Rhino.

As you know, legacy IE supports 'nice' document.all() feature to perform a selection of nodes.

it can be called as following way

X) document.all(1).tagName

Y) document.all.[object id].tagName

if i declare functions on my DOM as

public object all(object ___param)
{
   return ....;
}

query X can be processed, but query Y will throw an error (because rhino thinks it is a property.

Instead, if i put property as

public object all
{
     get
        {
                returns ...;
        }
}

Now query Y can processed, but query X ends up with error because it is property!

(It seems to be that i have to give up one of the choice within Rhino javascript interpreter.)

is it possible to create a function object to process both query X and query Y?

Thanks in advance!


Solution

  • There are a couple of problems here.

    First, the syntax in Y is incorrect. It should be:

    document.all[object id].tagName
    

    Second, if you care about compatibility, all is deprecated, even in Internet Explorer.

    Third, your public object all example also relies on deprecated JavaScript syntax. You'll want to use Object.defineProperty.

    Beyond that, you have another problem to solve: there is no standard way, and no way in Rhino, for a JavaScript object to have a "catch-all" method to handle any property name, which is the magic you're looking for from document.all. (The developing specification for ECMAScript 6 contains a feature which you could use called "proxies" but it is not supported in Rhino.) So really the only approach you could use would be to define the functionality on the Java side (see ScriptableObject, for example) and intercept the calls for property access. Or, you could put in a hack that treated the objects as immutable, so on initialization of the document object, you could initialize all the values of document[propertyname] but then they would stop working if you modified the DOM.

    So my recommendation is, this isn't worth it. But if it is, there's your roadmap.