summaryrefslogtreecommitdiff
path: root/vendor/oojs/oojs-ui/tests/QUnit.assert.equalDomElement.js
blob: f041c2583fbe630d1c503720a9575c065ce64fa6 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*!
 * A QUnit assertion to compare DOM node trees.
 *
 * Adapted from VisualEditor plugin for QUnit. Additionally supports comparing properties to
 * attributes (for dynamically generated nodes) and order-insensitive comparison of classes on DOM
 * nodes.
 *
 * @copyright 2011-2015 VisualEditor Team and others; see http://ve.mit-license.org
 * @copyright 2011-2015 OOjs Team and other contributors
 */

( function ( QUnit ) {

	/**
	 * Build a summary of an HTML element.
	 *
	 * Summaries include node name, text, attributes and recursive summaries of children.
	 * Used for serializing or comparing HTML elements.
	 *
	 * @private
	 * @param {HTMLElement} element Element to summarize
	 * @param {boolean} [includeHtml=false] Include an HTML summary for element nodes
	 * @return {Object} Summary of element.
	 */
	function getDomElementSummary( element, includeHtml ) {
		var i, name,
			summary = {
				type: element.nodeName.toLowerCase(),
				// $( '<div><textarea>Foo</textarea></div>' )[0].textContent === 'Foo', which breaks
				// comparisons :( childNodes are summarized anyway, this would just be a nicety
				// text: element.textContent,
				attributes: {},
				children: []
			};

		if ( includeHtml && element.nodeType === Node.ELEMENT_NODE ) {
			summary.html = element.outerHTML;
		}

		// Gather attributes
		if ( element.attributes ) {
			for ( i = 0; i < element.attributes.length; i++ ) {
				name = element.attributes[ i ].name;
				if ( name.substr( 0, 5 ) !== 'data-' && name !== 'id' ) {
					summary.attributes[ name ] = element.attributes[ i ].value;
				}
			}
		}

		// Sort classes
		if ( summary.attributes.class ) {
			summary.attributes.class = summary.attributes.class.split( ' ' ).sort().join( ' ' );
		}

		// Gather certain properties and pretend they are attributes.
		// Take note of casing differences.
		if ( element.value !== undefined ) {
			summary.attributes.value = element.value;
		}
		if ( element.readOnly !== undefined ) {
			summary.attributes.readonly = element.readOnly;
		}
		if ( element.checked !== undefined ) {
			summary.attributes.checked = element.checked;
		}
		if ( element.disabled !== undefined ) {
			summary.attributes.disabled = element.disabled;
		}
		if ( element.tabIndex !== undefined ) {
			summary.attributes.tabindex = element.tabIndex;
		}

		// Summarize children
		if ( element.childNodes ) {
			for ( i = 0; i < element.childNodes.length; i++ ) {
				summary.children.push( getDomElementSummary( element.childNodes[ i ], includeHtml ) );
			}
		}

		// Special handling for textareas, where we only want to account for the content as the 'value'
		// property, never as childNodes or textContent
		if ( summary.type === 'textarea' ) {
			// summary.text = '';
			summary.children = [];
		}

		return summary;
	}

	/**
	 * @method
	 * @static
	 */
	QUnit.assert.equalDomElement = function ( actual, expected, message, stringify ) {
		var actualSummary = getDomElementSummary( actual ),
			expectedSummary = getDomElementSummary( expected ),
			actualSummaryHtml = getDomElementSummary( actual, true ),
			expectedSummaryHtml = getDomElementSummary( expected, true );

		// When running with Karma, the objects are not nicely stringified in the output when the test
		// fails, only showing "Expected: [object Object], Actual: [object Object]" instead. Running
		// QUnit in browser does this, and stringifying causes double escaping in output.
		if ( stringify ) {
			actualSummaryHtml = JSON.stringify( actualSummaryHtml, null, 2 );
			expectedSummaryHtml = JSON.stringify( expectedSummaryHtml, null, 2 );
		}

		QUnit.push(
			QUnit.equiv( actualSummary, expectedSummary ), actualSummaryHtml, expectedSummaryHtml, message
		);
	};

}( QUnit ) );