Skip to content

Commit 4601b7c

Browse files
author
Richard Jones
committed
Make URL matching code less matchy
1 parent 3ce5830 commit 4601b7c

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ are given with the most recent entry first.
44
2008-09-01 1.4.6
55
Fixed:
66
- Bug introduced in 1.4.5 in RDBMS full-text indexing
7+
- Make URL matching code less matchy
78

89

910
2008-08-19 1.4.5

roundup/cgi/templating.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,11 +1247,18 @@ def is_view_ok(self):
12471247
class StringHTMLProperty(HTMLProperty):
12481248
hyper_re = re.compile(r'''(
12491249
(?P<url>
1250-
((ht|f)tp(s?)://|www\.)? # prefix
1250+
(
1251+
(ht|f)tp(s?):// # protocol
12511252
([\w]+:\w+@)? # username/password
1252-
(([\w\-]+\.)+([\w]{2,5})) # hostname
1253-
(:[\d]{1,5})? # port
1254-
(/[\w\-$.+!*(),;:@&=?/~\\#%]*)? # path etc.
1253+
([\w\-]+\.)+ # hostname
1254+
[\w]{2,5} # TLD
1255+
| # ... or ...
1256+
www\. # "www."
1257+
([\w\-]+\.)+ # hostname
1258+
[\w]{2,5} # TLD
1259+
)
1260+
(:[\d]{1,5})? # port
1261+
(/[\w\-$.+!*(),;:@&=?/~\\#%]*)? # path etc.
12551262
)|
12561263
(?P<email>[-+=%/\w\.]+@[\w\.\-]+)|
12571264
(?P<item>(?P<class>[A-Za-z_]+)(\s*)(?P<id>\d+))

test/test_templating.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,13 @@ def lookup(key) :
9191
def test_url_match(self):
9292
'''Test the URL regular expression in StringHTMLProperty.
9393
'''
94-
def t(s, **groups):
94+
def t(s, nothing=False, **groups):
9595
m = StringHTMLProperty.hyper_re.search(s)
96-
self.assertNotEquals(m, None, '%r did not match'%s)
96+
if nothing:
97+
self.assertEquals(m, None, '%r matched'%s)
98+
return
99+
else:
100+
self.assertNotEquals(m, None, '%r did not match'%s)
97101
d = m.groupdict()
98102
for g in groups:
99103
self.assertEquals(d[g], groups[g], '%s %r != %r in %r'%(g, d[g],
@@ -103,10 +107,12 @@ def t(s, **groups):
103107
t('http://roundup.net/', url='http://roundup.net/')
104108
t('<HTTP://roundup.net/>', url='HTTP://roundup.net/')
105109
t('www.a.ex', url='www.a.ex')
110+
t('foo.a.ex', nothing=True)
111+
t('StDevValidTimeSeries.GetObservation', nothing=True)
106112
t('http://a.ex', url='http://a.ex')
107113
t('http://a.ex/?foo&bar=baz\\.@!$%()qwerty',
108114
url='http://a.ex/?foo&bar=baz\\.@!$%()qwerty')
109-
t('www.net', url='www.net')
115+
t('www.foo.net', url='www.foo.net')
110116
111117
112118
t('i1', **{'class':'i', 'id':'1'})

0 commit comments

Comments
 (0)