Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test fixtures for issue #59 #60

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 38 additions & 6 deletions lib/plates.js
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
14 changes: 14 additions & 0 deletions test/api-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}()
)

}
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/test-21.html
Original file line number Diff line number Diff line change
@@ -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>
31 changes: 31 additions & 0 deletions test/fixtures/test-21.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
]
}
1 change: 1 addition & 0 deletions test/fixtures/test-21.out
Original file line number Diff line number Diff line change
@@ -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>
1 change: 1 addition & 0 deletions test/fixtures/test-22.html
Original file line number Diff line number Diff line change
@@ -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>
31 changes: 31 additions & 0 deletions test/fixtures/test-22.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
]
}
1 change: 1 addition & 0 deletions test/fixtures/test-22.out
Original file line number Diff line number Diff line change
@@ -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>