Skip to content

Commit

Permalink
use utils.getAttrs
Browse files Browse the repository at this point in the history
  • Loading branch information
arve0 committed Nov 24, 2015
1 parent 3eb5426 commit 17971c0
Showing 1 changed file with 14 additions and 73 deletions.
87 changes: 14 additions & 73 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
'use strict';

module.exports = function attributes(md) {
var utils = require('./utils.js');

// not tab, line feed, form feed, space, solidus, greater than sign, quotation mark, apostrophe and equals sign
var allowedKeyChars = /[^\t\n\f \/>"'=]/;
var pairSeparator = ' ';
var keySeparator = '=';
var classChar = '.';
var idChar = '#';
module.exports = function attributes(md) {

function curlyAttrs(state){
var l = state.tokens.length;
Expand All @@ -18,9 +13,10 @@ module.exports = function attributes(md) {
}

var inlineTokens = tokens[i].children;
if (inlineTokens.length <= 0) {
if (!inlineTokens || inlineTokens.length <= 0) {
continue;
}

var end = inlineTokens.length - 1;
var content = inlineTokens[end].content;

Expand All @@ -36,72 +32,17 @@ module.exports = function attributes(md) {
continue;
}

var key = '';
var value = '';
var parsingKey = true;
var valueInsideQuotes = false;

// read inside {}, excluding {, including }
for (var ii = curlyStart + 1; ii < content.length; ii++) {
var char = content.charAt(ii);

// switch to reading value if equal sign
if (char === keySeparator) {
parsingKey = false;
continue;
}

// {.class}
if (char === classChar && key === '') {
key = 'class';
parsingKey = false;
continue;
}

// {#id}
if (char === idChar && key === '') {
key = 'id';
parsingKey = false;
continue;
}

// {value="inside quotes"}
if (char === '"' && value === '') {
valueInsideQuotes = true;
continue;
}
if (char === '"' && valueInsideQuotes) {
valueInsideQuotes = false;
continue;
}

// read next key/value pair
if ((char === pairSeparator && !valueInsideQuotes) ||
char === '}') {
if (key === 'class' &&
tokens[i - 1].attrIndex(key) !== -1) {
var classIdx = tokens[i - 1].attrIndex(key);
tokens[i - 1].attrs[classIdx][1] += ' ' + value;
} else {
tokens[i - 1].attrPush([key, value]);
}
key = '';
value = '';
parsingKey = true;
continue;
}

// continue if character not allowed
if (parsingKey && char.search(allowedKeyChars) === -1) {
continue;
}

// no other conditions met; append to key/value
if (parsingKey) {
key += char;
continue;
// read inside {}
var attrs = utils.getAttrs(content, curlyStart + 1, content.length - 1);
for (var j=0, l=attrs.length; j<l; ++j) {
var key = attrs[j][0];
if (key === 'class' && tokens[i - 1].attrIndex(key) !== -1) {
// append space seperated text string
var classIdx = tokens[i - 1].attrIndex(key);
tokens[i - 1].attrs[classIdx][1] += ' ' + attrs[j][1];
} else {
tokens[i - 1].attrPush(attrs[j]);
}
value += char;
}

inlineTokens[end].content = content.slice(0, curlyStart).trim();
Expand Down

0 comments on commit 17971c0

Please sign in to comment.