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) {
198198 */
199199function htmlElementAsString ( elem ) {
200200 var out = [ ] ,
201+ className ,
201202 classes ,
202203 key ,
203204 attr ,
204205 i ;
205206
207+ if ( ! elem || ! elem . tagName ) {
208+ return '' ;
209+ }
210+
206211 out . push ( elem . tagName . toLowerCase ( ) ) ;
207212 if ( elem . id ) {
208213 out . push ( '#' + elem . id ) ;
209214 }
210215
211- if ( elem . className ) {
212- classes = elem . className . split ( ' ' ) ;
216+ className = elem . className ;
217+ if ( className && isString ( className ) ) {
218+ classes = className . split ( ' ' ) ;
213219 for ( i = 0 ; i < classes . length ; i ++ ) {
214220 out . push ( '.' + classes [ i ] ) ;
215221 }
Original file line number Diff line number Diff line change @@ -201,6 +201,32 @@ describe('utils', function () {
201201 }
202202 } ) , 'img#image-3[title="A picture of an apple"]' ) ;
203203 } ) ;
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+ } ) ;
204230 } ) ;
205231
206232 describe ( 'parseUrl' , function ( ) {
You can’t perform that action at this time.
0 commit comments