diffhtml/lite: `innerHTML(someNode, "only text")` fails
justjake opened this issue · 3 comments
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
.
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'));
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 😃
-
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.