Skip to content

Commit 9a736c9

Browse files
committedJan 12, 2016
Add classList polyFill in attempt to fix tests
1 parent e3dd79b commit 9a736c9

File tree

4 files changed

+252
-10
lines changed

4 files changed

+252
-10
lines changed
 

‎lib/grunt/utils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ var cachedAngularFiles = grunt.file.readJSON('lib/test/angular/files.json');
1010
var util = module.exports = {
1111

1212
testDependencies: {
13-
unit: ['bower_components/jquery/jquery.min.js', 'lib/test/jquery.simulate.js', 'dist/release/ui-grid.css', 'bower_components/lodash/dist/lodash.min.js', 'bower_components/csv-js/csv.js']
13+
unit: ['bower_components/jquery/jquery.min.js', 'lib/test/jquery.simulate.js','lib/test/classList.polyFill.js', 'dist/release/ui-grid.css', 'bower_components/lodash/dist/lodash.min.js', 'bower_components/csv-js/csv.js']
1414
},
1515

1616
testFiles: {

‎lib/test/classList.polyFill.js

+240
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
/*
2+
* classList.js: Cross-browser full element.classList implementation.
3+
* 1.1.20150312
4+
*
5+
* By Eli Grey, http://eligrey.com
6+
* License: Dedicated to the public domain.
7+
* See https://github.com/eligrey/classList.js/blob/master/LICENSE.md
8+
*/
9+
10+
/*global self, document, DOMException */
11+
12+
/*! @source http://purl.eligrey.com/github/classList.js/blob/master/classList.js */
13+
14+
if ("document" in self) {
15+
16+
// Full polyfill for browsers with no classList support
17+
// Including IE < Edge missing SVGElement.classList
18+
if (!("classList" in document.createElement("_"))
19+
|| document.createElementNS && !("classList" in document.createElementNS("http://www.w3.org/2000/svg","g"))) {
20+
21+
(function (view) {
22+
23+
"use strict";
24+
25+
if (!('Element' in view)) return;
26+
27+
var
28+
classListProp = "classList"
29+
, protoProp = "prototype"
30+
, elemCtrProto = view.Element[protoProp]
31+
, objCtr = Object
32+
, strTrim = String[protoProp].trim || function () {
33+
return this.replace(/^\s+|\s+$/g, "");
34+
}
35+
, arrIndexOf = Array[protoProp].indexOf || function (item) {
36+
var
37+
i = 0
38+
, len = this.length
39+
;
40+
for (; i < len; i++) {
41+
if (i in this && this[i] === item) {
42+
return i;
43+
}
44+
}
45+
return -1;
46+
}
47+
// Vendors: please allow content code to instantiate DOMExceptions
48+
, DOMEx = function (type, message) {
49+
this.name = type;
50+
this.code = DOMException[type];
51+
this.message = message;
52+
}
53+
, checkTokenAndGetIndex = function (classList, token) {
54+
if (token === "") {
55+
throw new DOMEx(
56+
"SYNTAX_ERR"
57+
, "An invalid or illegal string was specified"
58+
);
59+
}
60+
if (/\s/.test(token)) {
61+
throw new DOMEx(
62+
"INVALID_CHARACTER_ERR"
63+
, "String contains an invalid character"
64+
);
65+
}
66+
return arrIndexOf.call(classList, token);
67+
}
68+
, ClassList = function (elem) {
69+
var
70+
trimmedClasses = strTrim.call(elem.getAttribute("class") || "")
71+
, classes = trimmedClasses ? trimmedClasses.split(/\s+/) : []
72+
, i = 0
73+
, len = classes.length
74+
;
75+
for (; i < len; i++) {
76+
this.push(classes[i]);
77+
}
78+
this._updateClassName = function () {
79+
elem.setAttribute("class", this.toString());
80+
};
81+
}
82+
, classListProto = ClassList[protoProp] = []
83+
, classListGetter = function () {
84+
return new ClassList(this);
85+
}
86+
;
87+
// Most DOMException implementations don't allow calling DOMException's toString()
88+
// on non-DOMExceptions. Error's toString() is sufficient here.
89+
DOMEx[protoProp] = Error[protoProp];
90+
classListProto.item = function (i) {
91+
return this[i] || null;
92+
};
93+
classListProto.contains = function (token) {
94+
token += "";
95+
return checkTokenAndGetIndex(this, token) !== -1;
96+
};
97+
classListProto.add = function () {
98+
var
99+
tokens = arguments
100+
, i = 0
101+
, l = tokens.length
102+
, token
103+
, updated = false
104+
;
105+
do {
106+
token = tokens[i] + "";
107+
if (checkTokenAndGetIndex(this, token) === -1) {
108+
this.push(token);
109+
updated = true;
110+
}
111+
}
112+
while (++i < l);
113+
114+
if (updated) {
115+
this._updateClassName();
116+
}
117+
};
118+
classListProto.remove = function () {
119+
var
120+
tokens = arguments
121+
, i = 0
122+
, l = tokens.length
123+
, token
124+
, updated = false
125+
, index
126+
;
127+
do {
128+
token = tokens[i] + "";
129+
index = checkTokenAndGetIndex(this, token);
130+
while (index !== -1) {
131+
this.splice(index, 1);
132+
updated = true;
133+
index = checkTokenAndGetIndex(this, token);
134+
}
135+
}
136+
while (++i < l);
137+
138+
if (updated) {
139+
this._updateClassName();
140+
}
141+
};
142+
classListProto.toggle = function (token, force) {
143+
token += "";
144+
145+
var
146+
result = this.contains(token)
147+
, method = result ?
148+
force !== true && "remove"
149+
:
150+
force !== false && "add"
151+
;
152+
153+
if (method) {
154+
this[method](token);
155+
}
156+
157+
if (force === true || force === false) {
158+
return force;
159+
} else {
160+
return !result;
161+
}
162+
};
163+
classListProto.toString = function () {
164+
return this.join(" ");
165+
};
166+
167+
if (objCtr.defineProperty) {
168+
var classListPropDesc = {
169+
get: classListGetter
170+
, enumerable: true
171+
, configurable: true
172+
};
173+
try {
174+
objCtr.defineProperty(elemCtrProto, classListProp, classListPropDesc);
175+
} catch (ex) { // IE 8 doesn't support enumerable:true
176+
if (ex.number === -0x7FF5EC54) {
177+
classListPropDesc.enumerable = false;
178+
objCtr.defineProperty(elemCtrProto, classListProp, classListPropDesc);
179+
}
180+
}
181+
} else if (objCtr[protoProp].__defineGetter__) {
182+
elemCtrProto.__defineGetter__(classListProp, classListGetter);
183+
}
184+
185+
}(self));
186+
187+
} else {
188+
// There is full or partial native classList support, so just check if we need
189+
// to normalize the add/remove and toggle APIs.
190+
191+
(function () {
192+
"use strict";
193+
194+
var testElement = document.createElement("_");
195+
196+
testElement.classList.add("c1", "c2");
197+
198+
// Polyfill for IE 10/11 and Firefox <26, where classList.add and
199+
// classList.remove exist but support only one argument at a time.
200+
if (!testElement.classList.contains("c2")) {
201+
var createMethod = function(method) {
202+
var original = DOMTokenList.prototype[method];
203+
204+
DOMTokenList.prototype[method] = function(token) {
205+
var i, len = arguments.length;
206+
207+
for (i = 0; i < len; i++) {
208+
token = arguments[i];
209+
original.call(this, token);
210+
}
211+
};
212+
};
213+
createMethod('add');
214+
createMethod('remove');
215+
}
216+
217+
testElement.classList.toggle("c3", false);
218+
219+
// Polyfill for IE 10 and Firefox <24, where classList.toggle does not
220+
// support the second argument.
221+
if (testElement.classList.contains("c3")) {
222+
var _toggle = DOMTokenList.prototype.toggle;
223+
224+
DOMTokenList.prototype.toggle = function(token, force) {
225+
if (1 in arguments && !this.contains(token) === !force) {
226+
return force;
227+
} else {
228+
return _toggle.call(this, token);
229+
}
230+
};
231+
232+
}
233+
234+
testElement = null;
235+
}());
236+
237+
}
238+
239+
}
240+

‎test/karma.conf.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ module.exports = function(config) {
1414

1515
// list of files / patterns to load in the browser
1616
// note that the karmangular setup from util.createKarmangularConfig seems
17-
// to take precedence over this, but we can't remove this because then
17+
// to take precedence over this, but we can't remove this because then
1818
// the karmangular task doesn't appear to run. So includes the features/**/test, but
1919
// they don't get run if you've used the --fast or --core options
2020
files: [
2121
'bower_components/jquery/jquery.min.js',
2222
'lib/test/jquery.simulate.js',
23+
'lib/test/classList.polyFill.js',
2324
'bower_components/lodash/dist/lodash.min.js',
24-
25+
2526
'src/js/core/bootstrap.js',
2627
'src/js/**/*.js',
2728
'src/features/**/js/**/*.js',
@@ -105,7 +106,7 @@ module.exports = function(config) {
105106
customLaunchers: util.customLaunchers()
106107

107108
});
108-
109+
109110
// TODO(c0bra): remove once SauceLabs supports websockets.
110111
// This speeds up the capturing a bit, as browsers don't even try to use websocket. -- (thanks vojta)
111112
if (process.env.TRAVIS) {
@@ -114,7 +115,7 @@ module.exports = function(config) {
114115
config.reporters = ['dots', 'coverage'];
115116

116117
var buildLabel = 'TRAVIS #' + process.env.TRAVIS_BUILD_NUMBER + ' (' + process.env.TRAVIS_BUILD_ID + ')';
117-
118+
118119
// config.transports = ['websocket', 'xhr-polling'];
119120

120121
config.sauceLabs.build = buildLabel;
@@ -141,4 +142,4 @@ module.exports = function(config) {
141142
var bs = grunt.option('browsers').split(/,/).map(function(b) { return b.trim(); });
142143
config.browsers = bs;
143144
}
144-
};
145+
};

‎test/karma.debug.conf.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ module.exports = function(config) {
1616
files: [
1717
'bower_components/jquery/jquery.min.js',
1818
'lib/test/jquery.simulate.js',
19+
'lib/test/classList.polyFill.js',
1920
'bower_components/lodash/dist/lodash.min.js',
2021

2122
'lib/test/angular/1.3.6/angular.js',
2223
'lib/test/angular/1.3.6/angular-mocks.js',
2324
'lib/test/angular/1.3.6/angular-animate.js',
24-
25+
2526
'src/js/core/bootstrap.js',
2627
'src/js/**/*.js',
2728
'src/features/**/js/**/*.js',
@@ -105,7 +106,7 @@ module.exports = function(config) {
105106
customLaunchers: util.customLaunchers()
106107

107108
});
108-
109+
109110
// TODO(c0bra): remove once SauceLabs supports websockets.
110111
// This speeds up the capturing a bit, as browsers don't even try to use websocket. -- (thanks vojta)
111112
if (process.env.TRAVIS) {
@@ -114,7 +115,7 @@ module.exports = function(config) {
114115
config.reporters = ['dots', 'coverage'];
115116

116117
var buildLabel = 'TRAVIS #' + process.env.TRAVIS_BUILD_NUMBER + ' (' + process.env.TRAVIS_BUILD_ID + ')';
117-
118+
118119
// config.transports = ['websocket', 'xhr-polling'];
119120

120121
config.sauceLabs.build = buildLabel;
@@ -141,4 +142,4 @@ module.exports = function(config) {
141142
var bs = grunt.option('browsers').split(/,/).map(function(b) { return b.trim(); });
142143
config.browsers = bs;
143144
}
144-
};
145+
};

0 commit comments

Comments
 (0)
Please sign in to comment.