diff --git a/lib/token.js b/lib/token.js index a0cccc9..90daad5 100644 --- a/lib/token.js +++ b/lib/token.js @@ -146,4 +146,38 @@ Token.prototype.attrPush = function attrPush(attrData) { }; +/** + * Token.attrReplace(name, value) + * + * Replace all attributes with name `name` with one with the value `attrData` + **/ +Token.prototype.attrReplace = function attrReplace(name, value) { + var found; + + var attrs = this.attrs; + + if (attrs) { + // modify the existing attr is possible + for (var i = 0; i < attrs.length; i++) { + if (attrs[i][0] === name) { + if (!found) { + attrs[i][1] = value; + found = true; + } else { + // remove extra attrs with same name + attrs.splice(i, 1); + i--; + } + } + } + + // add a new attribute with such name if none was found + if (!found) { + attrs.push([name, value]); + } + } else { + this.attrs = [ [name, value] ]; + } +}; + module.exports = Token;