Skip to content

Commit 7a5e5c3

Browse files
Added error handling and better regex
Fixed #177
1 parent d15d5df commit 7a5e5c3

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/WebCompiler/Dependencies/SassDependencyResolver.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,14 @@ public override void UpdateFileDependencies(string path)
4949
string content = File.ReadAllText(info.FullName);
5050

5151
//match both <@import "myFile.scss";> and <@import url("myFile.scss");> syntax
52-
var matches = System.Text.RegularExpressions.Regex.Matches(content, "@import([\\s]+)(\\([\\S]+\\)([\\s]+))?(url\\()?('|\"|)(?<url>[^'\"\\)]+)('|\"|\\))");
52+
var matches = System.Text.RegularExpressions.Regex.Matches(content, "@import([\\s]+)(\\([\\S]+\\)([\\s]+))?(url\\()?('|\"|)(?<url>[^'\"\\):?:]+)('|\"|\\))");
5353
foreach (System.Text.RegularExpressions.Match match in matches)
5454
{
55-
FileInfo importedfile = new FileInfo(Path.Combine(info.DirectoryName, match.Groups["url"].Value));
55+
FileInfo importedfile = GetFileInfo(info, match);
56+
57+
if (importedfile == null)
58+
continue;
59+
5660
//if the file doesn't end with the correct extension, an import statement without extension is probably used, to re-add the extension (#175)
5761
if (string.Compare(importedfile.Extension, FileExtension, StringComparison.OrdinalIgnoreCase) != 0)
5862
{
@@ -72,5 +76,20 @@ public override void UpdateFileDependencies(string path)
7276
}
7377
}
7478
}
79+
80+
private static FileInfo GetFileInfo(FileInfo info, System.Text.RegularExpressions.Match match)
81+
{
82+
string url = match.Groups["url"].Value;
83+
84+
try
85+
{
86+
return new FileInfo(Path.Combine(info.DirectoryName, match.Groups["url"].Value));
87+
}
88+
catch (Exception)
89+
{
90+
// Not a valid file name
91+
return null;
92+
}
93+
}
7594
}
7695
}

0 commit comments

Comments
 (0)