Removing percy attribute before truncate#188
Conversation
There was a problem hiding this comment.
Pull request overview
This PR reorders operations in the getSource function to remove Percy data attributes from the source HTML before truncation, preventing these unwanted attributes from appearing in the final output if truncation occurs at a point that would otherwise preserve them.
Changes:
- Modified the order of operations to apply regex filtering before truncation instead of after
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
lib/core/utils/dq-element.js
Outdated
| const regex = /\s*data-percy-[^=]+="[^"]*"/g; | ||
| htmlString = htmlString.replace(regex, ''); | ||
| const regex = /\s*data-percy-[^=]+="[^"]*"/g; // Remove unwanted attributes | ||
| source = source.replace(regex, ''); |
There was a problem hiding this comment.
This will throw a TypeError if source is null or undefined. The replace operation should be performed after ensuring source has a value, or use optional chaining: source = (source || '').replace(regex, '')
| source = source.replace(regex, ''); | |
| source = (source || '').replace(regex, ''); |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated 3 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
lib/core/utils/dq-element.js
Outdated
| htmlString = htmlString.replace(regex, ''); | ||
| const regex = /\s*data-percy-[^=]+="[^"]*"/g; // Remove unwanted attributes | ||
| source = (source || '').replace(regex, ''); | ||
| const htmlString = truncate(source || ''); |
There was a problem hiding this comment.
The parameter passed to truncate is source || '', but this is redundant since source was already ensured to be a string on line 179 where it was assigned the result of (source || '').replace(regex, ''). If source was falsy before line 179, it's now an empty string, so the || '' fallback in line 180 is unnecessary.
| const htmlString = truncate(source || ''); | |
| const htmlString = truncate(source); |
<< Describe the changes >>
Closes: