Source body with only text

#6 ยท closed ยท 3 comments

View on GitHub โ†—

eydun

I like this library ๐Ÿ‘ Here are my test-files: **Main-page:** ```html <!DOCTYPE html> <html lang="en"> <head> <script src="https://cdn.jsdelivr.net/npm/i-html-element/i-html.min.js" defer></script> <script type="module" src="https://unpkg.com/[email protected]/invoker.js"></script> <title>Here comes the sun</title> <style> :root { --background: #fff9db !important; --color: #d9480f !important; --link-color: #364fc7 !important; --link-visited-color: #5f3dc4 !important; --border-color: #e67700 !important; } html { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji"; max-width: 70ch; margin: auto; line-height: 1.5; font-size: 1.125em; } .demo { border: 1px solid var(--border-color); border-radius: 8px; max-width: 70ch; min-height: 1ch; padding: 1rem; } </style> </head> <body> <main> <div class="demo"> <i-html src="the_sun.html" insert="append"> <div>Here comes the sun</div> </i-html> </div> </main> </body> </html> ``` **Inventory-page (the_sun.html):** ```html <!DOCTYPE html> <html lang="en"> <head> <title>Response</title> </head> <body> And I say, "It's alright" </body> </html> ``` This do not work (nothing is appended). But if I enclose the content in the "Inventory"-page in some element, then it works, e.g. `<div>And I say, "It's alright"</div>`. Is this intended behavior?

Comments

keithamus

Thanks for trying it out! This is kind of "works as intended". When fetching an HTML document the default selector is `body>*` (read more at ["Targeting the response"](https://www.keithcirkel.co.uk/i-html/#targeting-response)). Of course this makes it impossible to select text nodes, but I think that's probably a good thing. It means contrived examples can easily fail, but I think in practice it's really rare to _want_ to select a text node. For example in "real world" code you'd likely not respond with an HTML document that just had a text-node in the body. As you mention, wrapping in a `<div>` resolves the issue. Perhaps there's a good reason to append just the next node? I imagine almost all (if not all) cases would arguably made better by wrapping an element - which allow for example, more flexible styling with CSS. If you disagree and think there's a _really good_ reason to append just the text node let me know and we'll discuss it further, but meanwhile I'll close this as not planned.

eydun

Thank you for the reply. I agree, pure text-nodes "pollute" the logic, and are not need-to-have. While testing, it took me a while to figure out why I did not receive a response, because of no wrapper in the source. That's why I mentioned it.

keithamus

If you feel comfrotable doing so, it'd be great if you could make a PR explaining the problem and the workaround, somewhere in the docs page where you think it would fit. Given your experience here it would be great to get your input into the docs for this!