Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Commit

Permalink
Ensure results of GlobbingUrlBuilder are sorted:
Browse files Browse the repository at this point in the history
  • Loading branch information
DamianEdwards committed Feb 24, 2015
1 parent c8d17dd commit 16c1166
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 51 deletions.
75 changes: 32 additions & 43 deletions src/Microsoft.AspNet.Mvc.TagHelpers/Internal/GlobbingUrlBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,59 +131,64 @@ private IEnumerable<string> FindFiles(IEnumerable<string> includePatterns, IEnum
.OrderBy(path => path, new PathComparer());
}

private string ResolveMatchedPath(string matchedPath)
{
// Resolve the path to site root
var relativePath = new PathString("/" + matchedPath);
return RequestPathBase.Add(relativePath).ToString();
}

private class PathComparer : IComparer<string>
{
public int Compare(string x, string y)
{
// < 0 = x < y
// > 0 = x > y

var xSegments = x.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
var ySegments = y.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
if (x.Equals(y))
{
return 0;
}

var xExtIndex = x.LastIndexOf('.');
var yExtIndex = y.LastIndexOf('.');

var xNoExt = x.Substring(0, xExtIndex);
var yNoExt = y.Substring(0, yExtIndex);

if (xNoExt.Equals(yNoExt))
{
// Only extension differs so just compare the extension
var xExt = x.Substring(xExtIndex);
var yExt = y.Substring(yExtIndex);
return xExt.CompareTo(yExt);
}

var xSegments = xNoExt.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
var ySegments = yNoExt.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

if (xSegments.Length != ySegments.Length)
{
// Different path depths so shallower path wins
return xSegments.Length.CompareTo(ySegments.Length);
}

// Depth is the same and so do standard alphabetical comparison on each segment
// Depth is the same so compare each segment
for (int i = 0; i < xSegments.Length; i++)
{
var xSegment = xSegments[i];
var ySegment = ySegments[i];
var xToY = string.Compare(xSegment, ySegment);

var xToY = xSegment.CompareTo(ySegment);
if (xToY != 0)
{
return xToY;
}
}

// Should't get here, but if we do, hey, they're the same :)
return 0;
}

private static int GetPathDepth(string path)
{
var count = 0;

for (int i = 0; i < path.Length; i++)
{
var c = path[i];
if (c == '/' || c == '\\')
{
count++;
}
}

return count;
}
}

private string ResolveMatchedPath(string matchedPath)
{
// Resolve the path to site root
var relativePath = new PathString("/" + matchedPath);
return RequestPathBase.Add(relativePath).ToString();
}

private static string TrimLeadingSlash(string value)
Expand All @@ -199,21 +204,5 @@ private static string TrimLeadingSlash(string value)

return result;
}

private int GetPathDepth(string path)
{
var count = 0;

for (int i = 0; i < path.Length; i++)
{
var c = path[i];
if (c == '/' || c == '\\')
{
count++;
}
}

return count;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,33 +92,76 @@ public static TheoryData OrdersGlobbedMatchResultsCorrectly_Data
new FileNode("c.css"),
new FileNode("d.css")
}),
new FileNode("__A", new [] {
new FileNode("_A", new [] {
new FileNode("1.css"),
new FileNode("2.css")
}),
new FileNode("__A", new [] {
new FileNode("1.css"),
new FileNode("_1.css")
})
}),
/* expectedPaths */ new []
{
"/site.css",
"/__A/1.css", "/__A/2.css",
"/__A/_1.css", "/__A/1.css",
"/_A/1.css", "/_A/2.css",
"/A/c.css", "/A/d.css",
}
},
{
/* staticUrl */ "/site.css",
/* dirStructure */ new FileNode(null, new [] {
new FileNode("A", new [] {
new FileNode("1.css")
new FileNode("a.b.css"),
new FileNode("a-b.css"),
new FileNode("a_b.css"),
new FileNode("a.css"),
new FileNode("a.c.css")
})
}),
/* expectedPaths */ new []
{
"/site.css",
"/A/a.css", "/A/a.b.css", "/A/a.c.css", "/A/a_b.css", "/A/a-b.css"
}
},
{
/* staticUrl */ "/site.css",
/* dirStructure */ new FileNode(null, new [] {
new FileNode("B", new [] {
new FileNode("a.bss"),
new FileNode("a.css")
}),
new FileNode("A", new [] {
new FileNode("a.css"),
new FileNode("a.bss")
})
}),
/* expectedPaths */ new []
{
"/site.css",
"/A/a.bss", "/A/a.css",
"/B/a.bss", "/B/a.css"
}
},
{
/* staticUrl */ "/site.css",
/* dirStructure */ new FileNode(null, new [] {
new FileNode("B", new [] {
new FileNode("site2.css"),
new FileNode("site11.css")
}),
new FileNode("__A", new [] {
new FileNode("1.css")
new FileNode("A", new [] {
new FileNode("site2.css"),
new FileNode("site11.css")
})
}),
/* expectedPaths */ new []
{
"/site.css",
"/__A/1.css",
"/A/1.css"
"/A/site11.css", "/A/site2.css",
"/B/site11.css", "/B/site2.css"
}
}
};
Expand All @@ -136,7 +179,7 @@ public void OrdersGlobbedMatchResultsCorrectly(string staticUrl, FileNode dirStr
var globbingUrlBuilder = new GlobbingUrlBuilder(fileProvider, cache, requestPathBase);

// Act
var urlList = globbingUrlBuilder.BuildUrlList(staticUrl, "**/*.css", excludePattern: null);
var urlList = globbingUrlBuilder.BuildUrlList(staticUrl, "**/*.*", excludePattern: null);

// Assert
var collectionAssertions = expectedPaths.Select<string, Action<string>>(expected =>
Expand Down

0 comments on commit 16c1166

Please sign in to comment.