Skip to content

Commit 4a88a8a

Browse files
Marc-marc-marcfrodrigo
authored andcommitted
Update Phone.py to detect leading dash
fix #2610
1 parent 0b939d7 commit 4a88a8a

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

plugins/Phone.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,39 @@ def check(self, tags):
140140
err.append({"class": 30925, "subclass": stablehash64(tag), "text": T_("Not allowed char \"{0}\" in phone number tag \"{1}\"", phone_test, tag)})
141141
continue
142142

143+
# Detection and correction of invalid prefixes (e.g., "-", "-+")
144+
if phone.startswith('-'):
145+
# Case 1: Prefix "-+" (e.g., "-+32...")
146+
if phone.startswith('-+'):
147+
phone_stripped = phone[2:]
148+
if phone_stripped.startswith(self.code + " "):
149+
err.append({"class": 30924, "subclass": stablehash64(tag), "text": T_("Bad international prefix"), "fix": {tag: "+" + phone_stripped}})
150+
continue
151+
elif self.InternationalPrefix and self.InternationalPrefix.match(phone_stripped):
152+
err.append({"class": 30924, "subclass": stablehash64(tag), "text": T_("Bad international prefix"), "fix": {tag: "+" + phone_stripped}})
153+
continue
154+
elif self.MissingInternationalPrefix and self.MissingInternationalPrefix.match(phone_stripped):
155+
err.append({"class": 30924, "subclass": stablehash64(tag), "text": T_("Bad international prefix"), "fix": {tag: "+" + self.code + " " + phone_stripped}})
156+
continue
157+
# Case 2: Prefix "-" before an international prefix (e.g., "-32...")
158+
elif len(phone) > 1 and phone[1:].startswith(self.code):
159+
phone_stripped = phone[1:]
160+
if phone_stripped.startswith(self.code + " "):
161+
err.append({"class": 30924, "subclass": stablehash64(tag), "text": T_("Bad international prefix"), "fix": {tag: "+" + phone_stripped}})
162+
continue
163+
elif self.InternationalPrefix and self.InternationalPrefix.match(phone_stripped):
164+
err.append({"class": 30924, "subclass": stablehash64(tag), "text": T_("Bad international prefix"), "fix": {tag: "+" + phone_stripped}})
165+
continue
166+
# Case 3: Prefix "-" before a local prefix (e.g., "-0...")
167+
elif len(phone) > 1 and phone[1:].startswith(self.local_prefix):
168+
phone_stripped = phone[1:]
169+
if self.MissingInternationalPrefix and self.MissingInternationalPrefix.match(phone_stripped):
170+
err.append({"class": 30924, "subclass": stablehash64(tag), "text": T_("Bad international prefix"), "fix": {tag: "+" + self.code + " " + phone_stripped.replace(self.local_prefix, '', 1)}})
171+
continue
172+
# If the number remains invalid, report an error without proposing a fix
173+
err.append({"class": 30924, "subclass": stablehash64(tag), "text": T_("Invalid phone number")})
174+
continue
175+
143176
# Before local prefix
144177
if self.InternationalPrefix:
145178
r = self.InternationalPrefix.match(phone)
@@ -344,11 +377,14 @@ class father:
344377
("+32 1307", "1307"),
345378
("021231212", "+32 21231212"),
346379
("-021231212", "+32 21231212"),
380+
("-32 2 123 12 12", "+32 2 123 12 12"),
381+
("-+32 2 123 12 12", "+32 2 123 12 12"),
347382
("02 123 12 12", "+32 2 123 12 12"),
348383
):
349384
# Check the bad number's error and fix
350385
err = p.node(None, {"phone": bad})
351386
self.check_err(err, ("phone='{0}'".format(bad)))
387+
self.assertIn("fix", err[0], f"No fix proposed for phone number: {bad}")
352388
self.assertEqual(err[0]["fix"]["phone"], good)
353389

354390
# The correct number does not need fixing

0 commit comments

Comments
 (0)