#62 ✓resolved
Thatcher

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;
try{
if(script.type){
     types = script.type?script.type.split(";"):[];
     for(i=0;i<types.length;i++){
        if($env.scriptTypes[types[i]]){
if(script.src){
                   print("loading allowed external script :" + script.src);
                   base = "" + window.location;
                   var docWrites = [];
document.write = function(text){
docWrites.push(text);
}
load($env.location(script.src.match(/([^\?#]*)/)[1], base ));
if(p){
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;
}
               }else{
                    $env.loadInlineScript(script);
               }
          }else{
                if(!script.src && script.type == "text/javascript"){
                            $env.loadInlineScript(script);
                }
          }
       }
   }else{
                //anonymous type and anonymous src means inline
                if(!script.src){
                    $env.loadInlineScript(script);
                }
            }
        }catch(e){
            print("Error loading script.");
            print(e);
        }
    };

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

Pages