diff --git a/index.js b/index.js index 847d8e0..54c838e 100644 --- a/index.js +++ b/index.js @@ -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; @@ -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; @@ -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