Skip to content

Commit

Permalink
Added to load function the capability to accept data: URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
ZER0 committed Dec 30, 2011
1 parent e7b5349 commit c84063e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
6 changes: 4 additions & 2 deletions packages/api-utils/docs/sandbox.md
Expand Up @@ -13,7 +13,7 @@ string, in which case sandbox will get exact same privileges as a scripts
loaded from that URL. Argument also could be a DOM window object, to inherit
privileges from the window being passed. Finally if argument is omitted or is
`null` sandbox will have a chrome privileges giving it access to all the XPCOM
components. Optionally `sandbox` function can be passed a second optional
components. Optionally `sandbox` function can be passed a second optional
argument (See [sandbox documentation on MDN](https://developer.mozilla.org/en/Components.utils.Sandbox#Optional_parameter)
for details).

Expand All @@ -39,7 +39,9 @@ Version of JavaScript can be also specified via optional argument:

### Loading scripts ###

API provides limited API for loading scripts right form the local URLs.
API provides limited API for loading scripts right form the local URLs,
but data: URLs are supported.

load(scope, 'resource://path/to/my/script.js');
load(scope, 'file:///path/to/script.js');
load(scope, 'data:,var a = 5;');
14 changes: 12 additions & 2 deletions packages/api-utils/lib/sandbox.js
Expand Up @@ -62,8 +62,18 @@ exports.evaluate = evaluate;

/**
* Evaluates code under the given `uri` in the given `sandbox`.
*
* @param {String} uri
* The URL pointing to the script to load.
* It must be a local chrome:, resource:, file: or data: URL.
*/
function load(sandbox, uri) {
return scriptLoader.loadSubScript(uri, sandbox, 'UTF-8');
if (uri.indexOf('data:') === 0) {
let source = uri.substr(uri.indexOf(',') + 1);

return evaluate(sandbox, decodeURIComponent(source), '1.8', uri, 0);
} else {
return scriptLoader.loadSubScript(uri, sandbox, 'UTF-8');
}
}
exports.load = load;
exports.load = load;
18 changes: 18 additions & 0 deletions packages/api-utils/tests/test-sandbox.js
Expand Up @@ -82,10 +82,28 @@ exports['test load'] = function(assert) {
assert.equal(fixture.f(), 4, 'function was defined');
};

exports['test load with data: URL'] = function(assert) {
let code = "var a = 1; this.b = 2; function f() 4";
let fixture = sandbox();
load(fixture, "data:," + encodeURIComponent(code));

assert.equal(fixture.a, 1, 'global variable defined');
assert.equal(fixture.b, 2, 'global via `this` property was set');
assert.equal(fixture.f(), 4, 'function was defined');
};

exports['test load script with complex char'] = function(assert) {
let fixture = sandbox();
load(fixture, fixturesURI + 'sandbox-complex-character.js');
assert.equal(fixture.chars, 'გამარჯობა', 'complex chars were loaded correctly');
};

exports['test load script with data: URL and complex char'] = function(assert) {
let code = "var chars = 'გამარჯობა';";
let fixture = sandbox();
load(fixture, "data:," + encodeURIComponent(code));

assert.equal(fixture.chars, 'გამარჯობა', 'complex chars were loaded correctly');

This comment has been minimized.

Copy link
@mykmelez

mykmelez Dec 30, 2011

გამარჯობა, welcome, nice!

I wonder if we should factor out this string at some point, now that it's being used in three places. Hmm... it probably isn't useful enough to do so yet.

};

require('test').run(exports);

0 comments on commit c84063e

Please sign in to comment.