From 94c017f746df8623d0c154c490cc1a7959d6c7a0 Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Sat, 30 May 2015 05:33:24 +0300 Subject: [PATCH 1/2] Aggressive minification --- dist/domtokenlist.js | 143 +++++++++++++++++-------------------- dist/domtokenlist.min.js | 4 +- gulpfile.js | 4 +- src/DOMTokenList-newest.js | 10 +-- src/DOMTokenList.js | 131 ++++++++++++++++----------------- tests/index.html | 4 ++ 6 files changed, 140 insertions(+), 156 deletions(-) diff --git a/dist/domtokenlist.js b/dist/domtokenlist.js index 1ae0fed..c7d5b89 100644 --- a/dist/domtokenlist.js +++ b/dist/domtokenlist.js @@ -1,8 +1,8 @@ -/*! DOMTokenlist shim | Copyright 2015 Jonathan Wilsson and contributors. */ +/*! DOMTokenlist shim | Copyright 2015 Jonathan Wilsson and Bogdan Chadkin. */ ;(function () { 'use strict'; - if (!('DOMTokenList' in window)) { + if (!window.DOMTokenList) { return; } @@ -18,11 +18,11 @@ // arguments, easy to test for var iterateArg = function (fn) { return function () { - var args = arguments; - var i, max; + var tokens = arguments; + var i; - for(i = 0, max = args.length; i < max; i += 1) { - fn.call(this, args[i]); + for(i = 0; i < tokens.length; i += 1) { + fn.call(this, tokens[i]); } }; }; @@ -49,7 +49,7 @@ ;(function (window) { 'use strict'; - if ('DOMTokenList' in window && !window.QUnit) { + if (window.DOMTokenList) { return; } @@ -71,10 +71,6 @@ return -1; }; - var toArray = function (object) { - return arr.slice.call(object); - }; - var validateToken = function (token) { var whitespace = /[\u0009\u000A\u000C\u000D\u0020]/; @@ -84,102 +80,97 @@ }; var DOMTokenList = function (element, prop) { + var inst = this; var i; var values = []; if (element && prop) { - this.element = element; - this.prop = prop; + inst.element = element; + inst.prop = prop; - values = element[prop]; - if (values) { - values = values.replace(/^\s+|\s+$/g,'').split(/\s+/); + if (element[prop]) { + values = element[prop].replace(/^\s+|\s+$/g,'').split(/\s+/); for (i = 0; i < values.length; i++) { - this[i] = values[i]; + inst[i] = values[i]; } - } else { - values = []; } } - this.length = values.length; + inst.length = values.length; }; - DOMTokenList.prototype.add = function () { - var i; - var tokens = toArray(arguments); - - for (i = 0; i < tokens.length; i++) { - validateToken(tokens[i]); + DOMTokenList.prototype = { + add: function () { + var inst = this; + var i; + var tokens = arguments; - if (!this.contains(tokens[i])) { - arr.push.call(this, tokens[i]); + for (i = 0; i < tokens.length; i++) { + validateToken(tokens[i]); - this.length = toArray(this).length; + if (!inst.contains(tokens[i])) { + arr.push.call(inst, tokens[i]); + } } - } - if (this.element) { - this.element[this.prop] = this.toString(); - } - }; + if (inst.element) { + inst.element[inst.prop] = inst; + } + }, - DOMTokenList.prototype.contains = function (token) { - validateToken(token); + contains: function (token) { + validateToken(token); - return inArray(this, token) !== -1; - }; + return inArray(this, token) !== -1; + }, - DOMTokenList.prototype.item = function (index) { - return this[index] || null; - }; + item: function (index) { + return this[index] || null; + }, - DOMTokenList.prototype.remove = function () { - var i; - var key; - var tokens = toArray(arguments); - - for (i = 0; i < tokens.length; i++) { - validateToken(tokens[i]); + remove: function () { + var inst = this; + var i; + var key; + var tokens = arguments; - key = inArray(this, tokens[i]); + for (i = 0; i < tokens.length; i++) { + validateToken(tokens[i]); - if (key !== -1) { - arr.splice.call(this, key, 1); + key = inArray(inst, tokens[i]); - this.length = toArray(this).length; + if (key !== -1) { + arr.splice.call(inst, key, 1); + } } - } - - if (this.element) { - this.element[this.prop] = this.toString(); - } - }; - DOMTokenList.prototype.toggle = function (token, force) { - validateToken(token); - - if (!this.contains(token) && force === false) { - return false; - } + if (inst.element) { + inst.element[inst.prop] = inst; + } + }, - if (this.contains(token)) { - if (!force) { - this.remove(token); + toggle: function (token, force) { + var inst = this; + if(inst.contains(token)) { + if(force) { + return true; + } + inst.remove(token); return false; + } else { + if(force === false) { + return false; + } + + inst.add(token); + return true; } + }, - return true; + toString: function () { + return arr.join.call(this, ' '); } - - this.add(token); - - return true; - }; - - DOMTokenList.prototype.toString = function () { - return arr.join.call(this, ' '); }; window.DOMTokenList = DOMTokenList; diff --git a/dist/domtokenlist.min.js b/dist/domtokenlist.min.js index 76820d3..37b39f4 100644 --- a/dist/domtokenlist.min.js +++ b/dist/domtokenlist.min.js @@ -1,2 +1,2 @@ -/*! DOMTokenlist shim | Copyright 2015 Jonathan Wilsson and contributors. */ -!function(){"use strict";if("DOMTokenList"in window){var t=document.createElement("a").classList,e=DOMTokenList.prototype,n=e.add,i=e.remove,o=e.toggle;t.add("c1","c2");var r=function(t){return function(){var e,n,i=arguments;for(e=0,n=i.length;n>e;e+=1)t.call(this,i[e])}};t.contains("c2")||(e.add=r(n),e.remove=r(i)),t.toggle("c1",!0)||(e.toggle=function(t,e){return void 0===e?o.call(this,t):((e?n:rem).call(this,t),!!e)})}}(),function(t){"use strict";var e=[],n=function(t,n){var i;if(e.indexOf)return e.indexOf.call(t,n);for(i=0;i Jonathan Wilsson and Bogdan Chadkin. */\n', { - year: new Date().getFullYear() - })) + .pipe(header('/*! DOMTokenlist shim | Copyright <%= new Date().getFullYear() %> Jonathan Wilsson and Bogdan Chadkin. */\n')) .pipe(size({ showFiles: true })) diff --git a/src/DOMTokenList-newest.js b/src/DOMTokenList-newest.js index 0ac788f..f6df945 100644 --- a/src/DOMTokenList-newest.js +++ b/src/DOMTokenList-newest.js @@ -1,7 +1,7 @@ ;(function () { 'use strict'; - if (!('DOMTokenList' in window)) { + if (!window.DOMTokenList) { return; } @@ -17,11 +17,11 @@ // arguments, easy to test for var iterateArg = function (fn) { return function () { - var args = arguments; - var i, max; + var tokens = arguments; + var i; - for(i = 0, max = args.length; i < max; i += 1) { - fn.call(this, args[i]); + for(i = 0; i < tokens.length; i += 1) { + fn.call(this, tokens[i]); } }; }; diff --git a/src/DOMTokenList.js b/src/DOMTokenList.js index 84c2d7f..3af17af 100644 --- a/src/DOMTokenList.js +++ b/src/DOMTokenList.js @@ -1,7 +1,7 @@ ;(function (window) { 'use strict'; - if ('DOMTokenList' in window && !window.QUnit) { + if (window.DOMTokenList) { return; } @@ -23,10 +23,6 @@ return -1; }; - var toArray = function (object) { - return arr.slice.call(object); - }; - var validateToken = function (token) { var whitespace = /[\u0009\u000A\u000C\u000D\u0020]/; @@ -36,102 +32,97 @@ }; var DOMTokenList = function (element, prop) { + var inst = this; var i; var values = []; if (element && prop) { - this.element = element; - this.prop = prop; + inst.element = element; + inst.prop = prop; - values = element[prop]; - if (values) { - values = values.replace(/^\s+|\s+$/g,'').split(/\s+/); + if (element[prop]) { + values = element[prop].replace(/^\s+|\s+$/g,'').split(/\s+/); for (i = 0; i < values.length; i++) { - this[i] = values[i]; + inst[i] = values[i]; } - } else { - values = []; } } - this.length = values.length; + inst.length = values.length; }; - DOMTokenList.prototype.add = function () { - var i; - var tokens = toArray(arguments); - - for (i = 0; i < tokens.length; i++) { - validateToken(tokens[i]); + DOMTokenList.prototype = { + add: function () { + var inst = this; + var i; + var tokens = arguments; - if (!this.contains(tokens[i])) { - arr.push.call(this, tokens[i]); + for (i = 0; i < tokens.length; i++) { + validateToken(tokens[i]); - this.length = toArray(this).length; + if (!inst.contains(tokens[i])) { + arr.push.call(inst, tokens[i]); + } } - } - if (this.element) { - this.element[this.prop] = this.toString(); - } - }; + if (inst.element) { + inst.element[inst.prop] = inst; + } + }, - DOMTokenList.prototype.contains = function (token) { - validateToken(token); + contains: function (token) { + validateToken(token); - return inArray(this, token) !== -1; - }; + return inArray(this, token) !== -1; + }, - DOMTokenList.prototype.item = function (index) { - return this[index] || null; - }; + item: function (index) { + return this[index] || null; + }, - DOMTokenList.prototype.remove = function () { - var i; - var key; - var tokens = toArray(arguments); - - for (i = 0; i < tokens.length; i++) { - validateToken(tokens[i]); + remove: function () { + var inst = this; + var i; + var key; + var tokens = arguments; - key = inArray(this, tokens[i]); + for (i = 0; i < tokens.length; i++) { + validateToken(tokens[i]); - if (key !== -1) { - arr.splice.call(this, key, 1); + key = inArray(inst, tokens[i]); - this.length = toArray(this).length; + if (key !== -1) { + arr.splice.call(inst, key, 1); + } } - } - - if (this.element) { - this.element[this.prop] = this.toString(); - } - }; - DOMTokenList.prototype.toggle = function (token, force) { - validateToken(token); - - if (!this.contains(token) && force === false) { - return false; - } + if (inst.element) { + inst.element[inst.prop] = inst; + } + }, - if (this.contains(token)) { - if (!force) { - this.remove(token); + toggle: function (token, force) { + var inst = this; + if(inst.contains(token)) { + if(force) { + return true; + } + inst.remove(token); return false; + } else { + if(force === false) { + return false; + } + + inst.add(token); + return true; } + }, - return true; + toString: function () { + return arr.join.call(this, ' '); } - - this.add(token); - - return true; - }; - - DOMTokenList.prototype.toString = function () { - return arr.join.call(this, ' '); }; window.DOMTokenList = DOMTokenList; diff --git a/tests/index.html b/tests/index.html index 909383c..083f9c6 100644 --- a/tests/index.html +++ b/tests/index.html @@ -11,6 +11,10 @@
+ + From 0f9b4f7a4a7f4714198f51c877c36a9a3eec9604 Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Sat, 30 May 2015 16:31:06 +0300 Subject: [PATCH 2/2] Fixed tests for DOMTokenList-newest --- dist/domtokenlist.js | 2 +- dist/domtokenlist.min.js | 2 +- src/DOMTokenList-newest.js | 2 +- tests/index.html | 3 ++- tests/tests-DOMTokenList-newest.js | 34 ++++++++++++++++++++++++++++++ 5 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 tests/tests-DOMTokenList-newest.js diff --git a/dist/domtokenlist.js b/dist/domtokenlist.js index c7d5b89..2c332ef 100644 --- a/dist/domtokenlist.js +++ b/dist/domtokenlist.js @@ -40,7 +40,7 @@ return toggle.call(this, cls); } - (force ? add : rem).call(this, cls); + (force ? add : remove).call(this, cls); return !!force; }; } diff --git a/dist/domtokenlist.min.js b/dist/domtokenlist.min.js index 37b39f4..b49c3e0 100644 --- a/dist/domtokenlist.min.js +++ b/dist/domtokenlist.min.js @@ -1,2 +1,2 @@ /*! DOMTokenlist shim | Copyright 2015 Jonathan Wilsson and Bogdan Chadkin. */ -!function(){"use strict";if(window.DOMTokenList){var t=document.createElement("a").classList,e=DOMTokenList.prototype,n=e.add,i=e.remove,r=e.toggle;t.add("c1","c2");var o=function(t){return function(){var e,n=arguments;for(e=0;e + + - diff --git a/tests/tests-DOMTokenList-newest.js b/tests/tests-DOMTokenList-newest.js new file mode 100644 index 0000000..701490c --- /dev/null +++ b/tests/tests-DOMTokenList-newest.js @@ -0,0 +1,34 @@ +module('DOMTokenList-newest'); + +test('Add multiple tokenes', function () { + var list = document.createElement('a').classList; + + list.add('token-1', 'token-2'); + list = toArray(list); + + deepEqual(list, ['token-1', 'token-2'], 'Assert that tokens is "token-1" and "token-2".'); +}); + +test('Test toggle force with filled token list', function () { + var list = document.createElement('a').classList; + + list.add('token-1'); + + equal(list.toggle('token-1', false), false, 'Assert that toggle returns false.'); +}); + +test('Test toggle force = true with empty token list', function () { + var list = document.createElement('a').classList; + + ok(list.toggle('token-1', true), 'Assert that toggle returns true.'); +}); + +test('Test toggle force = false with empty token list', function () { + var list = document.createElement('a').classList; + var result = list.toggle('token-12', false); + + list = toArray(list); + + deepEqual(list, [], 'Assert that token list is empty'); + equal(result, false, 'Assert that toggle returns false.'); +});