-
Notifications
You must be signed in to change notification settings - Fork 0
Fix: Update CSS background URL regex to prevent empty URLs #67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Conversation
- Updated regex pattern from /url\(\s*?['"]?\s*?(.+?)\s*?["']?\s*?\)/ig to /url\(\s*?['"]?([^'"\s]+)['"]?\s*?\)/ig - Pattern now only matches non-empty URLs without whitespace - Prevents matching empty or whitespace-only url() values - Adds validation for empty src/href in img, video, svg, and picture elements - Improves _initWithFirstElementWithInfo to handle empty/whitespace strings Fixes: wp-media/wp-rocket#7116
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR fixes a bug where empty or whitespace-only URLs were being processed for LCP (Largest Contentful Paint) analysis. The changes ensure that elements with empty image sources are properly filtered out before being included in performance tracking.
- Updated CSS background URL regex to prevent matching empty strings
- Added validation to return null for elements with empty/whitespace-only sources
- Enhanced filtering logic in
_initWithFirstElementWithInfoto skip invalid sources
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/BeaconLcp.js | Updated regex pattern and added empty/whitespace validation for video, SVG, and picture elements; enhanced filtering in _initWithFirstElementWithInfo |
| test/BeaconLcp.test.js | Added comprehensive test coverage for empty and whitespace-only src/href handling across different element types |
Comments suppressed due to low confidence (1)
src/BeaconLcp.js:109
- Missing validation for empty or whitespace-only
srcin regularimgelements. Unlike video, svg, and picture elements which now validate and return null for empty sources (lines 114-117, 121-124, 133-136), regular img elements lack this validation. This inconsistency means empty img src attributes could still be processed. Add validation similar to other element types:\njavascript\nelse if (nodeName === \"img\") {\n element_info.type = \"img\";\n element_info.src = element.src;\n element_info.current_src = element.currentSrc;\n if (!element_info.src || !element_info.src.trim()) {\n return null;\n }\n}\n
if (nodeName === "img" && element.srcset) {
element_info.type = "img-srcset";
element_info.src = element.src;
element_info.srcset = element.srcset;
element_info.sizes = element.sizes;
element_info.current_src = element.currentSrc;
} else if (nodeName === "img") {
element_info.type = "img";
element_info.src = element.src;
element_info.current_src = element.currentSrc;
} else if (nodeName === "video") {
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Update CSS background URL regex to handle quoted URLs with spaces using backreference pattern: /url\(\s*(['")?)(.*?)\1\s*\)/ig - Improve hasSrc validation to explicitly check Array.isArray() for bg-img-set case instead of returning true for all non-strings - Update bg_set mapping to use new regex capture groups (URL in m[2]) - Add filter to exclude empty URLs after regex matching Addresses code review feedback from Copilot AI
Coverage summary from CodacySee diff coverage on Codacy
Coverage variation details
Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: Diff coverage details
Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: See your quality gate settings Change summary preferences |
Description
Fixes #7116
Avoid preloading empty href.
Type of change
Detailed scenario
What was tested
<a href="">elementsurl()values are not capturedHow to test
Verified CSS background images with empty url() values are not captured
Technical description
Documentation
This pull request improves the robustness of the
BeaconLcplogic by ensuring that elements with empty or whitespace-only image sources are skipped, preventing invalid images from being processed or reported. It also expands the test suite to cover these edge cases, increasing confidence in the correctness of the implementation.Robust handling of empty or whitespace-only sources:
_getElementInfomethod now returnsnullforimg,svg image,video, andpictureelements if theirsrc,href, orposterattributes are empty or contain only whitespace. This prevents invalid image data from being included._initWithFirstElementWithInfomethod has been updated to ensure only elements with valid, non-emptysrcorsrcsetare considered for LCP (Largest Contentful Paint) analysis.Regex improvement:
Expanded test coverage for edge cases:
src,href, orposterattributes are correctly skipped, and valid sources are properly handled forimg,svg,video, andpictureelements. [1] [2]Mandatory Checklist
Code validation
Code style