-
Notifications
You must be signed in to change notification settings - Fork 1
refactor: replace JSDOM with Cheerio for HTML parsing #5
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
Conversation
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.
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 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. |
| $('[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]!) | ||
| } | ||
| } | ||
| }) |
Copilot
AI
May 17, 2025
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.
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>
|
Nice work, sorry that didn't approve it in time :) but it looks good anyways!!! |