tipsy / j2html

Java to HTML generator. Enjoy typesafe HTML generation.
https://j2html.com/
Apache License 2.0
765 stars 136 forks source link

Fix document(HtmlTag) in TagCreator #188

Open obecker opened 3 years ago

obecker commented 3 years ago

There is this method in TagCreator:

    public static String document(HtmlTag htmlTag) {
        return document().render() + htmlTag.render();
    }

This returns a String by calling render(), thus it prevents using the new render possibilities introduced with https://github.com/tipsy/j2html/pull/179

A better approach would be if there is a Document class that implements Renderable and that handles the correct rendering.

Apparently this would break code written for j2html 1.*, so it is something we should consider for a 2.* release.

ritzow commented 2 years ago

That method is also inefficient given it renders the entire document and then does a string concatenation, duplicating the whole thing, instead of just writing to a StringBuilder, It could be updated to render to a shared StringBuilder and the unescaped text object doesn't even need to be created. But for version 2 I agree it should be its own type.


htmlTag.render(FlatHtml.inMemory(Config.global()).appendUnescapedText("<!DOCTYPE html>"), null);