Coder Social home page Coder Social logo

html-to-vdom's Introduction

html-to-vdom Build Status Coverage Status bitHound Score

About

This is yet another library to convert HTML into a vtree. It's used in conjunction with virtual-dom to convert template based views into virtual-dom views.

Note

As of v0.7.0, HTML attribute parsing has been improved by using React's list of attributes and properties to decide what to set on the VNode. This means that you should experience better compatibility and full support for HTML5. Custom attributes are also no longer lower cased automatically. Inline SVG is not yet supported, but will be worked on for the next version.

As of v0.6.0, converting sibling nodes without an enclosing parent tag returns an array of type VNode instead of throwing an error

As of v0.5.1, html-to-vdom no longer supports browsers without a full ES5 implementation.

As of v0.3.0, the VNode and VText classes need to be passed in during library initialization from the virtual-dom module you are using.
This is to reduce incompatibilties you might have due to depending on a different version of virtual-dom than the one this library would use.

Usage

var VNode = require('virtual-dom/vnode/vnode');
var VText = require('virtual-dom/vnode/vtext');

var convertHTML = require('html-to-vdom')({
    VNode: VNode,
    VText: VText
});

var html = '<div>Foobar</div>';

var vtree = convertHTML(html);
var createElement = require('virtual-dom/create-element');
var el = createElement(vtree);
document.body.appendChild(el);

Specifying a key

In order for virtual-dom to detect moves it needs a key. To specify your own custom method of finding a key pass in a method that takes the current tag and returns the key.

var convertHTML = require('html-to-vdom')({
    VNode: VNode,
    VText: VText
});

convertHTML({
    getVNodeKey: function (attributes) {
        return attributes.id;
    }
}, '<div id="foo"></div>');

If you have a single key method you can also pass the options first, allowing you to create a single bound method for all key lookups:

var convertHTMLWithKey = convertHTML.bind(null, {
    getVNodeKey: function (attributes) {
        return attributes.id;
    }
});

convertHTMLWithKey('<div id="foo"></div>');

Credits

Thanks to:

  • @nkzawa for making me aware of attribute lowercasing problems and submitting a PR to fix it
  • @mattferrin for noticing that promises could be removed from the API and contributing a PR to do so
  • @tiagorg for contributing a PR for style attribute parsing
  • @dariusriggins for adding VNode key support
  • @jsyang for removing the lodash dependency for a leaner build and improved performance
  • @bregenspan for making the dataset conversion standards-compliant
  • @jesseditson for adding <script> and <style> tag support and contributing to achieve better attribute/property handling

html-to-vdom's People

Contributors

timbeyer avatar themindfuldev avatar jesseditson avatar mattferrin avatar delapouite avatar jsyang avatar khirayama avatar

Watchers

steve avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.