From ef2fa2ae578eb4dd33665feee1a0b2244802abb9 Mon Sep 17 00:00:00 2001 From: guybedford Date: Tue, 20 Aug 2013 14:33:30 +0200 Subject: [PATCH] more tests, loader fixes --- loader.js | 91 ++++++++++++++++++++++++++++++++++++----- test/test-advanced.html | 29 +++++++++++++ test/test-dynamic1.html | 17 ++++++++ test/test-dynamic2.html | 5 +++ test/test-dynamic3.html | 26 ++++++++++++ test/test.html | 28 +++++-------- 6 files changed, 167 insertions(+), 29 deletions(-) create mode 100644 test/test-advanced.html create mode 100644 test/test-dynamic1.html create mode 100644 test/test-dynamic2.html create mode 100644 test/test-dynamic3.html diff --git a/loader.js b/loader.js index 776ec466..bdbfee6f 100644 --- a/loader.js +++ b/loader.js @@ -37,7 +37,7 @@ var moduleRegEx = /^\s*module\s+("[^"]+"|'[^']+')\s*\{/m; // AMD and CommonJS regexs for support - var amdDefineRegEx = /^\s*define\s*\(\s*("[^"]+"\s*,|'[^']+'\s*,)?\s*(\[(\s*("[^"]+"|'[^']+')\s*,)*(\s*("[^"]+"|'[^']+'))\])?/m; + var amdDefineRegEx = /^\s*define\s*\(\s*("[^"]+"\s*,|'[^']+'\s*,)?\s*(\[(\s*("[^"]+"|'[^']+')\s*,)*(\s*("[^"]+"|'[^']+')\s*)\])?/m; var cjsDefineRegEx = /^\s*define\s*\(\s*(function\s*|{|[_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*\))/m; var cjsRequireRegEx = /\s*require\s*\(\s*("([^"]+)"|'([^']+)')\s*\)/gm; var cjsExportsRegEx = /\s*exports\s*\[\s*('[^']+'|"[^"]+")\s*\]|\exports\s*\.\s*[_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*|exports\s*\=/m; @@ -45,6 +45,80 @@ // regex to check absolute urls var absUrlRegEx = /^\/|([^\:\/]*:\/\/)/; + // function to remove the comments from a string + function removeComments(str) { + // output + var curOutIndex = 0, + outString = ''; + + // mode variables + var singleQuote = false, + doubleQuote = false, + regex = false, + blockComment = false, + lineComment = false; + + // character buffer + var lastChar, curChar, + nextChar = str.charAt(0); + + for (var i = 0, l = str.length; i < l; i++) { + lastChar = curChar; + curChar = nextChar; + nextChar = str.charAt(i + 1); + + if (singleQuote) { + if (curChar === "'" && lastChar !== '\\') + singleQuote = false; + } + + else if (doubleQuote) { + if (curChar === '"' && lastChar !== '\\') + doubleQuote = false; + } + + else if (regex) { + if (curChar === '/' && lastChar !== '\\') + regex = false; + } + + else if (blockComment) { + if (curChar === '/' && lastChar === '*') { + blockComment = false; + curOutIndex = i + 1; + } + } + + else if (lineComment) { + if (nextChar === '\n' || nextChar === '\r') { + lineComment = false; + curOutIndex = i + 1; + } + } + + else { + doubleQuote = curChar === '"'; + singleQuote = curChar === "'"; + + if (curChar !== '/') + continue; + + if (nextChar === '*') { + blockComment = true; + outString += str.substring(curOutIndex, i - 1); + } + else if (nextChar === '/') { + lineComment = true; + outString += str.substring(curOutIndex, i - 1); + } + else { + regex = true; + } + } + } + return outString + str.substr(curOutIndex); + } + // configuration object extension // objects extend, everything else replaces var extend = function(objA, objB) { @@ -234,14 +308,6 @@ // do map config name = applyMap(name, parentName); - // allow for '/' package main referencing - // 'some-package@0.0.1/' -> 'some-package@0.0.1/some-package' - if (name.substr(name.length - 1, 1) == '/') { - var parts = name.indexOf(':') != -1 ? name.substr(name.indexOf(':') + 1).split('/') : name.split('/'); - var lastPart = parts[parts.length - 2]; - var lastPartName = lastPart.split('@')[0]; - name = name + lastPartName; - } } if (pluginName) @@ -258,7 +324,7 @@ jspm.config({ depends: sConfig }); } - + return name; }, resolve: function(name, options) { @@ -283,7 +349,7 @@ name = this.baseURL + (this.baseURL.substr(this.baseURL.length - 1, 1) != '/' ? '/' : '') + name; // js extension - if (!pluginMatch && name.substr(name.length - 3, 3) != '.js') + if (!pluginMatch) name += '.js'; return name; @@ -328,6 +394,9 @@ if (source.match(importRegEx) || source.match(exportRegEx) || source.match(moduleRegEx)) return; + // remove comments before doing regular expressions + source = removeComments(source); + var match; // depends config diff --git a/test/test-advanced.html b/test/test-advanced.html new file mode 100644 index 00000000..bdf5d87a --- /dev/null +++ b/test/test-advanced.html @@ -0,0 +1,29 @@ + + + + + + + + diff --git a/test/test-dynamic1.html b/test/test-dynamic1.html new file mode 100644 index 00000000..70cb18dc --- /dev/null +++ b/test/test-dynamic1.html @@ -0,0 +1,17 @@ + + + + + + + + + + \ No newline at end of file diff --git a/test/test-dynamic2.html b/test/test-dynamic2.html new file mode 100644 index 00000000..79f60b2b --- /dev/null +++ b/test/test-dynamic2.html @@ -0,0 +1,5 @@ + + + \ No newline at end of file diff --git a/test/test-dynamic3.html b/test/test-dynamic3.html new file mode 100644 index 00000000..406cd766 --- /dev/null +++ b/test/test-dynamic3.html @@ -0,0 +1,26 @@ + + + + + + + + \ No newline at end of file diff --git a/test/test.html b/test/test.html index 0e87fb4a..0e786937 100644 --- a/test/test.html +++ b/test/test.html @@ -102,23 +102,6 @@ return 'Mapped module not loaded'; } }, - { - name: 'Package shorthand', - run: function(complete) { - jspm.config({ - map: { - 'myapp': 'tests/my-app' - } - }); - jspm.import(['myapp/', 'myapp/sub'], complete) - }, - confirm: function(m1, m2) { - if (!m1.package) - return 'Package not loaded'; - if (!m2.package) - return 'Package subpath not loaded'; - } - }, { name: 'Package map configuration', run: function(complete) { @@ -129,7 +112,7 @@ } } }); - jspm.import(['myapp/map-test'], complete); + jspm.import(['tests/my-app/map-test'], complete); }, confirm: function(m) { if (!m.mapDep) @@ -233,6 +216,15 @@ confirm: function(m) { } }, + { + name: 'Font plugin', + run: function(complete) { + jspm.import('#google Port Lligat Slab, Droid Sans !font', complete); + }, + confirm: function(m) { + + } + }, { name: 'onLoad hook', run: function(complete) {