Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[lib] added in map via class feature

  • Loading branch information...
commit f1fb668b4506286f158a2dacc3a1634b51f3a43e 1 parent c96e239
Paolo Fragomeni authored
Showing with 38 additions and 17 deletions.
  1. +38 −17 lib/plates.js
55 lib/plates.js
View
@@ -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'
),
@@ -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);
+ }
+
}
}
}
}
-
+
}
}
}
@@ -93,4 +115,3 @@
this.exports && (exports.bind = function() {
return Plates.bind.apply(this, arguments);
});
-
Please sign in to comment.
Something went wrong with that request. Please try again.