From 910eaff68e6865248bf188b791415d58f1706b84 Mon Sep 17 00:00:00 2001 From: Hristo Chakarov Date: Sat, 17 Jan 2015 12:19:43 +0200 Subject: [PATCH] stripScripts now loads & executes external files --- Docs/Types/String.md | 19 ++++++++++++++++++- Source/Browser/Browser.js | 37 ++++++++++++++++++++++++++++++++----- Specs/Browser/Browser.js | 24 +++++++++++++++++++++++- 3 files changed, 73 insertions(+), 7 deletions(-) diff --git a/Docs/Types/String.md b/Docs/Types/String.md index 9656e8e79..0093d1c06 100644 --- a/Docs/Types/String.md +++ b/Docs/Types/String.md @@ -385,7 +385,12 @@ Strips the String of its * tags. +* (*string*) - The stripped text. +* (*array*) - Array of urls of external JavaScript files (if any). +* (*function*) - Callback function that executes all stripped JavaScript (inline + external) synchronously. ### Returns: @@ -396,6 +401,18 @@ Strips the String of its *Hello, World."; myString.stripScripts(); // returns 'Hello, World.' myString.stripScripts(true); // alerts 'Hello', then returns 'Hello, World.' + + var html = '
' + + '' + + ''; + html.stripScripts(function(code, html, urls, exec){ + // inject the stripped HTML + document.body.innerHTML = html; + // execute all JavaScript + exec(function(){ + alert('All scripts loaded and executed!'); + }); + }); diff --git a/Source/Browser/Browser.js b/Source/Browser/Browser.js index e59b16c9a..3e82faffa 100644 --- a/Source/Browser/Browser.js +++ b/Source/Browser/Browser.js @@ -144,13 +144,40 @@ Browser.exec = function(text){ }; String.implement('stripScripts', function(exec){ - var scripts = ''; - var text = this.replace(/]*>([\s\S]*?)<\/script>/gi, function(all, code){ - scripts += code + '\n'; + var scripts = []; + var inline = ''; + + var text = this.replace(/]*?(?:src=(?:"|')?([^"]+)(?:"|')?[^>]*)?>([\s\S]*?)<\/script>/gi, function( all, src, code ) { + inline += code + '\n'; + scripts.push({ + type: src ? 'src' : 'code', + source: src || code + }); return ''; }); - if (exec === true) Browser.exec(scripts); - else if (typeOf(exec) == 'function') exec(scripts, text); + + var urls = scripts.map(function(el){ return el.type == 'src' ? el.source : undefined; }).clean(); + var next = function(callback){ + if (!scripts.length){ + typeof callback == 'function' && callback(); + return; + } + var script = scripts.shift(); + if (script.type == 'code'){ + Browser.exec(script.source); + next(callback); + } else { + new Element('script', { + src: script.source, + events: { + load: function(){ next(callback); } + } + }).inject(document.head); + } + } + + if (exec === true) next(); + else if (typeof exec == 'function') exec(inline, text, urls, next); return text; }); diff --git a/Specs/Browser/Browser.js b/Specs/Browser/Browser.js index 36c89e05f..9b9c53a90 100644 --- a/Specs/Browser/Browser.js +++ b/Specs/Browser/Browser.js @@ -45,11 +45,33 @@ describe('String.stripScripts', function(){ it('should execute the stripped tags from the string', function(){ expect('
'.stripScripts(true)).toEqual('
'); expect(window.stripScriptsSpec).toEqual(42); - expect('
'.stripScripts(true)).toEqual('
'); + expect('
'.stripScripts(true)).toEqual('
'); expect(window.stripScriptsSpec).toEqual(24); expect('
'.stripScripts(true)).toEqual('
'); expect(window.stripScriptsSpec).toEqual(4242); }); + + it('should load & execute script files synchronously', function(){ + var div = new Element('div').inject(document.body); + [ + '
', + '', + '', + '', + '', + '' + ].join('').stripScripts(function(code, html, urls, fn){ + div.set('html', html); + fn(); + expect(div.get('text')).toEqual('dynamic content'); + }); + }); });