Skip to content
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

Be more tolerant to invalid css. #175

Merged
merged 6 commits into from
Feb 9, 2024
Merged

Be more tolerant to invalid css. #175

merged 6 commits into from
Feb 9, 2024

Conversation

mgautierfr
Copy link
Contributor

Return the original content if we cannot parse the css.


In case of invalid css tinycss2 doesn't not failed on parsing, but generate a ParserError token
https://doc.courtbouillon.org/tinycss2/stable/api_reference.html?highlight=parser#tinycss2.ast.ParseError

This is on serialization that exception occurs.
We even try/except all transformation to be sure.

Fix #155

Copy link
Collaborator

@benoit74 benoit74 left a comment

Choose a reason for hiding this comment

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

See comments

src/warc2zim/content_rewriting/css.py Outdated Show resolved Hide resolved
src/warc2zim/content_rewriting/css.py Outdated Show resolved Hide resolved
@mgautierfr
Copy link
Contributor Author

New version rewrite the css content using regex if tinycss2 fails to parse it.

@mgautierfr mgautierfr requested a review from benoit74 February 5, 2024 16:02
Copy link

codecov bot commented Feb 5, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (b2ca850) 86.57% compared to head (db8c7ac) 87.08%.
Report is 1 commits behind head on warc2zim2.

❗ Current head db8c7ac differs from pull request most recent head e9c1743. Consider uploading reports for the commit e9c1743 to get more accurate results

Additional details and impacted files
@@              Coverage Diff              @@
##           warc2zim2     #175      +/-   ##
=============================================
+ Coverage      86.57%   87.08%   +0.50%     
=============================================
  Files             13       13              
  Lines            849      867      +18     
  Branches         147      149       +2     
=============================================
+ Hits             735      755      +20     
+ Misses            97       96       -1     
+ Partials          17       16       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@benoit74 benoit74 left a comment

Choose a reason for hiding this comment

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

See comments.

Please add more tests to have 100% test coverage on the patch : https://app.codecov.io/gh/openzim/warc2zim/pull/175?src=pr&el=tree ; no need to do all test cases on all code branches, but at least have one test case on each of them, especially since here you are not testing the inline situation which is the one we have in production.

I think that we need:

  • logs when the normal CSS rewriting failed (a warning probably)
  • exception handling for the fallback rewriter: maybe simply a clear log with original content, because this is probably a very rare situation (I don't see why regex would fail) but it would make debugging way simpler
  • more fine-grained processing of CSS rules ; since we know that tinycss is permissive in terms of parsing, we could process / serialize CSS rules one by one and apply the fallback only on failing CSS rules (this could be done in a separate issue)

src/warc2zim/content_rewriting/css.py Outdated Show resolved Hide resolved
tests/test_js_rewriting.py Outdated Show resolved Hide resolved
tests/test_js_rewriting.py Outdated Show resolved Hide resolved
src/warc2zim/content_rewriting/css.py Outdated Show resolved Hide resolved
src/warc2zim/content_rewriting/css.py Outdated Show resolved Hide resolved
src/warc2zim/content_rewriting/css.py Show resolved Hide resolved
pyright automatically "promote" `bytes` type to `bytes|memoryview`.
So `str|bytes` is promoted to `str|bytes|memoryview` and the following
doesn't change the type of `bytes_or_text` variable as `memoryview` is not
covered.

```python
if instance(bytes_or_text, bytes):
    bytes_or_text = bytes_or_text.decode()
```

By disabling bytes type promoting, all cases are covered and
`bytes_or_text` is "converted" to `str`.
@mgautierfr mgautierfr force-pushed the css_error_tolerant branch 2 times, most recently from d6b1c89 to 88bce6f Compare February 7, 2024 13:14
@mgautierfr mgautierfr requested a review from benoit74 February 7, 2024 13:14
Copy link
Collaborator

@benoit74 benoit74 left a comment

Choose a reason for hiding this comment

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

All conversations have been addressed properly, thank you (and great!), but AFAIK you didn't provided feedback on the last 3 recommendations of #175 (review)

@mgautierfr
Copy link
Contributor Author

Sorry, I forget about these comments

logs when the normal CSS rewriting failed (a warning probably)

Indeed, will do

exception handling for the fallback rewriter: maybe simply a clear log with original content, because this is probably a very rare situation (I don't see why regex would fail) but it would make debugging way simpler

As you say, I don't see why regex would fail. If it is the case, it is more a bug in our code than a invalid input we have to handle somehow.

more fine-grained processing of CSS rules ; since we know that tinycss is permissive in terms of parsing, we could process / serialize CSS rules one by one and apply the fallback only on failing CSS rules (this could be done in a separate issue)

I have think about that, but not an easy task. We would have to reimplement tinycss2 serialize feature. And on top of that, it is not clear about what to do in case of parsing error. Even tinycss try to be smart and recover error but give up on parsing error (https://github.com/Kozea/tinycss2/blob/main/tinycss2/ast.py#L105-L115).

@benoit74
Copy link
Collaborator

benoit74 commented Feb 9, 2024

As you say, I don't see why regex would fail. If it is the case, it is more a bug in our code than a invalid input we have to handle somehow.

OK, let's live with it for now.

I have think about that, but not an easy task. We would have to reimplement tinycss2 serialize feature. And on top of that, it is not clear about what to do in case of parsing error. Even tinycss try to be smart and recover error but give up on parsing error (Kozea/tinycss2@main/tinycss2/ast.py#L105-L115).

Agreed! I've opened an issue to keep this information in mind: #176

Copy link
Collaborator

@benoit74 benoit74 left a comment

Choose a reason for hiding this comment

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

LGTM, I will merge it

mgautierfr and others added 3 commits February 9, 2024 11:00
When tinycss2 fails to parse, we use a regex rewriter to (trying to)
rewrite url in the css.

In case of invalid css tinycss2 doesn't not failed on parsing, but
generate a ParserError token
https://doc.courtbouillon.org/tinycss2/stable/api_reference.html?highlight=parser#tinycss2.ast.ParseError

This is on serialization that exception occurs.
We even try/except all transformation to be sure.

Fix #155
@benoit74 benoit74 merged commit f837179 into warc2zim2 Feb 9, 2024
4 checks passed
@benoit74 benoit74 deleted the css_error_tolerant branch February 9, 2024 10:09
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.

2 participants