Skip to content

Commit

Permalink
add documentation/notes for Location,document.domain,document.URL. No…
Browse files Browse the repository at this point in the history
… functional changes
  • Loading branch information
client9 committed Mar 23, 2010
1 parent 701dd59 commit d4e2a1e
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 45 deletions.
71 changes: 54 additions & 17 deletions src/html/document.js
Expand Up @@ -2,7 +2,7 @@
/**
* @class HTMLDocument
* The Document interface represents the entire HTML or XML document.
* Conceptually, it is the root of the document tree, and provides
* Conceptually, it is the root of the document tree, and provides
* the primary access to the document's data.
*
* @extends Document
Expand Down Expand Up @@ -153,11 +153,9 @@ __extend__(HTMLDocument.prototype, {
},
get anchors(){
return new HTMLCollection(this.getElementsByTagName('a'));

},
get applets(){
return new HTMLCollection(this.getElementsByTagName('applet'));

},
//document.head is non-standard
get head(){
Expand Down Expand Up @@ -207,7 +205,7 @@ __extend__(HTMLDocument.prototype, {
title.textContent = titleStr;
},

get body(){
get body(){
//console.log('get body');
if(!this.documentElement)
this.appendChild(this.createElement('html'));
Expand All @@ -233,12 +231,55 @@ __extend__(HTMLDocument.prototype, {
set cookie(cookie){
return Cookies.set(this, cookie);
},

/**
* document.location
*
* should be identical to window.location
*
* HTML5:
* http://dev.w3.org/html5/spec/Overview.html#the-location-interface
*
* Mozilla MDC:
* https://developer.mozilla.org/en/DOM/document.location
*
*/
get location(){
return this.baseURI;
},
set location(url){
this.baseURI = url;
},

/**
* document.URL (read-only)
*
* HTML DOM Level 2:
* http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-46183437
*
* HTML5:
* http://dev.w3.org/html5/spec/Overview.html#dom-document-url
*
* Mozilla MDC:
* https://developer.mozilla.org/en/DOM/document.URL
*/
get URL() {
return this.location;
},
set URL(url) {
this.location = url;
},

/**
* document.domain
*
* HTML5 Spec:
* http://dev.w3.org/html5/spec/Overview.html#dom-document-domain
*
* Mozilla MDC:
* https://developer.mozilla.org/en/DOM/document.domain
*
*/
get domain(){
var HOSTNAME = new RegExp('\/\/([^\:\/]+)'),
matches = HOSTNAME.exec(this.baseURI);
Expand All @@ -257,20 +298,21 @@ __extend__(HTMLDocument.prototype, {
this.baseURI = this.baseURI.replace(domainParts.join('.'), value);
}
},

get forms(){
return new HTMLCollection(this.getElementsByTagName('form'));
return new HTMLCollection(this.getElementsByTagName('form'));
},
get images(){
return new HTMLCollection(this.getElementsByTagName('img'));
},
get lastModified(){
get lastModified(){
/* TODO */
return this._lastModified;
return this._lastModified;
},
get links(){
return new HTMLCollection(this.getElementsByTagName('a'));
},
getElementsByName : function(name){
getElementsByName : function(name){
//returns a real Array + the NodeList
var retNodes = __extend__([],new NodeList(this, this.documentElement)),
node;
Expand All @@ -286,17 +328,12 @@ __extend__(HTMLDocument.prototype, {
return retNodes;
},
toString: function(){
return "[object HTMLDocument]";
},
get innerHTML(){
return this.documentElement.outerHTML;
return "[object HTMLDocument]";
},
get URL(){
return this.location;
get innerHTML(){
return this.documentElement.outerHTML;
},
set URL(url){
this.location = url;
}

});


Expand Down
69 changes: 41 additions & 28 deletions src/xhr/location.js
@@ -1,28 +1,41 @@

/**
* @todo: document
* Location
*
* Mozilla MDC:
* https://developer.mozilla.org/En/DOM/Window.location
* https://developer.mozilla.org/en/DOM/document.location
*
* HTML5: 6.10.4 The Location interface
* http://dev.w3.org/html5/spec/Overview.html#location
*
* HTML5: 2.5.3 Interfaces for URL manipulation
* http://dev.w3.org/html5/spec/Overview.html#url-decomposition-idl-attributes
* All of section 2.5 is worth reading, but 2.5.3 contains very
* detailed information on how getters/setter should work
*
*/
var HASH = new RegExp('(\\#.*)'),
HOSTNAME = new RegExp('\/\/([^\:\/]+)'),
PATHNAME = new RegExp('(\/[^\\?\\#]*)'),
PORT = new RegExp('\:(\\d+)\/'),
PROTOCOL = new RegExp('(^\\w*\:)'),
SEARCH = new RegExp('(\\?[^\\#]*)');
HOSTNAME = new RegExp('\/\/([^\:\/]+)'),
PATHNAME = new RegExp('(\/[^\\?\\#]*)'),
PORT = new RegExp('\:(\\d+)\/'),
PROTOCOL = new RegExp('(^\\w*\:)'),
SEARCH = new RegExp('(\\?[^\\#]*)');


Location = function(url, doc, history){
//console.log('Location url %s', url);
var $url = url
$document = doc?doc:null,
$history = history?history:null;
$document = doc?doc:null,
$history = history?history:null;

return {
get hash(){
var m = HASH.exec($url);
return m&&m.length>1?m[1]:"";
},
set hash(hash){
$url = this.protocol + this.host + this.pathname +
$url = this.protocol + this.host + this.pathname +
this.search + (hash.indexOf('#')===0?hash:"#"+hash);
if($history){
$history.add( $url, 'hash');
Expand All @@ -32,7 +45,7 @@ Location = function(url, doc, history){
return this.hostname + (this.port !== ""?":"+this.port:"");
},
set host(host){
$url = this.protocol + host + this.pathname +
$url = this.protocol + host + this.pathname +
this.search + this.hash;
if($history){
$history.add( $url, 'host');
Expand All @@ -45,7 +58,7 @@ Location = function(url, doc, history){
},
set hostname(hostname){
$url = this.protocol + hostname + ((this.port==="")?"":(":"+this.port)) +
this.pathname + this.search + this.hash;
this.pathname + this.search + this.hash;
if($history){
$history.add( $url, 'hostname');
}
Expand All @@ -55,7 +68,7 @@ Location = function(url, doc, history){
return $url;
},
set href(url){
$url = url;
$url = url;
if($history){
$history.add( $url, 'href');
}
Expand All @@ -67,7 +80,7 @@ Location = function(url, doc, history){
return m&&m.length>1?m[1]:"/";
},
set pathname(pathname){
$url = this.protocol + this.host + pathname +
$url = this.protocol + this.host + pathname +
this.search + this.hash;
if($history){
$history.add( $url, 'pathname');
Expand All @@ -79,7 +92,7 @@ Location = function(url, doc, history){
return m&&m.length>1?m[1]:"";
},
set port(port){
$url = this.protocol + this.hostname + ":"+port + this.pathname +
$url = this.protocol + this.hostname + ":"+port + this.pathname +
this.search + this.hash;
if($history){
$history.add( $url, 'port');
Expand All @@ -90,7 +103,7 @@ Location = function(url, doc, history){
return this.href && PROTOCOL.exec(this.href)[0];
},
set protocol(protocol){
$url = protocol + this.host + this.pathname +
$url = protocol + this.host + this.pathname +
this.search + this.hash;
if($history){
$history.add( $url, 'protocol');
Expand All @@ -102,7 +115,7 @@ Location = function(url, doc, history){
return m&&m.length>1?m[1]:"";
},
set search(search){
$url = this.protocol + this.host + this.pathname +
$url = this.protocol + this.host + this.pathname +
search + this.hash;
if($history){
$history.add( $url, 'search');
Expand All @@ -114,16 +127,16 @@ Location = function(url, doc, history){
},
assign: function(url){
var _this = this,
xhr;
xhr;

//console.log('assigning %s',url);
$url = url;
//we can only assign if this Location is associated with a document
if($document){
//console.log("fetching %s (async? %s)", url, $document.async);
xhr = new XMLHttpRequest();
xhr.open("GET", url, false);//$document.async);

if($document.toString()=="[object HTMLDocument]"){
//tell the xhr to not parse the document as XML
//console.log("loading html document");
Expand All @@ -133,7 +146,7 @@ Location = function(url, doc, history){
$document.baseURI = new Location(url, $document);
//console.log('new document baseURI %s', $document.baseURI);
__exchangeHTMLDocument__($document, xhr.responseText, url);
}
}
};
xhr.send(null, false);
}else{
Expand All @@ -151,9 +164,9 @@ Location = function(url, doc, history){
};
xhr.send();
}

};

},
reload: function(forceget){
//for now we have no caching so just proxy to assign
Expand Down Expand Up @@ -189,22 +202,22 @@ var __exchangeHTMLDocument__ = function(doc, text, url){
html.appendChild(body);
doc.appendChild(html);
//console.log('default error document \n %s', doc.documentElement.outerHTML);

//DOMContentLoaded event
if(doc.createEvent){
event = doc.createEvent('Events');
event.initEvent("DOMContentLoaded", false, false);
doc.dispatchEvent( event, false );

event = doc.createEvent('HTMLEvents');
event.initEvent("load", false, false);
doc.dispatchEvent( event, false );
}

//finally fire the window.onload event
//TODO: this belongs in window.js which is a event
// event handler for DOMContentLoaded on document

try{
if(doc === window.document){
console.log('triggering window.load')
Expand Down

0 comments on commit d4e2a1e

Please sign in to comment.