Permalink
Browse files

@flatiron/plates#59 - Do a better job at balancing tags when processi…

…ng sub-objects.
  • Loading branch information...
1 parent ccd9bbd commit 36223d5dfccaf231a886fc9acbde4f8f3a4d134f @dougalcampbell committed May 7, 2012
Showing with 39 additions and 6 deletions.
  1. +39 −6 lib/plates.js
View
@@ -11,32 +11,65 @@ 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) {
@0x00A

0x00A Aug 24, 2012

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.

+ var i = -1;
+ var idx = skip = 0;
+ matches = html.match(new RegExp(this.tag.source, 'g'));
+ do {
+ var components = this.tag.exec(matches[idx]);
+ if ( components[2] === tagname) {
+ if (components[4]) {
+ continue;
+ }
+ if (components[1]) {
+ --i;
+ ++skip;
+ } else {
+ ++i;
+ }
+ }
+ ++idx;
+ } while (i >= 0);
- iterate: function(html, value, components, tagname, key) {
+ // Now skip should tell us how many closing tags to look for
+ var start = 0;
+ for (var i = 0; i < skip; i++) {
+ start = html.indexOf('</' + tagname, start) + tagname.length + 3;
+ }
+
+ return html.substr(0, start);
+ },
+ 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?

2 comments on commit 36223d5

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!!

Please sign in to comment.