//! The trailing-punctuation and bare-domain rules are the reader's intake//! contract, not incidental parsing.usesuper::*;/// U+00A0, U+FEFF, U+0085 and U+2028 as literals, because the boundary between/// "part of the URL" and "the sentence around it" is exactly where JavaScript's/// whitespace class and Rust's disagree.const NBSP: char = '\u{00A0}';const BOM: char = '\u{FEFF}';const NEL: char = '\u{0085}';const LS: char = '\u{2028}';fn extract(text: &str) -> Vec<String> { extract_urls(Some(text))}// ── extractUrls ──────────────────────────────────────────────────────────────#[test]fn finds_every_http_url_in_shared_text_in_order() { assert_eq!( extract("read https://a.example/x then https://b.example/y"), ["https://a.example/x", "https://b.example/y"] );}#[test]fn dedupes_while_preserving_first_seen_order() { assert_eq!( extract("https://b.example https://a.example https://b.example"), ["https://b.example", "https://a.example"] );}#[test]fn strips_sentence_punctuation_that_is_not_part_of_the_url() { assert_eq!( extract("see https://example.com/post."), ["https://example.com/post"] ); assert_eq!( extract("(https://example.com/post)"), ["https://example.com/post"] );}#[test]fn keeps_parens_that_belong_to_the_url() { assert_eq!( extract("https://en.wikipedia.org/wiki/Mercury_(planet)"), ["https://en.wikipedia.org/wiki/Mercury_(planet)"] );}#[test]fn does_not_promote_bare_domains() { assert!(extract("go to example.com sometime").is_empty());}#[test]fn ignores_a_bare_scheme_with_no_host() { assert!(extract("https:// and https://.").is_empty());}#[test]fn is_empty_for_absent_and_empty_text() { assert!(extract_urls(None).is_empty()); assert!(extract("").is_empty());}/// Node vectors. The four whitespace characters are the load-bearing ones: a/// BOM, an NBSP and a line separator END a URL because JavaScript calls them/// whitespace, and a NEL does NOT because JavaScript does not — the exact/// inverse of Rust's own `\s`, which is why the class is spelled out.#[test]fn extract_urls_matches_the_typescript() {let vectors: &[(String, &[&str])] = &[ ("read https://a.example/x then https://b.example/y".into(), &["https://a.example/x", "https://b.example/y"], ), ("https://b.example https://a.example https://b.example".into(), &["https://b.example", "https://a.example"], ), ("see https://example.com/post.".into(), &["https://example.com/post"], ), ("(https://example.com/post)".into(), &["https://example.com/post"], ), ("https://en.wikipedia.org/wiki/Mercury_(planet)".into(), &["https://en.wikipedia.org/wiki/Mercury_(planet)"], ), ("go to example.com sometime".into(), &[]), ("https:// and https://.".into(), &[]), ("".into(), &[]),// The scheme match is case-insensitive and the URL is NOT lowercased —// `canonical_url` is the only thing that does that. ("HTTP://EXAMPLE.COM/A".into(), &["HTTP://EXAMPLE.COM/A"]), ("[https://example.com/a]".into(), &["https://example.com/a"]), ("{https://example.com/a}".into(), &["https://example.com/a"]), ("https://example.com/a))".into(), &["https://example.com/a"]), ("((https://example.com/a))".into(), &["https://example.com/a"], ), ("https://example.com/a(b)c".into(), &["https://example.com/a(b)c"], ), ("https://example.com/a,".into(), &["https://example.com/a"]), ("https://example.com/a;;;".into(), &["https://example.com/a"], ), ("<https://example.com/a>".into(), &["https://example.com/a"]), ( format!("https://example.com/a{NBSP}b"), &["https://example.com/a"], ), ( format!("https://example.com/a{BOM}b"), &["https://example.com/a"], ), ( format!("https://example.com/a{NEL}b"), &["https://example.com/a\u{0085}b"], ), ( format!("https://example.com/a{LS}b"), &["https://example.com/a"], ), ("ftp://example.com/a https://ok.example".into(), &["https://ok.example"], ), ("https://example.com/a?q=1#frag.".into(), &["https://example.com/a?q=1#frag"], ), ("https://example.com/path_(disambiguation)!".into(), &["https://example.com/path_(disambiguation)"], ), ("https://ex.com/a\nhttps://ex.com/b".into(), &["https://ex.com/a", "https://ex.com/b"], ), ("https://例え.テスト/パス".into(), &["https://例え.テスト/パス"], ), ("https://example.com/a'".into(), &["https://example.com/a"]), ("https://example.com/a\"".into(), &["https://example.com/a"]), ];for (text, expected) in vectors { assert_eq!(extract(text), *expected, "for {text:?}"); }}// ── isHttpUrl ────────────────────────────────────────────────────────────────#[test]fn is_true_only_when_the_whole_trimmed_text_is_one_url() { assert!(is_http_url(Some(" https://example.com/x "))); assert!(!is_http_url(Some("read https://example.com/x"))); assert!(!is_http_url(Some("https://a.example https://b.example")));}#[test]fn rejects_bare_domains_and_empties() { assert!(!is_http_url(Some("example.com"))); assert!(!is_http_url(Some(""))); assert!(!is_http_url(None));}/// `extract_urls` strips the period, so the stripped URL no longer equals the/// raw text: the capture is prose and belongs to the classifier.#[test]fn rejects_a_url_with_trailing_punctuation() { assert!(!is_http_url(Some("https://example.com/x.")));}/// Node vectors — including the pair that shows the trim uses JavaScript's/// whitespace class: a leading BOM is trimmed away and the text IS a URL, while/// a leading NEL is not and the text is not.#[test]fn is_http_url_matches_the_typescript() {let vectors: &[(String, bool)] = &[ (" https://example.com/x ".into(), true), ("read https://example.com/x".into(), false), ("https://a.example https://b.example".into(), false), ("example.com".into(), false), ("".into(), false), ("https://example.com/x.".into(), false), ("https://example.com/x".into(), true), (format!("{BOM}https://example.com/x"), true), (format!("{NEL}https://example.com/x"), false), ("https://".into(), false), ("HTTPS://EXAMPLE.COM".into(), true), ];for (text, expected) in vectors { assert_eq!(is_http_url(Some(text)), *expected, "for {text:?}"); }}// ── hostOf ───────────────────────────────────────────────────────────────────#[test]fn host_of_drops_scheme_www_and_port() { assert_eq!( host_of("https://www.example.com:8443/a/b?c=d"),"example.com" );}#[test]fn host_of_lowercases_and_strips_userinfo() { assert_eq!(host_of("https://User:pw@Example.COM/x"), "example.com");}#[test]fn host_of_keeps_a_non_www_subdomain() { assert_eq!( host_of("https://news.ycombinator.com/item?id=1"),"news.ycombinator.com" );}#[test]fn host_of_is_empty_for_a_non_url() { assert_eq!(host_of("example.com"), "");}/// Node vectors. `www.` comes off ONCE (`www.www.` keeps one), the userinfo cut/// is at the LAST `@`, and an IPv6 authority survives with its brackets.#[test]fn host_of_matches_the_typescript() {let vectors: &[(&str, &str)] = &[ ("https://www.example.com:8443/a/b?c=d", "example.com"), ("https://User:pw@Example.COM/x", "example.com"), ("https://news.ycombinator.com/item?id=1","news.ycombinator.com", ), ("example.com", ""), (" https://Example.com ", "example.com"), ("https://www.www.example.com", "www.example.com"), ("https://wwwexample.com", "wwwexample.com"), ("http://[::1]:8080/x", "[::1]"), ("https://a@b@example.com/x", "example.com"), ("https://example.com", "example.com"), ("https://example.com#frag", "example.com"), ("https://EXAMPLE.com:80", "example.com"), ];for (url, expected) in vectors { assert_eq!(host_of(url), *expected, "for {url:?}"); }}// ── canonicalUrl ─────────────────────────────────────────────────────────────#[test]fn canonical_url_lowercases_an_auto_capitalised_scheme() { assert_eq!( canonical_url("HTTPS://en.wikipedia.org/wiki/Cotton"),"https://en.wikipedia.org/wiki/Cotton" );}#[test]fn canonical_url_lowercases_the_host_but_never_the_path_query_or_fragment() { assert_eq!( canonical_url("http://Example.COM/Wiki/Cotton?Q=Yes#Frag"),"http://example.com/Wiki/Cotton?Q=Yes#Frag" );}#[test]fn canonical_url_trims_and_returns_non_urls_unchanged() { assert_eq!(canonical_url(" not a url "), "not a url");}#[test]fn canonical_url_is_identity_on_an_already_canonical_url() { assert_eq!( canonical_url("https://news.ycombinator.com/item?id=1"),"https://news.ycombinator.com/item?id=1" );}/// Node vectors. The `[\s\S]*` tail is why a newline inside the path survives —/// which in Rust means the pattern needs `(?s)`, and forgetting it would have/// returned the input unchanged instead.#[test]fn canonical_url_matches_the_typescript() {let vectors: &[(&str, &str)] = &[ ("HTTPS://en.wikipedia.org/wiki/Cotton","https://en.wikipedia.org/wiki/Cotton", ), ("http://Example.COM/Wiki/Cotton?Q=Yes#Frag","http://example.com/Wiki/Cotton?Q=Yes#Frag", ), (" not a url ", "not a url"), ("https://news.ycombinator.com/item?id=1","https://news.ycombinator.com/item?id=1", ), ("HtTp://EXAMPLE.com", "http://example.com"), ("https://", "https://"), ("HTTPS://EXAMPLE.COM/A\nB", "https://example.com/A\nB"), (" HTTPS://EX.COM/x ", "https://ex.com/x"),// Not an http(s) URL at all: returned trimmed and otherwise untouched. ("ftp://Example.com/x", "ftp://Example.com/x"), ];for (url, expected) in vectors { assert_eq!(canonical_url(url), *expected, "for {url:?}"); }}