Skip to content

Commit

Permalink
utils.getAttrs: remove unused end parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
arve0 committed May 20, 2018
1 parent 3b650c2 commit 2f879aa
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
18 changes: 9 additions & 9 deletions patterns.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = options => ([
transform: (tokens, i) => {
let token = tokens[i];
let start = token.info.lastIndexOf(options.leftDelimiter);
let attrs = utils.getAttrs(token.info, start, null, options);
let attrs = utils.getAttrs(token.info, start, options);
utils.addAttrs(attrs, token);
token.info = utils.removeDelimiter(token.info, options);
}
Expand Down Expand Up @@ -57,7 +57,7 @@ module.exports = options => ([
let token = tokens[i].children[j];
let endChar = token.content.indexOf(options.rightDelimiter);
let attrToken = tokens[i].children[j - 1];
let attrs = utils.getAttrs(token.content, 0, null, options);
let attrs = utils.getAttrs(token.content, 0, options);
utils.addAttrs(attrs, attrToken);
if (token.content.length === (endChar + 1)) {
tokens[i].children.splice(j, 1);
Expand Down Expand Up @@ -91,7 +91,7 @@ module.exports = options => ([
transform: (tokens, i) => {
let token = tokens[i + 2];
let tableOpen = utils.getMatchingOpeningToken(tokens, i);
let attrs = utils.getAttrs(token.content, 0, null, options);
let attrs = utils.getAttrs(token.content, 0, options);
// add attributes
utils.addAttrs(attrs, tableOpen);
// remove <p>{.c}</p>
Expand Down Expand Up @@ -121,7 +121,7 @@ module.exports = options => ([
transform: (tokens, i, j) => {
let token = tokens[i].children[j];
let content = token.content;
let attrs = utils.getAttrs(content, 0, null, options);
let attrs = utils.getAttrs(content, 0, options);
let openingToken = utils.getMatchingOpeningToken(tokens[i].children, j - 1);
utils.addAttrs(attrs, openingToken);
token.content = content.slice(content.indexOf(options.rightDelimiter) + 1);
Expand Down Expand Up @@ -153,7 +153,7 @@ module.exports = options => ([
transform: (tokens, i, j) => {
let token = tokens[i].children[j];
let content = token.content;
let attrs = utils.getAttrs(content, 0, null, options);
let attrs = utils.getAttrs(content, 0, options);
let ii = i - 2;
while (tokens[ii - 1] &&
tokens[ii - 1].type !== 'ordered_list_open' &&
Expand Down Expand Up @@ -194,7 +194,7 @@ module.exports = options => ([
transform: (tokens, i) => {
let token = tokens[i + 2];
let content = token.content;
let attrs = utils.getAttrs(content, 0, null, options);
let attrs = utils.getAttrs(content, 0, options);
let openingToken = utils.getMatchingOpeningToken(tokens, i);
utils.addAttrs(attrs, openingToken);
tokens.splice(i + 1, 3);
Expand Down Expand Up @@ -222,7 +222,7 @@ module.exports = options => ([
transform: (tokens, i, j) => {
let token = tokens[i].children[j];
let content = token.content;
let attrs = utils.getAttrs(content, content.lastIndexOf(options.leftDelimiter), null, options);
let attrs = utils.getAttrs(content, content.lastIndexOf(options.leftDelimiter), options);
utils.addAttrs(attrs, tokens[i - 2]);
let trimmed = content.slice(0, content.lastIndexOf(options.leftDelimiter));
token.content = last(trimmed) !== ' ' ?
Expand Down Expand Up @@ -252,7 +252,7 @@ module.exports = options => ([
],
transform: (tokens, i, j) => {
let token = tokens[i].children[j];
let attrs = utils.getAttrs(token.content, 0, null, options);
let attrs = utils.getAttrs(token.content, 0, options);
// find last closing tag
let ii = i + 1;
while (tokens[ii + 1] && tokens[ii + 1].nesting === -1) { ii++; }
Expand Down Expand Up @@ -281,7 +281,7 @@ module.exports = options => ([
transform: (tokens, i, j) => {
let token = tokens[i].children[j];
let content = token.content;
let attrs = utils.getAttrs(content, content.lastIndexOf(options.leftDelimiter), null, options);
let attrs = utils.getAttrs(content, content.lastIndexOf(options.leftDelimiter), options);
let ii = i + 1;
while (tokens[ii + 1] && tokens[ii + 1].nesting === -1) { ii++; }
let openingToken = utils.getMatchingOpeningToken(tokens, ii);
Expand Down
4 changes: 2 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ describe('markdown-it-attrs.utils', () => {
it('should parse {.class ..css-module #id key=val}', () => {
let src = '{.red ..mod #head key=val}';
let expected = [['class', 'red'], ['css-module', 'mod'], ['id', 'head'], ['key', 'val']];
let res = utils.getAttrs(src, 0, null, options);
let res = utils.getAttrs(src, 0, options);
assert.deepEqual(res, expected);
});

it('should parse [.class ..css-module #id key=val]', () => {
let src = '[.red ..mod #head key=val]';
let expected = [['class', 'red'], ['css-module', 'mod'], ['id', 'head'], ['key', 'val']];
let res = utils.getAttrs(src, 0, null, {
let res = utils.getAttrs(src, 0, {
leftDelimiter: '[',
rightDelimiter: ']'
});
Expand Down
7 changes: 3 additions & 4 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
* @param {int} start: where to start parsing (including {)
* @returns {2d array}: [['key', 'val'], ['class', 'red']]
*/
exports.getAttrs = function (str, start, end, options) {
// TODO: do not require `end`, stop when } is found
exports.getAttrs = function (str, start, options) {
// not tab, line feed, form feed, space, solidus, greater than sign, quotation mark, apostrophe and equals sign
const allowedKeyChars = /[^\t\n\f />"'=]/;
const pairSeparator = ' ';
Expand Down Expand Up @@ -67,7 +66,7 @@ exports.getAttrs = function (str, start, end, options) {
}

// read next key/value pair
if ((char_ === pairSeparator && !valueInsideQuotes) || i === end) {
if ((char_ === pairSeparator && !valueInsideQuotes)) {
if (key === '') {
// beginning or ending space: { .red } vs {.red}
continue;
Expand Down Expand Up @@ -142,7 +141,7 @@ exports.hasDelimiter = function (where, options) {
return false;
}

function validCurlyLength(curly) {
function validCurlyLength (curly) {
let isClass = curly.charAt(1) === '.';
let isId = curly.charAt(1) === '#';
return (isClass || isId)
Expand Down

0 comments on commit 2f879aa

Please sign in to comment.