Skip to content

Commit

Permalink
Merge pull request #207 from DavidVanDeursen/fix-external-url-path-regex
Browse files Browse the repository at this point in the history
Fix external url path regex
  • Loading branch information
WillemJann authored Jan 7, 2025
2 parents 21b12fb + 02733c1 commit c5299ad
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
28 changes: 26 additions & 2 deletions src/XLParser.Tests/FormulaAnalysisTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -636,14 +636,38 @@ public void ExternalWorkbookUrlPathHttpWithSpaceInDocument()
Assert.AreEqual("Sheet1", references.First().Worksheet);
}

[TestMethod]
public void ExternalWorkbookUrlPathHttpWithTildeInPath()
{
// See [#202](https://github.com/spreadsheetlab/XLParser/issues/202)
List<ParserReference> references = new FormulaAnalyzer(@"='http://example.com/~testfolder/[Book1.xlsx]Sheet1'!$A$1").ParserReferences().ToList();

Assert.AreEqual(1, references.Count);
Assert.AreEqual(@"http://example.com/~testfolder/", references.First().FilePath);
Assert.AreEqual("Book1.xlsx", references.First().FileName);
Assert.AreEqual("Sheet1", references.First().Worksheet);
}

[TestMethod]
public void ExternalWorkbookUrlPathHttpWithBackslashInPath()
{
// See [#201](https://github.com/spreadsheetlab/XLParser/issues/201)
List<ParserReference> references = new FormulaAnalyzer(@"='http:\\example.com\testfolder\[Book1.xlsx]Sheet1'!$A$1").ParserReferences().ToList();

Assert.AreEqual(1, references.Count);
Assert.AreEqual(@"http:\\example.com\testfolder\", references.First().FilePath);
Assert.AreEqual("Book1.xlsx", references.First().FileName);
Assert.AreEqual("Sheet1", references.First().Worksheet);
}

[TestMethod]
public void ExternalWorkbookUrlPathHttpWithPortNumberInPath()
{
// See [#194](https://github.com/spreadsheetlab/XLParser/issues/194)
List<ParserReference> references = new FormulaAnalyzer(@"='http://example.com:1234/test/[Book1.xlsx]Sheet1'!$A$1").ParserReferences().ToList();
List<ParserReference> references = new FormulaAnalyzer(@"='http://localhost:1234/test/[Book1.xlsx]Sheet1'!$A$1").ParserReferences().ToList();

Assert.AreEqual(1, references.Count);
Assert.AreEqual(@"http://example.com:1234/test/", references.First().FilePath);
Assert.AreEqual(@"http://localhost:1234/test/", references.First().FilePath);
Assert.AreEqual("Book1.xlsx", references.First().FileName);
Assert.AreEqual("Sheet1", references.First().Worksheet);
}
Expand Down
2 changes: 1 addition & 1 deletion src/XLParser/ExcelFormulaGrammar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public class ExcelFormulaGrammar : Grammar

// Source: http://stackoverflow.com/a/6416209/572635
private const string windowsFilePathRegex = @"(?:[a-zA-Z]:|\\?\\?[\w\-.$ @~]+)\\(([^<>\"" /\|?*\\']|( |''))*\\)*";
private const string urlPathRegex = @"http(s?)\://([\p{L}\p{N}-_]+\.[\p{L}\p{N}-_]*)+(:[0-9]+)?/([\p{L}\p{N}\-\.\?\,\'+&%\$#_ ()]*/)*";
private const string urlPathRegex = @"https?\:(//|\\\\)[\p{L}\p{N}\-_.]+(:[0-9]+)?(/|\\)([\p{L}\p{N}\-_.?,'+&%\$# ()~]*(/|\\))*";
private const string filePathRegex = @"(" + windowsFilePathRegex + @"|" + urlPathRegex + @")";
public Terminal FilePathToken { get; } = new RegexBasedTerminal(GrammarNames.TokenFilePath, filePathRegex)
{ Priority = TerminalPriority.FileNamePath };
Expand Down

0 comments on commit c5299ad

Please sign in to comment.