Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix external url path regex #207

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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