From 51ba83538a33eacdb7b863ba7df51262c754a556 Mon Sep 17 00:00:00 2001 From: Zixaphir Date: Fri, 24 Jan 2014 16:10:04 -0700 Subject: [PATCH] Better $.rmAll I verified an issue with the previous benchmark and even wrote a more efficient method based off el.cloneNode(false);, which I would use if cloneNode could inherit eventListeners Speaking of events. there is still no interface for gather all eventListeners on a given element (or its children). Apparently the issue is that the w3c doesn't think there is a legitimate use case for it. Heh. --- builds/4chan-X.user.js | 11 ++--------- builds/crx/script.js | 11 ++--------- src/General/Navigate.coffee | 3 +-- src/General/lib/$.coffee | 8 ++------ 4 files changed, 7 insertions(+), 26 deletions(-) diff --git a/builds/4chan-X.user.js b/builds/4chan-X.user.js index 27835f28c..690c1005d 100644 --- a/builds/4chan-X.user.js +++ b/builds/4chan-X.user.js @@ -575,12 +575,7 @@ })(); $.rmAll = function(root) { - var node, _i, _len, _ref; - _ref = __slice.call(root.childNodes); - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - node = _ref[_i]; - root.removeChild(node); - } + return root.textContent = ''; }; $.tn = function(s) { @@ -12156,13 +12151,11 @@ } }, clean: function() { - var board; g.threads.forEach(function(thread) { return thread.collect(); }); QuoteBacklink.containers = {}; - board = $('.board'); - return $.replace(board, board.cloneNode(false)); + return $.rmAll($('.board')); }, features: [['Thread Excerpt', ThreadExcerpt], ['Unread Count', Unread], ['Quote Threading', QuoteThreading], ['Thread Stats', ThreadStats], ['Thread Updater', ThreadUpdater], ['Thread Expansion', ExpandThread]], disconnect: function() { diff --git a/builds/crx/script.js b/builds/crx/script.js index 3fd4559a2..5a949b0c4 100644 --- a/builds/crx/script.js +++ b/builds/crx/script.js @@ -552,12 +552,7 @@ })(); $.rmAll = function(root) { - var node, _i, _len, _ref; - _ref = __slice.call(root.childNodes); - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - node = _ref[_i]; - root.removeChild(node); - } + return root.textContent = ''; }; $.tn = function(s) { @@ -12155,13 +12150,11 @@ } }, clean: function() { - var board; g.threads.forEach(function(thread) { return thread.collect(); }); QuoteBacklink.containers = {}; - board = $('.board'); - return $.replace(board, board.cloneNode(false)); + return $.rmAll($('.board')); }, features: [['Thread Excerpt', ThreadExcerpt], ['Unread Count', Unread], ['Quote Threading', QuoteThreading], ['Thread Stats', ThreadStats], ['Thread Updater', ThreadUpdater], ['Thread Expansion', ExpandThread]], disconnect: function() { diff --git a/src/General/Navigate.coffee b/src/General/Navigate.coffee index d37f664df..9795831a0 100644 --- a/src/General/Navigate.coffee +++ b/src/General/Navigate.coffee @@ -37,8 +37,7 @@ Navigate = g.threads.forEach (thread) -> thread.collect() QuoteBacklink.containers = {} - board = $('.board') - $.replace board, board.cloneNode false + $.rmAll $('.board') features: [ ['Thread Excerpt', ThreadExcerpt] diff --git a/src/General/lib/$.coffee b/src/General/lib/$.coffee index 48467ef33..c33f2271c 100755 --- a/src/General/lib/$.coffee +++ b/src/General/lib/$.coffee @@ -133,12 +133,8 @@ $.rm = do -> else (el) -> el.parentNode?.removeChild el -$.rmAll = (root) -> - # jsperf.com/emptify-element - for node in [root.childNodes...] - # HTMLSelectElement.remove !== Element.remove - root.removeChild node - return +# jsperf.com/emptify-element/9 +$.rmAll = (root) -> root.textContent = '' $.tn = (s) -> d.createTextNode s