Kozea / WeasyPrint

The awesome document factory
https://weasyprint.org
BSD 3-Clause "New" or "Revised" License
7.22k stars 686 forks source link

Support of `@media only` #2140

Open vpoulailleau opened 6 months ago

vpoulailleau commented 6 months ago

With weasyprint 62, I get WARNING: Invalid media type ' only print ' the whole @media rule was ignored at 3:5. for the following HTML:

<html lang="fr">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Titre de la page</title>
    <style>
    h1 {color: red;}
    @media only print {
      h1 {
        color: green;
      }
    }
    </style>
  </head>
  <body>
    <main>
      <h1>Contenu Principal</h1>
      <p>Ceci est un paragraphe de texte.</p>
    </main>
  </body>
</html>

According to https://www.w3.org/TR/mediaqueries-5/#typedef-media-query-list this is valid CSS. only seems not supported yet (I haven't tested not, or, and for @media not screen for instance).

I can easily workaround this limitation by removing only.

liZe commented 6 months ago

Hi!

Thanks for the report. Media queries are not very well supported, this is an old problem, see #494.

The main problem we have with media queries is that it’s hard to define the viewport dimension on paged media. But supporting "only" (and probably "not", "or" and "and", as long as we have only media types) is actually much easier.

Interested in adding this feature? That’s a good first issue if you have some Python skills and no idea about how WeasyPrint works!

The magic happens here: https://github.com/Kozea/WeasyPrint/blob/d5d7ce369aef035712cf73446f9085a32105846f/weasyprint/css/media_queries.py#L25-L39

vpoulailleau commented 6 months ago

@liZe I currently don't have the time right now. But I would be glad to help.

BTW, will there be a coding sprint for weasyprint at PyCon FR 2024? I planned to come and help Julien for the Python docs translation, but I would be happy to better understand weasyprint that I use quite often. We saw each other during last AFPy's assemblée générale !

liZe commented 6 months ago

I currently don't have the time right now. But I would be glad to help.

No problem, let’s save this chance to another newcomer!

BTW, will there be a coding sprint for weasyprint at PyCon FR 2024? I planned to come and help Julien for the Python docs translation, but I would be happy to better understand weasyprint that I use quite often. We saw each other during last AFPy's assemblée générale !

😄

I don’t think that we’ll have the time to organize a coding sprint because we’re quite busy with the organization (and Julien will be happy to have you). But we’ll probably find the time to talk about WeasyPrint’s internals!

vpoulailleau commented 6 months ago

I imagine it takes time to organize everything.

Yes, see you in Strasbourg!