Skip to content

Conversation

@jkyberneees
Copy link
Contributor

  • 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.
@jkyberneees jkyberneees requested a review from Copilot May 17, 2025 14:13
Copy link
Contributor

Copilot AI left a 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.

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]!)
}
}
})
Copy link

Copilot AI May 17, 2025

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.

Copilot uses AI. Check for mistakes.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@jkyberneees jkyberneees merged commit d09ca0c into main May 17, 2025
5 checks passed
@jkyberneees jkyberneees deleted the replacing-jsdom-by-cheerio branch May 17, 2025 14:16
@vbarzana
Copy link
Collaborator

Nice work, sorry that didn't approve it in time :) but it looks good anyways!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants