Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

support recursive with dominate.document() #157

Closed
MarisaKirisame opened this issue Feb 17, 2022 · 4 comments
Closed

support recursive with dominate.document() #157

MarisaKirisame opened this issue Feb 17, 2022 · 4 comments

Comments

@MarisaKirisame
Copy link

MarisaKirisame commented Feb 17, 2022

import dominate
from dominate.tags import *

with dominate.document() as outer_doc:
    with dominate.document() as inner_doc:
        p('meow')
    print(inner_doc)
print(outer_doc)

output

<!DOCTYPE html>
<html>
  <head>
    <title>Dominate</title>
  </head>
  <body>
    <p>meow</p>
  </body>
</html>
<!DOCTYPE html>
<html>
  <head>
    <title>Dominate</title>
  </head>
  <body>
    <html>
      <head>
        <title>Dominate</title>
      </head>
      <body>
        <p>meow</p>
      </body>
    </html>
  </body>
</html>
@Knio
Copy link
Owner

Knio commented Apr 3, 2022

No issue/problem/question stated and this looks working as intended

@Knio Knio closed this as completed Apr 3, 2022
@MarisaKirisame
Copy link
Author

@Knio
The tag represents the root of an HTML document. (from https://www.w3schools.com/tags/tag_html.asp)
so - i dont think it should nest, which is what is happening if you look at the output from the second occurence of
instead, I think a better design choice is, when a html root is entered/exited, it does not register itself as a child of what is on the context, breaking the chain.
roughly what happend is that
with dominate.document() as xxx: ...
should has no other effect then writing xxx.

This come up in real life, when I try to use dominate, as I try to create multiple webpage in a hirearchical order. I initially wrote a recursive function to do that, starting from the main entry, generating hyperlink on needed, but as dominate doesnt support that I have to rewrite it from a top-down to a bottom-up manner.

@Knio
Copy link
Owner

Knio commented Apr 5, 2022

i dont think it should nest

This project is not meant to be an html validator - i.e. it doesn't check that <td> is inside a <tr>, or that some tag has all the required attributes set, etc. and I don't want to special case any nodes. Also, I think nesting is legal (ok maybe only in an <embed> but I'm not going to check the whole spec)

with dominate.document() as xxx: ...
should has no other effect then writing xxx.

This is explicitly asking for it to be the context. Not doing so would break the expectations of the API. If you don't want the effect, why not just write xxx = dominate.document()?

If xxx = ... is still inside some outer context and causing problems:

but as dominate doesnt support that I have to rewrite it from a top-down to a bottom-up manner.

That seems like an excessive reaction, why don't you just subclass document to do what you want?

class mydoc(dominate.document):
    def _add_to_ctx(self): pass # don't add to contexts

With your example do what I think you want:

>>> with mydoc() as outer_doc:
...     with mydoc() as inner_doc:
...         p('meow')
...     print(inner_doc)
...
<dominate.tags.p at 1bb1a765160: 0 attributes, 1 child>
<!DOCTYPE html>
<html>
  <head>
    <title>Dominate</title>
  </head>
  <body>
    <p>meow</p>
  </body>
</html>
>>> print(outer_doc)
<!DOCTYPE html>
<html>
  <head>
    <title>Dominate</title>
  </head>
  <body></body>
</html>

@MarisaKirisame
Copy link
Author

Thx. the subclass solution is excatly what I want.

Repository owner locked and limited conversation to collaborators Jul 27, 2022
@Knio Knio converted this issue into discussion #163 Jul 27, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants