unified

Project:syntax-tree/hast-util-classnames

Package:hast-util-classnames@1.0.1

  1. Dependencies:1·Dependents:0
  2. hast utility to merge class names together
  1. util 105
  2. utility 105
  3. html 100
  4. unist 90
  5. hast 51
  6. hast-util 40

hast-util-classnames

Build Coverage Downloads Size Sponsors Backers Chat

hast utility to merge class names together.

Install

npm:

npm install hast-util-classnames

Use

var h = require('hastscript')
var classnames = require('hast-util-classnames')

console.log(classnames('alpha bravo', {bravo: false}, [123, 'charlie']))

var node = h('p.alpha', 'Hi!')
console.log(classnames(node, 'bravo', ['charlie', {delta: false, echo: 1}]))

Yields:

['123', 'alpha', 'charlie']
{
  type: 'element',
  tagName: 'p',
  properties: {className: ['alpha', 'bravo', 'charlie', 'echo']},
  children: [{type: 'text', value: 'Hi!'}]
}

API

classnames(node, …conditionals)

Utility to merge classes.

If the first argument is a node it should be an element. All conditionals are merged with the current class names and with each other, and then set on the element. Finally, node is returned.

classnames(…conditionals)

If the first argument is not a node, all conditionals are merged with each other, and then the resulting array is returned.

Conditional

A value that is either:

Security

Classes are typically not harmful, however, if someone were able to inject classes, it could mean that user-provided content looks like official content, which may cause security problems due to impersonation. Either do not use user input in classnames or use hast-util-sanitize to clean the tree.

Contribute

See contributing.md in syntax-tree/.github for ways to get started. See support.md for ways to get help.

This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.

License

MIT © Titus Wormer