File tree Expand file tree Collapse file tree 2 files changed +34
-2
lines changed Expand file tree Collapse file tree 2 files changed +34
-2
lines changed Original file line number Diff line number Diff line change @@ -198,18 +198,24 @@ function htmlTreeAsString(elem) {
198
198
*/
199
199
function htmlElementAsString ( elem ) {
200
200
var out = [ ] ,
201
+ className ,
201
202
classes ,
202
203
key ,
203
204
attr ,
204
205
i ;
205
206
207
+ if ( ! elem || ! elem . tagName ) {
208
+ return '' ;
209
+ }
210
+
206
211
out . push ( elem . tagName . toLowerCase ( ) ) ;
207
212
if ( elem . id ) {
208
213
out . push ( '#' + elem . id ) ;
209
214
}
210
215
211
- if ( elem . className ) {
212
- classes = elem . className . split ( ' ' ) ;
216
+ className = elem . className ;
217
+ if ( className && isString ( className ) ) {
218
+ classes = className . split ( ' ' ) ;
213
219
for ( i = 0 ; i < classes . length ; i ++ ) {
214
220
out . push ( '.' + classes [ i ] ) ;
215
221
}
Original file line number Diff line number Diff line change @@ -201,6 +201,32 @@ describe('utils', function () {
201
201
}
202
202
} ) , 'img#image-3[title="A picture of an apple"]' ) ;
203
203
} ) ;
204
+
205
+ it ( 'should return an empty string if the input element is falsy' , function ( ) {
206
+ assert . equal ( htmlElementAsString ( null ) , '' ) ;
207
+ assert . equal ( htmlElementAsString ( 0 ) , '' ) ;
208
+ assert . equal ( htmlElementAsString ( undefined ) , '' ) ;
209
+ } ) ;
210
+
211
+ it ( 'should return an empty string if the input element has no tagName property' , function ( ) {
212
+ assert . equal ( htmlElementAsString ( {
213
+ id : 'the-username' ,
214
+ className : 'form-control'
215
+ } ) , '' ) ;
216
+ } ) ;
217
+
218
+ it ( 'should gracefully handle when className is not a string (e.g. SVGAnimatedString' , function ( ) {
219
+ assert . equal ( htmlElementAsString ( {
220
+ tagName : 'INPUT' ,
221
+ id : 'the-username' ,
222
+ className : { } , // not a string
223
+ getAttribute : function ( key ) {
224
+ return {
225
+ name : 'username'
226
+ } [ key ] ;
227
+ }
228
+ } ) , 'input#the-username[name="username"]' ) ;
229
+ } ) ;
204
230
} ) ;
205
231
206
232
describe ( 'parseUrl' , function ( ) {
You can’t perform that action at this time.
0 commit comments