Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit 3143d38

Browse files
committed
Merge pull request #10207 from adobe/pflynn/fix-LiveDevMultiBrowser-loop
Cleaner fix for LiveDevMultiBrowser infinite loop
2 parents 19b38aa + f044e44 commit 3143d38

File tree

7 files changed

+82
-19
lines changed

7 files changed

+82
-19
lines changed

src/LiveDevelopment/LiveDevMultiBrowser.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ define(function (require, exports, module) {
389389
}
390390

391391
var filteredFiltered = allFiles.filter(function (item) {
392-
var parent = FileUtils.getDirectoryPath(item.fullPath);
392+
var parent = FileUtils.getParentPath(item.fullPath);
393393

394394
return (containingFolder.indexOf(parent) === 0);
395395
});
@@ -417,7 +417,7 @@ define(function (require, exports, module) {
417417
// We found no good match
418418
if (i === -1) {
419419
// traverse the directory tree up one level
420-
containingFolder = FileUtils.getDirectoryPath(containingFolder);
420+
containingFolder = FileUtils.getParentPath(containingFolder);
421421
// Are we still inside the project?
422422
if (containingFolder.indexOf(projectRoot) === -1) {
423423
stillInProjectTree = false;

src/LiveDevelopment/LiveDevelopment.js

+3-13
Original file line numberDiff line numberDiff line change
@@ -679,16 +679,6 @@ define(function LiveDevelopment(require, exports, module) {
679679
refPath,
680680
i;
681681

682-
// TODO: FileUtils.getParentFolder()
683-
function getParentFolder(path) {
684-
return path.substring(0, path.lastIndexOf('/', path.length - 2) + 1);
685-
}
686-
687-
function getFilenameWithoutExtension(filename) {
688-
var index = filename.lastIndexOf(".");
689-
return index === -1 ? filename : filename.slice(0, index);
690-
}
691-
692682
// Is the currently opened document already a file we can use for Live Development?
693683
if (doc) {
694684
refPath = doc.file.fullPath;
@@ -715,14 +705,14 @@ define(function LiveDevelopment(require, exports, module) {
715705
}
716706

717707
var filteredFiltered = allFiles.filter(function (item) {
718-
var parent = getParentFolder(item.fullPath);
708+
var parent = FileUtils.getParentPath(item.fullPath);
719709

720710
return (containingFolder.indexOf(parent) === 0);
721711
});
722712

723713
var filterIndexFile = function (fileInfo) {
724714
if (fileInfo.fullPath.indexOf(containingFolder) === 0) {
725-
if (getFilenameWithoutExtension(fileInfo.name) === "index") {
715+
if (FileUtils.getFilenameWithoutExtension(fileInfo.name) === "index") {
726716
if (hasOwnServerForLiveDevelopment) {
727717
if ((FileUtils.isServerHtmlFileExt(fileInfo.name)) ||
728718
(FileUtils.isStaticHtmlFileExt(fileInfo.name))) {
@@ -743,7 +733,7 @@ define(function LiveDevelopment(require, exports, module) {
743733
// We found no good match
744734
if (i === -1) {
745735
// traverse the directory tree up one level
746-
containingFolder = getParentFolder(containingFolder);
736+
containingFolder = FileUtils.getParentPath(containingFolder);
747737
// Are we still inside the project?
748738
if (containingFolder.indexOf(projectRoot) === -1) {
749739
stillInProjectTree = false;

src/file/FileUtils.js

+18-3
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ define(function (require, exports, module) {
443443
}
444444

445445
/**
446-
* Get the parent directory of a file. If a directory is passed in the directory is returned.
446+
* Get the parent directory of a file. If a directory is passed, the SAME directory is returned.
447447
* @param {string} fullPath full path to a file or directory
448448
* @return {string} Returns the path to the parent directory of a file or the path of a directory,
449449
* including trailing "/"
@@ -453,8 +453,22 @@ define(function (require, exports, module) {
453453
}
454454

455455
/**
456-
* Get the file name without the extension.
457-
* @param {string} filename File name of a file or directory
456+
* Get the parent folder of the given file/folder path. Differs from getDirectoryPath() when 'fullPath'
457+
* is a directory itself: returns its parent instead of the original path. (Note: if you already have a
458+
* FileSystemEntry, it's faster to use entry.parentPath instead).
459+
* @param {string} fullPath full path to a file or directory
460+
* @return {string} Path of containing folder (including trailing "/"); or "" if path was the root
461+
*/
462+
function getParentPath(fullPath) {
463+
if (fullPath === "/") {
464+
return "";
465+
}
466+
return fullPath.substring(0, fullPath.lastIndexOf("/", fullPath.length - 2) + 1);
467+
}
468+
469+
/**
470+
* Get the file name without the extension. Returns "" if name starts with "."
471+
* @param {string} filename File name of a file or directory, without preceding path
458472
* @return {string} Returns the file name without the extension
459473
*/
460474
function getFilenameWithoutExtension(filename) {
@@ -559,6 +573,7 @@ define(function (require, exports, module) {
559573
exports.isStaticHtmlFileExt = isStaticHtmlFileExt;
560574
exports.isServerHtmlFileExt = isServerHtmlFileExt;
561575
exports.getDirectoryPath = getDirectoryPath;
576+
exports.getParentPath = getParentPath;
562577
exports.getBaseName = getBaseName;
563578
exports.getRelativeFilename = getRelativeFilename;
564579
exports.getFilenameWithoutExtension = getFilenameWithoutExtension;

test/spec/FileUtils-test.js

+35-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,30 @@ define(function (require, exports, module) {
8585
});
8686

8787
it("should return the unchanged directory of a posix directory path", function () {
88-
expect(FileUtils.getDirectoryPath("C:/foo/bar/")).toBe("C:/foo/bar/");
88+
expect(FileUtils.getDirectoryPath("/foo/bar/")).toBe("/foo/bar/");
89+
});
90+
91+
it("should return the unchanged directory of a root path", function () {
92+
expect(FileUtils.getDirectoryPath("C:/")).toBe("C:/");
93+
expect(FileUtils.getDirectoryPath("/")).toBe("/");
94+
});
95+
});
96+
97+
describe("getParentPath", function () {
98+
99+
it("should get the parent directory of a normalized file path", function () {
100+
expect(FileUtils.getParentPath("C:/foo/bar/baz.txt")).toBe("C:/foo/bar/");
101+
expect(FileUtils.getParentPath("/foo/bar/baz.txt")).toBe("/foo/bar/");
102+
});
103+
104+
it("should return the parent directory of a normalized directory path", function () {
105+
expect(FileUtils.getParentPath("C:/foo/bar/")).toBe("C:/foo/");
106+
expect(FileUtils.getParentPath("/foo/bar/")).toBe("/foo/");
107+
});
108+
109+
it("should return '' given a root path", function () {
110+
expect(FileUtils.getParentPath("C:/")).toBe("");
111+
expect(FileUtils.getParentPath("/")).toBe("");
89112
});
90113
});
91114

@@ -142,6 +165,17 @@ define(function (require, exports, module) {
142165
expect(FileUtils.getFileExtension("foo.bar.baz..jaz.txt")).toBe("txt");
143166
});
144167
});
168+
169+
describe("getFilenameWithoutExtension", function () {
170+
171+
it("should remove last extension segment only", function () {
172+
expect(FileUtils.getFilenameWithoutExtension("foo.txt")).toBe("foo");
173+
expect(FileUtils.getFilenameWithoutExtension("foo.min.txt")).toBe("foo.min");
174+
expect(FileUtils.getFilenameWithoutExtension("foo")).toBe("foo");
175+
176+
expect(FileUtils.getFilenameWithoutExtension(".foo")).toBe("");
177+
});
178+
});
145179

146180
describe("getSmartFileExtension", function () {
147181

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<html>
2+
<head>
3+
<title>Test</title>
4+
<link rel="stylesheet" href="sub/test.css" />
5+
</head>
6+
<body>
7+
<h1>Hello</h1>
8+
</body>
9+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
body { background-color: red;}
2+
3+
h1 { color: blue; }

test/spec/LiveDevelopmentMultiBrowser-test.js

+12
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,18 @@ define(function (require, exports, module) {
102102
});
103103
});
104104

105+
it("should find an index.html in a parent directory", function () {
106+
runs(function () {
107+
waitsForDone(SpecRunnerUtils.openProjectFiles(["sub/test.css"]), "SpecRunnerUtils.openProjectFiles sub/test.css", 1000);
108+
});
109+
110+
waitsForLiveDevelopmentToOpen();
111+
112+
runs(function () {
113+
expect(LiveDevelopment._getCurrentLiveDoc().doc.url).toMatch(/\/index\.html$/);
114+
});
115+
});
116+
105117
it("should send all external stylesheets as related docs on start-up", function () {
106118
var liveDoc;
107119
runs(function () {

0 commit comments

Comments
 (0)