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

diffhtml/lite: innerHTML(someNode, "only text") fails #267

Closed
justjake opened this issue Apr 13, 2022 · 3 comments
Closed

diffhtml/lite: innerHTML(someNode, "only text") fails #267

justjake opened this issue Apr 13, 2022 · 3 comments

Comments

@justjake
Copy link
Contributor

I am trying to use innerHTML to update a DOM node to contain the given text. With innerHTML, this fails with the following DOMException:

DOMException: Failed to execute 'createElement' on 'Document': The tag name provided ('some text') is not a valid name.

Browser: Chrome
Package: diffhtml@1.0.0-beta.27
Import: diffhtml/lite

This works fine with diffhtml.

@tbranyen
Copy link
Owner

tbranyen commented Apr 14, 2022

The lite version does not include an HTML parser, so you will need to precompile the string ahead of time using the Babel plugin. Alternatively, you can use createTree to specify the tree.

innerHTML(someNode, createTree('#text', 'Some text'));

@justjake
Copy link
Contributor Author

justjake commented Apr 14, 2022

Alright, that makes some sense to me.

My use-case for diffhtml is to update a HTMLDivElement's innerHTML with either a static HTML string (which I am currently assigning to my node's node.innerHTML directly), or a ReactElement produced by vanilla React 16ish. Would the cheapest way to go about doing this be to use diffhtml/lite plus the React VDom conversion code in https://github.com/tbranyen/diffhtml/blob/master/packages/diffhtml-react-compat/lib/index.js ?

I don't need synthetic events, components, etc - what would you suggest as a direction for me? Is there another library that you're aware of that will diff actual DOM nodes against an innerHTML string and/or a VDom tree? Your library was the only one I found from some cursory Googling.

The other thing on my wishlist is a way to turn off MutationObserver, since I will only call diffhtml.innerHTML on nodes I know to be dirty.

Thanks 😃

@tbranyen
Copy link
Owner

  • Using strings will require parsing into some kind of object structure, so lite won't work for that, you'll need the parser from the core build. To support React elements and not components, you could do a transformation to put them into the correct shape: { nodeName: string, attributes: object, childNodes: array }.

  • As for diffing actual DOM nodes, I'm not entirely sure, maybe morphdom could aid your search: https://github.com/patrick-steele-idem/morphdom.

  • We don't have a way to turn off the mutation observer atm, but it's possible to implement that.

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

2 participants