Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

domm

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

domm

Template literal tags for creating DOM nodes.

  • 0.0.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-50%
Maintainers
1
Weekly downloads
 
Created
Source

What

Domm is a small library to create safe HTML and DOM nodes using ES6 template literals.

Why

It's lightweight and relies on native JS functionality. For anything non-trivial, this doesn't compete with a real template language like pug, or a library like react.

Installation

npm install --save domm

Usage

Basic

const D = require('domm');

D.html`<h1>Hello World!</h1>`;
// => '<h1>Hello World!</h1>'

D.dom`<h1>Hello World!</h1>`;
// => new instance of <HTMLHeadingElement>

D.dom`
    <a href="/">Home</a>
    <a href="/about">About</a>
`;
// => new instance of <NodeList> containing 2 links

Variable interpolation

// String
const headingText = 'Houses';

// Object
const headingAttrs = { class: 'heading-main' };

// Array
const names = ['Lannister', 'Stark', 'Tyrell'];

// Element
const backLink = document.querySelector('.js-link-back');

D.html`
    <div>
        <h1 ${headingAttrs}>${headingText}</h1>
        <ul>
            ${names.map(names => D.html`<li>${name}</li>`)}
        </ul>
        ${backLink}
    </div>
`;
// =>
// <div>
//     <h1 class="heading-main">Houses</h1>
//     <ul>
//         <li>Lannister</li><li>Stark</li><li>Tyrell</li>
//     </ul>
//     <a class="js-link-back" href="/">Back to home</a>
// </div>

Note that the list of names required D.html to be used on the inner template literal. Without this, the HTML in the list would have been escaped:

"&lt;li&gt;Lannister&lt;/li&gt;&lt;li&gt;Stark&lt;/li&gt;&lt;li&gt;Tyrell&lt;/li&gt;"

All strings that are interpolated are escaped in this way. To dangerously escape a variable that is not defined via a template literal, use the D constructor:

const title = someExternalFunctionForGettingTitle();

D.dom`<h1>${new D(title)}</h1>`;

FAQs

Package last updated on 02 Dec 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc