@@ -16,6 +16,20 @@ const KEY_NAME = 'data-vue-snapshot-serializer-key';
16
16
let key = 0 ;
17
17
let alreadyRemovedKey = true ;
18
18
19
+ /**
20
+ * In most cases, debug will be falsy, so we can skip
21
+ * the expensive cost of serializing the Cheerio object.
22
+ *
23
+ * @param {object } $ The markup as a cheerio object
24
+ * @return {string } The cheerio object serialized to markup
25
+ */
26
+ function logSpeedup ( $ ) {
27
+ if ( globalThis . vueSnapshots ?. debug ) {
28
+ return $ . html ( ) ;
29
+ }
30
+ return undefined ;
31
+ }
32
+
19
33
/**
20
34
* Safety check to ensure the vueWrapper contains the needed
21
35
* methods for attribute stringification.
@@ -98,7 +112,10 @@ const addSerializerKeys = function (vueWrapper) {
98
112
*/
99
113
const removeSerializerKeys = function ( $ , vueWrapper ) {
100
114
if ( ! alreadyRemovedKey ) {
101
- debugLogger ( { function : 'cheerioManipulation.js:removeSerializerKeys' } ) ;
115
+ debugLogger ( {
116
+ function : 'cheerioManipulation.js:removeSerializerKeys' ,
117
+ data : { $ : logSpeedup ( $ ) }
118
+ } ) ;
102
119
103
120
$ ( '[' + KEY_NAME + ']' ) . each ( ( index , element ) => {
104
121
const currentKey = $ ( element ) . attr ( KEY_NAME ) ;
@@ -139,7 +156,10 @@ const addInputValues = function ($, vueWrapper) {
139
156
globalThis . vueSnapshots ?. addInputValues &&
140
157
attributesCanBeStringified ( vueWrapper )
141
158
) {
142
- debugLogger ( { function : 'cheerioManipulation.js:addInputValues' } ) ;
159
+ debugLogger ( {
160
+ function : 'cheerioManipulation.js:addInputValues' ,
161
+ data : { $ : logSpeedup ( $ ) }
162
+ } ) ;
143
163
$ ( 'input, textarea, select' ) . each ( function ( index , element ) {
144
164
const currentKey = $ ( element ) . attr ( KEY_NAME ) ;
145
165
const keySelector = '[' + KEY_NAME + '="' + currentKey + '"]' ;
@@ -199,7 +219,10 @@ const stringifyAttributes = function ($, vueWrapper) {
199
219
globalThis . vueSnapshots ?. stringifyAttributes &&
200
220
attributesCanBeStringified ( vueWrapper )
201
221
) {
202
- debugLogger ( { function : 'cheerioManipulation.js:stringifyAttributes' } ) ;
222
+ debugLogger ( {
223
+ function : 'cheerioManipulation.js:stringifyAttributes' ,
224
+ data : { $ : logSpeedup ( $ ) }
225
+ } ) ;
203
226
$ ( '[' + KEY_NAME + ']' ) . each ( ( index , element ) => {
204
227
const currentKey = $ ( element ) . attr ( KEY_NAME ) ;
205
228
const keySelector = '[' + KEY_NAME + '="' + currentKey + '"]' ;
@@ -271,7 +294,10 @@ const stringifyAttributes = function ($, vueWrapper) {
271
294
*/
272
295
const removeAttributesViaRegex = function ( $ ) {
273
296
if ( globalThis . vueSnapshots ?. regexToRemoveAttributes ) {
274
- debugLogger ( { function : 'cheerioManipulation.js:removeAttributesViaRegex' } ) ;
297
+ debugLogger ( {
298
+ function : 'cheerioManipulation.js:removeAttributesViaRegex' ,
299
+ data : { $ : logSpeedup ( $ ) }
300
+ } ) ;
275
301
$ ( '*' ) . each ( function ( index , element ) {
276
302
Object . keys ( element . attribs ) . forEach ( function ( attributeName ) {
277
303
if ( globalThis . vueSnapshots ?. regexToRemoveAttributes . test ( attributeName ) ) {
@@ -289,7 +315,10 @@ const removeAttributesViaRegex = function ($) {
289
315
*/
290
316
const removeScopedStylesDataVIDAttributes = function ( $ ) {
291
317
if ( globalThis . vueSnapshots ?. removeDataVId ) {
292
- debugLogger ( { function : 'cheerioManipulation.js:removeScopedStylesDataVIDAttributes' } ) ;
318
+ debugLogger ( {
319
+ function : 'cheerioManipulation.js:removeScopedStylesDataVIDAttributes' ,
320
+ data : { $ : logSpeedup ( $ ) }
321
+ } ) ;
293
322
294
323
// [-\w]+ will catch 1 or more instaces of a-z, A-Z, 0-9, hyphen (-), or underscore (_)
295
324
const regex = / d a t a - v - [ - \w ] + / g;
@@ -318,7 +347,10 @@ const removeScopedStylesDataVIDAttributes = function ($) {
318
347
*/
319
348
const renameScopedVBindCustomProperties = function ( $ ) {
320
349
if ( globalThis . vueSnapshots ?. renameScopedVBindCSS ) {
321
- debugLogger ( { function : 'cheerioManipulation.js:renameScopedVBindCustomProperties' } ) ;
350
+ debugLogger ( {
351
+ function : 'cheerioManipulation.js:renameScopedVBindCustomProperties' ,
352
+ data : { $ : logSpeedup ( $ ) }
353
+ } ) ;
322
354
// String starts, there are exactly 8 characters using lowercase
323
355
// hexidecimal, no other characters allowed, then the string ends.
324
356
const scopeIdTester = / ^ [ 0 - 9 a - f ] { 8 } $ / ;
@@ -362,7 +394,10 @@ const renameScopedVBindCustomProperties = function ($) {
362
394
*/
363
395
const removeServerRenderedText = function ( $ ) {
364
396
if ( globalThis . vueSnapshots ?. removeServerRendered ) {
365
- debugLogger ( { function : 'cheerioManipulation.js:removeServerRenderedText' } ) ;
397
+ debugLogger ( {
398
+ function : 'cheerioManipulation.js:removeServerRenderedText' ,
399
+ data : { $ : logSpeedup ( $ ) }
400
+ } ) ;
366
401
$ ( '[data-server-rendered]' ) . removeAttr ( 'data-server-rendered' ) ;
367
402
}
368
403
} ;
@@ -375,7 +410,10 @@ const removeServerRenderedText = function ($) {
375
410
*/
376
411
const clearAttributes = function ( $ ) {
377
412
if ( globalThis . vueSnapshots ?. attributesToClear ?. length ) {
378
- debugLogger ( { function : 'cheerioManipulation.js:clearAttributes' } ) ;
413
+ debugLogger ( {
414
+ function : 'cheerioManipulation.js:clearAttributes' ,
415
+ data : { $ : logSpeedup ( $ ) }
416
+ } ) ;
379
417
globalThis . vueSnapshots . attributesToClear . forEach ( function ( attribute ) {
380
418
$ ( '[' + attribute + ']' ) . attr ( attribute , '' ) ;
381
419
} ) ;
@@ -389,7 +427,10 @@ const clearAttributes = function ($) {
389
427
*/
390
428
const clearInlineFunctions = function ( $ ) {
391
429
if ( globalThis . vueSnapshots ?. clearInlineFunctions ) {
392
- debugLogger ( { function : 'cheerioManipulation.js:clearInlineFunctions' } ) ;
430
+ debugLogger ( {
431
+ function : 'cheerioManipulation.js:clearInlineFunctions' ,
432
+ data : { $ : logSpeedup ( $ ) }
433
+ } ) ;
393
434
394
435
/**
395
436
* Takes a string and tells you if it is a function.
@@ -492,7 +533,10 @@ const stubOutDom = function ($) {
492
533
*/
493
534
const sortAttributes = function ( $ ) {
494
535
if ( globalThis . vueSnapshots ?. sortAttributes ) {
495
- debugLogger ( { function : 'cheerioManipulation.js:sortAttributes' } ) ;
536
+ debugLogger ( {
537
+ function : 'cheerioManipulation.js:sortAttributes' ,
538
+ data : { $ : logSpeedup ( $ ) }
539
+ } ) ;
496
540
$ ( '*' ) . each ( function ( index , element ) {
497
541
Object . keys ( element . attribs ) . sort ( ) . forEach ( function ( key ) {
498
542
let value = element . attribs [ key ] ;
@@ -514,7 +558,10 @@ const sortAttributes = function ($) {
514
558
*/
515
559
const sortClasses = function ( $ ) {
516
560
if ( globalThis . vueSnapshots ?. sortClasses ) {
517
- debugLogger ( { function : 'cheerioManipulation.js:sortClasses' } ) ;
561
+ debugLogger ( {
562
+ function : 'cheerioManipulation.js:sortClasses' ,
563
+ data : { $ : logSpeedup ( $ ) }
564
+ } ) ;
518
565
$ ( '*' ) . each ( function ( index , element ) {
519
566
const classes = element ?. attribs ?. class ?. trim ( ) ;
520
567
if ( classes ) {
0 commit comments