byteface / domonic

Create HTML with python 3 using a standard DOM API. Includes a python port of JavaScript for interoperability and tons of other cool features. A fast prototyping library.
https://domonic.readthedocs.io/
MIT License
129 stars 13 forks source link

f-string response includes closing tags for void elements #37

Closed kthy closed 2 years ago

kthy commented 2 years ago

When converting the html object with str and returning it as a response, closing tags for void elements like meta are correctly omitted.

When converting it in an f-string and returning it as a response, closing tags are erroneously included. Example of a simple Sanic route:

@app.route("/404.html")
async def not_found(req: request.Request) -> response.HTTPResponse:
    four_oh_four = html(
        head(
            meta(_charset="utf-8")
        ),
        body(
            h1("Page Not Found"),
            p("Sorry, but the page you were trying to view does not exist."),
        ),
        _lang="en"
    )
    return response.html(f"{four_oh_four}")

which generates the following HTML:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        </meta>
    </head>
    <body>
        <h1>Page Not Found</h1>
        <p>Sorry, but the page you were trying to view does not exist.</p>
    </body>
</html>

Note the errant </meta> on line 5. For comparison, the output with return response.html(str(four_oh_four)) is

<html lang="en"><head><meta charset="utf-8" /></head><body><h1>Page Not Found</h1><p>Sorry, but the page you were trying to view does not exist.</p></body></html>

but then I don't get the doctype element.

byteface commented 2 years ago

How very strange. I do apologies and will investigate. Thanks so much for the feedback it's appreciated.

In the shorterm please accept my apology and hopefully you can use another lib to beautifying. i.e.

output = render(html(body(h1('Hello, World!'))))
from html5print import HTMLBeautifier
print(HTMLBeautifier.beautify(output, 4))

and I will look into fixing this as soon as possible.

byteface commented 2 years ago

the doc type with str a bit harder in short term. it's part of a re-architecture requirement where my DOM needs to have a top level node. very sorry but you would have to prepend that manually until that gets figured out.

to explain what i mean by this. at the moment my dom can't have a comment outside the html tag for example. as it is the root node. while its illegal to have multiple nodes on a root it still should be possible. and hence other library dom builders don't quite map to mine yet. i.e. the hacks i had to do to expat parser. anyway I'm hoping to get into fixing that so it's compatible with html5lib for example. But it could take some time.

thanks for your patience and trying out the lib also. It helps me a lot ot get people using it and giving feedback.

kthy commented 2 years ago

No need to apologise, you've written a very useful library! Thanks for considering my feedback - there's probably more to come … :wink: