forked from garycourt/JSV
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated URI library. Tests/example now use proper urn parsing.
- Loading branch information
Showing
10 changed files
with
99 additions
and
1,391 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
(function () { | ||
var URI_NS = require("../uri"), | ||
URI = URI_NS.URI, | ||
pctEncChar = URI_NS.pctEncChar, | ||
NID$ = "(?:[0-9A-Za-z][0-9A-Za-z\\-]{1,31})", | ||
PCT_ENCODED$ = "(?:\\%[0-9A-Fa-f]{2})", | ||
TRANS$$ = "[0-9A-Za-z\\(\\)\\+\\,\\-\\.\\:\\=\\@\\;\\$\\_\\!\\*\\'\\/\\?\\#]", | ||
NSS$ = "(?:(?:" + PCT_ENCODED$ + "|" + TRANS$$ + ")+)", | ||
URN_SCHEME = new RegExp("^urn\\:(" + NID$ + ")$"), | ||
URN_PATH = new RegExp("^(" + NID$ + ")\\:(" + NSS$ + ")$"), | ||
URN_PARSE = /^([^\:]+)\:(.*)/, | ||
URN_EXCLUDED = /[\x00-\x20\\\"\&\<\>\[\]\^\`\{\|\}\~\x7F-\xFF]/g, | ||
UUID = /^[0-9A-Fa-f]{8}(?:\-[0-9A-Fa-f]{4}){3}\-[0-9A-Fa-f]{12}$/; | ||
|
||
//RFC 2141 | ||
URI.SCHEMES["urn"] = { | ||
parse : function (components, options) { | ||
var matches = components.path.match(URN_PATH), | ||
scheme, schemeHandler; | ||
|
||
if (!matches) { | ||
if (!options.tolerant) { | ||
components.errors.push("URN is not strictly valid."); | ||
} | ||
|
||
matches = components.path.match(URN_PARSE); | ||
} | ||
|
||
if (matches) { | ||
scheme = "urn:" + matches[1].toLowerCase(); | ||
schemeHandler = URI.SCHEMES[scheme]; | ||
|
||
//in order to serialize properly, | ||
//every URN must have a serializer that calls the URN serializer | ||
if (!schemeHandler) { | ||
schemeHandler = URI.SCHEMES[scheme] = {}; | ||
} | ||
if (!schemeHandler.serialize) { | ||
schemeHandler.serialize = URI.SCHEMES["urn"].serialize; | ||
} | ||
|
||
components.scheme = scheme; | ||
components.path = matches[2]; | ||
|
||
if (schemeHandler.parse) { | ||
schemeHandler.parse(components, options); | ||
} | ||
} else { | ||
components.errors.push("URN can not be parsed."); | ||
} | ||
|
||
return components; | ||
}, | ||
|
||
serialize : function (components, options) { | ||
var scheme = components.scheme || options.scheme, | ||
matches; | ||
|
||
if (scheme && scheme !== "urn") { | ||
var matches = scheme.match(URN_SCHEME); | ||
|
||
if (!matches) { | ||
matches = ["urn:" + scheme, scheme]; | ||
} | ||
|
||
components.scheme = "urn"; | ||
components.path = matches[1] + ":" + (components.path ? components.path.replace(URN_EXCLUDED, pctEncChar) : ""); | ||
} | ||
|
||
return components; | ||
} | ||
}; | ||
|
||
//RFC 4122 | ||
URI.SCHEMES["urn:uuid"] = { | ||
serialize : function (components, options) { | ||
//ensure UUID is valid | ||
if (!options.tolerant && (!components.path || !components.path.match(UUID))) { | ||
//invalid UUIDs can not have this scheme | ||
components.scheme = undefined; | ||
} | ||
|
||
return URI.SCHEMES["urn"].serialize(components, options); | ||
} | ||
}; | ||
}()); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.