diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..9a9cbbb --- /dev/null +++ b/.travis.yml @@ -0,0 +1,4 @@ +language: node_js +node_js: + - "0.11" + - "0.10" \ No newline at end of file diff --git a/README.md b/README.md index f0baa49..e399da5 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,11 @@ A simple front-end parameters manager for specifying global script parameters. ## Usage -Include [`params.js`](https://github.com/droptype/params/releases/tag/v1.0.0) in the page and initialize a parameters object. +Include [`params.js`](https://github.com/droptype/params/releases/tag/v1.2.0) +in the page and initialize a Params object. Or, include the +`params-1.2.0-min.html` HTML fragment, which initializes a Params instance +under `window.params`. (It’s also slightly smaller, omitting features that +are unnecessary if there‘s only one instance.) ```javascript params = new Params() @@ -107,3 +111,18 @@ params = new Params() true ``` +* Set and unescape a parameter: + + ```javascript + > params.setUnescaped('title', 'Green Eggs & Ham') + > params.title + 'Green Eggs & Ham' + ``` + +* Push and unescape a parameter: + + ```javascript + > params.pushUnescaped('tags', '<br>').pushUnescaped('tags', '<p>') + > params.tags + ['
', '

'] + ``` diff --git a/package.json b/package.json index 742a8a1..6cbfd86 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "params", - "version": "1.1.0", + "version": "1.2.0", "description": "", "dependencies": { }, diff --git a/source/params.coffee b/source/params.coffee index ed7590e..6e82e91 100644 --- a/source/params.coffee +++ b/source/params.coffee @@ -2,7 +2,23 @@ Parameters manager - https://github.com/droptype/params ### -INTERNAL = ['set', 'getAll', '_strict'] +INTERNAL = ['set', 'getAll', 'push', 'setUnescaped', 'pushUnescaped', '_strict'] + +# Unescape helper, adapted from [Underscore](http://underscorejs.org). +entities = + '&' : '&' + '<' : '<' + '>' : '>' + '"' : '"' + ''' : '\'' +exp = new RegExp('(&|<|>|"|')', 'g') +_unescapeHTML = (data_str) -> + unless data_str + return '' + return data_str.replace exp, (match) -> + return entities[match] + + class Params @@ -18,6 +34,8 @@ class Params # value - an arbitrary value to set # kwargs - an object of optional arguments # push - a Boolean indicating the key is should be set to an Array + # unescape - a Boolean indicating the value should have HTML + # entities unescaped first. # # Returns self for chaining. set: (key, value, kwargs={}) => @@ -25,6 +43,11 @@ class Params if key in INTERNAL throw new Error("Cannot set the '#{ key }' param, silly.") + if kwargs.unescape + unless typeof value is 'string' + throw new Error('Cannot unescape non-string values') + value = _unescapeHTML(value) + key_list = key.split('.') target_obj = this prev_key = null @@ -61,8 +84,14 @@ class Params return this + # Public: shortcut for `set` with `unescape: true`. + # + # Returns self for chaining. + setUnescaped: (key, value) => + return @set(key, value, unescape: true) - # Public: push a parameter. If they key is not already set, it will be created. + # Public: push a parameter. If they key is not already set, it will be + # created. Shortcut for `set` with `push: true`. # # key - a String key to set. MAY use dot notation to specify sub parameters. # value - an arbitrary value to set @@ -71,6 +100,11 @@ class Params push: (key, value) => return @set(key, value, push: true) + # Public: shortcut for `set` with `push: true, unescape: true`. + # + # Returns self for chaining. + pushUnescaped: (key, value) => + return @set(key, value, push: true, unescape: true) # Public: get all of the parameters. Used to avoid getting the internal # properties as well. diff --git a/test/test.coffee b/test/test.coffee index 7e6b080..9c21eff 100644 --- a/test/test.coffee +++ b/test/test.coffee @@ -45,6 +45,9 @@ describe 'Params', -> params = new Params() (-> params.set('set', 1)).should.throw() (-> params.set('getAll', 1)).should.throw() + (-> params.set('push', 1)).should.throw() + (-> params.set('setUnescaped', 1)).should.throw() + (-> params.set('pushUnescaped', 1)).should.throw() (-> params.set('_strict', 1)).should.throw() it 'should push a parameter', -> @@ -72,24 +75,32 @@ describe 'Params', -> (-> params.push('key.foo.bar', 1)).should.throw("Cannot push subparameter 'foo.bar'. 'foo' is not an Array.") params.key.foo.should.equal(1) + it 'should set parameters as escaped', -> + params = new Params() + params.set('key.subkey', '<') + params.key.subkey.should.equal('<') -# params = new Params() - -# console.log params.getAll() - -# params.set('foo', 'a').set('Bar', 1234) - -# console.log params.getAll() - -# params.set('foo', 9) - -# console.log params.getAll() + it 'should unescape parameters when using setUnescaped', -> + params = new Params() + params.setUnescaped('key', '<') + params.key.should.equal('<') + it 'should push parameters as escaped', -> + params = new Params() + params.push('key.subkey', '<') + params.key.subkey.should.eql(['<']) -# params.set('asdf.asdf.asdf', 9) + it 'should unescape parameters when using pushUnescaped', -> + params = new Params() + params.pushUnescaped('key', '<') + params.key.should.eql(['<']) -# console.log params.getAll() + it 'should allow for a mix of push and pushUnescaped', -> + params = new Params() + params.push('key', '<script>') + params.pushUnescaped('key', '<script>') + params.key.should.eql(['<script>', '