Permalink
Browse files

[fix] fixed the case where to mappings for the same attribute and val…

…ue existed

one changed the tag content and the other updated the attribute. in this case, only when the attribute one was declared last it worked. changing the order of the declarations made it not work. Now we are sorting the mappings before they are applied.
  • Loading branch information...
pgte committed Aug 23, 2012
1 parent bee4f18 commit 4d1ab92fba2a0780b102865b3c63cd33000bfd4e
View
@@ -10,6 +10,34 @@ var Plates = (typeof module !== 'undefined' && typeof module.exports !== 'undefi
var Merge = function Merge() {};
+
+ //
+ // compileMappings
+ //
+ // sort the mappings so that mappings for the same attribute and value go consecutive
+ // and inside those, those that change attributes appear first.
+ //
+ function compileMappings(oldMappings) {
+ var mappings = oldMappings.slice(0);
+ mappings.sort(function(map1, map2) {
+ if (map1.attribute !== map2.attribute) {
+ return map1.attribute < map2.attribute ? -1 : 1;
+ }
+ if (map1.value !== map2.value) {
+ return map1.value < map2.value ? -1 : 1;
+ }
+ if (! ('replace' in map1) && ! ('replace' in map2)) {
+ throw new Error('Conflicting mappings for attribute ' + map1.attribute + ' and value ' + map1.value);
+ }
+ if (map1.replace) {
+ return 1;
+ }
+ return -1;
+ });
+
+ return mappings;
+ }
+
Merge.prototype = {
nest: [],
@@ -165,6 +193,8 @@ var Plates = (typeof module !== 'undefined' && typeof module.exports !== 'undefi
//
if (mappings && mappings.length > 0) {
+ mappings = compileMappings(mappings);
+
for (var ii = mappings.length - 1; ii >= 0; ii--) {
var setAttribute = false
View
@@ -215,6 +215,31 @@ vows.describe('merge data into markup').addBatch({
return common.createTest('test-20', map);
}()
+ ),
+
+ '(21) Two maps on the same class, one for attribute work if the attribute one comes last': (
+
+ function() {
+ var map = Plates.Map();
+ map.class('author').to('name');
+ map.class('author').use('url').as('href');
+
+ return common.createTest('test-21', map);
+ }()
+
+ ),
+
+ '(22) Two maps on the same class, one for attribute work if the attribute one comes first': (
+
+ function() {
+ var map = Plates.Map();
+ map.class('author').use('url').as('href');
+ map.class('doesnotexist').to('donotcare');
+ map.class('author').to('name');
+
+ return common.createTest('test-22', map);
+ }()
+
)
}
@@ -0,0 +1 @@
+<a class="author" href=""></a>
@@ -0,0 +1,4 @@
+{
+ "name": "The Name",
+ "url": "//the.url.com/abc"
+}
@@ -0,0 +1 @@
+<a class="author" href="//the.url.com/abc">The Name</a>
@@ -0,0 +1 @@
+<a class="author" href=""></a>
@@ -0,0 +1,4 @@
+{
+ "name": "The Name",
+ "url": "//the.url.com/abc"
+}
@@ -0,0 +1 @@
+<a class="author" href="//the.url.com/abc">The Name</a>

0 comments on commit 4d1ab92

Please sign in to comment.