HTML5 Tag Generator

0.3.0 · abandoned · verified Sun Apr 19

html5-tag is a lightweight JavaScript utility designed for programmatically generating HTML5 tags and handling their attributes. Its primary function is to create HTML strings, correctly managing void elements (e.g., `<br>`, `<input>`) by omitting their closing tags, and automatically escaping attribute values to prevent XSS vulnerabilities. The current stable version is 0.3.0, released in 2016, indicating a very slow release cadence and likely an abandoned status. Unlike full-fledged templating engines or JSX, html5-tag focuses solely on simple string concatenation for server-side HTML generation, making it suitable for scenarios where a minimal, dependency-free solution for basic HTML element creation is preferred. A key differentiator is its automatic handling of attribute escaping and void elements, abstracting away common HTML syntax pitfalls.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to import and use the `tag` function to create various HTML elements, including void elements, attribute escaping, and the default 'div' behavior.

import tag from 'html5-tag';

// Basic usage: tag name, attributes object, and child content
const link = tag('a', { href: 'https://example.com', target: '_blank' }, 'Visit Example');
console.log(link);
// Expected: <a href="https://example.com" target="_blank">Visit Example</a>

// Void elements do not receive a closing tag
const linebreak = tag('br');
console.log(linebreak);
// Expected: <br>

// Attributes are automatically escaped
const divWithTitle = tag('div', { title: 'It\'s "a" test & <more>' }, 'Escaped Title');
console.log(divWithTitle);
// Expected: <div title="It&#39;&quot;a&quot; test &amp; &lt;more&gt;">Escaped Title</div>

// If tag is omitted, it defaults to 'div' (since v0.3.0)
const defaultDiv = tag({ class: 'container' }, 'Content goes here');
console.log(defaultDiv);
// Expected: <div class="container">Content goes here</div>

view raw JSON →