Skip to content
Browse files

Choose bonsai runner url less strict, add version check

  • Loading branch information...
1 parent 29b0685 commit 200f33742948a75df1014fc20ad441db453a6ddb @davidaurelio davidaurelio committed Aug 1, 2012
Showing with 65 additions and 20 deletions.
  1. +1 −0 CHANGELOG
  2. +7 −10 src/bootstrapper/_build/iframe.js
  3. +7 −10 src/bootstrapper/_build/worker.js
  4. +50 −0 src/bootstrapper/util.js
View
1 CHANGELOG
@@ -1,6 +1,7 @@
v0.3.1
-------------------
+* Make the search for the runner url more flexible, also check for version number (but don't rely on it).
v0.3.0 / 2012-07-25
-------------------
View
17 src/bootstrapper/_build/iframe.js
@@ -6,8 +6,9 @@ define([
'../context/iframe/bootstrap',
'../context/iframe/context',
'../../renderer/svg/svg',
- '../../tools'
-], function(player, bootstrapIframe, IframeRunnerContext, SvgRenderer, tools) {
+ '../../tools',
+ '../util'
+], function(player, bootstrapIframe, IframeRunnerContext, SvgRenderer, tools, bootstrapUtil) {
'use strict';
if(typeof window != 'undefined' && window.messageChannel) {
@@ -23,19 +24,15 @@ define([
} else {
window['bonsai'] = player;
- var envUrl = 'bonsai.js';
- var scripts = document.getElementsByTagName('script');
- for (var i=0, m = scripts.length; i < m; i++) {
- if(/bonsai[.](?:iframe[.])?(?:min[.])?js(?:$|\?|#)/.test(scripts[i].src)) {
- envUrl = scripts[i].src;
- }
- }
+ var scripts = tools.map(document.getElementsByTagName('script'), function(script) {
+ return script.src;
+ });
player.Renderer = SvgRenderer;
player.setup({
baseUrl: tools.baseUri(document),
runnerContext: IframeRunnerContext,
- runnerUrl: envUrl
+ runnerUrl: bootstrapUtil.chooseRunnerUrl(scripts, /iframe/) || 'bonsai.js'
});
}
});
View
17 src/bootstrapper/_build/worker.js
@@ -7,8 +7,9 @@ define([
'../context/worker/bootstrap',
'../context/worker/context',
'../../renderer/svg/svg',
- '../../tools'
-], function(player, MessageChannel, bootstrapWorker, WorkerRunnerContext, SvgRenderer, tools) {
+ '../../tools',
+ '../util'
+], function(player, MessageChannel, bootstrapWorker, WorkerRunnerContext, SvgRenderer, tools, bootstrapUtil) {
'use strict';
if(typeof importScripts != 'undefined') {
@@ -28,19 +29,15 @@ define([
} else {
window['bonsai'] = player;
- var workerLocation = 'bonsai.js';
- var scripts = document.getElementsByTagName('script');
- for (var i = 0, m = scripts.length; i < m; i++) {
- if(/bonsai[.](?:min[.])?js(?:$|\?|#)/.test(scripts[i].src)) {
- workerLocation = scripts[i].src;
- }
- }
+ var scripts = tools.map(document.getElementsByTagName('script'), function(script) {
+ return script.src;
+ });
player.Renderer = SvgRenderer;
player.setup({
baseUrl: tools.baseUri(document),
runnerContext: WorkerRunnerContext,
- runnerUrl: workerLocation
+ runnerUrl: bootstrapUtil.chooseRunnerUrl(scripts) || 'bonsai.js'
});
}
});
View
50 src/bootstrapper/util.js
@@ -0,0 +1,50 @@
+define([
+ '../version'
+], function(version) {
+ 'use strict';
+
+ function reEscape(string) {
+ return string.replace(/[\\^$*+?.()|{}[\]]/g, '\\$1');
+ }
+
+ function filter(sequence, callback, context) {
+ var element, filtered = [];
+ for (var i = 0, length = sequence.length; i < length; i += 1) {
+ element = sequence[i];
+ if (callback.call(context, element, i, sequence)) {
+ filtered.push(element);
+ }
+ }
+
+ return filtered;
+ }
+
+ var reVersion = RegExp('\\b' + reEscape(version) + '\\b');
+ var reTest = RegExp.prototype.test;
+
+ return {
+ /**
+ * Chooses a runner url from a list of candidate filenames.
+ *
+ * @param {Array} filenames All filenames to choose from.
+ * @param {RegExp} [additionalCheck] An additional regular expression to
+ * test the filenames with.
+ * @return {*}
+ */
+ chooseRunnerUrl: function(filenames, additionalCheck) {
+ var f = filenames;
+ filenames = filter(filenames, reTest, /(?:^|\/)bonsai.*\.js(?:$|\?|#)/i);
+ var filenamesHavingCheck = 0, filenamesHavingVersionAndCheck = 0; // 0 to be subscriptable
+ var filenamesHavingVersion = filter(filenames, reTest, reVersion);
+
+ if (additionalCheck) {
+ filenamesHavingCheck = filter(filenames, reTest, additionalCheck);
+ filenamesHavingVersionAndCheck =
+ filter(filenamesHavingVersion, reTest, additionalCheck);
+ }
+
+ return filenamesHavingVersionAndCheck[0] || filenamesHavingCheck[0] ||
+ filenamesHavingVersion[0] || filenames[0];
+ }
+ }
+});

0 comments on commit 200f337

Please sign in to comment.
Something went wrong with that request. Please try again.