From 5ee6c7bc194a869400a4861aa2a4f707b38e1dbf Mon Sep 17 00:00:00 2001 From: unscriptable Date: Tue, 5 Jun 2012 21:05:14 -0400 Subject: [PATCH] mark scripts so they can be removed easily --- src/curl/tdd/undefine.js | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/curl/tdd/undefine.js b/src/curl/tdd/undefine.js index 0719587b..19f2cd20 100644 --- a/src/curl/tdd/undefine.js +++ b/src/curl/tdd/undefine.js @@ -9,11 +9,20 @@ */ define(['curl/_privileged', 'require'], function (priv, require) { - var cache, cleanupScript; + var cache, cleanupScript, loadScript; cache = priv['cache']; - cleanupScript = typeof document != 'undefined' ? removeScript : noop; + cleanupScript = noop; + + if (typeof document != 'undefined') { + cleanupScript = removeScript; + loadScript = priv['core'].loadScript; + priv['core'].loadScript = function (def) { + var el = loadScript.apply(this, arguments); + el._curl_id = def.id; + } + } /** * Removes a module from curl.js's cache so that it can @@ -22,30 +31,22 @@ define(['curl/_privileged', 'require'], function (priv, require) { * @param moduleId {String|Array} the id of a module (or modules) */ return function undefine (moduleId) { - var ids, id, url; - + var ids, id; ids = [].concat(moduleId); while ((id = ids.pop())) { - if (cache[id] instanceof priv.Promise) { - url = require.toUrl(cache[id].ctxId || id); - } - else { - url = require.toUrl(id); - } delete cache[id]; - cleanupScript(url); + cleanupScript(id); } }; - function removeScript (url) { - var rx, scripts, i, script; - rx = new RegExp(url + '($|\\.)', 'i'); + function removeScript (id) { + var scripts, i, script; scripts = document.getElementsByTagName('script'); i = 0; while ((script = scripts[i++])) { - if (rx.test(script.src)) { + if (script._curl_id == id) { script.parentNode.removeChild(script); - scripts = []; // all done! + return; // all done! } } }