File tree Expand file tree Collapse file tree 2 files changed +36
-1
lines changed Expand file tree Collapse file tree 2 files changed +36
-1
lines changed Original file line number Diff line number Diff line change @@ -170,6 +170,7 @@ function visit(
170
170
// TODO remove this in v7 (this means the method will no longer be exported, under any name)
171
171
export { visit as walk } ;
172
172
173
+ /* eslint-disable complexity */
173
174
/**
174
175
* Stringify the given value. Handles various known special values and types.
175
176
*
@@ -242,11 +243,19 @@ function stringifyValue(
242
243
// them to strings means that instances of classes which haven't defined their `toStringTag` will just come out as
243
244
// `"[object Object]"`. If we instead look at the constructor's name (which is the same as the name of the class),
244
245
// we can make sure that only plain objects come out that way.
245
- return `[object ${ getConstructorName ( value ) } ]` ;
246
+ const objName = getConstructorName ( value ) ;
247
+
248
+ // Handle HTML Elements
249
+ if ( / ^ H T M L ( \w * ) E l e m e n t $ / . test ( objName ) ) {
250
+ return `[HTMLElement: ${ objName } ]` ;
251
+ }
252
+
253
+ return `[object ${ objName } ]` ;
246
254
} catch ( err ) {
247
255
return `**non-serializable** (${ err } )` ;
248
256
}
249
257
}
258
+ /* eslint-enable complexity */
250
259
251
260
function getConstructorName ( value : unknown ) : string {
252
261
const prototype : Prototype | null = Object . getPrototypeOf ( value ) ;
Original file line number Diff line number Diff line change @@ -263,6 +263,32 @@ describe('normalize()', () => {
263
263
} ) ;
264
264
} ) ;
265
265
266
+ describe ( 'handles HTML elements' , ( ) => {
267
+ test ( 'HTMLDivElement' , ( ) => {
268
+ expect (
269
+ normalize ( {
270
+ div : document . createElement ( 'div' ) ,
271
+ div2 : document . createElement ( 'div' ) ,
272
+ } ) ,
273
+ ) . toEqual ( {
274
+ div : '[HTMLElement: HTMLDivElement]' ,
275
+ div2 : '[HTMLElement: HTMLDivElement]' ,
276
+ } ) ;
277
+ } ) ;
278
+
279
+ test ( 'input elements' , ( ) => {
280
+ expect (
281
+ normalize ( {
282
+ input : document . createElement ( 'input' ) ,
283
+ select : document . createElement ( 'select' ) ,
284
+ } ) ,
285
+ ) . toEqual ( {
286
+ input : '[HTMLElement: HTMLInputElement]' ,
287
+ select : '[HTMLElement: HTMLSelectElement]' ,
288
+ } ) ;
289
+ } ) ;
290
+ } ) ;
291
+
266
292
describe ( 'calls toJSON if implemented' , ( ) => {
267
293
test ( 'primitive values' , ( ) => {
268
294
const a = new Number ( 1 ) as any ;
You can’t perform that action at this time.
0 commit comments