UNPKG

@wmfs/tag-builder

Version:

A simple tag builder which isn't specific to any markup language

39 lines (32 loc) 791 B
const defaults = require('lodash.defaults') class TagNode { constructor (name, options = {}) { this.options = defaults(options || {}, { includeClosingTag: true }) this.name = name this.tagContent = null this.attributes = [] this.childTags = [] } content (tagContent) { if (tagContent !== undefined) { this.tagContent = tagContent } return this } addAttribute (name, value) { if (value !== undefined) { if (value === null) { this.attributes.push(name) } else { this.attributes.push(`${name}="${value}"`) } } return this } addChildTag (tagName, options) { const tagNode = new TagNode(tagName, options) this.childTags.push(tagNode) return tagNode } } module.exports = TagNode