'use strict'; var ok = require('assert').ok; var HtmlAttribute = require('./HtmlAttribute'); var Node = require('./Node'); class HtmlAttributeCollection { constructor(attributes) { this.all = []; this.lookup = {}; if (attributes) { if (Array.isArray(attributes)) { attributes.forEach((attr) => { this.addAttribute(attr); }); } else { for (var attrName in attributes) { if (attributes.hasOwnProperty(attrName)) { let attrValue = attributes[attrName]; let attrDef; if (typeof attrValue === 'object' && !(attrValue instanceof Node)) { attrDef = attrValue; attrDef.name = attrName; } else { attrDef = { name: attrName, value: attrValue }; } this.addAttribute(attrDef); } } } } } addAttribute(newAttr) { if (arguments.length === 2) { let name = arguments[0]; let expression = arguments[1]; newAttr = new HtmlAttribute(name, expression); } else if (!HtmlAttribute.isHtmlAttribute(newAttr)) { newAttr = new HtmlAttribute(newAttr); } var name = newAttr.name; if (this.lookup.hasOwnProperty(name)) { for (var i=0; i 0; } getAttribute(name) { return this.lookup[name]; } setAttributeValue(name, value) { var attr = this.getAttribute(name); if (attr) { attr.value = value; } else { this.addAttribute({ name: name, value: value }); } } getAttributes() { return this.all; } toJSON() { return this.all; } toString() { return JSON.stringify(this.all); } } module.exports = HtmlAttributeCollection;