Skip to content

CssInline.inlineFragment(html, css) fails silently if input contains html, head, style, or body tags #693

@morrica

Description

@morrica

CssInline.inlineFragment(html, css) fails silently if input contains html, head, style, or body tags (or leading whitespace #692).

The following inputs will all fail silently and return whitespace.

String html = """
            <html>
            <head>
            <style>
                h1 {
                    color: blue;
                }
            </style>
            </head>
            <body>
            <h1>Hello, World!</h1>
            </body>
            </html>
        """.trim();

String inlined = CssInline.inlineFragment(html, css);
System.out.println("inlined: '%s'".formatted(inlined));
String html = """
            <head>
            <style>
                h1 {
                    color: blue;
                }
            </style>
            </head>
            <body>
            <h1>Hello, World!</h1>
            </body>
        """.trim();

String inlined = CssInline.inlineFragment(html, css);
System.out.println("inlined: '%s'".formatted(inlined));
String html = """
            <body>
            <style>
                h1 {
                    color: blue;
                }
            </style>
            <h1>Hello, World!</h1>
            </body>
        """.trim();

String inlined = CssInline.inlineFragment(html, css);
System.out.println("inlined: '%s'".formatted(inlined));
String html = """
            <body>
            <h1>Hello, World!</h1>
            </body>
        """.trim();

String inlined = CssInline.inlineFragment(html, css);
System.out.println("inlined: '%s'".formatted(inlined));
String html = """
            <style>
                h1 {
                    color: blue;
                }
            </style>
            <h1>Hello, World!</h1>
        """.trim();

String inlined = CssInline.inlineFragment(html, css);
System.out.println("inlined: '%s'".formatted(inlined));

I can see that inlineFragment() is not designed for full HTML pages, but certainly a valid body tag should be handled correctly?

I can also see that since what I am doing is applying css to full html emails, what I really want to use in my case is:

CssInlineConfig inlineConfig = new CssInlineConfig.Builder()
                    .setExtraCss(myStyles)
                    .build();
html = CssInline.inline(html, inlineConfig);

I think that a common use case is "Take this html (fragment or page) and apply this other css. I think that the method signature of inlineFragment(html, css) naturally fits that use case and so that is why I naturally tried that first. I would suggest including an inline(html, css) method would match the common use case and prevent other frustrations people might have going down the wrong path and attempting to use inlineFragment(). Just a suggestion :-)

Overall this package is very useful and solved a big problem I was having 👍

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions