Commit aa5b2ecf authored by fxa's avatar fxa

introduced UriTemplateError as exception, so the interface changed from string...

introduced UriTemplateError as exception, so the interface changed from string to UriTemplateError (as the rfc suggested)
parent f88149e5
......@@ -32,6 +32,7 @@
SRC_HOME = 'src',
SRC_FILES = [
// cannot use the fileList, because the order matters
path.join(SRC_HOME, 'UriTemplateError.js'),
path.join(SRC_HOME, 'objectHelper.js'),
path.join(SRC_HOME, 'charHelper.js'),
path.join(SRC_HOME, 'pctEncoder.js'),
......
......@@ -62,6 +62,7 @@ MIT License, see http://mit-license.org/
Release Notes
-------------
* 0.3.0 introduced UriTemplateError as exception, so the interface changed from string to UriTemplateError (as the rfc suggested)
* 0.2.4 fixed double encoding according [RubenVerborgh] and some Prefix modifiers bugs
* 0.2.3 fixed bug with empty objects ('{?empty}' with '{empty:{}}' shall expand to '?empty=')
* 0.2.2 fixed pct encoding bug with multibyte utf8 chars
......
......@@ -11,8 +11,8 @@ module.exports = (function () {
bitwise: true, // bitwise is forbidden
curly: true, // must put oneliners in curly braces
eqeqeq: true, // must use ===
forin: true,
immed: true, // wrap immediately called functions
forin: true, // for in must use hasOwnProperty()
immed: true, // immediately called functions must be wrapped in ()
newcap: true, // CAP constructors
noarg: true, // arguments.caller is evil
noempty: true, // empty blocks are forbidden
......@@ -21,9 +21,7 @@ module.exports = (function () {
strict: true, // must use "use strict"
undef: true, // forbids use of undefined variables
unused: true, // forbids unused variables
maxcomplexity: 19 // much too high. should be max. 10
maxcomplexity: 20 // much too high. should be max. 10
},
JSHINT_GLOBALS = {
"module": true
......
......@@ -19,7 +19,7 @@
["{a,.b}", false]
]
},
"Prefix modifiers": {
"Prefix modifiers and exploded": {
"level": 4,
"variables": {
"text": "I like octal numbers",
......@@ -40,8 +40,34 @@
["{emptyObject:1}", ""],
["{filledObject:1}", false],
["{emptyArr:1}", ""],
["{filledArr:1}", false]
["{filledArr:1}", false],
["{text:1:1}", false],
["{?*}", false],
["{?:1}", false],
["{?a,:1}", false],
["{*}", false],
["{text**}", false],
["{text:1*}", false],
["{text*:1}", false]
]
},
"Prefix modifiers, next generation": {
"level": 4,
"variables": {
"bool": false,
"int": 123,
"float": 123.4,
"exp": 1.2e+3,
"null": null
},
"testcases": [
["{bool:1}", "f"],
["{int:1}", "1"],
["{float:1}", "1"],
["{exp:1}", "1"],
["{null:1}", ""],
["{null:1}", ""]
]
}
}
......@@ -33,7 +33,7 @@
"uritemplate-test/spec-examples-by-sections.json",
"uritemplate-test/spec-examples.json"
],
"version": "0.2.4",
"version": "0.3.0",
"readmeFilename": "README.md",
"gitHead": "901b85201a821427dfb4591b56aea3a70d45c67c",
"devDependencies": {
......
/*jshint unused:false */
/*global pctEncoder, rfcCharHelper*/
/*global pctEncoder, rfcCharHelper, encodingHelper*/
var LiteralExpression = (function () {
"use strict";
function LiteralExpression (literal) {
this.literal = LiteralExpression.encodeLiteral(literal);
this.literal = encodingHelper.encodeLiteral(literal);
}
LiteralExpression.encodeLiteral = function (literal) {
var
result = '',
index,
chr = '';
for (index = 0; index < literal.length; index += chr.length) {
chr = pctEncoder.pctCharAt(literal, index);
if (chr.length > 1) {
result += chr;
}
else {
result += rfcCharHelper.isReserved(chr) || rfcCharHelper.isUnreserved(chr) ? chr : pctEncoder.encodeCharacter(chr);
}
// chr = literal.charAt(index);
// result += rfcCharHelper.isReserved(chr) || rfcCharHelper.isUnreserved(chr) ? chr : pctEncoder.encodeCharacter(chr);
}
return result;
};
LiteralExpression.prototype.expand = function () {
return this.literal;
};
......
/*jshint unused:false */
var UriTemplateError = (function () {
"use strict";
function UriTemplateError (options) {
this.options = options;
}
UriTemplateError.prototype.toString = function () {
if (JSON && JSON.stringify) {
return JSON.stringify(this.options);
}
else {
return this.options;
}
};
return UriTemplateError;
}());
/*jshint unused:false */
/*global parse, objectHelper*/
/*global parse, objectHelper, UriTemplateError*/
var UriTemplate = (function () {
"use strict";
function UriTemplate (templateText, expressions) {
......@@ -24,5 +24,6 @@ var UriTemplate = (function () {
};
UriTemplate.parse = parse;
UriTemplate.UriTemplateError = UriTemplateError;
return UriTemplate;
}());
/*jshint unused:false */
/*global pctEncoder, rfcCharHelper, isDefined, LiteralExpression, objectHelper*/
/*global pctEncoder, rfcCharHelper, isDefined, objectHelper, encodingHelper*/
var VariableExpression = (function () {
"use strict";
// helper function if JSON is not available
......@@ -46,7 +46,7 @@ var VariableExpression = (function () {
if (result.length > 0) {
result += operator.separator;
}
result += (valueIsArr) ? LiteralExpression.encodeLiteral(varspec.varname) : operator.encode(currentKey);
result += (valueIsArr) ? encodingHelper.encodeLiteral(varspec.varname) : operator.encode(currentKey);
result += '=' + operator.encode(currentValue);
}
return result;
......@@ -83,7 +83,7 @@ var VariableExpression = (function () {
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
value = value.toString();
if (operator.named) {
result += LiteralExpression.encodeLiteral(varspec.varname);
result += encodingHelper.encodeLiteral(varspec.varname);
if (value === '') {
result += operator.ifEmpty;
continue;
......@@ -101,7 +101,7 @@ var VariableExpression = (function () {
}
else if (!varspec.exploded) {
if (operator.named) {
result += LiteralExpression.encodeLiteral(varspec.varname);
result += encodingHelper.encodeLiteral(varspec.varname);
if (!isDefined(value)) {
result += operator.ifEmpty;
continue;
......
......@@ -3,15 +3,15 @@
var charHelper = (function () {
"use strict";
function isAlpha(chr) {
function isAlpha (chr) {
return (chr >= 'a' && chr <= 'z') || ((chr >= 'A' && chr <= 'Z'));
}
function isDigit(chr) {
function isDigit (chr) {
return chr >= '0' && chr <= '9';
}
function isHexDigit(chr) {
function isHexDigit (chr) {
return isDigit(chr) || (chr >= 'a' && chr <= 'f') || (chr >= 'A' && chr <= 'F');
}
......
......@@ -25,9 +25,27 @@ var encodingHelper = (function () {
return encode(text, true);
}
function encodeLiteral (literal) {
var
result = '',
index,
chr = '';
for (index = 0; index < literal.length; index += chr.length) {
chr = pctEncoder.pctCharAt(literal, index);
if (chr.length > 1) {
result += chr;
}
else {
result += rfcCharHelper.isReserved(chr) || rfcCharHelper.isUnreserved(chr) ? chr : pctEncoder.encodeCharacter(chr);
}
}
return result;
}
return {
encode: encode,
encodePassReserved: encodePassReserved
encodePassReserved: encodePassReserved,
encodeLiteral: encodeLiteral
};
}());
......@@ -8,7 +8,7 @@ var operators = (function () {
var
bySymbol = {};
function create(symbol) {
function create (symbol) {
bySymbol[symbol] = {
symbol: symbol,
separator: (symbol === '?') ? '&' : (symbol === '' || symbol === '+' || symbol === '#') ? ',' : symbol,
......@@ -30,13 +30,15 @@ var operators = (function () {
create(';');
create('?');
create('&');
return {valueOf: function (chr) {
return {
valueOf: function (chr) {
if (bySymbol[chr]) {
return bySymbol[chr];
}
if ("=,!@|".indexOf(chr) >= 0) {
throw new Error('Illegal use of reserved operator "' + chr + '"');
return null;
}
return bySymbol[''];
}};
}
};
}());
/*jshint unused:false */
/*global pctEncoder, operators, charHelper, rfcCharHelper, LiteralExpression, UriTemplate, VariableExpression*/
/*global pctEncoder, operators, charHelper, rfcCharHelper, LiteralExpression, UriTemplate, VariableExpression, UriTemplateError*/
var parse = (function () {
"use strict";
function parseExpression (outerText) {
function parseExpression (expressionText) {
var
text,
operator,
varspecs = [],
varspec = null,
......@@ -14,34 +14,42 @@ var parse = (function () {
chr = '';
function closeVarname () {
varspec = {varname: text.substring(varnameStart, index), exploded: false, maxLength: null};
var varname = expressionText.substring(varnameStart, index);
if (varname.length === 0) {
throw new UriTemplateError({expressionText: expressionText, message: "a varname must be specified", position: index});
}
varspec = {varname: varname, exploded: false, maxLength: null};
varnameStart = null;
}
function closeMaxLength () {
if (maxLengthStart === index) {
throw new Error("after a ':' you have to specify the length. position = " + index);
throw new UriTemplateError({expressionText: expressionText, message: "after a ':' you have to specify the length", position: index});
}
varspec.maxLength = parseInt(text.substring(maxLengthStart, index), 10);
varspec.maxLength = parseInt(expressionText.substring(maxLengthStart, index), 10);
maxLengthStart = null;
}
// remove outer braces
text = outerText.substr(1, outerText.length - 2);
operator = (function (operatorText) {
var op = operators.valueOf(operatorText);
if (op === null) {
throw new UriTemplateError({expressionText: expressionText, message: "illegal use of reserved operator", position: index, operator: operatorText});
}
return op;
}(expressionText.charAt(0)));
index = operator.symbol.length;
// determine operator
operator = operators.valueOf(text.charAt(0));
index = (operator.symbol === '') ? 0 : 1;
varnameStart = index;
for (; index < text.length; index += chr.length) {
chr = pctEncoder.pctCharAt(text, index);
for (; index < expressionText.length; index += chr.length) {
chr = pctEncoder.pctCharAt(expressionText, index);
if (varnameStart !== null) {
// the spec says: varname = varchar *( ["."] varchar )
// so a dot is allowed except for the first char
if (chr === '.') {
if (varnameStart === index) {
throw new Error('a varname MUST NOT start with a dot -- see position ' + index);
throw new UriTemplateError({expressionText: expressionText, message: "a varname MUST NOT start with a dot", position: index});
}
continue;
}
......@@ -52,11 +60,11 @@ var parse = (function () {
}
if (maxLengthStart !== null) {
if (index === maxLengthStart && chr === '0') {
throw new Error('A :prefix must not start with digit 0 -- see position ' + index);
throw new UriTemplateError({expressionText: expressionText, message: "A :prefix must not start with digit 0", position: index});
}
if (charHelper.isDigit(chr)) {
if (index - maxLengthStart >= 4) {
throw new Error('A :prefix must max 4 digits -- see position ' + index);
throw new UriTemplateError({expressionText: expressionText, message: "A :prefix must have max 4 digits", position: index});
}
continue;
}
......@@ -64,20 +72,23 @@ var parse = (function () {
}
if (chr === ':') {
if (varspec.maxLength !== null) {
throw new Error('only one :maxLength is allowed per varspec at position ' + index);
throw new UriTemplateError({expressionText: expressionText, message: "only one :maxLength is allowed per varspec", position: index});
}
if (varspec.exploded) {
throw new UriTemplateError({expressionText: expressionText, message: "an exploeded varspec MUST NOT be varspeced", position: index});
}
maxLengthStart = index + 1;
continue;
}
if (chr === '*') {
if (varspec === null) {
throw new Error('explode exploded at position ' + index);
throw new UriTemplateError({expressionText: expressionText, message: "exploded without varspec", position: index});
}
if (varspec.exploded) {
throw new Error('explode exploded twice at position ' + index);
throw new UriTemplateError({expressionText: expressionText, message: "exploded twice", position: index});
}
if (varspec.maxLength) {
throw new Error('an explode (*) MUST NOT follow to a prefix, see position ' + index);
throw new UriTemplateError({expressionText: expressionText, message: "an explode (*) MUST NOT follow to a prefix", position: index});
}
varspec.exploded = true;
continue;
......@@ -89,7 +100,7 @@ var parse = (function () {
varnameStart = index + 1;
continue;
}
throw new Error("illegal character '" + chr + "' at position " + index + ' of "' + text + '"');
throw new UriTemplateError({expressionText: expressionText, message: "illegal character", character: chr, position: index});
} // for chr
if (varnameStart !== null) {
closeVarname();
......@@ -98,10 +109,10 @@ var parse = (function () {
closeMaxLength();
}
varspecs.push(varspec);
return new VariableExpression(outerText, operator, varspecs);
return new VariableExpression(expressionText, operator, varspecs);
}
function parseTemplate (uriTemplateText) {
function parse (uriTemplateText) {
// assert filled string
var
index,
......@@ -113,7 +124,7 @@ var parse = (function () {
chr = uriTemplateText.charAt(index);
if (literalStart !== null) {
if (chr === '}') {
throw new Error('brace was closed in position ' + index + " but never opened");
throw new UriTemplateError({templateText: uriTemplateText, message: "unopened brace closed", position: index});
}
if (chr === '{') {
if (literalStart < index) {
......@@ -128,13 +139,21 @@ var parse = (function () {
if (braceOpenIndex !== null) {
// here just { is forbidden
if (chr === '{') {
throw new Error('brace was opened in position ' + braceOpenIndex + " and cannot be reopened in position " + index);
throw new UriTemplateError({templateText: uriTemplateText, message: "brace already opened", position: index});
}
if (chr === '}') {
if (braceOpenIndex + 1 === index) {
throw new Error("empty braces on position " + braceOpenIndex);
throw new UriTemplateError({templateText: uriTemplateText, message: "empty braces", position: braceOpenIndex});
}
try {
expressions.push(parseExpression(uriTemplateText.substring(braceOpenIndex + 1, index)));
}
catch (error) {
if (error.prototype === UriTemplateError.prototype) {
throw new UriTemplateError({templateText: uriTemplateText, message: error.options.message, position: braceOpenIndex + error.options.position, details: error.options});
}
throw error;
}
expressions.push(parseExpression(uriTemplateText.substring(braceOpenIndex, index + 1)));
braceOpenIndex = null;
literalStart = index + 1;
}
......@@ -143,7 +162,7 @@ var parse = (function () {
throw new Error('reached unreachable code');
}
if (braceOpenIndex !== null) {
throw new Error("brace was opened on position " + braceOpenIndex + ", but never closed");
throw new UriTemplateError({templateText: uriTemplateText, message: "unclosed brace", position: braceOpenIndex});
}
if (literalStart < uriTemplateText.length) {
expressions.push(new LiteralExpression(uriTemplateText.substr(literalStart)));
......@@ -151,5 +170,5 @@ var parse = (function () {
return new UriTemplate(uriTemplateText, expressions);
}
return parseTemplate;
return parse;
}());
......@@ -9,7 +9,7 @@ module.exports = (function () {
var NOISY = false;
function log (text) {
if (NOISY) {
console.log(text);
console.log.call(null, arguments);
}
}
......@@ -32,19 +32,20 @@ module.exports = (function () {
uriTemplate = UriTemplate.parse(template);
}
catch (error) {
if (expected === false) {
log('ok. expected error found');
if (expected === false && error.constructor.prototype === UriTemplate.UriTemplateError.prototype) {
log('ok. expected error found', error);
return;
}
console.log('error', error);
test.fail('chapter ' + chapterName + ', template ' + template + ' threw error, when parsing: ' + error);
console.log('error.constructor.prototype', error.constructor.prototype);
test.fail('chapter ' + chapterName + ', template ' + template + ' threw error: ' + error);
return;
}
test.ok(!!uriTemplate, 'uri template could not be parsed');
try {
actual = uriTemplate.expand(variables);
if (expected === false) {
test.fail('chapter ' + chapterName + ', template ' + template + ' expected to fail, but returned \'' + actual + '\'!');
test.fail('chapter ' + chapterName + ', template ' + template + ' expected to fail, but returned \'' + actual + '\'');
return;
}
}
......@@ -53,7 +54,7 @@ module.exports = (function () {
return;
}
console.log('error', error);
test.fail('chapter ' + chapterName + ', template ' + template + ' threw error, when expanding: ' + JSON.stringify(error, null, 4));
test.fail('chapter ' + chapterName + ', template "' + template + '" threw error, when expanding: ' + JSON.stringify(error, null, 4));
return;
}
if (expected.constructor === Array) {
......
module.exports = (function () {
"use strict";
var
sandbox = require('nodeunit').utils.sandbox,
context = {};
sandbox('src/charHelper.js', context);
sandbox('src/pctEncoder.js', context);
sandbox('src/rfcCharHelper.js', context);
sandbox('src/encodingHelper.js', context);
var encodingHelper = context.encodingHelper;
return {
'encodeLiteral works as expected': function (test) {
test.equal(typeof encodingHelper.encodeLiteral, 'function');
test.equal(encodingHelper.encodeLiteral('a b'), 'a%20b');
test.equal(encodingHelper.encodeLiteral('a%20b'), 'a%20b');
test.done();
}
};
}());
\ No newline at end of file
......@@ -12,10 +12,8 @@ module.exports = (function () {
var LiteralExpression = context.LiteralExpression;
return {
'LiteralExpression.encodeLiteral works as expected': function (test) {
test.equal(typeof LiteralExpression.encodeLiteral, 'function');
test.equal(LiteralExpression.encodeLiteral('a b'), 'a%20b');
test.equal(LiteralExpression.encodeLiteral('a%20b'), 'a%20b');
'LiteralExpression has an expand method': function (test) {
test.equal(typeof LiteralExpression.prototype.expand, 'function');
test.done();
}
};
......
......@@ -5,6 +5,7 @@ module.exports = (function () {
var
sandbox = require('nodeunit').utils.sandbox,
context = {};
sandbox('src/UriTemplateError.js', context);
sandbox('src/objectHelper.js', context);
sandbox('src/charHelper.js', context);
sandbox('src/pctEncoder.js', context);
......
module.exports = (function () {
"use strict";
var context = {};
require('nodeunit').utils.sandbox('src/UriTemplateError.js', context);
var UriTemplateError = context.UriTemplateError;
return {
"meaningful toString": function (test) {
var error = new UriTemplateError({});
test.equal(error.toString().indexOf('object'), -1);
test.done();
}
};
}());
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment