Philipp Hörist pushed to branch master at gajim / gajim
Commits: c8f9bbdb by lovetox at 2022-06-19T23:51:31+02:00 fix: Styling: Parse text correctly which contains uris and addresses
Use one combined regex for uris and addresses so we get no overlaps
Fixes #10974
- - - - - be62995e by lovetox at 2022-06-20T00:20:29+02:00 fix: Styling: Improve uri regex
Add more url delimiters
- - - - - 03033dfa by lovetox at 2022-06-20T00:21:05+02:00 fix: Styling: Improve address regex
Allow "-" in domain part
- - - - - 6df045ce by lovetox at 2022-06-20T00:21:08+02:00 chore: Styling: Add more tests
- - - - -
2 changed files:
- gajim/common/styling.py - test/no_gui/test_styling.py
Changes:
===================================== gajim/common/styling.py ===================================== @@ -43,11 +43,13 @@ BLOCK_NESTED_RX = re.compile(PRE_NESTED_RX + '|' + QUOTE_RX, re.S | re.M) UNQUOTE_RX = re.compile(r'^> |^>', re.M)
-URI_RX = r'((?P<protocol>[\w-]+://?|www[.])[\S()<>]+?(?=[,]?(\s|\Z)+))' -URI_RX = re.compile(URI_RX) +URI_RX = r'(?P<uri>([\w-]+://|www[.])[\S()<>]+?(?=[,>]?(\s|\Z)+))' +ADDRESS_RX = r'(?P<address>\b((xmpp|mailto):)?[\w-]*@([\w-]*?.)+[\w]+([?].*?(?=([\s),]|$)))?)' # noqa: E501 +URI_ADDRESS_RX = URI_RX + '|' + ADDRESS_RX
-ADDRESS_RX = r'(\b(?P<protocol>(xmpp|mailto):)?[\w-]*@(\w*?.)+[\w]+([?].*?(?=([\s),]|$)))?)' # noqa: E501 +URI_RX = re.compile(URI_RX) ADDRESS_RX = re.compile(ADDRESS_RX) +URI_ADDRESS_RX = re.compile(URI_ADDRESS_RX)
EMOJI_RX = emoji_data.get_regex() EMOJI_RX = re.compile(EMOJI_RX) @@ -319,12 +321,8 @@ def make(match: Match[str], is_address: bool) -> BaseUri: offset_bytes, is_address)
- for match in URI_RX.finditer(line): - uri = make(match, False) - uris.append(uri) - - for match in ADDRESS_RX.finditer(line): - uri = make(match, True) + for match in URI_ADDRESS_RX.finditer(line): + uri = make(match, bool(match.group('address'))) uris.append(uri)
return uris
===================================== test/no_gui/test_styling.py ===================================== @@ -325,6 +325,8 @@ EMAILS = [ 'asd@asd.at', 'asd@asd.asd.at', + 'asd@asd.asd-asd.at', + 'asd-asd@asd.asdasd.at', 'mailto:foo@bar.com.uk', ]
@@ -339,6 +341,7 @@ URL_WITH_TEXT = [ ('see this http://userid@example.com/ link', 'http://userid@example.com/'), ('see this http://userid@example.com/, and ..', 'http://userid@example.com/'), + ('http://userid@example.com/', 'http://userid@example.com/'), ]
XMPP_URIS = [ @@ -392,6 +395,5 @@ def test_xmpp_uris(self): self.assertTrue(text[start:end] == result)
- if __name__ == "__main__": unittest.main()
View it on GitLab: https://dev.gajim.org/gajim/gajim/-/compare/9914c246090155bf1f8daba85424b3c6...