Skip to content

Commit

Permalink
commiting fixes for window load / domcontentloaded events
Browse files Browse the repository at this point in the history
  • Loading branch information
thatcher committed Nov 25, 2009
1 parent 7f7acfa commit 021610f
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 21,444 deletions.
10,369 changes: 0 additions & 10,369 deletions dist/env.js

This file was deleted.

10,999 changes: 0 additions & 10,999 deletions dist/env.rhino.js

This file was deleted.

45 changes: 25 additions & 20 deletions src/dom/document.js
Expand Up @@ -35,14 +35,11 @@ var DOMDocument = function(implementation, docParentWindow) {
};
DOMDocument.prototype = new DOMNode;
__extend__(DOMDocument.prototype, {
toString : function(){
return '[object HTMLDocument]';
},
addEventListener : function(){ window.addEventListener.apply(this, arguments); },
removeEventListener : function(){ window.removeEventListener.apply(this, arguments); },
attachEvent : function(){ window.addEventListener.apply(this, arguments); },
detachEvent : function(){ window.removeEventListener.apply(this, arguments); },
dispatchEvent : function(){ window.dispatchEvent.apply(this, arguments); },
addEventListener : function(type, fn){ __addEventListener__(this, type, fn); },
removeEventListener : function(type){ __removeEventListener__(this, type); },
attachEvent : function(type, fn){ __addEventListener__(this, type, fn); },
detachEvent : function(type){ __removeEventListener__(this, type); },
dispatchEvent : function(event, bubbles){ __dispatchEvent__(this, event, bubbles); },

get styleSheets(){
return [];/*TODO*/
Expand All @@ -65,7 +62,8 @@ __extend__(DOMDocument.prototype, {
loadXML : function(xmlString) {
// create DOM Document
if(this === $document){
$debug("Setting internal window.document");

$info("Setting internal window.document ");
$document = this;
}
// populate Document with Parsed Nodes
Expand Down Expand Up @@ -109,23 +107,30 @@ __extend__(DOMDocument.prototype, {

$info("Sucessfully loaded document at "+url);

// first fire body-onload event
var event = document.createEvent();
event.initEvent("load");
// first fire body-onload event
var bodyLoad = _this.createEvent();
bodyLoad.initEvent("load");
try { // assume <body> element, but just in case....
$w.document.getElementsByTagName('body')[0].
dispatchEvent( event, false );
_this.getElementsByTagName('body')[0].
dispatchEvent( bodyLoad, false );
} catch (e){;}

// then fire window-onload event
event = document.createEvent();
event.initEvent("load");
$w.dispatchEvent( event, false );
// then fire this onload event
//event = _this.createEvent();
//event.initEvent("load");
//_this.dispatchEvent( event, false );

//also use DOMContentLoaded event
var domContentLoaded = document.createEvent();
var domContentLoaded = _this.createEvent();
domContentLoaded.initEvent("DOMContentLoaded");
$w.dispatchEvent( domContentLoaded, false );
_this.dispatchEvent( domContentLoaded, false );

//finally fire the window.onload event
if(_this === document){
var windowLoad = _this.createEvent();
windowLoad.initEvent("load", false, false);
$w.dispatchEvent( windowLoad, false );
}

};
xhr.send();
Expand Down
6 changes: 3 additions & 3 deletions src/dom/element.js
Expand Up @@ -20,9 +20,9 @@ __extend__(DOMElement.prototype, {
set tagName(name){
this.nodeName = name;
},
addEventListener : function(){ window.addEventListener.apply(this, arguments) },
removeEventListener : function(){ window.removeEventListener.apply(this, arguments) },
dispatchEvent : function(){ window.dispatchEvent.apply(this, arguments) },
addEventListener : function(type, fn, phase){ __addEventListener__(this, type, fn); },
removeEventListener : function(type){ __removeEventListener__(this, type); },
dispatchEvent : function(event, bubbles){ __dispatchEvent__(this, event, bubbles); },
getAttribute: function(name) {
var ret = null;
// if attribute exists, use it
Expand Down
7 changes: 3 additions & 4 deletions src/event/event.js
Expand Up @@ -3,9 +3,7 @@ $debug("Defining Event");
* event.js
*/
var Event = function(options){
if(options === undefined){
options={target:window,currentTarget:window};
}
options={};
__extend__(this,{
CAPTURING_PHASE : 1,
AT_TARGET : 2,
Expand All @@ -16,7 +14,7 @@ var Event = function(options){
$cancelable = options.cancelable?options.cancelable:true,
$currentTarget = options.currentTarget?options.currentTarget:null,
$eventPhase = options.eventPhase?options.eventPhase:Event.CAPTURING_PHASE,
$target = options.target?options.target:document,
$target = options.target?options.target:null,
$timestamp = options.timestamp?options.timestamp:new Date().getTime().toString(),
$type = options.type?options.type:"";
return __extend__(this,{
Expand All @@ -25,6 +23,7 @@ var Event = function(options){
get currentTarget(){return $currentTarget;},
get eventPhase(){return $eventPhase;},
get target(){return $target;},
set target(target){ $target = target;},
get timestamp(){return $timestamp;},
get type(){return $type;},
initEvent: function(type,bubbles,cancelable){
Expand Down
11 changes: 4 additions & 7 deletions src/html/document.js
Expand Up @@ -19,9 +19,6 @@ var HTMLDocument = function(implementation, docParentWindow, docReferrer) {
};
HTMLDocument.prototype = new DOMDocument;
__extend__(HTMLDocument.prototype, {
toString : function(){
return '[object HTMLDocument]';
},
createElement: function(tagName){
//print('createElement :'+tagName);
// throw Exception if the tagName string contains an illegal character
Expand Down Expand Up @@ -244,7 +241,7 @@ var __elementPopped__ = function(ns, name, node){
// only fire event if we actually had something to load
if (node.src && node.src.length > 0){
var event = doc.createEvent();
event.initEvent( okay ? "load" : "error" );
event.initEvent( okay ? "load" : "error", false, false );
node.dispatchEvent( event, false );
}
}
Expand All @@ -259,7 +256,7 @@ var __elementPopped__ = function(ns, name, node){
$env.loadFrame(node, $env.location(node.src));

var event = doc.createEvent();
event.initEvent("load");
event.initEvent("load", false, false);
node.dispatchEvent( event, false );
}
}
Expand All @@ -268,7 +265,7 @@ var __elementPopped__ = function(ns, name, node){
if (node.href && node.href.length > 0){
// don't actually load anything, so we're "done" immediately:
var event = doc.createEvent();
event.initEvent("load");
event.initEvent("load", false, false);
node.dispatchEvent( event, false );
}
}
Expand All @@ -277,7 +274,7 @@ var __elementPopped__ = function(ns, name, node){
if (node.src && node.src.length > 0){
// don't actually load anything, so we're "done" immediately:
var event = doc.createEvent();
event.initEvent("load");
event.initEvent("load", false, false);
node.dispatchEvent( event, false );
}
}
Expand Down
99 changes: 59 additions & 40 deletions src/window/event.js
Expand Up @@ -2,42 +2,55 @@
* event.js
*/
// Window Events
$debug("Initializing Window Event.");
//$debug("Initializing Window Event.");
var $events = [{}],
$onerror,
$onload,
$onunload;

function __addEventListener__(target, type, fn){

//$debug("adding event listener \n\t" + type +" \n\tfor "+target+" with callback \n\t"+fn);
if ( !target.uuid ) {
target.uuid = $events.length;
$events[target.uuid] = {};
}
if ( !$events[target.uuid][type] ){
$events[target.uuid][type] = [];
}
if ( $events[target.uuid][type].indexOf( fn ) < 0 ){
$events[target.uuid][type].push( fn );
}
};


$w.addEventListener = function(type, fn){
$debug("adding event listener \n\t" + type +" \n\tfor "+this+" with callback \n\t"+fn);
if ( !this.uuid ) {
this.uuid = $events.length;
$events[this.uuid] = {};
}
if ( !$events[this.uuid][type] ){
$events[this.uuid][type] = [];
}
if ( $events[this.uuid][type].indexOf( fn ) < 0 ){
$events[this.uuid][type].push( fn );
}
__addEventListener__(this, type, fn);
};

$w.removeEventListener = function(type, fn){
if ( !this.uuid ) {
this.uuid = $events.length;
$events[this.uuid] = {};

function __removeEventListener__(target, type, fn){
if ( !target.uuid ) {
target.uuid = $events.length;
$events[target.uuid] = {};
}
if ( !$events[this.uuid][type] ){
$events[this.uuid][type] = [];
if ( !$events[target.uuid][type] ){
$events[target.uuid][type] = [];
}
$events[this.uuid][type] =
$events[this.uuid][type].filter(function(f){
$events[target.uuid][type] =
$events[target.uuid][type].filter(function(f){
return f != fn;
});
};

$w.dispatchEvent = function(event, bubbles){
$debug("dispatching event " + event.type);
$w.removeEventListener = function(type, fn){
__removeEventListener__(this, type, fn)
};



function __dispatchEvent__(target, event, bubbles){
//$debug("dispatching event " + event.type);

//the window scope defines the $event object, for IE(^^^) compatibility;
$event = event;
Expand All @@ -46,35 +59,41 @@ $w.dispatchEvent = function(event, bubbles){
bubbles = true;

if (!event.target) {
$debug("no event target : "+event.target);
event.target = this;
//$debug("no event target : "+event.target);
event.target = target;
}
$debug("event target: " + event.target);
if ( event.type && (this.nodeType ||
this === window ||
this.__proto__ === window ||
this.$thisWindowsProxyObject === window)) {
$debug("nodeType: " + this.nodeType);
if ( this.uuid && $events[this.uuid][event.type] ) {
var _this = this;
$events[this.uuid][event.type].forEach(function(fn){
$debug('calling event handler '+fn+' on target '+_this);
//$debug("event target: " + event.target);
if ( event.type && (target.nodeType ||
target === window ||
target.__proto__ === window ||
target.$thisWindowsProxyObject === window)) {
//$debug("nodeType: " + target.nodeType);
if ( target.uuid && $events[target.uuid][event.type] ) {
var _this = target;
//$debug('calling event handlers '+$events[target.uuid][event.type].length);
$events[target.uuid][event.type].forEach(function(fn){
//$debug('calling event handler '+fn+' on target '+_this);
fn( event );
});
}

if (this["on" + event.type]) {
$debug('calling event handler on'+event.type+' on target '+this);
this["on" + event.type](event);
if (target["on" + event.type]) {
//$debug('calling event handler on'+event.type+' on target '+target);
target["on" + event.type](event);
}
}else{
$debug("non target: " + event.target + " \n this->"+this);
//$debug("non target: " + event.target + " \n this->"+target);
}
if (bubbles && this.parentNode){
this.parentNode.dispatchEvent(event);
if (bubbles && target.parentNode){
//$debug('bubbling to parentNode '+target.parentNode);
__dispatchEvent__(target.parentNode, event, bubbles);
}
};

$w.dispatchEvent = function(event, bubbles){
__dispatchEvent__(this, event, bubbles);
};

$w.__defineGetter__('onerror', function(){
return function(){
//$w.dispatchEvent('error');
Expand Down
1 change: 0 additions & 1 deletion test/qunit
Submodule qunit deleted from 57d1a2
3 changes: 2 additions & 1 deletion test/unit/parser.js
Expand Up @@ -105,7 +105,8 @@ test("Really Ugly HTML Parsing", function() {
should('parse the document without error',{
be:'safe',
test:function(){
window.open('html/malformed.html');
var doc = document.implementation.createDocument();
doc.load('html/malformed.html');
}
});

Expand Down

0 comments on commit 021610f

Please sign in to comment.