UNPKG

dotnode

Version:

.NET-like MVC framework for Node.js

70 lines (45 loc) 1.6 kB
var extend = require('xtend'); var TagBuilder = function (tagName) { this._tagName = tagName; this._innerHtml = ''; this._attributes = {}; }; // #region public methods TagBuilder.prototype.setAttribute = function (key, value) { this._attributes[key] = value; }; TagBuilder.prototype.mergeAttributes = function (attributes) { extend(this._attributes, attributes || {}); }; TagBuilder.prototype.addClass = function (cssClass) { if (typeof this._attributes.class != 'string') { this._attributes.class = cssClass; } else { this._attributes.class += ' ' + cssClass; } this._attributes.class = this._attributes.class.replace(/\s+/g,' ').trim(); }; TagBuilder.prototype.setInnerHtml = function (html) { this._innerHtml = html; }; TagBuilder.prototype.toString = function (isVoidTag) { var attributesString = this._getAttributesString(this._attributes); if (isVoidTag) { return '<' + this._tagName + attributesString + '/>'; } else { return '<' + this._tagName + attributesString + '>' + this._innerHtml + '</' + this._tagName + '>'; } }; // #endregion // #region private methods TagBuilder.prototype._getAttributesString = function (attributes) { var attributesString = ''; for (var key in attributes) { if (attributes.hasOwnProperty(key)) { attributesString += ' ' + key + '="' + attributes[key] + '"'; } } return attributesString; }; // #endregion module.exports = TagBuilder;