A client-side DOM to vdom parser based on DOMParser API, compatible with virtual-dom.
virtual-dom with progressive enhancement in mind: we use server-side rendering to achieve good first-page performance then re-attach vdom rendering client-side.
This means we need a solid implementation of html to vdom parser, while there are existing implementations, we would like a solution that's well-tested and make use of existing browser API.
vdom-parser, a small module that bridges the gap between server-side and client-side rendering.
- Use DOMParser for better performance and smaller filesize (around 10KB when minified).
- No direct dependency, peer-dependent on
virtual-dommajor version (v2 currently).
- VNode key support to minimize re-rendering.
- 100% test coverage, covering common usage such as inline svg, style and script tags (unfortunately
phantomjsstill lacks HTML support in DOMParser, so our travis coverage remains incomplete).
- For DOM element input, all modern browsers are supported.
- For HTML string input, your browser need to support HTML input on DOMParser API.
- Using DOMParser polyfill you can make this works on older browsers and phantomjs, see test cases comment on potential gotcha, and always trim your string before input.
- For IE9 and HTML string input, using polyfill we can get most nodes under
document.bodyto work, but anything else will throw
Invalid target element for this operation. Maybe better to cut the mustard and use server-side rendering.
- For Opera 12, be aware that their DOM attributes are not namespaced, it doesn't affect
virtual-domdiffing or patching, but don't expect
- To minimize client-side patching, you can output server-rendered HTML minified (instead of formatted with newlines and indents), this removes unnecessary text node from your generated vdom so
virtual-domdon't have to remove or mutate them.
npm install vdom-parser --save
// server-side rendervar parser = ;var nodeCache = documentbody;var vdomCache = ;// client-side rendervar vdom = ;// diff and patchvar patches = ;;
See test cases for more examples.
VText, see virtual-dom documentation.
Should be a DOM Element or HTML String.
Note: for string input,
<head> will return empty VText as we only support nodes under
document.head. DOM element input doesn't have this limit.
Optional attribute name (eg. 'id' or 'data-id') for VNode key lookup, exposing VNode key as html attribute to minimize client-side patching.