[Pkg-javascript-commits] [less.js] 21/285: Fix tests

Jonas Smedegaard dr at jones.dk
Mon Oct 26 23:23:33 UTC 2015


This is an automated email from the git hooks/post-receive script.

js pushed a commit to annotated tag v2.0.0
in repository less.js.

commit f65da551683b82bae577ff6747b9a81e43b6a10f
Author: Luke Page <luke.a.page at gmail.com>
Date:   Mon Feb 24 09:40:18 2014 +0000

    Fix tests
---
 lib/less/browser.js                              |  2 +-
 lib/less/environments/browser.js                 | 13 +++-
 lib/less/environments/node.js                    | 88 +++++++++++++++++++++++-
 lib/less/parser.js                               | 10 +--
 test/browser/css/postProcessor/postProcessor.css |  2 +-
 test/browser/css/rootpath/urls.css               | 16 ++---
 test/browser/runner-rootpath-options.js          |  2 +-
 7 files changed, 115 insertions(+), 18 deletions(-)

diff --git a/lib/less/browser.js b/lib/less/browser.js
index 7f2517d..93b3f84 100644
--- a/lib/less/browser.js
+++ b/lib/less/browser.js
@@ -349,7 +349,7 @@ function loadStyleSheet(sheet, callback, reload, remaining, modifyVars) {
             relativeUrls: env.relativeUrls};
 
         newFileInfo.entryPath = newFileInfo.currentDirectory;
-        newFileInfo.rootpath = less.rootpath || newFileInfo.currentDirectory;
+        newFileInfo.rootpath = env.rootpath || newFileInfo.currentDirectory;
 
         if (webInfo) {
             webInfo.remaining = remaining;
diff --git a/lib/less/environments/browser.js b/lib/less/environments/browser.js
index efd8e98..11cbace 100644
--- a/lib/less/environments/browser.js
+++ b/lib/less/environments/browser.js
@@ -6,12 +6,12 @@ var fileCache = {};
 //        isFileProtocol is global
 
 function getXMLHttpRequest() {
-    if (window.XMLHttpRequest) {
+    if (window.XMLHttpRequest && (window.location.protocol !== "file:" || !window.ActiveXObject)) {
         return new XMLHttpRequest();
     } else {
         try {
             /*global ActiveXObject */
-            return new ActiveXObject("MSXML2.XMLHTTP.3.0");
+            return new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e) {
             log("browser doesn't support AJAX.", logLevel.errors);
             return null;
@@ -39,6 +39,9 @@ less.Parser.environment = {
     isPathAbsolute: function isPathAbsolute(env, filename) {
         return /^(?:[a-z-]+:|\/|\\)/.test(filename);
     },
+    alwaysMakePathsAbsolute: function alwaysMakePathsAbsolute() {
+        return true;
+    },
     pathDiff: function pathDiff(url, baseUrl) {
         // diff between two paths to create a relative path
 
@@ -62,6 +65,12 @@ less.Parser.environment = {
         }
         return diff;
     },
+    join: function join(basePath, laterPath) {
+        if (!basePath) {
+            return laterPath;
+        }
+        return this.extractUrlParts(laterPath, basePath).path;
+    },
     // helper function, not part of API
     extractUrlParts: function extractUrlParts(url, baseUrl) {
         // urlParts[1] = protocol&hostname || /
diff --git a/lib/less/environments/node.js b/lib/less/environments/node.js
index 2fdb36b..7aab317 100644
--- a/lib/less/environments/node.js
+++ b/lib/less/environments/node.js
@@ -19,7 +19,93 @@ module.exports = {
         return filename.slice(0, j + 1);
     },
     isPathAbsolute: function(env, filename) {
-        return /^(?:[a-z-]+:|\/|\\)/.test(filename);
+        return (/^(?:[a-z-]+:|\/|\\)/).test(filename);
+    },
+    alwaysMakePathsAbsolute: function alwaysMakePathsAbsolute() {
+        return false;
+    },
+    join: function join(basePath, laterPath) {
+        if (!basePath) {
+            return laterPath;
+        }
+        return basePath + laterPath;
+    },
+    pathDiff: function pathDiff(url, baseUrl) {
+        // diff between two paths to create a relative path
+
+        var urlParts = this.extractUrlParts(url),
+            baseUrlParts = this.extractUrlParts(baseUrl),
+            i, max, urlDirectories, baseUrlDirectories, diff = "";
+        if (urlParts.hostPart !== baseUrlParts.hostPart) {
+            return "";
+        }
+        max = Math.max(baseUrlParts.directories.length, urlParts.directories.length);
+        for(i = 0; i < max; i++) {
+            if (baseUrlParts.directories[i] !== urlParts.directories[i]) { break; }
+        }
+        baseUrlDirectories = baseUrlParts.directories.slice(i);
+        urlDirectories = urlParts.directories.slice(i);
+        for(i = 0; i < baseUrlDirectories.length-1; i++) {
+            diff += "../";
+        }
+        for(i = 0; i < urlDirectories.length-1; i++) {
+            diff += urlDirectories[i] + "/";
+        }
+        return diff;
+    },
+    // helper function, not part of API
+    extractUrlParts: function extractUrlParts(url, baseUrl) {
+        // urlParts[1] = protocol&hostname || /
+        // urlParts[2] = / if path relative to host base
+        // urlParts[3] = directories
+        // urlParts[4] = filename
+        // urlParts[5] = parameters
+
+        var urlPartsRegex = /^((?:[a-z-]+:)?\/+?(?:[^\/\?#]*\/)|([\/\\]))?((?:[^\/\\\?#]*[\/\\])*)([^\/\\\?#]*)([#\?].*)?$/i,
+            urlParts = url.match(urlPartsRegex),
+            returner = {}, directories = [], i, baseUrlParts;
+
+        if (!urlParts) {
+            throw new Error("Could not parse sheet href - '"+url+"'");
+        }
+
+        // Stylesheets in IE don't always return the full path
+        if (baseUrl && (!urlParts[1] || urlParts[2])) {
+            baseUrlParts = baseUrl.match(urlPartsRegex);
+            if (!baseUrlParts) {
+                throw new Error("Could not parse page url - '"+baseUrl+"'");
+            }
+            urlParts[1] = urlParts[1] || baseUrlParts[1] || "";
+            if (!urlParts[2]) {
+                urlParts[3] = baseUrlParts[3] + urlParts[3];
+            }
+        }
+
+        if (urlParts[3]) {
+            directories = urlParts[3].replace(/\\/g, "/").split("/");
+
+            // extract out . before .. so .. doesn't absorb a non-directory
+            for(i = 0; i < directories.length; i++) {
+                if (directories[i] === ".") {
+                    directories.splice(i, 1);
+                    i -= 1;
+                }
+            }
+
+            for(i = 0; i < directories.length; i++) {
+                if (directories[i] === ".." && i > 0) {
+                    directories.splice(i-1, 2);
+                    i -= 2;
+                }
+            }
+        }
+
+        returner.hostPart = urlParts[1];
+        returner.directories = directories;
+        returner.path = urlParts[1] + directories.join("/");
+        returner.fileUrl = returner.path + (urlParts[4] || "");
+        returner.url = returner.fileUrl + (urlParts[5] || "");
+        return returner;
     },
     loadFile: function(env, filename, currentDirectory, callback) {
         var fullFilename,
diff --git a/lib/less/parser.js b/lib/less/parser.js
index b145d9c..f357b87 100644
--- a/lib/less/parser.js
+++ b/lib/less/parser.js
@@ -103,11 +103,13 @@ less.Parser = function Parser(env) {
                 //   then rootpath should become 'less/module/nav/'
                 // - If path of imported file is '../mixins.less' and rootpath is 'less/', 
                 //   then rootpath should become 'less/../'
-                var originalRelativePath = less.Parser.environment.getPath(env, path);
-                if(newFileInfo.relativeUrls && !less.Parser.environment.isPathAbsolute(env, path) && originalRelativePath) {
-                    newFileInfo.rootpath = newFileInfo.rootpath + originalRelativePath; // append (sub|sup) directory path of imported file
-                }
                 newFileInfo.currentDirectory = less.Parser.environment.getPath(env, resolvedFilename);
+                if(newFileInfo.relativeUrls) {
+                    newFileInfo.rootpath = less.Parser.environment.join((env.rootpath || ""), less.Parser.environment.pathDiff(newFileInfo.currentDirectory, newFileInfo.entryPath));
+                    if (!less.Parser.environment.isPathAbsolute(env, newFileInfo.rootpath) && less.Parser.environment.alwaysMakePathsAbsolute()) {
+                        newFileInfo.rootpath = less.Parser.environment.join(newFileInfo.entryPath, newFileInfo.rootpath);
+                    }
+                }
                 newFileInfo.filename = resolvedFilename;
 
                 var newEnv = new tree.parseEnv(env);
diff --git a/test/browser/css/postProcessor/postProcessor.css b/test/browser/css/postProcessor/postProcessor.css
index 76de30b..8e54894 100644
--- a/test/browser/css/postProcessor/postProcessor.css
+++ b/test/browser/css/postProcessor/postProcessor.css
@@ -1,4 +1,4 @@
 hr {height:50px;}
 .test {
-  color: #ffffff;
+  color: white;
 }
diff --git a/test/browser/css/rootpath/urls.css b/test/browser/css/rootpath/urls.css
index 9618e8e..1a5060e 100644
--- a/test/browser/css/rootpath/urls.css
+++ b/test/browser/css/rootpath/urls.css
@@ -1,20 +1,20 @@
- at import "https://www.github.com/modify-this.css";
- at import "https://www.github.com/modify-again.css";
+ at import "https://localhost/modify-this.css";
+ at import "https://localhost/modify-again.css";
 .modify {
-  my-url: url("https://www.github.com/a.png");
+  my-url: url("https://localhost/a.png");
 }
 .modify {
-  my-url: url("https://www.github.com/b.png");
+  my-url: url("https://localhost/b.png");
 }
 @font-face {
   src: url("/fonts/garamond-pro.ttf");
-  src: local(Futura-Medium), url(https://www.github.com/fonts.svg#MyGeometricModern) format("svg");
+  src: local(Futura-Medium), url(https://localhost/fonts.svg#MyGeometricModern) format("svg");
 }
 #shorthands {
   background: url("http://www.lesscss.org/spec.html") no-repeat 0 4px;
 }
 #misc {
-  background-image: url(https://www.github.com/images/image.jpg);
+  background-image: url(https://localhost/images/image.jpg);
 }
 #data-uri {
   background: url(data:image/png;charset=utf-8;base64,
@@ -28,8 +28,8 @@
   background: transparent url('data:image/svg+xml, <svg version="1.1"><g></g></svg>');
 }
 .comma-delimited {
-  background: url(https://www.github.com/bg.jpg) no-repeat, url(https://www.github.com/bg.png) repeat-x top left, url(https://www.github.com/bg);
+  background: url(https://localhost/bg.jpg) no-repeat, url(https://localhost/bg.png) repeat-x top left, url(https://localhost/bg);
 }
 .values {
-  url: url('https://www.github.com/Trebuchet');
+  url: url('https://localhost/Trebuchet');
 }
diff --git a/test/browser/runner-rootpath-options.js b/test/browser/runner-rootpath-options.js
index 5e475bb..65cb1f1 100644
--- a/test/browser/runner-rootpath-options.js
+++ b/test/browser/runner-rootpath-options.js
@@ -1,3 +1,3 @@
 var less = {};
-less.rootpath = "https://www.github.com/";
+less.rootpath = "https://localhost/";
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/less.js.git



More information about the Pkg-javascript-commits mailing list