Test fixtures for issue #59 #60

Closed
wants to merge 4 commits into
from
View
44 lib/plates.js
@@ -11,32 +11,64 @@ var Plates = (typeof process !== 'undefined' && typeof process.title !== 'undefi
Merge.prototype = {
- nest: [],
+ nest: [],
+ depth: 0, // iteration depth
tag: new RegExp([
'<',
'(/?)', // 2 - is closing
'([-:\\w]+)', // 3 - name
'((?:[-\\w]+(?:', '=',
- '(?:\\w+|["|\'](?:.*)["|\']))?)*)', // 4 - attributes
+ '(?:\\w+|["|\'](?:.*?)["|\']))?)*?)', // 4 - attributes
'(/?)', // 5 - is self-closing
'>'
].join('\\s*')),
- attr: /([\-\w]*)=(?:["\']([\-\.\w\s\/:;&#]*)["\'])/gi,
+ attr: /([\-\w]*)=(?:["\']([\-\.\w\s\/:;&#]*?)["\'])/gi,
hasClass: function(str, className) {
return str.split(' ').indexOf(className) > -1;
},
+
+ balance: function(html, tagname) {
+ var i = idx = skip = 0;
+ matches = html.match(new RegExp(this.tag.source, 'g')); // globalize
+ do {
+ var components = this.tag.exec(matches[idx]);
+ if ( components[2] === tagname) {
+ if (components[4]) {
+ continue; // ignore isSelfClosing
+ }
+ if (components[1]) {
+ --i;
+ ++skip; // isClosing
+ } else {
+ ++i; // opening, matches segment-wrapping tagname
+ }
+ }
+ ++idx;
+ } while (i > 0);
- iterate: function(html, value, components, tagname, key) {
+ // Now skip should tell us how many closing tags to look for
+ var end = 0;
+ for (var i = 0; i < skip; i++) {
+ end = html.indexOf('</' + tagname, end) + tagname.length + 3;
+ }
+
+ return html.substr(0, end);
+ },
+ iterate: function(html, value, components, tagname, key) {
+
var output = '',
- segment = html.slice(
+
+/* segment = html.slice(
html.search(components.input),
html.lastIndexOf(tagname) + tagname.length + 1
),
-
+*/
+ segment = this.balance(html.substr(html.search(components.input)), tagname),
+
data = {};
// Is it an array?
View
14 test/api-test.js
@@ -215,6 +215,20 @@ vows.describe('merge data into markup').addBatch({
return common.createTest('test-20', map);
}()
+ ),
+
+ '(21) Keep your place after handling nested objects': (
+
+ function() {
+ return common.createTest('test-21');
+ }()
+ ),
+
+ '(22) Keep your place after handling nested objects, more varied html': (
+
+ function() {
+ return common.createTest('test-22');
+ }()
)
}
View
1 test/fixtures/test-21.html
@@ -0,0 +1 @@
+<div id="entries"><div class="entry"><div class="created"></div><div class="user"><div class="name"></div><div class="city"></div><div class="state"></div></div><div class="custid"></div></div></div>
View
31 test/fixtures/test-21.json
@@ -0,0 +1,31 @@
+{
+ "entry": [
+ {
+ "custid": 10101,
+ "created": "2001-01-01",
+ "user": {
+ "name": "John Q. User",
+ "city": "Atlanta",
+ "state": "GA"
+ }
+ },
+ {
+ "custid": 20202,
+ "created": "2002-02-02",
+ "user": {
+ "name": "Jane Q. User",
+ "city": "Mobile",
+ "state": "AL"
+ }
+ },
+ {
+ "custid": 30303,
+ "created": "2003-0303",
+ "user": {
+ "name": "Professor X",
+ "city": "Menlo",
+ "state": "CA"
+ }
+ }
+ ]
+}
View
1 test/fixtures/test-21.out
@@ -0,0 +1 @@
+<div id="entries"><div class="entry"><div class="created">2001-01-01</div><div class="user"><div class="name">John Q. User</div><div class="city">Atlanta</div><div class="state">GA</div></div><div class="custid">10101</div></div><div class="entry"><div class="created">2002-02-02</div><div class="user"><div class="name">Jane Q. User</div><div class="city">Mobile</div><div class="state">AL</div></div><div class="custid">20202</div></div><div class="entry"><div class="created">2003-0303</div><div class="user"><div class="name">Professor X</div><div class="city">Menlo</div><div class="state">CA</div></div><div class="custid">30303</div></div></div>
View
1 test/fixtures/test-22.html
@@ -0,0 +1 @@
+<ul id="entries"><li class="entry"><div class="created"></div><div class="user"><div class="name"></div><div class="city"></div><div class="state"></div></div><div class="custid"></div></li></ul>
View
31 test/fixtures/test-22.json
@@ -0,0 +1,31 @@
+{
+ "entry": [
+ {
+ "custid": 10101,
+ "created": "2001-01-01",
+ "user": {
+ "name": "John Q. User",
+ "city": "Atlanta",
+ "state": "GA"
+ }
+ },
+ {
+ "custid": 20202,
+ "created": "2002-02-02",
+ "user": {
+ "name": "Jane Q. User",
+ "city": "Mobile",
+ "state": "AL"
+ }
+ },
+ {
+ "custid": 30303,
+ "created": "2003-0303",
+ "user": {
+ "name": "Professor X",
+ "city": "Menlo",
+ "state": "CA"
+ }
+ }
+ ]
+}
View
1 test/fixtures/test-22.out
@@ -0,0 +1 @@
+<ul id="entries"><li class="entry"><div class="created">2001-01-01</div><div class="user"><div class="name">John Q. User</div><div class="city">Atlanta</div><div class="state">GA</div></div><div class="custid">10101</div></li><li class="entry"><div class="created">2002-02-02</div><div class="user"><div class="name">Jane Q. User</div><div class="city">Mobile</div><div class="state">AL</div></div><div class="custid">20202</div></li><li class="entry"><div class="created">2003-0303</div><div class="user"><div class="name">Professor X</div><div class="city">Menlo</div><div class="state">CA</div></div><div class="custid">30303</div></li></ul>