Contact: fumanchu@aminus.org

Log in as guest/misc to create tickets

Changeset 146

Show
Ignore:
Timestamp:
02/18/09 13:32:41
Author:
fumanchu
Message:

urilib.js: More URI functions.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • urilib.js

    r145 r146  
    2323    "([?](.*))?"); 
    2424 
     25urilib.AbsoluteURIError = function (message) { 
     26    // Error thrown when relativeURI functions are called on an absoluteURI. 
     27    this.message = message || "relativeURI functions cannot be called on an absoluteURI"; 
     28} 
     29urilib.AbsoluteURIError.prototype.toString = function () { 
     30    return 'AbsoluteURIError: "' + this.message + '"'; 
     31} 
     32 
     33urilib.RelativeURIError = function (message) { 
     34    // Error thrown when absoluteURI functions are called on a relativeURI. 
     35    this.message = message || "absoluteURI functions cannot be called on a relativeURI"; 
     36} 
     37urilib.RelativeURIError.prototype.toString = function () { 
     38    return 'RelativeURIError: "' + this.message + '"'; 
     39} 
     40 
    2541urilib.URI = function (scheme, authority, path, query) { 
    2642    this.scheme = scheme || ""; 
     
    4359}; 
    4460 
     61urilib.URI.prototype.is_relative = function () { 
     62    return (this.path.length > 0 && this.path.charAt(0) != '/'); 
     63} 
     64 
     65urilib.URI.prototype.net_path = function () { 
     66    // Return the 'net_path' ("//" authority [ abs_path ]) of this URI. 
     67    if (this.is_relative()) throw urilib.RelativeURIError(); 
     68    return "//" + this.authority + this.path; 
     69}; 
     70 
     71urilib.URI.prototype.abs_path = function () { 
     72    // Return the 'abs_path' ("/" path_segments) of this URI. 
     73    if (this.is_relative()) throw urilib.RelativeURIError(); 
     74    return this.path; 
     75}; 
     76 
     77urilib.URI.prototype.rel_path = function () { 
     78    // Return the 'rel_path' (rel_segment [ abs_path ]) of this URI. 
     79    if (!this.is_relative()) throw urilib.AbsoluteURIError(); 
     80    return this.path; 
     81}; 
     82 
     83urilib.URI.prototype.hier_part = function (include_authority) { 
     84    // Return the 'hier_part' ( net_path | abs_path ) [ "?" query ] of this URI. 
     85    //  
     86    // If 'include_authority' is false (the default), returns the 
     87    // abs_path [+ query] by omitting the authority component. 
     88    // Otherwise, returns the net_path [+ query] by including the 
     89    // authority component. 
     90    if (this.is_relative()) throw urilib.RelativeURIError(); 
     91    var result = include_authority ? this.net_path() : this.path; 
     92    if (this.query != '') result = result + "?" + this.query; 
     93    return result; 
     94}; 
     95 
     96urilib.URI.prototype.server_relative = function () { 
     97    // Return the 'server relative' ( abs_path | rel_path ) [ "?" query ] form of this URI. 
     98    var result = this.path; 
     99    if (this.query != '') result = result + "?" + this.query; 
     100    return result; 
     101}; 
    45102 
    46103urilib.parse = function (uri) {