Permalink
Browse files

based on work by @fsimeon for #123. This resolves addRule statements …

…which provide a protocol:// endpoint without sacrificing the relative URL structures
  • Loading branch information...
Jakobo committed Jun 13, 2012
1 parent 965bcd3 commit 136732e6d6abe392795fc5f199c0967c9e1e4df0
Showing with 58 additions and 19 deletions.
  1. +13 −7 src/inject.coffee
  2. +1 −0 tests/index.html
  3. +40 −0 tests/inject/bugs.js
  4. +4 −0 tests/inject/includes/bugs/bug_123/program.js
  5. +0 −12 tests/inject/inject.js
View
@@ -82,6 +82,7 @@ defineStaticRequireRegex = /^[\r\n\s]*define\(\s*("\S+",|'\S+',|\s*)\s*\[([^\]]*
requireGreedyCapture = /require.*/
commentRegex = /(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/mg
relativePathRegex = /^(.\/|..\/).*/
+absolutePathRegex = /^[A-Za-z]+:\/\//
###
lscache configuration
@@ -943,14 +944,19 @@ applyRules = (moduleId, save, relativePath) ->
workingPath = if typeof(rule.path) is "string" then rule.path else rule.path(workingPath)
if rule?.pointcuts?.before then pointcuts.before.push(rule.pointcuts.before)
if rule?.pointcuts?.after then pointcuts.after.push(rule.pointcuts.after)
+
# apply global rules for all paths
- if workingPath.indexOf("/") isnt 0
- if typeof(userConfig.moduleRoot) is "undefined" then throw new Error("Module Root must be defined")
- else if typeof(userConfig.moduleRoot) is "string" then workingPath = "#{userConfig.moduleRoot}#{workingPath}"
- else if typeof(userConfig.moduleRoot) is "function" then workingPath = userConfig.moduleRoot(workingPath)
-
- if typeof(relativePath) is "string"
- workingPath = basedir(relativePath) + moduleId
+ # if the stack has yielded an http:// URL, stop mucking with it
+ if !absolutePathRegex.test(workingPath)
+ # does not begin with a /. This makes it relative
+ if workingPath.indexOf("/") isnt 0
+ if typeof(userConfig.moduleRoot) is "undefined" then throw new Error("Module Root must be defined")
+ else if typeof(userConfig.moduleRoot) is "string" then workingPath = "#{userConfig.moduleRoot}#{workingPath}"
+ else if typeof(userConfig.moduleRoot) is "function" then workingPath = userConfig.moduleRoot(workingPath)
+
+ # if we have a relative path, resolve based on that
+ if typeof(relativePath) is "string"
+ workingPath = basedir(relativePath) + moduleId
if !fileSuffix.test(workingPath) then workingPath = "#{workingPath}.js"
View
@@ -43,6 +43,7 @@
<script type="text/javascript" src="/tests/manifest/manifest.js"></script>
<script type="text/javascript" src="/tests/inject/inject.js"></script>
+ <script type="text/javascript" src="/tests/inject/bugs.js"></script>
</head>
<body>
View
@@ -0,0 +1,40 @@
+module("Inject Bugs", {
+ setup: function() {
+ if (localStorage) {
+ localStorage.clear();
+ }
+ Inject.reset();
+ },
+ teardown: function() {
+ if (localStorage) {
+ localStorage.clear();
+ }
+ }
+});
+
+asyncTest("#105 exceptions surfaced correctly in console", 1, function() {
+ var oldError = window.onerror;
+ window.onerror = function(err, where, line) {
+ ok(/Parse error/.test(err), "raised syntax error exception");
+ window.onerror = oldError;
+ start();
+ return true;
+ };
+ require.setModuleRoot("/tests/inject/includes/bugs");
+ require.run("bug_105");
+});
+
+// When a relative path is resolved, additional tests keep running
+asyncTest("#123 applyRules: a resolved relative path continues through stack", 1, function() {
+ require.setModuleRoot("/tests/modules-1.0/includes/bugs");
+ require.addRule(/^a_bug_123\//, {
+ path: function(path) {
+ path = path.replace("a_bug_123", "bug_123");
+ return ([
+ location.protocol, "//", location.host,
+ "/tests/inject/includes/bugs/", path
+ ]).join("");
+ }
+ });
+ require.run("a_bug_123/program");
+});
@@ -0,0 +1,4 @@
+// ensure this file runs
+exports.foo = "bar";
+ok(true, "bug 123 loaded");
+start();
View
@@ -30,15 +30,3 @@ asyncTest("Make sure query string params are handled properly in pointcut paths"
start();
});
});
-
-asyncTest("#105 exceptions surfaced correctly in console", 1, function() {
- var oldError = window.onerror;
- window.onerror = function(err, where, line) {
- ok(/Parse error/.test(err), "raised syntax error exception");
- window.onerror = oldError;
- start();
- return true;
- };
- require.setModuleRoot("/tests/inject/includes/bugs");
- require.run("bug_105");
-});

0 comments on commit 136732e

Please sign in to comment.