Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Test fixtures for issue #59 #60

Open
wants to merge 4 commits into from

3 participants

Dougal Campbell Federico Rampazzo paolo fragomeni
Dougal Campbell

Here are my tests for issue #59, "Another nested object problem"

Federico Rampazzo

Thanks for the fix!

After using plates for quite some time I got the same bug you described in issue #59
It's pretty easy to get caught by this one, still don't understand how I didn't manage to trigger it before!

The main problem here is that we're trying to parse HTML by using regular expressions (http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags)

I'm not sure how much having a proper xml parser would impact speed, but using regex is quite bug prone in my book.

Hope this get merged up!

Dougal Campbell

Yeah, even though I came up with a patch that worked for me (never did get around to making an updated pull request, though), I wasn't 100% happy with it. It seems brittle, and I keep thinking there's some other edge-case lurking in there. Glad if it helped you, though.

paolo fragomeni

have you run this through benchmarkjs or something similar? this looks good and i'd like to pull it in, but I'd like to be sure it doesnt incur exponential overhead with the regexp creation and execution.

Dougal Campbell

No, I haven't benchmarked it, and I didn't think about the regex creation being looped. Also, I think I made additional changes on my fork after I made that pull request. I'll try to find time this weekend to give it another look and hopefully submit an updated pull request.

Thanks for taking a look!

The idea is really good, just needs some minor refinement ;) Thanks!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
44 lib/plates.js
View
@@ -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?
14 test/api-test.js
View
@@ -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');
+ }()
)
}
1  test/fixtures/test-21.html
View
@@ -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 test/fixtures/test-21.json
View
@@ -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  test/fixtures/test-21.out
View
@@ -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  test/fixtures/test-22.html
View
@@ -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 test/fixtures/test-22.json
View
@@ -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  test/fixtures/test-22.out
View
@@ -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>
Something went wrong with that request. Please try again.