2.0.0-betha • Published 11 months ago

js-to-html v2.0.0-betha

Weekly downloads
129
License
MIT
Repository
github
Last release
11 months ago

js-to-html

Create HTML text from JS object

extending npm-version downloads build coverage climate dependencies qa-control

language: English also available in: Spanish

Install

$ npm install js-to-html

API

html.TAGNAME(attributes, content)

Returns an Html object with TAGNAME, attributes and content.

content could be

  • a string expression
  • an array of children. Each child could be
    • a string expression
    • an Html object

attributes must be a plain object. Each property of the object will be an html attribute (example: {colspan:3, id:"abc"}). Some attributes names are reserved words, you can use them with the same name (example: {class:'examples'}). Some attributes (like class) could contain lists (example: {class:['examples', 'lists']}).

Html.toHtmlText(opts)

Returns an Html Text

optvalue
prettyreturns a pretty and indented text

Example

var html = require('js-to-html').html;

console.log(
    html.div(
        {'class':'the_class', id:'47'},
        [
            html.p('First paragraph'),
            html.p('Second paragraph'),
        ]
    ).toHtmlText({pretty:true})
)

/* logs:
<div class=the_class id=47>
  <p>First paragraph</p>
  <p>Second paragraph</p>
</div>
*/

Html.toHtmlDoc(opts)

Same as Html.toHtmlText(opts) but returns doctype in the first line and completes with con HTML, HEAD, BODY and TITLE elements:

var html = require("js-to-html").html;

console.log(
    html.img({src:'photo.png'}).toHtmlDoc({title:"my photo", pretty:true})
)

/*
<!doctype html>
<html>
  <head>
    <title>my photo</title>
  </head>
  <body>
    <img src=photo.png>
  </body>
</html>
*/
optvalue
prettyreturns a pretty and indented text
incompletedo not complete with html, head y body tags
titletext title

Using with DOM in client-side

All html objects have a create method that build a DOM Element ready to append to a existing one. create builds the element and inside elements too.

var html = jsToHtml.html;

document.body.appendChild(
    html.div([
        html.h1('Log in'),
        html.div([
            html.input({name: 'user', placeholder:'user'}),
            html.input({name: 'pass', type: 'password'})
        ])
    ]).create()
);

Special behavior

typenameuse
functionhtml._textsimple text (like createTextNode)
functionhtml._commenthtml comment (like < !-- ... -- >)
attributeclassListfor a class name list (this module rejects class with spaces like {"class": "una otra separada por espacio"} )

Insecure mode

html.insecureModeEnabled = true;
console.log(html.div({id:'this'}, html.includeHtml('<svg xml:....> </svg>')));

Notes

  • In the future it will be smart to handle style attribute like {style:{color: "blue", background: "none"}}

Tests with real devices

NPM versionDeviceOSnavobs
0.9.1Samsung Galaxy Note 4Android 6.0.1Chrome Mobile 44.0.2403
0.9.1Blue Vivo Air LTEAndroid 5.0.2Chrome Mobile 50.0.2661
0.9.1Samsung Galaxy S3Android 4.3.0Android 4.3.0
0.9.1HTC DesireAndroid 2.2.2Android 2.2.2polyfill:classList
0.9.1iPad mini RetinaiOS 8.4.0Mobile Safari 8.0.0
0.9.1VMWareWinXPIE 8.0.0polyfill:many

License

MIT

2.0.0-betha

11 months ago

1.3.2

2 years ago

1.3.1

2 years ago

1.3.0

2 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.1.0

3 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.9.3

7 years ago

0.9.2

8 years ago

0.9.1

8 years ago

0.9.0

8 years ago

0.8.3

8 years ago

0.8.2

8 years ago

0.8.1

8 years ago

0.8.0

8 years ago

0.7.0

8 years ago

0.6.4

8 years ago

0.6.3

8 years ago

0.6.2

8 years ago

0.6.1

8 years ago

0.5.1

8 years ago

0.5.0

8 years ago

0.4.2

9 years ago

0.4.1

9 years ago

0.4.0

9 years ago

0.3.4

9 years ago

0.3.3

9 years ago

0.3.2

9 years ago

0.3.1

9 years ago

0.3.0

9 years ago

0.2.11

9 years ago

0.2.10

9 years ago

0.2.9

9 years ago

0.2.8

9 years ago

0.2.7

9 years ago

0.2.6

9 years ago

0.2.4

9 years ago

0.2.3

9 years ago

0.2.2

9 years ago

0.2.1

9 years ago

0.2.0

9 years ago

0.1.3

9 years ago

0.1.2

9 years ago

0.1.1

9 years ago