Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: No such thing as location in non-browser environments (Node.js, etc.) #45

Merged
merged 1 commit into from
Aug 27, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
{
"type": "GPL",
"url": "http://opensource.org/licenses/GPL-3.0"
}
}
],
"description": "URI.js is a Javascript library for working with URLs.",
"keywords": [
Expand Down
80 changes: 42 additions & 38 deletions src/URI.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ var _use_module = typeof module !== "undefined" && module.exports,
}

if (url === undefined) {
url = location.href + "";
if (typeof location !== 'undefined') {
url = location.href + "";
} else {
url = "";
}
}

this.href(url);
Expand Down Expand Up @@ -258,7 +262,7 @@ URI.parse = function(string) {
};
URI.parseHost = function(string, parts) {
// extract host:port
var pos = string.indexOf('/'),
var pos = string.indexOf('/'),
t;

if (pos === -1) {
Expand Down Expand Up @@ -310,7 +314,7 @@ URI.parseUserinfo = function(string, parts) {
parts.username = null;
parts.password = null;
}

return string;
};
URI.parseQuery = function(string) {
Expand Down Expand Up @@ -355,7 +359,7 @@ URI.build = function(parts) {
if (parts.protocol) {
t += parts.protocol + ":";
}

if (!parts.urn && (t || parts.hostname)) {
t += '//';
}
Expand Down Expand Up @@ -416,7 +420,7 @@ URI.buildUserinfo = function(parts) {

t += "@";
}

return t;
};
URI.buildQuery = function(data, duplicates) {
Expand Down Expand Up @@ -705,7 +709,7 @@ p.is = function(what) {
switch (what.toLowerCase()) {
case 'relative':
return relative;

case 'absolute':
return !relative;

Expand All @@ -732,7 +736,7 @@ p.is = function(what) {

case 'idn':
return idn;

case 'url':
return !this._parts.urn;

Expand Down Expand Up @@ -769,7 +773,7 @@ p.port = function(v, build) {
if (this._parts.urn) {
return v === undefined ? '' : this;
}

if (v !== undefined) {
if (v === 0) {
v = null;
Expand All @@ -792,7 +796,7 @@ p.hostname = function(v, build) {
if (this._parts.urn) {
return v === undefined ? '' : this;
}

if (v !== undefined) {
var x = {};
URI.parseHost(v, x);
Expand All @@ -806,7 +810,7 @@ p.host = function(v, build) {
if (this._parts.urn) {
return v === undefined ? '' : this;
}

if (v === undefined) {
return this._parts.hostname ? URI.buildHost(this._parts) : "";
} else {
Expand All @@ -819,7 +823,7 @@ p.authority = function(v, build) {
if (this._parts.urn) {
return v === undefined ? '' : this;
}

if (v === undefined) {
return this._parts.hostname ? URI.buildAuthority(this._parts) : "";
} else {
Expand All @@ -832,19 +836,19 @@ p.userinfo = function(v, build) {
if (this._parts.urn) {
return v === undefined ? '' : this;
}

if (v === undefined) {
if (!this._parts.username) {
return "";
}

var t = URI.buildUserinfo(this._parts);
return t.substring(0, t.length -1);
} else {
if (v[v.length-1] !== '@') {
v += '@';
}

URI.parseUserinfo(v, this._parts);
this.build(!build);
return this;
Expand All @@ -856,7 +860,7 @@ p.subdomain = function(v, build) {
if (this._parts.urn) {
return v === undefined ? '' : this;
}

// convenience, return "www" from "www.example.org"
if (v === undefined) {
if (!this._parts.hostname || this.is('IP')) {
Expand Down Expand Up @@ -888,7 +892,7 @@ p.domain = function(v, build) {
if (this._parts.urn) {
return v === undefined ? '' : this;
}

if (typeof v === 'boolean') {
build = v;
v = undefined;
Expand Down Expand Up @@ -932,7 +936,7 @@ p.tld = function(v, build) {
if (this._parts.urn) {
return v === undefined ? '' : this;
}

if (typeof v === 'boolean') {
build = v;
v = undefined;
Expand Down Expand Up @@ -978,12 +982,12 @@ p.directory = function(v, build) {
if (this._parts.urn) {
return v === undefined ? '' : this;
}

if (v === undefined || v === true) {
if (!this._parts.path && !this._parts.hostname) {
return '';
}

if (this._parts.path === '/') {
return '/';
}
Expand Down Expand Up @@ -1024,7 +1028,7 @@ p.filename = function(v, build) {
if (this._parts.urn) {
return v === undefined ? '' : this;
}

if (v === undefined || v === true) {
if (!this._parts.path || this._parts.path === '/') {
return "";
Expand Down Expand Up @@ -1061,7 +1065,7 @@ p.suffix = function(v, build) {
if (this._parts.urn) {
return v === undefined ? '' : this;
}

if (v === undefined || v === true) {
if (!this._parts.path || this._parts.path === '/') {
return "";
Expand Down Expand Up @@ -1113,29 +1117,29 @@ p.segment = function(segment, v, build) {
path = this.path(),
absolute = path.substring(0, 1) === '/',
segments = path.split(separator);

if (typeof segment !== 'number') {
build = v;
v = segment;
segment = undefined;
}

if (segment !== undefined && typeof segment !== 'number') {
throw new Error("Bad segment '" + segment + "', must be 0-based integer");
}

if (absolute) {
segments.shift();
}

if (segment < 0) {
// allow negative indexes to address from the end
segment = Math.max(segments.length + segment, 0);
}

if (v === undefined) {
return segment === undefined
? segments
return segment === undefined
? segments
: segments[segment];
} else if (segment === null || segments[segment] === undefined) {
if (isArray(v)) {
Expand All @@ -1156,11 +1160,11 @@ p.segment = function(segment, v, build) {
segments.splice(segment, 1);
}
}

if (absolute) {
segments.unshift("");
}

return this.path(segments.join(separator), build);
};

Expand Down Expand Up @@ -1211,7 +1215,7 @@ p.normalize = function() {
.normalizeFragment(false)
.build();
}

return this
.normalizeProtocol(false)
.normalizeHostname(false)
Expand Down Expand Up @@ -1256,7 +1260,7 @@ p.normalizePath = function(build) {
if (this._parts.urn) {
return this;
}

if (!this._parts.path || this._parts.path === '/') {
return this;
}
Expand Down Expand Up @@ -1408,11 +1412,11 @@ p.absoluteTo = function(base) {
var resolved = this.clone(),
properties = ['protocol', 'username', 'password', 'hostname', 'port'],
basedir;

if (this._parts.urn) {
throw new Error('URNs do not have any generally defined hierachical components');
}

if (this._parts.hostname) {
return resolved;
}
Expand All @@ -1439,11 +1443,11 @@ p.relativeTo = function(base) {
properties = ['protocol', 'username', 'password', 'hostname', 'port'],
common,
_base;

if (this._parts.urn) {
throw new Error('URNs do not have any generally defined hierachical components');
}

if (!(base instanceof URI)) {
base = new URI(base);
}
Expand All @@ -1454,7 +1458,7 @@ p.relativeTo = function(base) {

common = URI.commonPath(relative.path(), base.path());
_base = base.directory();

for (var i = 0, p; p = properties[i]; i++) {
relative._parts[p] = null;
}
Expand Down Expand Up @@ -1561,7 +1565,7 @@ p.equals = function(uri) {
return true;
};

(typeof module !== 'undefined' && module.exports
(typeof module !== 'undefined' && module.exports
? module.exports = URI
: window.URI = URI
);
Expand Down