Permalink
Browse files

[lib] chain added

  • Loading branch information...
1 parent 494b7b9 commit b430a416979446f8c09c58d50fefc0a0fa77ad1e Paolo Fragomeni committed Dec 26, 2011
Showing with 126 additions and 46 deletions.
  1. BIN lib/.plates.js.swp
  2. +67 −46 lib/plates.js
  3. +59 −0 test/simple-test.js
View
Binary file not shown.
View
@@ -1,3 +1,4 @@
+
;var Plates = function main(undefined) {
var Merge = function Merge() {};
@@ -18,25 +19,22 @@
].join('\\s*')
),
- attr: new RegExp(
- '([-\\w]*)=(?:["\']([-\.\\w\\s]*)["\'])',
- 'gi'
- ),
+ attr: /([-\w]*)=(?:["\']([-\.\w\s]*)["\'])/gi,
hasClass: function(str, className) {
return str.indexOf(className) > -1;
},
bind: function bind(html, data, map) {
- var that = this;
- var out = '';
- var pos = 0;
- var openers = 0;
- var updates = '';
- var insertion = null;
- var matchmode = false;
- var insert = true;
+ var that = this,
+ out = '',
+ pos = 0,
+ openers = 0,
+ updates = [],
+ matchmode = false,
+ createmode = false,
+ insert = true;
if (map) {
map = map.mappings;
@@ -67,7 +65,7 @@
//
// its not a closing tag!
//
- else {
+ else if (!selfclosing) {
++openers;
insert = false;
}
@@ -97,25 +95,42 @@
if (map) {
//
+ // if the map specifies a replacement to be made,
+ // but there is no matching key found in the attributes
+ // of the tag, we should create the attribute and then
+ // popuate it.
+ //
+ createmode = false;
+
+ //
// look at all of the items in the map and try to apply
// the mappings for all of the values that have been spec'd.
//
+
for (var i = map.length - 1; i >= 0; i--) {
+ //
+ // There may be an exact match for the key
+ // that the map wants to replace.
+ //
if (map[i].replace === key && data[map[i].dataKey]) {
- insertion = {
- start: idx,
- end: attr.length
- };
- updates = data[map[i].dataKey];
+ updates.push({
+ data: data[map[i].dataKey],
+ insertion: {
+ start: idx,
+ end: attr.length
+ }
+ });
+ continue;
}
//
// find out if this tag is the target by looking at an
// arbitrary attribute that the user has provided.
//
- if (map[i].attribute === key && map[i].value === value) {
- updates = data[map[i].dataKey];
+ if (map[i].attribute === key && !map[i].replace &&
+ (map[i].value === value || key === 'class' && that.hasClass(value))) {
+ updates.push({ data: data[map[i].dataKey] });
matchmode = true;
}
}
@@ -127,7 +142,7 @@
// object and looking for this attribute value as an object key.
//
if (key === 'id' && data[value]) {
- updates = data[value];
+ updates.push({ data: data[value] });
matchmode = true;
}
}
@@ -136,31 +151,38 @@
//
// should we write data?
//
- if (updates) {
+ if (updates.length > 0) {
//
- // position the new content
+ // there may be multiple updates from the map
//
- if (insertion) {
+
+ var update = null;
+ while(update = updates.pop()) {
//
- // in some cases the content goes inside the tag.
- //
- var start = ~(index + insertion.start + insertion.end + 1);
- var value = html.slice(0, start);
- value += updates + '"';
- value += html.slice(insertion.start + insertion.end);
- out += value;
-
- insertion = null;
- updates = null;
- }
- else {
- //
- // in other cases, it goes inside the tag body.
+ // position the new content
//
- out += html.slice(0, index + match.length) + updates;
- updates = null;
+
+ if (update.insertion) {
+
+ //
+ // in some cases the content goes inside the tag.
+ //
+ var i = update.insertion;
+ var start = ~(index + i.start + i.end + 1);
+ var value = html.slice(0, start);
+
+ value += update.data + '"';
+ value += html.slice(i.start + i.end);
+ out += value;
+ }
+ else {
+ //
+ // in other cases, it goes inside the tag body.
+ //
+ out += html.slice(0, index + match.length) + update.data;
+ }
}
}
else if (insert) {
@@ -170,7 +192,7 @@
//
out += html.slice(0, pos);
}
-
+
//
// reduce the initial html from existence.
//
@@ -188,9 +210,9 @@
if (!(this instanceof Mapper)) { return new Mapper(val); }
this.mappings = [];
};
-
- function last() {
- if (this.mappings.length < 1) {
+
+ function last(newitem) {
+ if (newitem) {
this.mappings.push({});
}
return this.mappings[this.mappings.length-1];
@@ -202,8 +224,7 @@
return this;
},
where: function(val) {
- var map = last.call(this);
- map.attribute = val;
+ last.call(this, true).attribute = val;
return this;
},
is: function(val) {
View
@@ -0,0 +1,59 @@
+
+
+ // map.where(<attribute>).is(<value>).use(<data-key>).as(<attribute>);
+
+
+ var html = '111<div id="outer">222<div id="inner">333</div><img class="test" src=""/>444</div>555';
+
+ !function() {
+ var data = { "foo": "New Value" };
+ var map = Plates.Map();
+
+ map.where('class').is('test').use('foo').as('src');
+ map.where('id').is('inner').use('foo');
+
+ console.log(Plates.bind(html, data, map));
+ }();
+
+
+
+ // Able to match an `id` with the value of `foo`, but could not find a data
+ // key with with the name `fodo`. Should fail to populate, shouldn't affect
+ // the inner values of the match.
+
+
+ !function() {
+
+ var data = { "foo": "New Value" };
+ var map = Plates.Map();
+
+ map.where("id").is('foo').use("fodo");
+ console.log(Plates.bind(html, data, map));
+ }();
+
+
+
+
+ // Able to match an `id` with the value of `foo` as well as find a data
+ // key with the value `foo`. Should replace all inner content of the tag.
+
+
+ !function() {
+ var data = { "foo": "New Value" };
+ var map = Plates.Map();
+
+ map.where("id").is('inner').use("foo");
+ console.log(Plates.bind(html, data, map));
+ }();
+
+
+ // without using a map, there should be a match between
+ // the data-keys and the IDs in the HTML fragment.
+
+
+ !function() {
+ var data = { "outer": "New Value" };
+ console.log(Plates.bind(html, data));
+ }();
+
+

0 comments on commit b430a41

Please sign in to comment.