Sandboxed Compatibility

Sandboxed Compatibility

Hi There,
yesterday I released a tiny cross browser zero dependencies library which aim is to make sandboxes creation easy for any kind of purpose.
One of these purposes is to avoid conflicts between different libraries thanks to sandbox nature and its clear environment which is both not affected from other global objects and does not affect them, unless explicitly.
This lib is called <b>Elsewhere</b> and you can find the related post here:
<a href="http://webreflection.blogspot.com/2009/07/elsewhere-sandboxes-have-never-been.html">http://webreflection.blogspot.com/2009/07/elsewhere-sandboxes-have-never-been.html</a>
The reason I am writing this email is because as first example I showed how to include and use jQuery in a way that does not require the noConflict call and creating a quick bridge to the dollar function sending the parent.document as default context. So far so good.
The problem is that jQuery uses in different cases the global document as default context. As example, getElementById is performed, obviously, via document.getElementById(selector.slice(1))
This simply means that there is no way to specify a default document context in the entire library. I thought about solutions and here I am with some suggestion which make sense only if jQuery developers will agree with me that a default document context could be useful in many cases.
<b>Avoiding Problems With The Rest Of The Library</b>
To make my proposal as simple as possible without affecting anything else, the first suggestion is about a local document variable. Since jQuery is created in its own scope but document is searched outside this scope (while window is referenced to speed up the look up) we could simply add another variable after window variable declaration.
<span style="font-family: courier new,monospace;">(function(){</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">var </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">    // Will speed up references to window, and allows munging its name.</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">    window = this,</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">    // Will make default document context more flexible</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">    document = window.document,</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">    ...</span>
Above snippet will be just a reference that will cause zero side effects for the entire lib. What's next? A static public function able to set up that variable.
<span style="font-family: courier new,monospace;">// make document var redefinition possible outside this scope</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">jQuery.setDefaultDocument = function(context){</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">    document = context || window.document;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">};</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">jQuery.fn = jQuery.prototype = {</span>
<span style="font-family: courier new,monospace;">    ...</span>
With just these few bytes jQuery will become automatically "sandboxable" and I do not think there will be any side effects, <b>BUT</b> ... there is always a but, a change over that local variable will change the document for every instance and I don't know how many problems this could cause ...
At the same time, since jQuery uses Sizzle, the problem could be solved directly there, in sizzle, since if I am not wrong the variable origContext can make Sizzle calls "sandboxable" but unfortunately in jQuery 1.3.2 Sizzle is used to find, but not inside the init method.
Accordingly, to solve latest problem I wonder if this snippet would be the best one:
<span style="font-family: courier new,monospace;">// let's say we are in a runtime created iframe scope ...</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">parent.jQuery = (function(anonymous){</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">    var document = parent.document,</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">        init = jQuery.fn.init,</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        Sizzle = jQuery.find</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">    ;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">    anonymous.prototype = jQuery.prototype;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">    return function(selector, context){</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        return init.call(new anonymous, Sizzle(selector, document));</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">    };</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">})(Function());</span>
I guess if this is the right way even performances will be the same, isn't it?
Thanks for any kind of suggestion, idea, comment.
Best Regards,
Andrea Giammarchi