Skip to content

Commit 096bcbf

Browse files
authored
Support virtual paths (~/) in RelativePathAdjuster (#318)
[release]
1 parent 3952d89 commit 096bcbf

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/WebOptimizer.Core/Processors/RelativePathAdjuster.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.IO;
43
using System.Text.RegularExpressions;
54
using System.Threading.Tasks;
6-
using System.Linq;
75
using Microsoft.AspNetCore.Hosting;
86
using Microsoft.Extensions.FileProviders;
97
using WebOptimizer;
10-
using Microsoft.AspNetCore.DataProtection.KeyManagement;
118
using WebOptimizer.Utils;
129

1310
namespace WebOptimizer
@@ -75,9 +72,17 @@ private static byte[] Adjust(IAssetContext config, string key)
7572
string queryOnly = pathAndQuery.Length == 2 ? ("?" + pathAndQuery[1]) : string.Empty;
7673

7774
// get filepath of included file
78-
if (!UrlPathUtils.TryMakeAbsolutePathFromInclude(appPath, key, pathOnly, out string filePath))
79-
// path to included file is invalid
80-
return match.Value;
75+
string filePath;
76+
if (pathOnly.StartsWith("~/"))
77+
{
78+
filePath = UrlPathUtils.MakeAbsolute(appPath, pathOnly.Substring(2));
79+
}
80+
else
81+
{
82+
if (!UrlPathUtils.TryMakeAbsolutePathFromInclude(appPath, key, pathOnly, out filePath))
83+
// path to included file is invalid
84+
return match.Value;
85+
}
8186

8287
string relativePath = MakeRelative(routePath, filePath);
8388

test/WebOptimizer.Core.Test/Processors/RelativePathAdjusterTest.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Collections.Generic;
2-
using System.IO;
32
using System.Linq;
43
using System.Threading.Tasks;
54
using Microsoft.AspNetCore.Hosting;
@@ -28,6 +27,11 @@ public class RelativePathAdjusterTest
2827
[InlineData("/dist/all.css", "css/site.css", "url(../img/foo.png)", "url(../img/foo.png)")]
2928
[InlineData("dist/all.css", "css/site.css", "url(img/foo.png)", "url(../css/img/foo.png)")]
3029
[InlineData("dist/all.css", "css/site.css", "url(../img/foo.png)", "url(../img/foo.png)")]
30+
[InlineData("/css/all.css", "css/site.css", "url(~/img/foo.png)", "url(../img/foo.png)")]
31+
[InlineData("/css/all.css", "css/sub/site.css", "url(~/img/foo.png)", "url(../img/foo.png)")]
32+
[InlineData("/dist/sub/all.css", "css/sub/site.css", "url(~/img/foo.png)", "url(../../img/foo.png)")]
33+
[InlineData("/dist/sub/all.css", "css/site.css", "url(~/img/foo.png)", "url(../../img/foo.png)")]
34+
[InlineData("dist/sub/all.css", "css/site.css", "url(~/img/foo.png)", "url(../../img/foo.png)")]
3135
public async Task AdjustRelativePaths_Success(string route, string inputPath, string url, string newUrl)
3236
{
3337
var adjuster = new RelativePathAdjuster();

0 commit comments

Comments
 (0)