Skip to content

Commit

Permalink
[lib] added in map via class feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Paolo Fragomeni committed Nov 16, 2011
1 parent c96e239 commit f1fb668
Showing 1 changed file with 38 additions and 17 deletions.
55 changes: 38 additions & 17 deletions lib/plates.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@
tag: new RegExp(
[
'\\s*',
'<', // opening bracket
'(/?)', // 1 - is closing
'([-:\\w]+)', // 2 - name of tag
'((?:\\s+[-\\w]+(?:', // attribute key/value pairs
'=',
'(?:' +
'<',
'(/?)', // 1 - is closing
'([-:\\w]+)', // 2 - name
'((?:\\s+[-\\w]+(?:', '=', '(?:' +
'\\w+|' +
'"[^"]*"|' +
'\'[^\']*\'))?)*)', // 3 - attributes
'(/?)', // 4 - self-closing
'>' // closing bracket
].join('\\s*'), // whitespace liberal
'\'[^\']*\'))?)*)', // 3 - attributes
'(/?)', // 4 - is self-closing
'>'
].join('\\s*'),
'gi'
),

Expand Down Expand Up @@ -57,22 +55,46 @@
// if there is a map, the user wants an explicit
// data-key to tag-attribute match.
//
var match = false, idx = 0;
for (var key in map) {
if (data[key]) {
while (matchedAttr = this.attr.exec(matchedTag[3])) {

//
// has an attribute who's value is a match.
// an attribute who's value is a match.
//
console.log(map[key])
if (matchedAttr[1] === map[key] && data[matchedAttr[2]]) {
var idx = matchedTag.index + matchedTag[0].length;
html = html.slice(0, idx) + data[matchedAttr[2]] + html.slice(idx);
if (matchedAttr[1] === map[key]) {

match = false, idx = 0;

//
// if the preferred attribute is `class`, split the class and
// make sure that the actual key is a match with the data.
//
if (map[key] === 'class') {
var classNames = matchedAttr[2].split(/\s+/);
for (var i = 0, l = classNames.length; i < l; i++) {
if (classNames[i] === key) {
match = true; break;
}
}
}

//
// if there is a match from the class, or there is a match of
// the key, then splice the data into the string where it belongs.
//
if (matchedAttr[2] === key || match) {
var d = match ? key : matchedAttr[2];
idx = matchedTag.index + matchedTag[0].length;
html = html.slice(0, idx) + data[d] + html.slice(idx);
}

}
}
}
}

}
}
}
Expand All @@ -93,4 +115,3 @@
this.exports && (exports.bind = function() {
return Plates.bind.apply(this, arguments);
});

0 comments on commit f1fb668

Please sign in to comment.