From 87c215b2daa42315beb89f04d9f42f97dbe24ef7 Mon Sep 17 00:00:00 2001 From: Ilya Kantor Date: Thu, 31 Dec 2015 21:07:08 +0300 Subject: [PATCH] Add attrReplace A function to replace attr value for plugins. --- lib/token.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) 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;