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, $ ) {
|
2021-06-06 22:58:12 +00:00
|
|
|
"use strict";
|
2015-04-03 19:21:16 +00:00
|
|
|
|
|
|
|
var domEqual = QUnit.assert.domEqual = function( selector, modifier, message ) {
|
|
|
|
|
|
|
|
var assert = this;
|
|
|
|
|
|
|
|
// Get current state prior to modifier
|
2023-03-30 07:56:33 +00:00
|
|
|
var expected = extract( assert, selector, message );
|
2015-04-03 19:21:16 +00:00
|
|
|
|
|
|
|
function done() {
|
2023-03-30 07:56:33 +00:00
|
|
|
var actual = extract( assert, selector, message );
|
|
|
|
assert.pushResult( {
|
|
|
|
result: QUnit.equiv( actual, expected ),
|
|
|
|
actual: actual,
|
|
|
|
expected: expected,
|
|
|
|
message: message
|
|
|
|
} );
|
2015-04-03 19:21:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run modifier (async or sync), then compare state via done()
|
|
|
|
if ( modifier.length ) {
|
|
|
|
modifier( done );
|
|
|
|
} else {
|
|
|
|
modifier();
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
domEqual.properties = [
|
|
|
|
"disabled",
|
2023-05-10 12:46:30 +00:00
|
|
|
"nodeName",
|
2015-04-03 19:21:16 +00:00
|
|
|
"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",
|
|
|
|
"role",
|
|
|
|
"tabIndex",
|
|
|
|
"title"
|
|
|
|
];
|
|
|
|
|
2017-05-08 14:47:27 +00:00
|
|
|
function camelCase( string ) {
|
|
|
|
return string.replace( /-([\da-z])/gi, function( all, letter ) {
|
|
|
|
return letter.toUpperCase();
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2015-04-03 19:21:16 +00:00
|
|
|
function getElementStyles( elem ) {
|
|
|
|
var styles = {};
|
|
|
|
var style = elem.ownerDocument.defaultView ?
|
|
|
|
elem.ownerDocument.defaultView.getComputedStyle( elem, null ) :
|
|
|
|
elem.currentStyle;
|
2023-05-10 12:46:30 +00:00
|
|
|
var key, camelKey;
|
|
|
|
var len = style.length;
|
2015-04-03 19:21:16 +00:00
|
|
|
|
2023-05-10 12:46:30 +00:00
|
|
|
while ( len-- ) {
|
|
|
|
key = style[ len ];
|
|
|
|
camelKey = camelCase( key );
|
|
|
|
|
|
|
|
if ( typeof style[ key ] === "string" ) {
|
|
|
|
styles[ camelKey ] = style[ key ];
|
2015-04-03 19:21:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return styles;
|
|
|
|
}
|
|
|
|
|
2020-07-23 13:54:16 +00:00
|
|
|
// Returns 0 if v1 == v2, -1 if v1 < v2, 1 if v1 > v2
|
|
|
|
function compareVersions( v1, v2 ) {
|
|
|
|
var i,
|
|
|
|
rVersionParts = /^(\d+)\.(\d+)\.(\d+)/,
|
|
|
|
v1p = rVersionParts.exec( v1 ) || [ ],
|
|
|
|
v2p = rVersionParts.exec( v2 ) || [ ];
|
|
|
|
|
|
|
|
for ( i = 1; i <= 3; i++ ) {
|
|
|
|
if ( +v1p[ i ] > +v2p[ i ] ) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if ( +v1p[ i ] < +v2p[ i ] ) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function jQueryVersionSince( version ) {
|
|
|
|
return compareVersions( $.fn.jquery, version ) >= 0;
|
|
|
|
}
|
|
|
|
|
2023-03-30 07:56:33 +00:00
|
|
|
function extract( assert, selector, message ) {
|
2015-04-17 16:10:36 +00:00
|
|
|
var elem = $( selector );
|
|
|
|
if ( !elem.length ) {
|
2023-03-30 07:56:33 +00:00
|
|
|
assert.pushResult( {
|
|
|
|
result: false,
|
|
|
|
actual: null,
|
|
|
|
expected: null,
|
|
|
|
message: "domEqual failed, can't extract " + selector + ", message was: " + message
|
|
|
|
} );
|
2015-04-03 19:21:16 +00:00
|
|
|
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" );
|
2020-07-23 13:54:16 +00:00
|
|
|
|
|
|
|
// jQuery >=3.4.0 uses a special focus/blur handler pair
|
|
|
|
// needed to fix various issues with checkboxes/radio buttons
|
|
|
|
// as well as being able to pass data in focus triggers.
|
|
|
|
// However, this leaves dummy focus & blur events if any of these
|
|
|
|
// events were ever listened to at a particular element. There's not
|
|
|
|
// a lot UI can do to fix this so just skip these handlers for
|
|
|
|
// data comparisons in tests.
|
|
|
|
// See https://github.com/jquery/jquery/issues/4496
|
|
|
|
if ( result.events && jQueryVersionSince( "3.4.0" ) ) {
|
|
|
|
$.each( [ "focus", "blur" ], function( index, eventType ) {
|
|
|
|
if ( !result.events[ eventType ] ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-20 18:11:03 +00:00
|
|
|
// Filter special jQuery focus-related handlers out.
|
|
|
|
result.events[ eventType ] = result.events[ eventType ]
|
|
|
|
.filter( function( eventData ) {
|
|
|
|
var handlerBody = eventData.handler.toString().replace(
|
|
|
|
/^[^{]+\{[\s\n]*((?:.|\n)*?)\s*;?\s*\}[^}]*$/,
|
|
|
|
"$1"
|
|
|
|
);
|
|
|
|
|
|
|
|
// Only these special jQuery internal handlers
|
|
|
|
// have the `namespace` field set to `false`;
|
|
|
|
// all other events use a string value, possibly
|
|
|
|
// an empty string if no namespace was set.
|
|
|
|
return eventData.namespace !== false &&
|
|
|
|
|
|
|
|
// If a focus event was triggered without adding a handler first,
|
|
|
|
// jQuery attaches an empty handler at the beginning of a trigger
|
|
|
|
// call. Ignore this handler as well; it's a function with just
|
|
|
|
// `return true;` in the body.
|
|
|
|
// Handle the minified version as well.
|
|
|
|
handlerBody !== "return true" && handlerBody !== "return!0";
|
|
|
|
} );
|
2020-07-23 13:54:16 +00:00
|
|
|
|
|
|
|
// Remove empty eventData collections to follow jQuery behavior.
|
2021-02-20 18:11:03 +00:00
|
|
|
if ( !result.events[ eventType ].length ) {
|
2020-07-23 13:54:16 +00:00
|
|
|
delete result.events[ eventType ];
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Simulate empty events collections removal to follow jQuery behavior.
|
|
|
|
if ( !Object.keys( result.events ).length ) {
|
|
|
|
result.events = undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-03 19:21:16 +00:00
|
|
|
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() {
|
2023-03-30 07:56:33 +00:00
|
|
|
return extract( assert, $( 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
} );
|