diff --git a/dist/$.js b/dist/$.js index 3c7b8c4..75de3b8 100644 --- a/dist/$.js +++ b/dist/$.js @@ -1,11 +1,12 @@ /*globals Node:true, NodeList:true*/ -$ = (function (document, window, $) { +$$ = (function (document, window, $$) { // Node covers all elements, but also the document objects var node = Node.prototype, nodeList = NodeList.prototype, forEach = 'forEach', trigger = 'trigger', each = [][forEach], + cookies = {}, // note: createElement requires a string in Firefox dummy = document.createElement('i'); @@ -49,7 +50,13 @@ $ = (function (document, window, $) { return this; }; - $ = function (s) { + + // Get a cookie value + cookies.get = function(key) { + return unescape(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + escape(key).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null; + } + + $$ = function (s) { // querySelectorAll requires a string with a length // otherwise it throws an exception var r = document.querySelectorAll(s || '☺'), @@ -60,10 +67,17 @@ $ = (function (document, window, $) { return length == 1 ? r[0] : r; }; - // $.on and $.trigger allow for pub/sub type global + // $$.on and $$.trigger allow for pub/sub type global // custom events. - $.on = node.on.bind(dummy); - $[trigger] = node[trigger].bind(dummy); + $$.on = node.on.bind(dummy); + $$[trigger] = node[trigger].bind(dummy); + + $$.cookies = cookies + + if ( typeof define === "function") { + define( "jsmin", [], function () { return $$; } ); + } + + return $$; +})(document, this); - return $; -})(document, this); \ No newline at end of file diff --git a/dist/$.min.js b/dist/$.min.js index 99505a4..0955827 100644 --- a/dist/$.min.js +++ b/dist/$.min.js @@ -1 +1 @@ -$=function(t,n,e){var i=Node.prototype,r=NodeList.prototype,o="forEach",u="trigger",c=[][o],s=t.createElement("i");return r[o]=c,n.on=i.on=function(t,n){return this.addEventListener(t,n,!1),this},r.on=function(t,n){return this[o](function(e){e.on(t,n)}),this},n[u]=i[u]=function(n,e){var i=t.createEvent("HTMLEvents");return i.initEvent(n,!0,!0),i.data=e||{},i.eventName=n,i.target=this,this.dispatchEvent(i),this},r[u]=function(t){return this[o](function(n){n[u](t)}),this},e=function(n){var e=t.querySelectorAll(n||"☺"),i=e.length;return 1==i?e[0]:e},e.on=i.on.bind(s),e[u]=i[u].bind(s),e}(document,this); \ No newline at end of file +$$=function(t,n,e){var i=Node.prototype,r=NodeList.prototype,o="forEach",c="trigger",u=[][o],s={},a=t.createElement("i");return r[o]=u,n.on=i.on=function(t,n){return this.addEventListener(t,n,!1),this},r.on=function(t,n){return this[o](function(e){e.on(t,n)}),this},n[c]=i[c]=function(n,e){var i=t.createEvent("HTMLEvents");return i.initEvent(n,!0,!0),i.data=e||{},i.eventName=n,i.target=this,this.dispatchEvent(i),this},r[c]=function(t){return this[o](function(n){n[c](t)}),this},s.get=function(n){return unescape(t.cookie.replace(RegExp("(?:(?:^|.*;)\\s*"+escape(n).replace(/[\-\.\+\*]/g,"\\$&")+"\\s*\\=\\s*([^;]*).*$)|^.*$"),"$1"))||null},e=function(n){var e=t.querySelectorAll(n||"☺"),i=e.length;return 1==i?e[0]:e},e.on=i.on.bind(a),e[c]=i[c].bind(a),e.cookies=s,"function"==typeof define&&define("jsmin",[],function(){return e}),e}(document,this); \ No newline at end of file diff --git a/test/spec/api.js b/test/spec/api.js index 07c1cc9..e237579 100644 --- a/test/spec/api.js +++ b/test/spec/api.js @@ -1,76 +1,76 @@ -/*globals afterEach: true, describe: true, it: true, expect: true, beforeEach: true, appendToDom:true, destroyDom:true, $:true*/ +/*globals afterEach: true, describe: true, it: true, expect: true, beforeEach: true, appendToDom:true, destroyDom:true, $$:true*/ 'use strict'; describe('api', function () { describe('no selector', function () { it('should return on method', function () { - assert(typeof $.on === 'function'); + assert(typeof $$.on === 'function'); }); it('should return trigger method', function () { - assert(typeof $.trigger === 'function'); + assert(typeof $$.trigger === 'function'); }); }); describe('empty selector', function () { - var $empty = $(''); + var $$empty = $$(''); it('should return on method', function () { - assert(typeof $empty.on === 'function'); + assert(typeof $$empty.on === 'function'); }); it('should return trigger method', function () { - assert(typeof $empty.trigger === 'function'); + assert(typeof $$empty.trigger === 'function'); }); }); describe('iterator', function () { - var $link; + var $$link; beforeEach(function () { appendToDom('section', ['a', 'a', 'a']); - $link = $('section > a'); + $$link = $$('section > a'); }); afterEach(destroyDom); it('should have forEach', function () { - assert(typeof $link.forEach === 'function'); + assert(typeof $$link.forEach === 'function'); }); it('should loop', function () { var ctr = 0; - $link.forEach(function () { + $$link.forEach(function () { ctr++; }); assert(ctr === 3); }); it('should have each element passed to iterator', function () { - $link.forEach(function (el, i) { - assert(el === $link[i]); + $$link.forEach(function (el, i) { + assert(el === $$link[i]); }); }); }); describe('matched selector', function () { - var $link; + var $$link; beforeEach(function () { appendToDom('section', ['a']); - $link = $('section > a'); + $$link = $$('section > a'); }); afterEach(destroyDom); it('should return on method', function () { - assert(typeof $link.on === 'function'); + assert(typeof $$link.on === 'function'); }); it('should return trigger method', function () { - assert(typeof $link.trigger === 'function'); + assert(typeof $$link.trigger === 'function'); }); }); }); diff --git a/test/spec/chaining.js b/test/spec/chaining.js index 098e5e0..d5aa64f 100644 --- a/test/spec/chaining.js +++ b/test/spec/chaining.js @@ -1,51 +1,51 @@ -/*globals noop:true, appendToDom:true, destroyDom:true, $:true*/ +/*globals noop:true, appendToDom:true, destroyDom:true, $$:true*/ 'use strict'; describe('chaining', function () { describe('no selector', function () { it('should return trigger method from on', function () { - assert(typeof $.on('event', noop).trigger === 'function'); + assert(typeof $$.on('event', noop).trigger === 'function'); }); it('should return on method from trigger', function () { - assert(typeof $.on('event', noop).trigger('event').on === 'function'); + assert(typeof $$.on('event', noop).trigger('event').on === 'function'); }); }); // left in, note that Firefox does not support empty selectors describe('unmatched selector', function () { it('should return trigger method from on', function () { - assert(typeof $('unmatched').on('event', noop).trigger === 'function'); + assert(typeof $$('unmatched').on('event', noop).trigger === 'function'); }); it('should return on method from trigger', function () { - assert(typeof $('unmatched').on('event', noop).trigger('event').on === 'function'); + assert(typeof $$('unmatched').on('event', noop).trigger('event').on === 'function'); }); }); describe('empty selector', function () { it('should still allow for chained methods', function () { - assert(typeof $('').on('event', noop).trigger === 'function'); + assert(typeof $$('').on('event', noop).trigger === 'function'); }); }); describe('matched selector', function () { - var $link; + var $$link; beforeEach(function () { appendToDom('section', ['a']); - $link = $('section > a'); + $$link = $$('section > a'); }); afterEach(destroyDom); it('should return trigger method from on', function () { - assert(typeof $link.on('event', noop).trigger === 'function'); + assert(typeof $$link.on('event', noop).trigger === 'function'); }); it('should return on method from trigger', function () { - assert(typeof $link.on('event', noop).trigger('event').on === 'function'); + assert(typeof $$link.on('event', noop).trigger('event').on === 'function'); }); }); }); diff --git a/test/spec/delegation.js b/test/spec/delegation.js index 7cffd1f..ab6bb5f 100644 --- a/test/spec/delegation.js +++ b/test/spec/delegation.js @@ -1,17 +1,17 @@ -/*globals appendToDom:true, destroyDom:true, $:true*/ +/*globals appendToDom:true, destroyDom:true, $$:true*/ 'use strict'; describe('delegation', function () { - var $section, - $article, + var $$section, + $$article, spy; beforeEach(function () { spy = sinon.spy(); appendToDom('section', ['article', 'a', 'a']); - $section = $('section'); - $article = $('section > article'); + $$section = $$('section'); + $$article = $$('section > article'); }); afterEach(destroyDom); @@ -19,27 +19,27 @@ describe('delegation', function () { it('should trigger container events from children', function () { var target; - $section + $$section .on('event', spy) .on('event', function (event) { target = event.target; }); - $article.trigger('event'); + $$article.trigger('event'); assert(spy.called); - assert(target === $article); + assert(target === $$article); }); it('should trigger click event using event delegation', function () { - $('body').delegate('section > a', 'click', spy); - $('section > a')[0].trigger('click'); + $$('body').delegate('section > a', 'click', spy); + $$('section > a')[0].trigger('click'); assert(spy.called); - $('section > a')[0].trigger('click'); + $$('section > a')[0].trigger('click'); assert(spy.calledTwice); - $('section > a')[1].trigger('click'); + $$('section > a')[1].trigger('click'); assert(spy.calledThrice); }); }); diff --git a/test/spec/elements.js b/test/spec/elements.js index 56b8b8a..2e47f61 100644 --- a/test/spec/elements.js +++ b/test/spec/elements.js @@ -1,4 +1,4 @@ -/*globals appendToDom:true, destroyDom:true, $:true*/ +/*globals appendToDom:true, destroyDom:true, $$:true*/ 'use strict'; describe('elements', function () { @@ -9,7 +9,7 @@ describe('elements', function () { afterEach(destroyDom); it('should grab an element from the DOM', function () { - var link = $('div > a'); + var link = $$('div > a'); assert(link.outerHTML === ''); }); diff --git a/test/spec/events.js b/test/spec/events.js index 7a7fdd2..c10649c 100644 --- a/test/spec/events.js +++ b/test/spec/events.js @@ -1,4 +1,4 @@ -/*globals $:true*/ +/*globals $$:true*/ 'use strict'; describe('events', function () { @@ -13,11 +13,11 @@ describe('events', function () { }); it('should assign an event to an element', function () { - var $body = $('body'); + var $$body = $$('body'); - $body.on('click', spy); + $$body.on('click', spy); - $body.trigger('click'); + $$body.trigger('click'); sinon.assert.called(spy); }); @@ -39,17 +39,17 @@ describe('events', function () { }); it('should not trigger an event on a non-element', function () { - $('.this-isnt-on-the-dom').on('event', spy); + $$('.this-isnt-on-the-dom').on('event', spy); - $('.this-isnt-on-the-dom').trigger('event'); + $$('.this-isnt-on-the-dom').trigger('event'); sinon.assert.notCalled(spy); }); it('should assign an event to the internal element', function () { - $.on('event', spy); + $$.on('event', spy); - $.trigger('event'); + $$.trigger('event'); sinon.assert.called(spy); }); @@ -62,7 +62,7 @@ describe('events', function () { afterEach(destroyDom); it('should trigger', function () { - $('div > a').on('click', spy).trigger('click'); + $$('div > a').on('click', spy).trigger('click'); sinon.assert.calledTwice(spy); }); @@ -76,8 +76,8 @@ describe('events', function () { afterEach(destroyDom); it('should trigger', function () { - $('div > a').addEventListener('click', spy); - $('div > a').trigger('click'); + $$('div > a').addEventListener('click', spy); + $$('div > a').trigger('click'); sinon.assert.called(spy); }); });