Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ module.exports = function(grunt) {
'spec/browser/json2.js',
'spec/browser/injectNoder.js',
'spec/browser/**/*.spec.js', {
pattern: 'src/browser-modules/**',
included: false
}, {
pattern: 'dist/browser/**',
included: false
}, {
Expand Down
79 changes: 79 additions & 0 deletions spec/browser/sourceURL.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
var expect = global.expect ? global.expect : require("expect.js");
var noder = global.noder || require('../../dist/node/noder.js');

describe("SourceURL", function() {
var SourceURL;
if (global.noder) {
var newRootModule = noder.createContext({
packaging: {
requestConfig: {
sync: true
},
baseUrl: "/base/src/browser-modules/"
}
});
SourceURL = newRootModule.require('sourceURL.js');
} else {
SourceURL = require('../../src/browser-modules/sourceURL.js');
}

it('should generate sourceURL Basic', function() {
var src = "util/json";
var location = {
protocol: "http:",
hostname: "test.aria.com"
};
var sourceURL = SourceURL.generateSourceURL(location, src);
expect(sourceURL).to.equal('http://test.aria.com/util/json');
});

it('should generate sourceURL Relative', function() {
var src = "./util/json";
var location = {
protocol: "http:",
pathname: "/dist",
hostname: "test.aria.com"
};
var sourceURL = SourceURL.generateSourceURL(location, src);
expect(sourceURL).to.equal('http://test.aria.com/dist/util/json');
});

it('should generate sourceURL Absolute', function() {
var src = "/util/json";
var location = {
protocol: "http:",
hostname: "test.aria.com"
};
var sourceURL = SourceURL.generateSourceURL(location, src);
expect(sourceURL).to.equal('http://test.aria.com/util/json');
});

it('should generate sourceURL With URL', function() {
var src = "http://test.aria.com/util/json";
var location = {
protocol: "http:",
pathname: "/dist",
hostname: "test.aria.com"
};
var sourceURL = SourceURL.generateSourceURL(location, src);
expect(sourceURL).to.equal('http://test.aria.com/util/json');
});

it('should generate sourceURL With Scheme', function() {
var src = "aria://util/json";
var location = {
protocol: "http:",
pathname: "/dist",
hostname: "test.aria.com"
};
var sourceURL = SourceURL.generateSourceURL(location, src);
expect(sourceURL).to.equal('aria://util/json');
});

it('should generate sourceURL NoLocation', function() {
var src = "util/json";
var sourceURL = SourceURL.generateSourceURL(undefined, src);
expect(sourceURL).to.equal('util/json');
});

});
4 changes: 3 additions & 1 deletion src/browser-modules/eval.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var location = global.location;
var SourceURL = require("../sourceURL.js");

module.exports = function(code, fileName) {
var res = {};
// Using the 'arguments[1].res = ...' trick because IE does not let eval return a function
if (fileName) {
code = ['/*\n * File: ', fileName, '\n */\narguments[1].res=', code, '\n//# sourceURL=', fileName].join('');
code = ['/*\n * File: ', fileName, '\n */\narguments[1].res=', code, '\n//# sourceURL=', SourceURL.generateSourceURL(location, fileName)].join('');
} else {
code = 'arguments[1].res=' + code;
}
Expand Down
33 changes: 33 additions & 0 deletions src/browser-modules/sourceURL.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2012 Amadeus s.a.s.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
module.exports = {
generateSourceURL: function(location, src) {
var sourceURL = src;
// Check if src match a an URL
if (typeof src === "string" && location && !(/[a-z]*:\/\//.test(src))) {
sourceURL = location.protocol + "//" + location.hostname;

if (src.length > 0 && src[0] === "/") {
sourceURL += src;
} else if (src.length > 1 && src[0] === "." && src[1] === "/") {
sourceURL += location.pathname + src.substring(1);
} else {
sourceURL += "/" + src;
}
}

return sourceURL;
}
};