Browse Source

New .attrJoin() & .attrSet() methods in Token

pull/186/head
Vitaly Puzrin 9 years ago
parent
commit
18f1531024
  1. 34
      lib/token.js
  2. 39
      test/misc.js

34
lib/token.js

@ -146,4 +146,38 @@ Token.prototype.attrPush = function attrPush(attrData) {
};
/**
* Token.attrSet(name, value)
*
* Set `name` attribute to `value`. Override old value if exists.
**/
Token.prototype.attrSet = function attrSet(name, value) {
var idx = this.attrIndex(name),
attrData = [ name, value ];
if (idx < 0) {
this.attrPush(attrData);
} else {
this.attrs[idx] = attrData;
}
};
/**
* Token.attrJoin(name, value)
*
* Join value to existing attribute via space. Or create new attribute if not
* exists. Useful to operate with token classes.
**/
Token.prototype.attrJoin = function attrJoin(name, value) {
var idx = this.attrIndex(name);
if (idx < 0) {
this.attrPush([ name, value ]);
} else {
this.attrs[idx][1] = this.attrs[idx][1] + ' ' + value;
}
};
module.exports = Token;

39
test/misc.js

@ -303,3 +303,42 @@ describe('smartquotes', function () {
});
});
describe('Token attributes', function () {
it('.attrJoin', function () {
var md = markdownit();
var tokens = md.parse('```'),
t = tokens[0];
t.attrJoin('class', 'foo');
t.attrJoin('class', 'bar');
assert.strictEqual(
md.renderer.render(tokens, md.options),
'<pre><code class="foo bar"></code></pre>\n'
);
});
it('.attrSet', function () {
var md = markdownit();
var tokens = md.parse('```'),
t = tokens[0];
t.attrSet('class', 'foo');
assert.strictEqual(
md.renderer.render(tokens, md.options),
'<pre><code class="foo"></code></pre>\n'
);
t.attrSet('class', 'bar');
assert.strictEqual(
md.renderer.render(tokens, md.options),
'<pre><code class="bar"></code></pre>\n'
);
});
});

Loading…
Cancel
Save