Cache regular expressions, set up functions and selectors, only fire required functions.

This commit is contained in:
Nicolas Stepien 2011-09-26 01:40:22 +02:00
parent f9f620c3d0
commit c0ceefcfad
2 changed files with 103 additions and 9 deletions

View File

@ -527,29 +527,90 @@
return Array.prototype.slice.call(root.querySelectorAll(selector)); return Array.prototype.slice.call(root.querySelectorAll(selector));
}; };
filter = { filter = {
regexps: {},
init: function() { init: function() {
var filter, filters, key, m, regx, _i, _len; var filter, key, m, regx, _i, _len, _results;
HTMLBlockquoteElement.prototype.toString = function() { HTMLBlockquoteElement.prototype.toString = function() {
return ($.el('a', { return ($.el('a', {
innerHTML: this.innerHTML.replace(/<br>/g, '\n') innerHTML: this.innerHTML.replace(/<br>/g, '\n')
})).textContent; })).textContent;
}; };
filters = {}; _results = [];
for (key in config.filter) { for (key in config.filter) {
if (!(m = conf[key].match(/(.+)/g))) { if (!(m = conf[key].match(/(.+)/g))) {
continue; continue;
} }
filters[key] = []; this.regexps[key] = [];
for (_i = 0, _len = m.length; _i < _len; _i++) { for (_i = 0, _len = m.length; _i < _len; _i++) {
filter = m[_i]; filter = m[_i];
try { try {
if ((regx = eval(filter)).constructor === RegExp) { if ((regx = eval(filter)).constructor === RegExp) {
filters[key].push(regx); this.regexps[key].push(regx);
} }
} catch (_e) {} } catch (_e) {}
} }
_results.push(this.regexps[key].length ? g.callbacks.push(this[key]) : void 0);
} }
return log(filters); return _results;
},
test: function(key, value) {
var regexp, _i, _len, _ref;
_ref = filter.regexps[key];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
regexp = _ref[_i];
if (regexp.test(value)) {
return true;
}
}
},
name: function(root) {
var name;
if (!(name = root.className === 'op' ? $('.postername', root).textContent : $('.commentpostername', root).textContent)) {
return;
}
return filter.test('name', name);
},
trip: function(root) {
var trip;
if (!(trip = $('.postertrip', root))) {
return;
}
return filter.test('trip', trip.textContent);
},
mail: function(root) {
var mail;
if (!(mail = $('.linkmail', root))) {
return;
}
return filter.test('mail', mail.href);
},
sub: function(root) {
var sub;
if (!(sub = root.className === 'op' ? $('.filetitle', root).textContent : $('.replytitle', root).textContent)) {
return;
}
return filter.test('sub', sub);
},
com: function(root) {
var com;
if (!(com = $('blockquote', root).toString())) {
return;
}
return filter.test('com', com);
},
file: function(root) {
var file;
if (!(file = $('.filesize span', root))) {
return;
}
return filter.test('file', file.title);
},
md5: function(root) {
var img;
if (!(img = $('img[md5]', root))) {
return;
}
return filter.test('md5', img.getAttribute('md5'));
} }
}; };
expandComment = { expandComment = {

View File

@ -384,20 +384,53 @@ $$ = (selector, root=d.body) ->
Array::slice.call root.querySelectorAll selector Array::slice.call root.querySelectorAll selector
filter = filter =
regexps: {}
init: -> init: ->
HTMLBlockquoteElement.prototype.toString = -> HTMLBlockquoteElement.prototype.toString = ->
return ($.el 'a', innerHTML: @innerHTML.replace /<br>/g, '\n').textContent return ($.el 'a', innerHTML: @innerHTML.replace /<br>/g, '\n').textContent
filters = {}
for key of config.filter for key of config.filter
unless m = conf[key].match /(.+)/g unless m = conf[key].match /(.+)/g
continue continue
filters[key] = [] @regexps[key] = []
for filter in m for filter in m
try if (regx = eval filter).constructor is RegExp try if (regx = eval filter).constructor is RegExp
filters[key].push regx @regexps[key].push regx
#only execute what's filterable
g.callbacks.push @[key] if @regexps[key].length
log filters test: (key, value) ->
for regexp in filter.regexps[key]
return true if regexp.test value
name: (root) ->
unless (name = if root.className is 'op' then $('.postername', root).textContent else $('.commentpostername', root).textContent)
return
filter.test 'name', name
trip: (root) ->
unless trip = $('.postertrip', root)
return
filter.test 'trip', trip.textContent
mail: (root) ->
unless mail = $('.linkmail', root)
return
filter.test 'mail', mail.href
sub: (root) ->
unless(sub = if root.className is 'op' then $('.filetitle', root).textContent else $('.replytitle', root).textContent)
return
filter.test 'sub', sub
com: (root) ->
unless com = $('blockquote', root).toString()
return
filter.test 'com', com
file: (root) ->
unless file = $ '.filesize span', root
return
filter.test 'file', file.title
md5: (root) ->
unless img = $ 'img[md5]', root
return
filter.test 'md5', img.getAttribute('md5')
expandComment = expandComment =
init: -> init: ->