2023-03-30 07:56:33 +00:00
|
|
|
// With custom modifications - all are marked with
|
|
|
|
// a "Custom modification" comment.
|
2015-04-29 14:49:41 +00:00
|
|
|
( function( factory ) {
|
|
|
|
if ( typeof define === "function" && define.amd ) {
|
|
|
|
|
|
|
|
// AMD. Register as an anonymous module.
|
|
|
|
define( [
|
|
|
|
"qunit"
|
|
|
|
], factory );
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// Browser globals
|
|
|
|
factory( QUnit );
|
|
|
|
}
|
|
|
|
}( function( QUnit ) {
|
|
|
|
|
2015-02-03 00:21:44 +00:00
|
|
|
function inArray( haystack, needle ) {
|
|
|
|
for ( var i = 0; i < haystack.length; i++ ) {
|
2015-04-29 14:49:41 +00:00
|
|
|
if (
|
|
|
|
( needle instanceof RegExp && needle.test( haystack[ i ] ) )||
|
|
|
|
( typeof needle === "string" && haystack[ i ] === needle )
|
|
|
|
) {
|
2015-02-03 00:21:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2015-04-29 14:49:41 +00:00
|
|
|
|
|
|
|
function check( element, search ) {
|
|
|
|
var i, classAttribute, elementClassArray,
|
2015-02-03 00:21:44 +00:00
|
|
|
missing = [],
|
|
|
|
found = [];
|
|
|
|
|
|
|
|
if ( element.jquery && element.length !== 1 ) {
|
2015-04-29 14:49:41 +00:00
|
|
|
throw new Error( "Class checks can only be performed on a single element on a collection" );
|
2015-02-03 00:21:44 +00:00
|
|
|
}
|
2015-04-29 14:49:41 +00:00
|
|
|
|
2015-02-03 00:21:44 +00:00
|
|
|
element = element.jquery ? element[ 0 ] : element;
|
|
|
|
classAttribute = element.getAttribute( "class" );
|
2015-04-29 14:49:41 +00:00
|
|
|
|
2015-02-03 00:21:44 +00:00
|
|
|
if ( classAttribute ) {
|
2015-04-29 14:49:41 +00:00
|
|
|
elementClassArray = splitClasses( classAttribute );
|
|
|
|
if ( search instanceof RegExp ) {
|
|
|
|
if ( inArray( elementClassArray, search ) ) {
|
|
|
|
found.push( search );
|
2015-02-03 00:21:44 +00:00
|
|
|
} else {
|
2015-04-29 14:49:41 +00:00
|
|
|
missing.push( search );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for( i = 0; i < search.length; i++ ) {
|
|
|
|
if ( !inArray( elementClassArray, search[ i ] ) ) {
|
|
|
|
missing.push( search[ i ] );
|
|
|
|
} else {
|
|
|
|
found.push( search[ i ] );
|
|
|
|
}
|
2015-02-03 00:21:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2015-04-29 14:49:41 +00:00
|
|
|
missing = search;
|
2015-02-03 00:21:44 +00:00
|
|
|
}
|
|
|
|
|
2015-04-29 14:49:41 +00:00
|
|
|
return {
|
|
|
|
missing: missing,
|
|
|
|
found: found,
|
|
|
|
element: element,
|
|
|
|
classAttribute: classAttribute
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function splitClasses( classes ) {
|
|
|
|
return classes.match( /\S+/g ) || [];
|
|
|
|
}
|
|
|
|
|
|
|
|
function pluralize( message, classes ) {
|
|
|
|
return message + ( classes.length > 1 ? "es" : "" );
|
2015-02-03 00:21:44 +00:00
|
|
|
}
|
|
|
|
|
2023-03-30 07:56:33 +00:00
|
|
|
// Custom modification: removing QUnit.extend
|
|
|
|
var key;
|
|
|
|
var qunitAssertExtensions = {
|
2015-02-03 00:21:44 +00:00
|
|
|
hasClasses: function( element, classes, message ) {
|
2015-04-29 14:49:41 +00:00
|
|
|
var classArray = splitClasses( classes ),
|
|
|
|
results = check( element, classArray );
|
|
|
|
|
|
|
|
message = message || pluralize( "Element must have class", classArray );
|
|
|
|
|
2023-03-30 07:56:33 +00:00
|
|
|
// Custom modification: push -> pushResult
|
|
|
|
this.pushResult( {
|
|
|
|
result: !results.missing.length,
|
|
|
|
actual: results.found.join( " " ),
|
|
|
|
expected: classes,
|
|
|
|
message: message
|
|
|
|
} );
|
2015-02-03 00:21:44 +00:00
|
|
|
},
|
|
|
|
lacksClasses: function( element, classes, message ) {
|
2015-04-29 14:49:41 +00:00
|
|
|
var classArray = splitClasses( classes ),
|
|
|
|
results = check( element, classArray );
|
|
|
|
|
|
|
|
message = message || pluralize( "Element must not have class", classArray );
|
|
|
|
|
2023-03-30 07:56:33 +00:00
|
|
|
// Custom modification: push -> pushResult
|
|
|
|
this.pushResult( {
|
|
|
|
result: !results.found.length,
|
|
|
|
actual: results.found.join( " " ),
|
|
|
|
expected: classes,
|
|
|
|
message: message
|
|
|
|
} );
|
2015-04-29 14:49:41 +00:00
|
|
|
},
|
|
|
|
hasClassesStrict: function( element, classes, message ) {
|
|
|
|
var result,
|
|
|
|
classArray = splitClasses( classes ),
|
|
|
|
results = check( element, classArray );
|
|
|
|
|
|
|
|
message = message || pluralize( "Element must only have class", classArray );
|
|
|
|
|
|
|
|
result = !results.missing.length && results.element.getAttribute( "class" ) &&
|
|
|
|
splitClasses( results.element.getAttribute( "class" ) ).length ===
|
|
|
|
results.found.length;
|
|
|
|
|
2023-03-30 07:56:33 +00:00
|
|
|
// Custom modification: push -> pushResult
|
|
|
|
this.pushResult( {
|
|
|
|
result: result,
|
|
|
|
actual: results.found.join( " " ),
|
|
|
|
expected: classes,
|
|
|
|
message: message
|
|
|
|
} );
|
2015-04-29 14:49:41 +00:00
|
|
|
},
|
|
|
|
hasClassRegex: function( element, regex, message ) {
|
|
|
|
var results = check( element, regex );
|
|
|
|
|
|
|
|
message = message || "Element must have class matching " + regex;
|
|
|
|
|
2023-03-30 07:56:33 +00:00
|
|
|
// Custom modification: push -> pushResult
|
|
|
|
this.pushResult( {
|
|
|
|
result: !!results.found.length,
|
|
|
|
actual: results.found.join( " " ),
|
|
|
|
expected: regex,
|
|
|
|
message: message
|
|
|
|
} );
|
2015-04-29 14:49:41 +00:00
|
|
|
},
|
|
|
|
lacksClassRegex: function( element, regex, message ) {
|
|
|
|
var results = check( element, regex );
|
|
|
|
|
|
|
|
message = message || "Element must not have class matching " + regex;
|
|
|
|
|
2023-03-30 07:56:33 +00:00
|
|
|
// Custom modification: push -> pushResult
|
|
|
|
this.pushResult( {
|
|
|
|
result: results.missing.length,
|
|
|
|
actual: results.missing.join( " " ),
|
|
|
|
expected: regex,
|
|
|
|
message: message
|
|
|
|
} );
|
2015-04-29 14:49:41 +00:00
|
|
|
},
|
|
|
|
hasClassStart: function( element, partialClass, message ) {
|
|
|
|
var results = check( element, new RegExp( "^" + partialClass ) );
|
|
|
|
|
|
|
|
message = message || "Element must have class starting with " + partialClass;
|
|
|
|
|
2023-03-30 07:56:33 +00:00
|
|
|
// Custom modification: push -> pushResult
|
|
|
|
this.pushResult( {
|
|
|
|
result: results.found.length,
|
|
|
|
actual: results.found.join( " " ),
|
|
|
|
expected: partialClass,
|
|
|
|
message: message
|
|
|
|
} );
|
2015-04-29 14:49:41 +00:00
|
|
|
},
|
|
|
|
lacksClassStart: function( element, partialClass, message ) {
|
|
|
|
var results = check( element, new RegExp( "^" + partialClass ) );
|
|
|
|
|
|
|
|
message = message || "Element must not have class starting with " + partialClass;
|
|
|
|
|
2023-03-30 07:56:33 +00:00
|
|
|
// Custom modification: push -> pushResult
|
|
|
|
this.pushResult( {
|
|
|
|
result: results.missing.length,
|
|
|
|
actual: results.missing.join( " " ),
|
|
|
|
expected: partialClass,
|
|
|
|
message: message
|
|
|
|
} );
|
2015-04-29 14:49:41 +00:00
|
|
|
},
|
|
|
|
hasClassPartial: function( element, partialClass, message ) {
|
|
|
|
var results = check( element, new RegExp( partialClass ) );
|
|
|
|
|
|
|
|
message = message || "Element must have class containing '" + partialClass + "'";
|
|
|
|
|
2023-03-30 07:56:33 +00:00
|
|
|
// Custom modification: push -> pushResult
|
|
|
|
this.pushResult( {
|
|
|
|
result: results.found.length,
|
|
|
|
actual: results.found.join( " " ),
|
|
|
|
expected: partialClass,
|
|
|
|
message: message
|
|
|
|
} );
|
2015-04-29 14:49:41 +00:00
|
|
|
},
|
|
|
|
lacksClassPartial: function( element, partialClass, message ) {
|
|
|
|
var results = check( element, new RegExp( partialClass ) );
|
|
|
|
|
|
|
|
message = message || "Element must not have class containing '" + partialClass + "'";
|
|
|
|
|
2023-03-30 07:56:33 +00:00
|
|
|
// Custom modification: push -> pushResult
|
|
|
|
this.pushResult( {
|
|
|
|
result: results.missing.length,
|
|
|
|
actual: results.missing.join( " " ),
|
|
|
|
expected: partialClass,
|
|
|
|
message: message
|
|
|
|
} );
|
2015-04-29 14:49:41 +00:00
|
|
|
},
|
|
|
|
lacksAllClasses: function( element, message ) {
|
|
|
|
element = element.jquery ? element[ 0 ] : element;
|
|
|
|
|
|
|
|
var classAttribute = element.getAttribute( "class" ) || "",
|
|
|
|
classes = splitClasses( classAttribute );
|
|
|
|
|
|
|
|
message = message || "Element must not have any classes";
|
|
|
|
|
2023-03-30 07:56:33 +00:00
|
|
|
// Custom modification: push -> pushResult
|
|
|
|
this.pushResult( {
|
|
|
|
result: !classes.length,
|
|
|
|
actual: !classes.length,
|
|
|
|
expected: true,
|
|
|
|
message: message
|
|
|
|
} );
|
2015-04-29 14:49:41 +00:00
|
|
|
},
|
|
|
|
hasSomeClass: function( element, message ) {
|
|
|
|
element = element.jquery ? element[ 0 ] : element;
|
|
|
|
|
|
|
|
var classAttribute = element.getAttribute( "class" ) || "",
|
|
|
|
classes = splitClasses( classAttribute );
|
|
|
|
|
|
|
|
message = message || "Element must have a class";
|
|
|
|
|
2023-03-30 07:56:33 +00:00
|
|
|
// Custom modification: push -> pushResult
|
|
|
|
this.pushResult( {
|
|
|
|
result: classes.length,
|
|
|
|
actual: classes.length,
|
|
|
|
expected: true,
|
|
|
|
message: message
|
|
|
|
} );
|
2015-02-03 00:21:44 +00:00
|
|
|
}
|
2023-03-30 07:56:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Custom modification: removing QUnit.extend
|
|
|
|
for ( key in qunitAssertExtensions ) {
|
|
|
|
QUnit.assert[ key ] = qunitAssertExtensions[ key ];
|
|
|
|
}
|
|
|
|
|
2015-04-29 14:49:41 +00:00
|
|
|
} ) );
|