#62 ✓resolved

platform/rhino.js should allow document.write during html script tag parsing

Reported by Thatcher | March 24th, 2009 @ 04:24 PM | in 1.0 Release

again thanks to justin meyer for this. I really wasnt aware of this behavior.

the parser should allow the document.write function to append text as hmtl(xml) fragments when parsing a script tag.

and any other use of document.write in say a callback will replace the document entirely.

I quote:

//add parser to calls to loadLocalScript, overwrite document.write, and insert written text into parser
$env.loadLocalScript = function(script, p){
print("loading script ");
var types, type, src, i, base;
     types = script.type?script.type.split(";"):[];
                   print("loading allowed external script :" + script.src);
                   base = "" + window.location;
                   var docWrites = [];
document.write = function(text){
load($env.location(script.src.match(/([^\?#]*)/)[1], base ));
var start = p.m_xml.slice(0,p.m_iP);
var end = p.m_xml.slice(p.m_iP);
p.m_xml = start+docWrites.join('')+end;
                if(!script.src && script.type == "text/javascript"){
                //anonymous type and anonymous src means inline
            print("Error loading script.");

If a script tag, call load script with the parser
else if(iEvt == XMLP._ELM_E) {                  // End-Element Event
 var oldParent = iNodeParent;
 iNodeParent = iNodeParent.parentNode;         // ascend one level of the DOM Tree
 //handle script tag
 if(oldParent.nodeName.toLowerCase() == 'script'){
$policy.loadScript(oldParent, p);

Comments and changes to this ticket

  • Thatcher

    Thatcher March 24th, 2009 @ 04:27 PM

    the solution is based entirely on justins method but a little cleaner due to my familiarity with the code base.

    I added a method the the xml parser 'appendFragment' and since document.write and document.writeln are already stubbed we simple alias them in $env.loadScript and return them to the original fuinction after each script parse.

  • Thatcher

    Thatcher September 9th, 2009 @ 04:29 PM

    • State changed from “new” to “resolved”

    should have been marked closed awhile ago

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

a javascript browser environment

People watching this ticket