mirror of
https://github.com/gnh1201/welsonjs.git
synced 2025-10-25 18:11:16 +00:00
Refactor path normalization and absolute path check
Introduced a new normalizePath function in file.js to handle path trimming and BOM removal. Updated isAbsolutePath to use normalizePath for more robust path checking. Modified msoffice.js to use isAbsolutePath instead of manual string checks for determining absolute paths.
This commit is contained in:
parent
7cba7895fd
commit
b6a95a7c50
19
lib/file.js
19
lib/file.js
|
|
@ -171,18 +171,22 @@ function loadEnvFromArgs(args, callback) {
|
|||
}
|
||||
}
|
||||
|
||||
function isAbsolutePath(path) {
|
||||
function normalizePath(path) {
|
||||
if (typeof path !== "string")
|
||||
return false;
|
||||
|
||||
path = path.replace(/^\s+/, "").replace(/\s+$/, "");
|
||||
return (function(s) {
|
||||
s = s.trim();
|
||||
return (s.charAt(0) === "\uFEFF" ? s.slice(1) : s);
|
||||
})(path);
|
||||
}
|
||||
|
||||
if (path.charAt(0) === "\uFEFF")
|
||||
path = path.slice(1);
|
||||
function isAbsolutePath(path) {
|
||||
var normalizedPath = normalizePath(path);
|
||||
|
||||
return (/^[a-zA-Z]:[\\/]/).test(path) ||
|
||||
(/^[\\/]{2,}/).test(path) ||
|
||||
(/^\//).test(path);
|
||||
return (/^[a-zA-Z]:[\\/]/).test(normalizedPath) ||
|
||||
(/^[\\/]{2,}/).test(normalizedPath) ||
|
||||
(/^\//).test(normalizedPath);
|
||||
};
|
||||
|
||||
exports.fileExists = fileExists;
|
||||
|
|
@ -201,6 +205,7 @@ exports.appendFile = appendFile;
|
|||
exports.rotateFile = rotateFile;
|
||||
exports.loadEnvFromFile = loadEnvFromFile;
|
||||
exports.loadEnvFromArgs = loadEnvFromArgs;
|
||||
exports.normalizePath = normalizePath;
|
||||
exports.isAbsolutePath = isAbsolutePath;
|
||||
|
||||
exports.CdoCharset = PipeIPC.CdoCharset;
|
||||
|
|
|
|||
|
|
@ -128,7 +128,7 @@ function PowerPoint() {
|
|||
this.open = function(filename) {
|
||||
if (typeof filename !== "undefined") {
|
||||
// check type of the path
|
||||
if (filename.indexOf(":\\") < 0 && filename.indexOf(":/") < 0) {
|
||||
if (!FILE.isAbsolutePath(filename)) {
|
||||
filename = SYS.getCurrentWorkingDirectory() + "\\" + filename; // get absolute path
|
||||
}
|
||||
if (FILE.fileExists(filename)) {
|
||||
|
|
@ -210,7 +210,7 @@ function Word() {
|
|||
this.open = function(filename) {
|
||||
if (typeof filename !== "undefined") {
|
||||
// check type of the path
|
||||
if (filename.indexOf(":\\") < 0 && filename.indexOf(":/") < 0) {
|
||||
if (!FILE.isAbsolutePath(filename)) {
|
||||
filename = SYS.getCurrentWorkingDirectory() + "\\" + filename; // get absolute path
|
||||
}
|
||||
if (FILE.fileExists(filename)) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user