String To Html
A very simple browser script to parse html strings into document fragments.
Usage
Install the package with yarn or npm.
yarn add string-to-html # Or use npm npm install string-to-html
If you want to use this directly in your browser download and include the dist/string-to-html.js
file in a script tag on your page.
Example usage:
; // Omit this line if you use the es5 version. const people = 'Alfred' 'Jodocus' 'Kwak' 'Dolf'; const htmlString = ` <ul> </ul>`; const fragment = ;documentbody;
When & why?
For when you want to use vanilla JS and parse html strings into a DOM fragments. It's saves a time and is much easier than using document.createFragment()
and appending document.createElement()
as child elements.
Why a domfragment instead of innerHTML ? Because you can do DOM operations on the fragment before inserting in into the document.
Building and Testing
- Install depedencies with
yarn install
. - To bundle and transpile run
yarn build
this will output a transpiled, common js and es2015 module version ofstring-to-html.js
to thedist/
directory. - Run tests with
yarn test
or opentest.html
in your browser after runningyarn build-test
.