Stranger6667 / css-inline

High-performance library for inlining CSS into HTML 'style' attributes
https://css-inline.org/
MIT License
243 stars 29 forks source link

inline_fragment not worked for python #388

Open PAvel00m opened 1 month ago

PAvel00m commented 1 month ago

Hi, when passing valid html to a function, an empty string is returned

Python 3.8.18 MacOS Sequoia 15.0

template = """
        <html>
            <head></head>
            <body>
                <table>
                    </tbody>
                        <tr>
                            <th>Test</th>
                            <td>text</td>
                        </tr>
                    </tbody>
                </table>
            </body>
        </html>
"""

rendered_content = css_inline.inline_fragment(
    template,
    """
    body {
        font-family: Arial, sans-serif;
    }

    table, th, td {
        border: 1px solid black;
        border-collapse: collapse;
    }
    """
)
Stranger6667 commented 1 month ago

Thanks for the report!

I haven't anticipated this use case as template in your example is not a fragment, but a complete HTML. However, returning an empty string is confusing indeed. Not sure if it would be better to return an error on non-fragment inputs or just fall back to something like this automatically:

css_inline.inline(template, extra_css="""
    body {
        font-family: Arial, sans-serif;
    }

    table, th, td {
        border: 1px solid black;
        border-collapse: collapse;
    }
    """)
# <html><head></head>
#            <body style="font-family: Arial, sans-serif;">
#                <table style="border: 1px solid black;border-collapse: collapse;">
#
#                        <tbody><tr>
#                             <th style="border: 1px solid black;border-collapse: collapse;">Test</th>
#                            <td style="border: 1px solid black;border-collapse: collapse;">text</td>
#                        </tr>
#                    </tbody>
#                </table>
#
#
# </body></html>

In any event, I think the behavior of inline_fragment should be changed + docs should be updated to reflect this case.

PAvel00m commented 1 month ago

it's just that I use inline_fragment after rendering the jinja2 template and not always the template is html and I then don't have to wrap in tags like BeautifulSoup does

Stranger6667 commented 1 month ago

Makes sense! Then, I'd add some fallback for non-fragments to avoid whitespace-only output

PAvel00m commented 1 month ago

I did, it's just the blank line is really confusing. thanks for the reply!

Stranger6667 commented 1 month ago

I did, it's just the blank line is really confusing. thanks for the reply!

You are very welcome! To clarify, I meant that I am going to implement it directly in css-inline, so there is no need to put such conditions in your code. I don't have an ETA, but it should be a relatively simple change, so maybe in a few weeks I'll make a patch release :)

PAvel00m commented 1 month ago

Great, thank you!