refactor: replace JSDOM with Cheerio for HTML parsing#5
Merged
jkyberneees merged 2 commits intomainfrom May 17, 2025
Merged
Conversation
Contributor
jkyberneees
commented
May 17, 2025
- Updated package.json to include Cheerio as a dependency and modified the test command to include coverage.
- Refactored csp-generator.browser.ts to use Cheerio for parsing HTML instead of DOMParser, allowing for better compatibility in Node.js/Bun environments.
- Updated csp-generator.ts to utilize Cheerio for parsing and extracting resource references, replacing JSDOM usage.
- Removed JSDOM setup from browser tests and adjusted tests to work with the new Cheerio implementation.
…Node.js environments - Updated package.json to include Cheerio as a dependency and modified the test command to include coverage. - Refactored csp-generator.browser.ts to use Cheerio for parsing HTML instead of DOMParser, allowing for better compatibility in Node.js/Bun environments. - Updated csp-generator.ts to utilize Cheerio for parsing and extracting resource references, replacing JSDOM usage. - Removed JSDOM setup from browser tests and adjusted tests to work with the new Cheerio implementation.
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR refactors the CSP generator to replace JSDOM with Cheerio for HTML parsing, easing compatibility with Node.js/Bun environments and simplifying the test setup.
- Updated package.json to include Cheerio and removed JSDOM.
- Refactored source files (csp-generator.ts and csp-generator.browser.ts) to use Cheerio, with conditional paths for differing environments.
- Adjusted tests in csp-generator.browser.test.ts to no longer rely on JSDOM.
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| test/csp-generator.browser.test.ts | Removed JSDOM initialization and global DOM API setup in tests. |
| src/csp-generator.ts | Replaced JSDOM with Cheerio for HTML parsing and resource extraction. |
| src/csp-generator.browser.ts | Added conditional logic for Cheerio vs. DOMParser based on environment. |
| package.json | Removed jsdom dependency and added cheerio dependency; updated test command. |
Comment on lines
+275
to
+294
| $('[style]').each((_, el) => { | ||
| this.detectedInlineStyle = true | ||
| await this.extractCssUrls(el.getAttribute('style') || '', 'style-src') | ||
| } | ||
| for (const styleEl of Array.from(doc.querySelectorAll('style'))) { | ||
| this.extractCssUrls($(el).attr('style') || '', 'style-src') | ||
| }) | ||
| $('style').each((_, styleEl) => { | ||
| this.detectedInlineStyle = true | ||
| await this.extractCssUrls(styleEl.textContent || '', 'style-src') | ||
| } | ||
| this.extractCssUrls($(styleEl).text() || '', 'style-src') | ||
| // Also extract CSS URLs from the style block for images/fonts | ||
| const styleText = $(styleEl).text() || '' | ||
| // Extract url()s from style block | ||
| let match: RegExpExecArray | null | ||
| const urlRe = /url\(\s*(['"]?)([^)'"\s]+)\1\s*\)/gi | ||
| while ((match = urlRe.exec(styleText))) { | ||
| this.resolveAndAdd('style-src', match[2]!) | ||
| // If the URL is an image, also add to img-src | ||
| if (/\.(png|jpg|jpeg|gif|svg|webp|bmp|ico)$/i.test(match[2]!)) { | ||
| this.resolveAndAdd('img-src', match[2]!) | ||
| } | ||
| } | ||
| }) |
There was a problem hiding this comment.
The inline styles extraction now calls 'this.extractCssUrls' without awaiting its result; if this function is asynchronous, the change may cause out-of-order processing. Consider ensuring that extractCssUrls is synchronous or adapting the iteration to handle asynchronous calls properly.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Collaborator
|
Nice work, sorry that didn't approve it in time :) but it looks good anyways!!! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.