Skip to content

Cascadingly capture css @import urls and extracting urls from separate css item - #345

Merged
NGTmeaty merged 3 commits into
mainfrom
embedded-css
Jun 18, 2025
Merged

Cascadingly capture css @import urls and extracting urls from separate css item#345
NGTmeaty merged 3 commits into
mainfrom
embedded-css

Conversation

@yzqzss

@yzqzss yzqzss commented Jun 16, 2025

Copy link
Copy Markdown
Collaborator
  • Supported extracting URLs from individual embedded CSS items. ("embedded" means the CSS item is must derived from a HTML item.)
  • Supported following CSS @import jumps, capturing @import URLs cascadingly.
  • To prevent infinite @import jumps, added the --max-css-jump option to limit it.
  • Since the tdewolff/parse parser currently does not support nested CSS syntax, the previous regex extractor was added back as a fallback when tdewolff/parse fails. (Also improved the previous CSS regex; it should now produce less non-existent links.)

close: #323

@yzqzss yzqzss changed the title Cascadingly crawling css @import urls and extracting urls from separate css item Cascadingly capture css @import urls and extracting urls from separate css item Jun 16, 2025
@yzqzss yzqzss added enhancement New feature or request extractor GSoC labels Jun 16, 2025
@yzqzss yzqzss mentioned this pull request Jun 16, 2025
3 tasks
@yzqzss
yzqzss requested a review from Copilot June 16, 2025 17:29

This comment was marked as outdated.

@yzqzss
yzqzss force-pushed the embedded-css branch 2 times, most recently from d805226 to 9a2b065 Compare June 16, 2025 18:00
yzqzss added 2 commits June 17, 2025 08:58
also add `--max-css-jump` flag to limit the max `@import` jump
@yzqzss
yzqzss requested a review from Copilot June 17, 2025 01:38

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 adds support for cascading CSS @import handling by detecting embedded CSS items, extracting their URLs, and controlling import depth.

  • Introduced detection of embedded CSS items and jump-count logic
  • Hooked into HTML and CSS extraction to enqueue @import URLs as children
  • Added MaxCSSJump configuration and CLI flag to limit import recursion

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
internal/pkg/postprocessor/item.go Skip depth limit for embedded CSS items when crawling assets
internal/pkg/postprocessor/extractor/html.go Call AddAtImportLinksToItemChild after parsing CSS in HTML <style> blocks
internal/pkg/postprocessor/extractor/css_embedded.go New logic for detecting embedded CSS and handling @import jump limits
internal/pkg/postprocessor/assets.go Added branch for extracting assets from CSS items and capturing @import links
internal/pkg/config/config.go Added MaxCSSJump field to configuration
cmd/get.go Exposed --max-css-jump CLI flag
Comments suppressed due to low confidence (3)

internal/pkg/postprocessor/extractor/css_embedded.go:1

  • There are no unit tests covering the new embedded CSS extractor logic. Please add tests for IsEmbeddedCSS, GetEmbeddedCSSJump, and AddAtImportLinksToItemChild to validate expected behavior.
package extractor

internal/pkg/postprocessor/item.go:74

  • The word "embeded" is misspelled; please correct it to "embedded".
// 1. the item [is not an embeded css item] and [is a child has a depth (without redirections) bigger than 2].

internal/pkg/postprocessor/extractor/css_embedded.go:26

  • The variable cssLogger is not defined or imported in this file, which will cause a compile error. Please import the appropriate logging package or reference the correct logger instance.
cssLogger.Warn("item is not an embedded CSS, returning 0 @import jump", "func", "GetEmbeddedCSSJump", "item_id", item.GetShortID())

Comment thread internal/pkg/postprocessor/extractor/css_embedded.go
@yzqzss
yzqzss requested review from NGTmeaty and willmhowes June 17, 2025 01:41
@yzqzss

yzqzss commented Jun 17, 2025

Copy link
Copy Markdown
Collaborator Author

The biggest issue with this CSS parser at the moment is that it doesn't support nested CSS yet, which is widely used today.

tdewolff/parse#92

Writing a fix for upstream might be time-consuming, so the mitigation is to add "if parsing fails, fall back to the regex based extractor we used before" logic. I'll to add the logic hours later...

NGTmeaty
NGTmeaty previously approved these changes Jun 17, 2025

@NGTmeaty NGTmeaty left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As is, this looks really good! I'll keep my eyes out for the additional patch for nested CSS!

@yzqzss
yzqzss marked this pull request as draft June 18, 2025 05:30
@yzqzss
yzqzss marked this pull request as ready for review June 18, 2025 06:10
if p.Err() != nil && !errors.Is(p.Err(), io.EOF) {
parseErr = p.Err()
cssLogger.Error("error parsing CSS", "error", parseErr, "inline", inline)
cssLogger.Error("error parsing CSS", "inline", inline, "error", parseErr)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parseErr include newlines, like this:

unexpected token '[' in declaration on line 5 and column 5
    5:     [aria-hidden="true"] {
           ^

So I put the err parameter at the end to make the terminal log look nicer.

@NGTmeaty NGTmeaty left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great! And thanks a ton for adding the regex fallback.

@NGTmeaty
NGTmeaty merged commit 5e9f1f3 into main Jun 18, 2025
@NGTmeaty
NGTmeaty deleted the embedded-css branch June 18, 2025 21:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request extractor GSoC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[GSoC] Better CSS extractor

3 participants