├── .editorconfig ├── .gitignore ├── .jshintrc ├── .travis.yml ├── README.md ├── index.js ├── lib ├── convert-tag-attributes.js ├── get-property-info │ └── htmldom │ │ ├── index.js │ │ ├── masks.js │ │ └── property-config.js ├── html-to-vdom.js ├── htmlparser-to-vdom.js ├── parse-html.js └── property-setters │ ├── attribute.js │ ├── index.js │ └── property.js ├── package.json └── test ├── html-to-vdom ├── index.js └── lib │ ├── convert-tag-attributes │ ├── attributes.js │ ├── decode.js │ ├── edge-cases.js │ ├── properties.js │ └── styles.js │ └── html-to-vdom │ └── index.js ├── mocha.opts └── support └── node.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = space 6 | indent_size = 4 7 | end_of_line = lf 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | // Settings 3 | "passfail" : false, // Stop on first error. 4 | "maxerr" : 100, // Maximum errors before stopping. 5 | 6 | 7 | // Predefined globals whom JSHint will ignore. 8 | "browser" : true, // Standard browser globals e.g. `window`, `document`. 9 | 10 | "node" : true, 11 | "rhino" : false, 12 | "couch" : false, 13 | "wsh" : false, // Windows Scripting Host. 14 | 15 | "jquery" : true, 16 | "prototypejs" : false, 17 | "mootools" : false, 18 | "dojo" : false, 19 | 20 | "predef" : [ // Extra globals. 21 | "google", // Google API variable. 22 | "Android", // PhoneGap variable. 23 | "describe", // Testing 24 | "beforeEach", 25 | "afterEach", 26 | "it", 27 | "sinon", 28 | "should", 29 | "Promise" 30 | ], 31 | 32 | 33 | // Development. 34 | "debug" : false, // Allow debugger statements e.g. browser breakpoints. 35 | "devel" : true, // Allow development statements e.g. `console.log();`. 36 | 37 | 38 | // EcmaScript 5. 39 | // "es5" : true, // Allow EcmaScript 5 syntax. 40 | "strict" : false, // Require `use strict` pragma in every file. 41 | "globalstrict" : false, // Allow global "use strict" (also enables 'strict'). 42 | 43 | 44 | // The Good Parts. 45 | "asi" : false, // Tolerate Automatic Semicolon Insertion (no semicolons). 46 | "laxbreak" : true, // Tolerate unsafe line breaks e.g. `return [\n] x` without semicolons. 47 | "bitwise" : false, // Prohibit bitwise operators (&, |, ^, etc.). 48 | "boss" : false, // Tolerate assignments inside if, for & while. Usually conditions & loops are for comparison, not assignments. 49 | "curly" : true, // Require {} for every new block or scope. 50 | "eqeqeq" : true, // Require triple equals i.e. `===`. 51 | "eqnull" : false, // Tolerate use of `== null`. 52 | "evil" : false, // Tolerate use of `eval`. 53 | "expr" : false, // Tolerate `ExpressionStatement` as Programs. 54 | "forin" : false, // Tolerate `for in` loops without `hasOwnPrototype`. 55 | "immed" : true, // Require immediate invocations to be wrapped in parens e.g. `( function(){}() );` 56 | "latedef" : true, // Prohibit variable use before definition. 57 | "loopfunc" : false, // Allow functions to be defined within loops. 58 | "noarg" : true, // Prohibit use of `arguments.caller` and `arguments.callee`. 59 | "regexp" : true, // Prohibit `.` and `[^...]` in regular expressions. 60 | "regexdash" : false, // Tolerate unescaped last dash i.e. `[-...]`. 61 | "scripturl" : true, // Tolerate script-targeted URLs. 62 | "shadow" : false, // Allows re-define variables later in code e.g. `var x=1; x=2;`. 63 | "supernew" : false, // Tolerate `new function () { ... };` and `new Object;`. 64 | "undef" : true, // Require all non-global variables be declared before they are used. 65 | 66 | 67 | // Personal styling prefrences. 68 | "quotmark" : "single", 69 | "indent" : 4, // Consistent indentation (requires jshint directive on files) 70 | "newcap" : true, // Require capitalization of all constructor functions e.g. `new F()`. 71 | "noempty" : true, // Prohibit use of empty blocks. 72 | "nonew" : true, // Prohibit use of constructors for side-effects. 73 | "nomen" : false, // Prohibit use of initial or trailing underscores in names. 74 | "onevar" : false, // Allow only one `var` statement per function. 75 | "plusplus" : false, // Prohibit use of `++` & `--`. 76 | "sub" : false, // Tolerate all forms of subscript notation besides dot notation e.g. `dict['key']` instead of `dict.key`. 77 | "trailing" : true, // Prohibit trailing whitespaces. 78 | "white" : false // Check against strict whitespace and indentation rules. 79 | } 80 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - "0.10" 5 | - "0.11" 6 | - "0.12" 7 | - "iojs" 8 | - "4.2" 9 | - "5.4" 10 | - "5.5" 11 | - "node" 12 | script: "NODE_ENV=test ./node_modules/.bin/istanbul cover \ 13 | ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && \ 14 | cat ./coverage/lcov.info | ./node_modules/.bin/coveralls --verbose" 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | html-to-vdom [![Build Status](https://travis-ci.org/TimBeyer/html-to-vdom.svg?branch=master)](https://travis-ci.org/TimBeyer/html-to-vdom) [![Coverage Status](https://coveralls.io/repos/TimBeyer/html-to-vdom/badge.svg?branch=master&service=github)](https://coveralls.io/github/TimBeyer/html-to-vdom?branch=master) [![bitHound Score](https://www.bithound.io/github/TimBeyer/html-to-vdom/badges/score.svg)](https://www.bithound.io/github/TimBeyer/html-to-vdom) 2 | ============ 3 | 4 | About 5 | ----- 6 | 7 | This is yet another library to convert HTML into a [vtree](https://github.com/Matt-Esch/vtree). 8 | It's used in conjunction with [virtual-dom](https://github.com/Matt-Esch/virtual-dom) to convert template based views into `virtual-dom` views. 9 | 10 | Note 11 | ---- 12 | 13 | As of v0.7.0, HTML attribute parsing has been improved by using [React's list of attributes and properties](https://github.com/facebook/react/blob/c265504fe2fdeadf0e5358879a3c141628b37a23/src/renderers/dom/shared/HTMLDOMPropertyConfig.js) 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. 14 | 15 | As of v0.6.0, converting sibling nodes without an enclosing parent tag returns an array of type `VNode` instead of throwing an error 16 | 17 | As of v0.5.1, `html-to-vdom` no longer supports browsers without a full ES5 implementation. 18 | 19 | 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. 20 | 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. 21 | 22 | Usage 23 | ----- 24 | 25 | ```javascript 26 | var VNode = require('virtual-dom/vnode/vnode'); 27 | var VText = require('virtual-dom/vnode/vtext'); 28 | 29 | var convertHTML = require('html-to-vdom')({ 30 | VNode: VNode, 31 | VText: VText 32 | }); 33 | 34 | var html = '
Foobar
'; 35 | 36 | var vtree = convertHTML(html); 37 | var createElement = require('virtual-dom/create-element'); 38 | var el = createElement(vtree); 39 | document.body.appendChild(el); 40 | ``` 41 | 42 | #### Specifying a key 43 | 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. 44 | 45 | ```javascript 46 | var convertHTML = require('html-to-vdom')({ 47 | VNode: VNode, 48 | VText: VText 49 | }); 50 | 51 | convertHTML({ 52 | getVNodeKey: function (attributes) { 53 | return attributes.id; 54 | } 55 | }, '
'); 56 | ``` 57 | 58 | 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: 59 | 60 | ```javascript 61 | var convertHTMLWithKey = convertHTML.bind(null, { 62 | getVNodeKey: function (attributes) { 63 | return attributes.id; 64 | } 65 | }); 66 | 67 | convertHTMLWithKey('
'); 68 | ``` 69 | 70 | Credits 71 | ------- 72 | 73 | Thanks to: 74 | * [@nkzawa](https://github.com/nkzawa) for making me aware of attribute lowercasing problems and submitting a PR to fix it 75 | * [@mattferrin](https://github.com/mattferrin) for noticing that promises could be removed from the API and contributing a PR to do so 76 | * [@tiagorg](https://github.com/tiagorg) for contributing a PR for style attribute parsing 77 | * [@dariusriggins](https://github.com/dariusriggins) for adding VNode key support 78 | * [@jsyang](https://github.com/jsyang) for removing the `lodash` dependency for a leaner build and [improved performance](http://jsperf.com/html-to-vdom-lodash-vs-native) 79 | * [@bregenspan](https://github.com/bregenspan) for making the dataset conversion standards-compliant 80 | * [@jesseditson](https://github.com/jesseditson) for adding `'; 125 | var converted = convertHTML(html); 126 | var script = converted.children[0]; 127 | should.exist(script); 128 | script.tagName.should.eql('script'); 129 | script.children.length.should.eql(1); 130 | script.children[0].text.should.eql('alert("bar!");'); 131 | }); 132 | }); 133 | 134 | describe('when converting HTML containing a style tag', function () { 135 | it('converts to a virtualdom node', function () { 136 | var html = '
'; 137 | var converted = convertHTML(html); 138 | var script = converted.children[0]; 139 | should.exist(script); 140 | script.tagName.should.eql('style'); 141 | script.children.length.should.eql(1); 142 | script.children[0].text.should.eql('h1 {color:red;} p {color:blue;} '); 143 | }); 144 | }); 145 | 146 | describe('when converting HTML containing CDATA', function () { 147 | it('returns an empty string instead (cdata is unsupported)', function () { 148 | var html = [''].join(' '); 155 | var converted = convertHTML(html); 156 | converted.text.should.eql(''); 157 | }); 158 | }); 159 | 160 | describe('when converting HTML containing a directive', function () { 161 | it('returns an empty string instead (directives are unsupported)', function () { 162 | var html = ''; 163 | var converted = convertHTML(html); 164 | converted.text.should.eql(''); 165 | }); 166 | }); 167 | 168 | describe('when converting HTML containing a comment', function () { 169 | it('returns an empty string instead (comments are unsupported)', function () { 170 | var html = '
'; 171 | var converted = convertHTML(html); 172 | var comment = converted.children[0]; 173 | comment.text.should.eql(''); 174 | }); 175 | }); 176 | }); 177 | -------------------------------------------------------------------------------- /test/mocha.opts: -------------------------------------------------------------------------------- 1 | --reporter spec 2 | --require test/support/node 3 | --check-leaks 4 | --recursive 5 | -------------------------------------------------------------------------------- /test/support/node.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var chaiAsPromised = require('chai-as-promised'); 3 | var should = chai.should(); // jshint ignore:line 4 | 5 | chai.use(chaiAsPromised); 6 | 7 | global.should = should; 8 | --------------------------------------------------------------------------------