Skip to content

Commit

Permalink
Updated URI library. Tests/example now use proper urn parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
garycourt committed Sep 24, 2011
1 parent 5492b19 commit 793c171
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 1,391 deletions.
1 change: 1 addition & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ <h1>JSV: JSON Schema Validator</h1>

</script>
<script type="text/javascript" src="../lib/uri/uri.js"></script>
<script type="text/javascript" src="../lib/uri/schemes/urn.js"></script>
<script type="text/javascript" src="../lib/jsv.js"></script>
<script type="text/javascript" src="../lib/json-schema-draft-03.js"></script>
<script type="text/javascript" src="../lib/json-schema-draft-02.js"></script>
Expand Down
86 changes: 86 additions & 0 deletions lib/uri/schemes/urn.js
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);
}
};
}());
17 changes: 0 additions & 17 deletions lib/uri/tests/index.html

This file was deleted.

118 changes: 0 additions & 118 deletions lib/uri/tests/qunit.css

This file was deleted.

Loading

0 comments on commit 793c171

Please sign in to comment.