corrade-http-templates – Blame information for rev 57
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
57 | office | 1 | /* |
2 | * Experimental assertion for comparing DOM objects. |
||
3 | * |
||
4 | * Serializes an element and some properties and attributes and its children if any, |
||
5 | * otherwise the text. Then compares the result using deepEqual(). |
||
6 | */ |
||
7 | define( [ |
||
8 | "qunit", |
||
9 | "jquery" |
||
10 | ], function( QUnit, $ ) { |
||
11 | |||
12 | var domEqual = QUnit.assert.domEqual = function( selector, modifier, message ) { |
||
13 | |||
14 | var assert = this; |
||
15 | |||
16 | // Get current state prior to modifier |
||
17 | var expected = extract( selector, message ); |
||
18 | |||
19 | function done() { |
||
20 | var actual = extract( selector, message ); |
||
21 | assert.push( QUnit.equiv( actual, expected ), actual, expected, message ); |
||
22 | } |
||
23 | |||
24 | // Run modifier (async or sync), then compare state via done() |
||
25 | if ( modifier.length ) { |
||
26 | modifier( done ); |
||
27 | } else { |
||
28 | modifier(); |
||
29 | done(); |
||
30 | } |
||
31 | }; |
||
32 | |||
33 | domEqual.properties = [ |
||
34 | "disabled", |
||
35 | "readOnly" |
||
36 | ]; |
||
37 | |||
38 | domEqual.attributes = [ |
||
39 | "autocomplete", |
||
40 | "aria-activedescendant", |
||
41 | "aria-controls", |
||
42 | "aria-describedby", |
||
43 | "aria-disabled", |
||
44 | "aria-expanded", |
||
45 | "aria-haspopup", |
||
46 | "aria-hidden", |
||
47 | "aria-labelledby", |
||
48 | "aria-pressed", |
||
49 | "aria-selected", |
||
50 | "aria-valuemax", |
||
51 | "aria-valuemin", |
||
52 | "aria-valuenow", |
||
53 | "class", |
||
54 | "href", |
||
55 | "id", |
||
56 | "nodeName", |
||
57 | "role", |
||
58 | "tabIndex", |
||
59 | "title" |
||
60 | ]; |
||
61 | |||
62 | function getElementStyles( elem ) { |
||
63 | var styles = {}; |
||
64 | var style = elem.ownerDocument.defaultView ? |
||
65 | elem.ownerDocument.defaultView.getComputedStyle( elem, null ) : |
||
66 | elem.currentStyle; |
||
67 | var key, len; |
||
68 | |||
69 | if ( style && style.length && style[ 0 ] && style[ style[ 0 ] ] ) { |
||
70 | len = style.length; |
||
71 | while ( len-- ) { |
||
72 | key = style[ len ]; |
||
73 | if ( typeof style[ key ] === "string" ) { |
||
74 | styles[ $.camelCase( key ) ] = style[ key ]; |
||
75 | } |
||
76 | } |
||
77 | |||
78 | // Support: Opera, IE <9 |
||
79 | } else { |
||
80 | for ( key in style ) { |
||
81 | if ( typeof style[ key ] === "string" ) { |
||
82 | styles[ key ] = style[ key ]; |
||
83 | } |
||
84 | } |
||
85 | } |
||
86 | |||
87 | return styles; |
||
88 | } |
||
89 | |||
90 | function extract( selector, message ) { |
||
91 | var elem = $( selector ); |
||
92 | if ( !elem.length ) { |
||
93 | QUnit.push( false, null, null, |
||
94 | "domEqual failed, can't extract " + selector + ", message was: " + message ); |
||
95 | return; |
||
96 | } |
||
97 | |||
98 | var result = {}; |
||
99 | var children; |
||
100 | $.each( domEqual.properties, function( index, attr ) { |
||
101 | var value = elem.prop( attr ); |
||
102 | result[ attr ] = value != null ? value : ""; |
||
103 | } ); |
||
104 | $.each( domEqual.attributes, function( index, attr ) { |
||
105 | var value = elem.attr( attr ); |
||
106 | result[ attr ] = value != null ? value : ""; |
||
107 | } ); |
||
108 | result.style = getElementStyles( elem[ 0 ] ); |
||
109 | result.events = $._data( elem[ 0 ], "events" ); |
||
110 | result.data = $.extend( {}, elem.data() ); |
||
111 | delete result.data[ $.expando ]; |
||
112 | children = elem.children(); |
||
113 | if ( children.length ) { |
||
114 | result.children = elem.children().map( function() { |
||
115 | return extract( $( this ) ); |
||
116 | } ).get(); |
||
117 | } else { |
||
118 | result.text = elem.text(); |
||
119 | } |
||
120 | return result; |
||
121 | } |
||
122 | |||
123 | } ); |