Add IPv6 URL pattern support with comprehensive test cases

- Add IPv6 URL pattern matching to support URLs like http://[::]:8000/
- Separate IPv6 URL pattern from main regex for better maintainability
- Add extensive test cases covering:
  - Basic IPv6 URLs with ports
  - URLs with paths and query parameters
  - Compressed IPv6 forms
  - Link-local and multicast addresses
  - Mixed scenarios and markdown contexts
This commit is contained in:
Bryan Lee
2025-01-07 14:14:51 +08:00
parent a3837a1e4e
commit c8d5b2da45

View File

@ -24,12 +24,18 @@ const oni = @import("oniguruma");
/// handling them well requires a non-regex approach. /// handling them well requires a non-regex approach.
pub const regex = pub const regex =
"(?:" ++ url_schemes ++ "(?:" ++ url_schemes ++
\\)(?:[\w\-.~:/?#@!$&*+,;=%]+(?:[\(\[]\w*[\)\]])?)+(?<![,.])|(?:\.\.\/|\.\/*|\/)[\w\-.~:\/?#@!$&*+,;=%]+(?:\/[\w\-.~:\/?#@!$&*+,;=%]*)* \\)(?:
++ ipv6_url_pattern ++
\\|[\w\-.~:/?#@!$&*+,;=%]+(?:[\(\[]\w*[\)\]])?)+(?<![,.])|(?:\.\.\/|\.\/*|\/)[\w\-.~:\/?#@!$&*+,;=%]+(?:\/[\w\-.~:\/?#@!$&*+,;=%]*)*
; ;
const url_schemes = const url_schemes =
\\https?://|mailto:|ftp://|file:|ssh:|git://|ssh://|tel:|magnet:|ipfs://|ipns://|gemini://|gopher://|news: \\https?://|mailto:|ftp://|file:|ssh:|git://|ssh://|tel:|magnet:|ipfs://|ipns://|gemini://|gopher://|news:
; ;
const ipv6_url_pattern =
\\(?:\[[:0-9a-fA-F]+(?:[:0-9a-fA-F]*)+\](?::[0-9]+)?)
;
test "url regex" { test "url regex" {
const testing = std.testing; const testing = std.testing;
@ -194,6 +200,55 @@ test "url regex" {
.input = "[link](/home/user/ghostty.user/example)", .input = "[link](/home/user/ghostty.user/example)",
.expect = "/home/user/ghostty.user/example", .expect = "/home/user/ghostty.user/example",
}, },
// IPv6 URL tests - Basic tests
.{
.input = "Serving HTTP on :: port 8000 (http://[::]:8000/)",
.expect = "http://[::]:8000/",
},
.{
.input = "IPv6 address https://[2001:db8::1]:8080/path",
.expect = "https://[2001:db8::1]:8080/path",
},
.{
.input = "IPv6 localhost http://[::1]:3000",
.expect = "http://[::1]:3000",
},
.{
.input = "Complex IPv6 https://[2001:db8:85a3:8d3:1319:8a2e:370:7348]:443/",
.expect = "https://[2001:db8:85a3:8d3:1319:8a2e:370:7348]:443/",
},
// IPv6 URL tests - URLs with paths and query parameters
.{
.input = "IPv6 with path https://[2001:db8::1]/path/to/resource",
.expect = "https://[2001:db8::1]/path/to/resource",
},
.{
.input = "IPv6 with query https://[2001:db8::1]:8080/api?param=value&other=123",
.expect = "https://[2001:db8::1]:8080/api?param=value&other=123",
},
// IPv6 URL tests - Compressed forms
.{
.input = "IPv6 compressed http://[2001:db8::]:80/",
.expect = "http://[2001:db8::]:80/",
},
.{
.input = "IPv6 multiple zeros http://[2001:0:0:0:0:0:0:1]",
.expect = "http://[2001:0:0:0:0:0:0:1]",
},
// IPv6 URL tests - Special cases
.{
.input = "IPv6 link-local https://[fe80::1234:5678:9abc]",
.expect = "https://[fe80::1234:5678:9abc]",
},
.{
.input = "IPv6 multicast http://[ff02::1]/stream",
.expect = "http://[ff02::1]/stream",
},
// IPv6 URL tests - Mixed scenarios
.{
.input = "IPv6 in markdown [link](http://[2001:db8::1]/docs)",
.expect = "http://[2001:db8::1]/docs",
},
}; };
for (cases) |case| { for (cases) |case| {