-
Notifications
You must be signed in to change notification settings - Fork 476
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
Added more supporting methods for URN paths. #201
Changes from 5 commits
4be8afa
8d68ead
1840662
46b270e
8b057a7
158011d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -324,6 +324,42 @@ | |
'%3D': '=' | ||
} | ||
} | ||
}, | ||
urnpath: { | ||
// The characters under `encode` are the characters called out by RFC 2141 as being acceptable | ||
// for usage in a URN. RFC2141 also calls out "-", ".", and "_" as acceptable characters, but | ||
// these aren't encoded by encodeURIComponent, so we don't have to call them out here. Also | ||
// note that the colon character is not featured in the encoding map; this is because URI.js | ||
// gives the colons in URNs semantic meaning as the delimiters of path segements, and so it | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If |
||
// should not appear unencoded in a segment itself. | ||
// See also the note above about RFC3986 and capitalalized hex digits. | ||
encode: { | ||
expression: /%(21|24|27|28|29|2A|2B|2C|3B|3D|40)/ig, | ||
map: { | ||
'%21': '!', | ||
'%24': '$', | ||
'%27': '\'', | ||
'%28': '(', | ||
'%29': ')', | ||
'%2A': '*', | ||
'%2B': '+', | ||
'%2C': ',', | ||
'%3B': ';', | ||
'%3D': '=', | ||
'%40': '@' | ||
} | ||
}, | ||
// These characters are the characters called out by RFC2141 as "reserved" characters that | ||
// should never appear in a URN, plus the colon character (see note above). | ||
decode: { | ||
expression: /[\/\?#:]/g, | ||
map: { | ||
'/': '%2F', | ||
'?': '%3F', | ||
'#': '%23', | ||
':': '%3A' | ||
} | ||
} | ||
} | ||
}; | ||
URI.encodeQuery = function(string, escapeQuerySpace) { | ||
|
@@ -350,22 +386,6 @@ | |
return string; | ||
} | ||
}; | ||
URI.recodePath = function(string) { | ||
var segments = (string + '').split('/'); | ||
for (var i = 0, length = segments.length; i < length; i++) { | ||
segments[i] = URI.encodePathSegment(URI.decode(segments[i])); | ||
} | ||
|
||
return segments.join('/'); | ||
}; | ||
URI.decodePath = function(string) { | ||
var segments = (string + '').split('/'); | ||
for (var i = 0, length = segments.length; i < length; i++) { | ||
segments[i] = URI.decodePathSegment(segments[i]); | ||
} | ||
|
||
return segments.join('/'); | ||
}; | ||
// generate encode/decode path functions | ||
var _parts = {'encode':'encode', 'decode':'decode'}; | ||
var _part; | ||
|
@@ -387,8 +407,40 @@ | |
|
||
for (_part in _parts) { | ||
URI[_part + 'PathSegment'] = generateAccessor('pathname', _parts[_part]); | ||
URI[_part + 'UrnPathSegment'] = generateAccessor('urnpath', _parts[_part]); | ||
} | ||
|
||
var generateSegmentedPathFunction = function(_sep, _codingFuncName, _innerCodingFuncName) { | ||
return function(string) { | ||
// Why pass in names of functions, rather than the function objects themselves? The | ||
// definitions of some functions (but in particular, URI.decode) will occasionally change due | ||
// to URI.js having ISO8859 and Unicode modes. Passing in the name and getting it will ensure | ||
// that the functions we use here are "fresh". | ||
var actualCodingFunc; | ||
if (!_innerCodingFuncName) { | ||
actualCodingFunc = URI[_codingFuncName]; | ||
} else { | ||
actualCodingFunc = function(string) { | ||
return URI[_codingFuncName](URI[_innerCodingFuncName](string)); | ||
}; | ||
} | ||
|
||
var segments = (string + '').split(_sep); | ||
|
||
for (var i = 0, length = segments.length; i < length; i++) { | ||
segments[i] = actualCodingFunc(segments[i]); | ||
} | ||
|
||
return segments.join(_sep); | ||
}; | ||
}; | ||
|
||
// This takes place outside the above loop because we don't want, e.g., encodeUrnPath functions. | ||
URI.decodePath = generateSegmentedPathFunction('/', 'decodePathSegment'); | ||
URI.decodeUrnPath = generateSegmentedPathFunction(':', 'decodeUrnPathSegment'); | ||
URI.recodePath = generateSegmentedPathFunction('/', 'encodePathSegment', 'decode'); | ||
URI.recodeUrnPath = generateSegmentedPathFunction(':', 'encodeUrnPathSegment', 'decode'); | ||
|
||
URI.encodeReserved = generateAccessor('reserved', 'encode'); | ||
|
||
URI.parse = function(string, parts) { | ||
|
@@ -946,9 +998,13 @@ | |
p.pathname = function(v, build) { | ||
if (v === undefined || v === true) { | ||
var res = this._parts.path || (this._parts.hostname ? '/' : ''); | ||
return v ? URI.decodePath(res) : res; | ||
return v ? (this._parts.urn ? URI.decodeUrnPath : URI.decodePath)(res) : res; | ||
} else { | ||
this._parts.path = v ? URI.recodePath(v) : '/'; | ||
if (this._parts.urn) { | ||
this._parts.path = v ? URI.recodeUrnPath(v) : ''; | ||
} else { | ||
this._parts.path = v ? URI.recodePath(v) : '/'; | ||
} | ||
this.build(!build); | ||
return this; | ||
} | ||
|
@@ -1624,6 +1680,7 @@ | |
if (this._parts.urn) { | ||
return this | ||
.normalizeProtocol(false) | ||
.normalizePath(false) | ||
.normalizeQuery(false) | ||
.normalizeFragment(false) | ||
.build(); | ||
|
@@ -1670,16 +1727,22 @@ | |
return this; | ||
}; | ||
p.normalizePath = function(build) { | ||
var _path = this._parts.path; | ||
if (!_path) { | ||
return this; | ||
} | ||
|
||
if (this._parts.urn) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can this be structured in a way that avoids the "christmas tree" effect of indentation? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It certainly can be; in order to do that, we'd have to duplicate the |
||
this._parts.path = URI.recodeUrnPath(this._parts.path); | ||
this.build(!build); | ||
return this; | ||
} | ||
|
||
if (!this._parts.path || this._parts.path === '/') { | ||
if (this._parts.path === '/') { | ||
return this; | ||
} | ||
|
||
var _was_relative; | ||
var _path = this._parts.path; | ||
var _leadingParents = ''; | ||
var _parent, _pos; | ||
|
||
|
@@ -1763,9 +1826,12 @@ | |
|
||
URI.encode = escape; | ||
URI.decode = decodeURIComponent; | ||
this.normalize(); | ||
URI.encode = e; | ||
URI.decode = d; | ||
try { | ||
this.normalize(); | ||
} finally { | ||
URI.encode = e; | ||
URI.decode = d; | ||
} | ||
return this; | ||
}; | ||
|
||
|
@@ -1776,9 +1842,12 @@ | |
|
||
URI.encode = strictEncodeURIComponent; | ||
URI.decode = unescape; | ||
this.normalize(); | ||
URI.encode = e; | ||
URI.decode = d; | ||
try { | ||
this.normalize(); | ||
} finally { | ||
URI.encode = e; | ||
URI.decode = d; | ||
} | ||
return this; | ||
}; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
proper format and content would be:
.pathname()
to properly en/decode URN paths - (Issue #201, mlefoster).normalize()
and.normalizePath()
to properly normalize URN pathsURI.encodeUrnPathSegment()
URI.decodeUrnPathSegment()
URI.decodeUrnPath()
URI.recodeUrnPath()