mirror of
https://github.com/mozilla/pdf.js.git
synced 2026-07-29 10:27:21 +02:00
Merge pull request #20531 from calixteman/issue20523
Avoid to have a mail link with string having the format ddd@d.dddd
This commit is contained in:
commit
884cd20156
@ -90,6 +90,10 @@ describe("autolinker", function () {
|
|||||||
["partl@mail.boku.ac.at", "mailto:partl@mail.boku.ac.at"],
|
["partl@mail.boku.ac.at", "mailto:partl@mail.boku.ac.at"],
|
||||||
["Irene.Hyna@bmwf.ac.at", "mailto:Irene.Hyna@bmwf.ac.at"],
|
["Irene.Hyna@bmwf.ac.at", "mailto:Irene.Hyna@bmwf.ac.at"],
|
||||||
["<hi@foo.bar.baz>", "mailto:hi@foo.bar.baz"],
|
["<hi@foo.bar.baz>", "mailto:hi@foo.bar.baz"],
|
||||||
|
[
|
||||||
|
"foo@用户@例子.广告",
|
||||||
|
"mailto:%E7%94%A8%E6%88%B7@%E4%BE%8B%E5%AD%90.%E5%B9%BF%E5%91%8A",
|
||||||
|
],
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -144,6 +148,7 @@ describe("autolinker", function () {
|
|||||||
"http//[]", // Empty IPv6 address.
|
"http//[]", // Empty IPv6 address.
|
||||||
"abc.example.com", // URL without scheme.
|
"abc.example.com", // URL without scheme.
|
||||||
"JD?M$0QP)lKn06l1apKDC@\\qJ4B!!(5m+j.7F790m", // Not a valid email.
|
"JD?M$0QP)lKn06l1apKDC@\\qJ4B!!(5m+j.7F790m", // Not a valid email.
|
||||||
|
"262@0.302304", // Invalid domain.
|
||||||
].join("\n")
|
].join("\n")
|
||||||
);
|
);
|
||||||
expect(matches.length).toEqual(0);
|
expect(matches.length).toEqual(0);
|
||||||
|
|||||||
@ -133,10 +133,12 @@ class Autolinker {
|
|||||||
|
|
||||||
static #regex;
|
static #regex;
|
||||||
|
|
||||||
|
static #numericTLDRegex;
|
||||||
|
|
||||||
static findLinks(text) {
|
static findLinks(text) {
|
||||||
// Regex can be tested and verified at https://regex101.com/r/rXoLiT/2.
|
// Regex can be tested and verified at https://regex101.com/r/rXoLiT/2.
|
||||||
this.#regex ??=
|
this.#regex ??=
|
||||||
/\b(?:https?:\/\/|mailto:|www\.)(?:[\S--[\p{P}<>]]|\/|[\S--[\[\]]]+[\S--[\p{P}<>]])+|\b[\S--[@\p{Ps}\p{Pe}<>]]+@([\S--[\p{P}<>]]+(?:\.[\S--[\p{P}<>]]+)+)/gmv;
|
/\b(?:https?:\/\/|mailto:|www\.)(?:[\S--[\p{P}<>]]|\/|[\S--[\[\]]]+[\S--[\p{P}<>]])+|(?=\p{L})[\S--[@\p{Ps}\p{Pe}<>]]+@([\S--[\p{P}<>]]+(?:\.[\S--[\p{P}<>]]+)+)/gmv;
|
||||||
|
|
||||||
const [normalizedText, diffs] = normalize(text, { ignoreDashEOL: true });
|
const [normalizedText, diffs] = normalize(text, { ignoreDashEOL: true });
|
||||||
const matches = normalizedText.matchAll(this.#regex);
|
const matches = normalizedText.matchAll(this.#regex);
|
||||||
@ -150,11 +152,19 @@ class Autolinker {
|
|||||||
url.startsWith("https://")
|
url.startsWith("https://")
|
||||||
) {
|
) {
|
||||||
raw = url;
|
raw = url;
|
||||||
} else if (URL.canParse(`http://${emailDomain}`)) {
|
} else if (emailDomain) {
|
||||||
raw = url.startsWith("mailto:") ? url : `mailto:${url}`;
|
const hostname = URL.parse(`http://${emailDomain}`)?.hostname;
|
||||||
} else {
|
if (!hostname) {
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
|
this.#numericTLDRegex ??= /\.\d+$/;
|
||||||
|
if (this.#numericTLDRegex.test(hostname)) {
|
||||||
|
// Skip emails with a numeric TLD as domain.
|
||||||
|
continue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
raw ??= url.startsWith("mailto:") ? url : `mailto:${url}`;
|
||||||
|
|
||||||
const absoluteURL = createValidAbsoluteUrl(raw, null, {
|
const absoluteURL = createValidAbsoluteUrl(raw, null, {
|
||||||
addDefaultProtocol: true,
|
addDefaultProtocol: true,
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user