Skip to content
Browse files

Removing some of the nastier comments, and trimming up the style a bit.

  • Loading branch information...
1 parent 3d8d54a commit 08b9745eefaf858c4ffd2cfa33516d68e4fe9113 isaacs committed Jan 3, 2010
Showing with 23 additions and 33 deletions.
  1. +23 −33 lib/url.js
View
56 lib/url.js
@@ -7,21 +7,23 @@ exports.format = url_format;
// define these here so at least they only have to be compiled once on the first module load.
var protocolPattern = /^([a-z0-9]+:)/,
portPattern = /:[0-9]+$/,
- slashedProtocols = {
+ nonHostChars = ["/", "?", ";", "#"],
+ hostlessProtocol = {
+ "file":true,
+ "file:":true
+ },
+ slashedProtocol = {
"http":true, "https":true, "ftp":true, "gopher":true, "file":true,
"http:":true, "https:":true, "ftp:":true, "gopher:":true, "file:":true
},
- hostlessProtocols = {
- "file":true,
- "file:":true
- },
path = require("path"), // internal module, guaranteed to be loaded already.
querystring; // don't load unless necessary.
-
+
function url_parse (url, parseQueryString) {
- if (url && typeof(url) === "object" && ("href" in url)) return url;
+ if (url && typeof(url) === "object" && url.href) return url;
- var out = { href : url }, rest = url;
+ var out = { href : url },
+ rest = url;
var proto = protocolPattern.exec(rest);
if (proto) {
@@ -32,16 +34,15 @@ function url_parse (url, parseQueryString) {
// figure out if it's got a host
var slashes = rest.substr(0, 2) === "//";
- if (slashes && !(proto && hostlessProtocols[proto])) {
+ if (slashes && !(proto && hostlessProtocol[proto])) {
rest = rest.substr(2);
out.slashes = true;
}
- if ((slashes || (proto && !slashedProtocols[proto])) && !hostlessProtocols[proto]) {
+ if (!hostlessProtocol[proto] && (slashes || (proto && !slashedProtocol[proto]))) {
// there's a hostname.
-
// the first instance of /, ?, ;, or # ends the host.
// don't enforce full RFC correctness, just be unstupid about it.
- var nonHostChars = ["/", "?", ";", "#"], firstNonHost = -1;
+ var firstNonHost = -1;
for (var i = 0, l = nonHostChars.length; i < l; i ++) {
var index = rest.indexOf(nonHostChars[i]);
if (index !== -1 && (firstNonHost < 0 || index < firstNonHost)) firstNonHost = index;
@@ -77,7 +78,7 @@ function url_parse (url, parseQueryString) {
rest = rest.slice(0, qm);
}
if (rest) out.pathname = rest;
-
+
return out;
};
@@ -109,7 +110,7 @@ function url_format (obj) {
// only the slashedProtocols get the //. Not mailto:, xmpp:, etc.
// unless they had them to begin with.
- if (obj.slashes || (!protocol || slashedProtocols[protocol]) && host !== false) {
+ if (obj.slashes || (!protocol || slashedProtocol[protocol]) && host !== false) {
host = "//" + (host || "");
if (pathname && pathname.charAt(0) !== "/") pathname = "/" + pathname;
} else if (!host) host = "";
@@ -142,17 +143,16 @@ function url_resolveObject (source, relative) {
}
if (relative.protocol && relative.protocol !== source.protocol) {
- // urls are psychotic jerks. Design by committee over many years, yay!
// if it's a known url protocol, then changing the protocol does weird things
// first, if it's not file:, then we MUST have a host, and if there was a path
// to begin with, then we MUST have a path.
// if it is file:, then the host is dropped, because that's known to be hostless.
// anything else is assumed to be absolute.
- if (!slashedProtocols[relative.protocol]) return relative;
+ if (!slashedProtocol[relative.protocol]) return relative;
source.protocol = relative.protocol;
- if (!relative.host && !hostlessProtocols[relative.protocol]) {
+ if (!relative.host && !hostlessProtocol[relative.protocol]) {
var relPath = (relative.pathname || "").split("/");
while (relPath.length && !(relative.host = relPath.shift()));
if (!relative.host) relative.host = "";
@@ -179,9 +179,8 @@ function url_resolveObject (source, relative) {
removeAllDots = mustEndAbs,
srcPath = source.pathname && source.pathname.split("/") || [],
relPath = relative.pathname && relative.pathname.split("/") || [],
- psychotic = source.protocol && !slashedProtocols[source.protocol] && source.host !== undefined;
+ psychotic = source.protocol && !slashedProtocol[source.protocol] && source.host !== undefined;
-
// if the url is a non-slashed url, then relative links like ../.. should be able
// to crawl up to the hostname, as well. This is strange.
// source.protocol has already been set by now.
@@ -210,7 +209,6 @@ function url_resolveObject (source, relative) {
mustEndAbs = mustEndAbs && (relPath[0] === "" || srcPath[0] === "");
}
-
if (isRelAbs) {
// it's absolute.
source.host = (relative.host || relative.host === "") ? relative.host : source.host;
@@ -244,12 +242,9 @@ function url_resolveObject (source, relative) {
return source;
}
-
// resolve dots.
- // urls are psychotic idiots.
// if a url ENDs in . or .., then it must get a trailing slash.
// however, if it ends in anything else non-slashy, then it must NOT get a trailing slash.
- // ././././. must resolve to / in the end.
var last = srcPath.slice(-1)[0];
var hasTrailingSlash = (
(source.host || relative.host) && (last === "." || last === "..")
@@ -272,23 +267,18 @@ function url_resolveObject (source, relative) {
}
srcPath = dirs;
}
- if (hasTrailingSlash && (srcPath.length < 2 || srcPath.slice(-1)[0] !== "")) {
- srcPath.push("");
- }
+ if (hasTrailingSlash && (srcPath.length < 2 || srcPath.slice(-1)[0] !== "")) srcPath.push("");
// put the host back
- if ( psychotic ) {
- source.host = srcPath[0] === "" ? "" : srcPath.shift();
- }
- mustEndAbs = mustEndAbs || (
- source.host && srcPath.length
- );
+ if ( psychotic ) source.host = srcPath[0] === "" ? "" : srcPath.shift();
+
+ mustEndAbs = mustEndAbs || (source.host && srcPath.length);
+
if (mustEndAbs && srcPath[0] !== "") srcPath.unshift("")
source.pathname = srcPath.join("/");
return source;
-
};
function parseHost (host) {

0 comments on commit 08b9745

Please sign in to comment.
Something went wrong with that request. Please try again.