aboutsummaryrefslogtreecommitdiffstats
path: root/external/qunit-assert-classes/qunit-assert-classes.js
blob: f61046bc85b2188ee331e413f9ea43bf7b1405ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
( function( QUnit ) {
	function inArray( haystack, needle ) {
		for ( var i = 0; i < haystack.length; i++ ) {
			if ( haystack[ i ] === needle ) {
				return true;
			}
		}
		return false;
	}
	function check( element, classes, stateVal, message ) {
		var i, result, classAttribute, elementClassArray,
			classArray = classes.split( " " ),
			missing = [],
			found = [];

		if ( element.jquery && element.length !== 1 ) {
			throw( "Class checks can only be performed on a single element on a collection" );
		}
		element = element.jquery ? element[ 0 ] : element;
		classAttribute = element.getAttribute( "class" );
		message = message || "Element must " + ( stateVal? "" : "not " ) + "have classes";
		if ( classAttribute ) {
			elementClassArray = classAttribute.split( " " );
			for( i = 0; i < classArray.length; i++ ) {
				if ( !inArray( elementClassArray, classArray[ i ] ) ) {
					missing.push( classArray[ i ] );
				} else {
					found.push( classArray[ i ] );
				}
			}
		} else {
			missing = classArray;
		}

		result = stateVal ? !missing.length : !found.length;
		QUnit.push( result, classes, result ? classes : found.join( " " ), message );
	}

	QUnit.extend( QUnit.assert, {
		hasClasses: function( element, classes, message ) {
			check( element, classes, true, message );
		},
		lacksClasses: function( element, classes, message ) {
			check( element, classes, false, message );
		}
	});
})( QUnit );