Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cannot put <meta charset=""> first #140

Open
allefeld opened this issue Mar 7, 2021 · 3 comments
Open

cannot put <meta charset=""> first #140

allefeld opened this issue Mar 7, 2021 · 3 comments

Comments

@allefeld
Copy link

allefeld commented Mar 7, 2021

In HTML5 it is recommended to explicitly specify the character encoding with a meta element in the head:

<meta charset="utf-8">

To make sure this encoding is also applied to the title of the document, it should come before the title element. However, if I try to do so with the code

import dominate
doc = dominate.document()
with doc.head:
    dominate.tags.meta(charset='utf-8')
    dominate.tags.title('Document title')
print(doc.render())

the result is

<!DOCTYPE html>
<html>
  <head>
    <title>Dominate</title>
    <meta charset="utf-8">
    <title>Document title</title>
  </head>
  <body></body>
</html>

i.e. I get two title elements. If I specify the title instead as an argument of dominate.document, there is only one title element, but it is before the meta element.

I see two ways around this:

  1. The title attribute of dominate.document is only used to generate a title element if none has been added manually.

  2. dominate.document gets an additional charset argument.

My preference would be (1).

@Knio
Copy link
Owner

Knio commented Mar 8, 2021

document is a thin wrapper that's pretty much just for quick prototyping. If you want more control, I would suggest just using tags.html as your root element instead of document. Other simple things you could do is just removing doc.title_node or doing doc.children.insert(0, meta_node).

If this is more of a feature request than a bug report, adding an automatic meta tag to document (in the right place) could be a good idea.

@allefeld
Copy link
Author

allefeld commented Mar 8, 2021

Honestly not sure whether this is a bug report or a feature request. ;) But I agree, document could be more than just for prototyping, but a convenience function which makes sure that things like <meta charset=""> are there.

I wasn't aware that I could use tags.html instead of document, thanks! But then, how do I get the <!DOCTYPE html>? Just write it myself?

@ipapop
Copy link

ipapop commented Mar 12, 2021

I have a similar issue trying to set the doc rendered opening html tag to html lang="en" using dominate.tags.html
I guess it is rather a feature request though..
Edit : as a workaround I am using beautiful soup to modify the opening html tag..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants