diff --git a/dist/domtokenlist-umd.js b/dist/domtokenlist-umd.js new file mode 100644 index 0000000..6f027ed --- /dev/null +++ b/dist/domtokenlist-umd.js @@ -0,0 +1,138 @@ +/*! DOMTokenlist shim | Copyright 2016 Jonathan Wilsson and Bogdan Chadkin. */ +;(function(root, factory) { + if (typeof define === 'function' && define.amd) { + define([], factory); + } else if (typeof exports === 'object') { + module.exports = factory(); + } else { + root.DOMTokenList = factory(); + } +}(this, function() { +'use strict'; + +var arr = []; + +var inArray = function (array, value) { + var i; + + if (arr.indexOf) { + return arr.indexOf.call(array, value); + } + + for (i = 0; i < array.length; i++) { + if (array[i] === value) { + return i; + } + } + + return -1; +}; + +var validateToken = function (token) { + var whitespace = /[\u0009\u000A\u000C\u000D\u0020]/; + + if (token === '' || whitespace.test(token)) { + throw new Error('Token must not be empty or contain whitespace.'); + } +}; + +var DOMTokenList = function (element, prop) { + var inst = this; + var i; + var values = []; + + if (element && prop) { + inst.element = element; + inst.prop = prop; + + if (element[prop]) { + values = element[prop].replace(/^\s+|\s+$/g, '').split(/\s+/); + + for (i = 0; i < values.length; i++) { + inst[i] = values[i]; + } + } + } + + inst.length = values.length; +}; + +DOMTokenList.prototype = { + add: function () { + var inst = this; + var i; + var tokens = arguments; + + for (i = 0; i < tokens.length; i++) { + validateToken(tokens[i]); + + if (!inst.contains(tokens[i])) { + arr.push.call(inst, tokens[i]); + } + } + + if (inst.element) { + inst.element[inst.prop] = inst; + } + }, + + contains: function (token) { + validateToken(token); + + return inArray(this, token) !== -1; + }, + + item: function (index) { + return this[index] || null; + }, + + remove: function () { + var tokens = arguments; + var inst = this; + var key; + var i; + + for (i = 0; i < tokens.length; i++) { + validateToken(tokens[i]); + + key = inArray(inst, tokens[i]); + + if (key !== -1) { + arr.splice.call(inst, key, 1); + } + } + + if (inst.element) { + inst.element[inst.prop] = inst; + } + }, + + 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; + } + }, + + toString: function () { + return arr.join.call(this, ' '); + } +}; + +return DOMTokenList; +})); diff --git a/dist/domtokenlist-umd.min.js b/dist/domtokenlist-umd.min.js new file mode 100644 index 0000000..26f80b9 --- /dev/null +++ b/dist/domtokenlist-umd.min.js @@ -0,0 +1,2 @@ +/*! DOMTokenlist shim | Copyright 2016 Jonathan Wilsson and Bogdan Chadkin. */ +!function(t,n){"function"==typeof define&&define.amd?define([],n):"object"==typeof exports?module.exports=n():t.DOMTokenList=n()}(this,function(){"use strict";var t=[],n=function(n,e){var r;if(t.indexOf)return t.indexOf.call(n,e);for(r=0;r Jonathan Wilsson and Bogdan Chadkin. */\n')) + .pipe(header(copyright)) .pipe(size({ - showFiles: true + showFiles: true, })) .pipe(gulp.dest('dist')) .pipe(uglify({ - preserveComments: 'some' + preserveComments: 'some', })) .pipe(rename({ - suffix: '.min' + suffix: '.min', })) .pipe(size({ - showFiles: true + showFiles: true, + })) + .pipe(gulp.dest('dist')); +}); + +gulp.task('build-umd', ['lint'], function () { + return gulp.src('src/DOMTokenList.js') + .pipe(concat('domtokenlist-umd.js')) + .pipe(umd({ + exports: function(file) { + return 'DOMTokenList'; + }, + namespace: function(file) { + return 'DOMTokenList'; + }, + })) + .pipe(header(copyright)) + .pipe(gulp.dest('dist')) + .pipe(uglify({ + preserveComments: 'some', + })) + .pipe(rename({ + suffix: '.min', })) .pipe(gulp.dest('dist')); }); -gulp.task('dev', ['build'], function () { - gulp.watch(files, ['build']) +gulp.task('dev', ['build', 'build-umd'], function () { + gulp.watch(files, ['build', 'build-umd']) }); -gulp.task('default', ['build']); +gulp.task('default', ['build', 'build-umd']); diff --git a/package.json b/package.json index f0c9a28..de2cd8c 100644 --- a/package.json +++ b/package.json @@ -17,13 +17,16 @@ "browserstack-runner": "^0.3.5", "gulp": "^3.9.0", "gulp-concat": "^2.5.2", + "gulp-filter": "^3.0.1", "gulp-header": "^1.2.2", + "gulp-iife": "^0.2.4", "gulp-jscs": "^3.0.0", "gulp-jshint": "^2.0.0", "gulp-rename": "^1.2.2", "gulp-size": "^2.0.0", "gulp-uglify": "^1.2.0", - "jshint": "^2.9.1-rc2", + "gulp-umd": "^0.2.0", + "jshint": "^2.9.1-rc3", "qunitjs": "^1.18.0" }, "author": "Jonathan Wilsson ", diff --git a/src/DOMTokenList.js b/src/DOMTokenList.js index 5a8fe51..3ac4c4f 100644 --- a/src/DOMTokenList.js +++ b/src/DOMTokenList.js @@ -1,129 +1,125 @@ -;typeof window !== 'undefined' && (function (window) { - 'use strict'; +'use strict'; - var arr = []; +var arr = []; - var inArray = function (array, value) { - var i; +var inArray = function (array, value) { + var i; - if (arr.indexOf) { - return arr.indexOf.call(array, value); - } + if (arr.indexOf) { + return arr.indexOf.call(array, value); + } - for (i = 0; i < array.length; i++) { - if (array[i] === value) { - return i; - } + for (i = 0; i < array.length; i++) { + if (array[i] === value) { + return i; } + } - return -1; - }; + return -1; +}; - var validateToken = function (token) { - var whitespace = /[\u0009\u000A\u000C\u000D\u0020]/; +var validateToken = function (token) { + var whitespace = /[\u0009\u000A\u000C\u000D\u0020]/; - if (token === '' || whitespace.test(token)) { - throw new Error('Token must not be empty or contain whitespace.'); - } - }; + if (token === '' || whitespace.test(token)) { + throw new Error('Token must not be empty or contain whitespace.'); + } +}; - var DOMTokenList = function (element, prop) { - var inst = this; - var i; - var values = []; +var DOMTokenList = function (element, prop) { + var inst = this; + var i; + var values = []; - if (element && prop) { - inst.element = element; - inst.prop = prop; + if (element && prop) { + inst.element = element; + inst.prop = prop; - if (element[prop]) { - values = element[prop].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++) { - inst[i] = values[i]; - } + for (i = 0; i < values.length; i++) { + inst[i] = values[i]; } } + } - inst.length = values.length; - }; + inst.length = values.length; +}; - DOMTokenList.prototype = { - add: function () { - var inst = this; - var i; - var tokens = arguments; +DOMTokenList.prototype = { + add: function () { + var inst = this; + var i; + var tokens = arguments; - for (i = 0; i < tokens.length; i++) { - validateToken(tokens[i]); + for (i = 0; i < tokens.length; i++) { + validateToken(tokens[i]); - if (!inst.contains(tokens[i])) { - arr.push.call(inst, tokens[i]); - } + if (!inst.contains(tokens[i])) { + arr.push.call(inst, tokens[i]); } + } - if (inst.element) { - inst.element[inst.prop] = inst; - } - }, + if (inst.element) { + inst.element[inst.prop] = inst; + } + }, - contains: function (token) { - validateToken(token); + contains: function (token) { + validateToken(token); - return inArray(this, token) !== -1; - }, + return inArray(this, token) !== -1; + }, - item: function (index) { - return this[index] || null; - }, + item: function (index) { + return this[index] || null; + }, - remove: function () { - var tokens = arguments; - var inst = this; - var key; - var i; + remove: function () { + var tokens = arguments; + var inst = this; + var key; + var i; - for (i = 0; i < tokens.length; i++) { - validateToken(tokens[i]); + for (i = 0; i < tokens.length; i++) { + validateToken(tokens[i]); - key = inArray(inst, tokens[i]); + key = inArray(inst, tokens[i]); - if (key !== -1) { - arr.splice.call(inst, key, 1); - } + if (key !== -1) { + arr.splice.call(inst, key, 1); } + } - if (inst.element) { - inst.element[inst.prop] = inst; - } - }, + if (inst.element) { + inst.element[inst.prop] = inst; + } + }, - toggle: function (token, force) { - var inst = this; + toggle: function (token, force) { + var inst = this; - if (inst.contains(token)) { - if (force) { - return true; - } + if (inst.contains(token)) { + if (force) { + return true; + } - inst.remove(token); + inst.remove(token); + return false; + } else { + if (force === false) { return false; - } else { - if (force === false) { - return false; - } - - inst.add(token); - - return true; } - }, - toString: function () { - return arr.join.call(this, ' '); + inst.add(token); + + return true; } - }; + }, - window.DOMTokenList = DOMTokenList; -}(window)); + toString: function () { + return arr.join.call(this, ' '); + } +};