Skip to content

Commit

Permalink
Added tests and code for script-onload event handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
gleneivey committed Jul 24, 2009
1 parent d15aa2c commit 19805a0
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 32 deletions.
28 changes: 18 additions & 10 deletions dist/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -3522,9 +3522,19 @@ var DOMImplementation = function() {
this.errorChecking = true; // by default, test for exceptions
};

var $handleEndOfNormalOrEmptyElement = function(node, doc){
var $handleEndOfNormalOrEmptyElement = function(node, doc, p){
if(node.nodeName.toLowerCase() == 'script'){
p.replaceEntities = true;
$env.loadLocalScript(node, p);

if (node.nodeName.toLowerCase() == 'iframe'){
// only fire event if we actually had something to load
if (node.src && node.src.length > 0){
var event = doc.createEvent();
event.initEvent("load");
node.dispatchEvent( event );
}
}
else if (node.nodeName.toLowerCase() == 'iframe'){
if (node.src && node.src.length > 0){
// don't actually load anything, so we're "done" immediately:
var event = doc.createEvent();
Expand Down Expand Up @@ -3776,13 +3786,7 @@ function __parseLoop__(impl, doc, p) {
}

else if(iEvt == XMLP._ELM_E) { // End-Element Event
//handle script tag
if(iNodeParent.nodeName.toLowerCase() == 'script'){
p.replaceEntities = true;
$env.loadLocalScript(iNodeParent, p);
}
else
$handleEndOfNormalOrEmptyElement(iNodeParent, doc);
$handleEndOfNormalOrEmptyElement(iNodeParent, doc, p);
iNodeParent = iNodeParent.parentNode; // ascend one level of the DOM Tree
}

Expand Down Expand Up @@ -3870,7 +3874,7 @@ function __parseLoop__(impl, doc, p) {
}


$handleEndOfNormalOrEmptyElement(iNode, doc);
$handleEndOfNormalOrEmptyElement(iNode, doc, p);
iNodeParent.appendChild(iNode); // attach Element to parentNode
}
else if(iEvt == XMLP._TEXT || iEvt == XMLP._ENTITY) { // TextNode and entity Events
Expand Down Expand Up @@ -6681,7 +6685,11 @@ __extend__(HTMLScriptElement.prototype, {
},
set type(value){
this.setAttribute('type',value);
},
onload: function(event){
__eval__(this.getAttribute('onload')||'')
}

});

$w.HTMLScriptElement = HTMLScriptElement;$debug("Defining HTMLSelectElement");
Expand Down
28 changes: 18 additions & 10 deletions dist/env.rhino.js
Original file line number Diff line number Diff line change
Expand Up @@ -3959,9 +3959,19 @@ var DOMImplementation = function() {
this.errorChecking = true; // by default, test for exceptions
};

var $handleEndOfNormalOrEmptyElement = function(node, doc){
var $handleEndOfNormalOrEmptyElement = function(node, doc, p){
if(node.nodeName.toLowerCase() == 'script'){
p.replaceEntities = true;
$env.loadLocalScript(node, p);

if (node.nodeName.toLowerCase() == 'iframe'){
// only fire event if we actually had something to load
if (node.src && node.src.length > 0){
var event = doc.createEvent();
event.initEvent("load");
node.dispatchEvent( event );
}
}
else if (node.nodeName.toLowerCase() == 'iframe'){
if (node.src && node.src.length > 0){
// don't actually load anything, so we're "done" immediately:
var event = doc.createEvent();
Expand Down Expand Up @@ -4213,13 +4223,7 @@ function __parseLoop__(impl, doc, p) {
}

else if(iEvt == XMLP._ELM_E) { // End-Element Event
//handle script tag
if(iNodeParent.nodeName.toLowerCase() == 'script'){
p.replaceEntities = true;
$env.loadLocalScript(iNodeParent, p);
}
else
$handleEndOfNormalOrEmptyElement(iNodeParent, doc);
$handleEndOfNormalOrEmptyElement(iNodeParent, doc, p);
iNodeParent = iNodeParent.parentNode; // ascend one level of the DOM Tree
}

Expand Down Expand Up @@ -4307,7 +4311,7 @@ function __parseLoop__(impl, doc, p) {
}


$handleEndOfNormalOrEmptyElement(iNode, doc);
$handleEndOfNormalOrEmptyElement(iNode, doc, p);
iNodeParent.appendChild(iNode); // attach Element to parentNode
}
else if(iEvt == XMLP._TEXT || iEvt == XMLP._ENTITY) { // TextNode and entity Events
Expand Down Expand Up @@ -7118,7 +7122,11 @@ __extend__(HTMLScriptElement.prototype, {
},
set type(value){
this.setAttribute('type',value);
},
onload: function(event){
__eval__(this.getAttribute('onload')||'')
}

});

$w.HTMLScriptElement = HTMLScriptElement;$debug("Defining HTMLSelectElement");
Expand Down
24 changes: 14 additions & 10 deletions src/dom/implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,19 @@ var DOMImplementation = function() {
this.errorChecking = true; // by default, test for exceptions
};

var $handleEndOfNormalOrEmptyElement = function(node, doc){
var $handleEndOfNormalOrEmptyElement = function(node, doc, p){
if(node.nodeName.toLowerCase() == 'script'){
p.replaceEntities = true;
$env.loadLocalScript(node, p);

if (node.nodeName.toLowerCase() == 'iframe'){
// only fire event if we actually had something to load
if (node.src && node.src.length > 0){
var event = doc.createEvent();
event.initEvent("load");
node.dispatchEvent( event );
}
}
else if (node.nodeName.toLowerCase() == 'iframe'){
if (node.src && node.src.length > 0){
// don't actually load anything, so we're "done" immediately:
var event = doc.createEvent();
Expand Down Expand Up @@ -266,13 +276,7 @@ function __parseLoop__(impl, doc, p) {
}

else if(iEvt == XMLP._ELM_E) { // End-Element Event
//handle script tag
if(iNodeParent.nodeName.toLowerCase() == 'script'){
p.replaceEntities = true;
$env.loadLocalScript(iNodeParent, p);
}
else
$handleEndOfNormalOrEmptyElement(iNodeParent, doc);
$handleEndOfNormalOrEmptyElement(iNodeParent, doc, p);
iNodeParent = iNodeParent.parentNode; // ascend one level of the DOM Tree
}

Expand Down Expand Up @@ -360,7 +364,7 @@ function __parseLoop__(impl, doc, p) {
}


$handleEndOfNormalOrEmptyElement(iNode, doc);
$handleEndOfNormalOrEmptyElement(iNode, doc, p);
iNodeParent.appendChild(iNode); // attach Element to parentNode
}
else if(iEvt == XMLP._TEXT || iEvt == XMLP._ENTITY) { // TextNode and entity Events
Expand Down
3 changes: 3 additions & 0 deletions src/html/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ __extend__(HTMLScriptElement.prototype, {
},
set type(value){
this.setAttribute('type',value);
},
onload: function(event){
__eval__(this.getAttribute('onload')||'')
}
});

Expand Down
26 changes: 26 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,32 @@ <h2 id="userAgent"></h2>
</script>
<img id="anImg" src="html/img1.png" onload="imgOnloadEventHandler();" />

<script>
function onloadHandlerThatShouldntExecute(){
var t = document.createTextNode(
'Paragraph created by execution of script-onload *BAD* event handler.');
var p = document.createElement('p');
p.setAttribute('id', 'pShouldntBeCreated');
p.appendChild(t);
document.getElementsByTagName('body')[0].appendChild(p);
}
</script>
<script onload="onloadHandlerThatShouldntExecute();">
function scriptOnloadEventHandler(tag){
var t = document.createTextNode(
'Paragraph created by execution of script-onload event handler.');
var p = document.createElement('p');
p.setAttribute('id', 'pCreatedByScriptOnload'+tag);
p.appendChild(t);
document.getElementsByTagName('body')[0].appendChild(p);
}
</script>
<script src="html/missingScript.js"
onload="scriptOnloadEventHandler('A');" />
<script src="html/missingScript.js"
onload="scriptOnloadEventHandler('B');"></script>


<dl id="dl" style="display:none;">
<div id="main" style="display: none;">
<p id="firstp">See <a id="simon1" href="http://simon.incutio.com/archive/2003/03/25/#getElementsBySelector" rel="bookmark">this blog entry</a> for more information.</p>
Expand Down
21 changes: 19 additions & 2 deletions test/unit/onload.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

module("onload-events");

// depends on <script> block in test/index.html
// depends on <script> blocks and elements in test/index.html
test("Execution of onload events in top-level document",
function() {

// top-level window-onload works, or test framework wouldn't run.....
expect(6);
expect(9);

var mtch = document.getElementById('pCreatedByBodyOnload').innerHTML.
match(/dynamically-generated paragraph/);
Expand Down Expand Up @@ -58,6 +58,23 @@ test("Execution of onload events in top-level document",
try{ ok(aCounter == 11,
"img-onload handler executes when img.src assigned");
}catch(e){print(e);}

mtch = document.getElementById('pCreatedByScriptOnloadA').innerHTML.
match(/script-onload event handler/);
try{ ok(mtch && mtch.length > 0,
"Got confirmation that script-onload handler executed, empty tag");
}catch(e){print(e);}

mtch = document.getElementById('pCreatedByScriptOnloadB').innerHTML.
match(/script-onload event handler/);
try{ ok(mtch && mtch.length > 0,
"Script-onload handler executed, with open/close tag pair");
}catch(e){print(e);}

mtch = document.getElementById('pShouldntBeCreated');
try{ ok(!(mtch),
"Confirmed that script-onload handler that shouldn't execute actually didn't");
}catch(e){print(e);}
});


Expand Down

0 comments on commit 19805a0

Please sign in to comment.