From b0dbeab9ac00ffa257a4b75a9177a374bdf2795a Mon Sep 17 00:00:00 2001 From: Zaid Date: Thu, 20 Oct 2016 00:49:56 -0400 Subject: [PATCH] added a function that normalizes paths along with some constructive feedback on existing code --- lib/helpers.coffee | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/lib/helpers.coffee b/lib/helpers.coffee index 14c0214..7206f0a 100644 --- a/lib/helpers.coffee +++ b/lib/helpers.coffee @@ -14,7 +14,7 @@ exports.escapeValue = (object) -> if (typeof object == "string") return "\"" + object.replace("\\","\\\\").replace("\"","\\\"") + "\"" return object; - + exports.escapeHtml = (string) -> entityMap = { "<": "<" @@ -66,16 +66,19 @@ exports.deserializeArray = (array) -> exports.localPathToRemote = (localPath) -> pathMaps = atom.config.get('php-debug.PathMaps') for pathMap in pathMaps - remote = pathMap.substring(0,pathMap.indexOf(";")) - local = pathMap.substring(pathMap.indexOf(";")+1) + remote = fixPath(pathMap.substring(0,pathMap.indexOf(";"))) + local = fixPath(pathMap.substring(pathMap.indexOf(";")+1)) if localPath.indexOf(local) == 0 path = localPath.replace(local, remote) if remote.indexOf('/') != null # remote path appears to be a unix path, so replace any \'s with /'s' path = path.replace(/\\/g, '/') else if remote.indexOf('\\') != null + # This might not work in some cases # remote path appears to be a windows path, so replace any /'s with \'s' path = path.replace(/\//g, '\\') + else + atom.notifications.addError "Oops, looks like php-debug can't determine the remote path's type" return path.replace('file://','') return localPath.replace('file://','') @@ -83,9 +86,11 @@ exports.remotePathToLocal = (remotePath) -> pathMaps = atom.config.get('php-debug.PathMaps') remotePath = decodeURI(remotePath) for pathMap in pathMaps - remote = pathMap.substring(0,pathMap.indexOf(";")) - local = pathMap.substring(pathMap.indexOf(";")+1) + remote = fixPath(pathMap.substring(0,pathMap.indexOf(";"))) + local = fixPath(pathMap.substring(pathMap.indexOf(";")+1)) if remotePath.indexOf('/') != null && remotePath.indexOf('/') != 0 + #Consider throwing an error instead of modifying invalid input + #can check with require('path').posix.isAbsolute() adjustedPath = '/' + remotePath if adjustedPath.indexOf(remote) == 0 return adjustedPath.replace(remote, local) @@ -94,5 +99,15 @@ exports.remotePathToLocal = (remotePath) -> if remotePath.indexOf(remote) == 0 return remotePath.replace(remote, local) break - + return remotePath.replace('file://','') + +#Get rid of extra slashes and trailing slashes +#Both of them give us a hard time +fixPath = (path) -> + modpath = require 'path' + path = modpath.normalize(path) + lastChar = path.slice(-1) + if lastChar == '/' || lastChar == '\\' + return path.slice(0, path.length - 1) + return path