2015-04-03 19:21:16 +00:00
|
|
|
/*
|
|
|
|
* Experimental assertion for comparing DOM objects.
|
|
|
|
*
|
|
|
|
* Serializes an element and some properties and attributes and its children if any,
|
|
|
|
* otherwise the text. Then compares the result using deepEqual().
|
|
|
|
*/
|
|
|
|
define( [
|
|
|
|
"qunit",
|
|
|
|
"jquery"
|
|
|
|
], function( QUnit, $ ) {
|
|
|
|
|
|
|
|
var domEqual = QUnit.assert.domEqual = function( selector, modifier, message ) {
|
|
|
|
|
|
|
|
var assert = this;
|
|
|
|
|
|
|
|
// Get current state prior to modifier
|
2015-04-17 16:10:36 +00:00
|
|
|
var expected = extract( selector, message );
|
2015-04-03 19:21:16 +00:00
|
|
|
|
|
|
|
function done() {
|
2015-04-17 16:10:36 +00:00
|
|
|
var actual = extract( selector, message );
|
2015-04-03 19:21:16 +00:00
|
|
|
assert.push( QUnit.equiv( actual, expected ), actual, expected, message );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run modifier (async or sync), then compare state via done()
|
|
|
|
if ( modifier.length ) {
|
|
|
|
modifier( done );
|
|
|
|
} else {
|
|
|
|
modifier();
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
domEqual.properties = [
|
|
|
|
"disabled",
|
|
|
|
"readOnly"
|
|
|
|
];
|
|
|
|
|
|
|
|
domEqual.attributes = [
|
|
|
|
"autocomplete",
|
|
|
|
"aria-activedescendant",
|
|
|
|
"aria-controls",
|
|
|
|
"aria-describedby",
|
|
|
|
"aria-disabled",
|
|
|
|
"aria-expanded",
|
|
|
|
"aria-haspopup",
|
|
|
|
"aria-hidden",
|
|
|
|
"aria-labelledby",
|
|
|
|
"aria-pressed",
|
|
|
|
"aria-selected",
|
|
|
|
"aria-valuemax",
|
|
|
|
"aria-valuemin",
|
|
|
|
"aria-valuenow",
|
|
|
|
"class",
|
|
|
|
"href",
|
|
|
|
"id",
|
|
|
|
"nodeName",
|
|
|
|
"role",
|
|
|
|
"tabIndex",
|
|
|
|
"title"
|
|
|
|
];
|
|
|
|
|
|
|
|
function getElementStyles( elem ) {
|
|
|
|
var styles = {};
|
|
|
|
var style = elem.ownerDocument.defaultView ?
|
|
|
|
elem.ownerDocument.defaultView.getComputedStyle( elem, null ) :
|
|
|
|
elem.currentStyle;
|
|
|
|
var key, len;
|
|
|
|
|
|
|
|
if ( style && style.length && style[ 0 ] && style[ style[ 0 ] ] ) {
|
|
|
|
len = style.length;
|
|
|
|
while ( len-- ) {
|
|
|
|
key = style[ len ];
|
|
|
|
if ( typeof style[ key ] === "string" ) {
|
|
|
|
styles[ $.camelCase( key ) ] = style[ key ];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Support: Opera, IE <9
|
|
|
|
} else {
|
|
|
|
for ( key in style ) {
|
|
|
|
if ( typeof style[ key ] === "string" ) {
|
|
|
|
styles[ key ] = style[ key ];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return styles;
|
|
|
|
}
|
|
|
|
|
2015-04-17 16:10:36 +00:00
|
|
|
function extract( selector, message ) {
|
|
|
|
var elem = $( selector );
|
|
|
|
if ( !elem.length ) {
|
|
|
|
QUnit.push( false, null, null,
|
2015-04-03 19:21:16 +00:00
|
|
|
"domEqual failed, can't extract " + selector + ", message was: " + message );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var result = {};
|
|
|
|
var children;
|
|
|
|
$.each( domEqual.properties, function( index, attr ) {
|
|
|
|
var value = elem.prop( attr );
|
2016-06-08 17:03:42 +00:00
|
|
|
result[ attr ] = value != null ? value : "";
|
2015-08-21 04:13:44 +00:00
|
|
|
} );
|
2015-04-03 19:21:16 +00:00
|
|
|
$.each( domEqual.attributes, function( index, attr ) {
|
|
|
|
var value = elem.attr( attr );
|
2016-06-08 17:03:42 +00:00
|
|
|
result[ attr ] = value != null ? value : "";
|
2015-08-21 04:13:44 +00:00
|
|
|
} );
|
2015-04-03 19:21:16 +00:00
|
|
|
result.style = getElementStyles( elem[ 0 ] );
|
|
|
|
result.events = $._data( elem[ 0 ], "events" );
|
|
|
|
result.data = $.extend( {}, elem.data() );
|
|
|
|
delete result.data[ $.expando ];
|
|
|
|
children = elem.children();
|
|
|
|
if ( children.length ) {
|
2015-08-21 04:13:44 +00:00
|
|
|
result.children = elem.children().map( function() {
|
2015-04-03 19:21:16 +00:00
|
|
|
return extract( $( this ) );
|
2015-08-21 04:13:44 +00:00
|
|
|
} ).get();
|
2015-04-03 19:21:16 +00:00
|
|
|
} else {
|
|
|
|
result.text = elem.text();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
} );
|