minor improve html.py, new url_encode=True/False test, review and close 1279
This commit is contained in:
+7
-9
@@ -317,7 +317,7 @@ def URL(a=None,
|
|||||||
response = current.response
|
response = current.response
|
||||||
if response.static_version and response.static_version_urls:
|
if response.static_version and response.static_version_urls:
|
||||||
args = [function] + args
|
args = [function] + args
|
||||||
function = '_'+str(response.static_version)
|
function = '_' + str(response.static_version)
|
||||||
|
|
||||||
if '.' in function:
|
if '.' in function:
|
||||||
function, extension = function.rsplit('.', 1)
|
function, extension = function.rsplit('.', 1)
|
||||||
@@ -330,18 +330,16 @@ def URL(a=None,
|
|||||||
if args:
|
if args:
|
||||||
if url_encode:
|
if url_encode:
|
||||||
if encode_embedded_slash:
|
if encode_embedded_slash:
|
||||||
other = '/' + '/'.join([urllib.quote(str(
|
other = '/' + '/'.join([urllib.quote(str(x), '') for x in args])
|
||||||
x), '') for x in args])
|
|
||||||
else:
|
else:
|
||||||
other = args and urllib.quote(
|
other = args and urllib.quote('/' + '/'.join([str(x) for x in args]))
|
||||||
'/' + '/'.join([str(x) for x in args]))
|
|
||||||
else:
|
else:
|
||||||
other = args and ('/' + '/'.join([str(x) for x in args]))
|
other = args and ('/' + '/'.join([str(x) for x in args]))
|
||||||
else:
|
else:
|
||||||
other = ''
|
other = ''
|
||||||
|
|
||||||
if other.endswith('/'):
|
if other.endswith('/'):
|
||||||
other += '/' # add trailing slash to make last trailing empty arg explicit
|
other += '/' # add trailing slash to make last trailing empty arg explicit
|
||||||
|
|
||||||
list_vars = []
|
list_vars = []
|
||||||
for (key, vals) in sorted(vars.items()):
|
for (key, vals) in sorted(vars.items()):
|
||||||
@@ -364,11 +362,11 @@ def URL(a=None,
|
|||||||
h_args = '/%s/%s/%s%s' % (application, controller, function2, other)
|
h_args = '/%s/%s/%s%s' % (application, controller, function2, other)
|
||||||
|
|
||||||
# how many of the vars should we include in our hash?
|
# how many of the vars should we include in our hash?
|
||||||
if hash_vars is True: # include them all
|
if hash_vars is True: # include them all
|
||||||
h_vars = list_vars
|
h_vars = list_vars
|
||||||
elif hash_vars is False: # include none of them
|
elif hash_vars is False: # include none of them
|
||||||
h_vars = ''
|
h_vars = ''
|
||||||
else: # include just those specified
|
else: # include just those specified
|
||||||
if hash_vars and not isinstance(hash_vars, (list, tuple)):
|
if hash_vars and not isinstance(hash_vars, (list, tuple)):
|
||||||
hash_vars = [hash_vars]
|
hash_vars = [hash_vars]
|
||||||
h_vars = [(k, v) for (k, v) in list_vars if k in hash_vars]
|
h_vars = [(k, v) for (k, v) in list_vars if k in hash_vars]
|
||||||
|
|||||||
+27
-44
@@ -98,6 +98,11 @@ class TestBareHelpers(unittest.TestCase):
|
|||||||
self.assertEqual(rtn, '/a/c/f/x/y/z?p=1&p=3&q=2&_signature=5d01b982fd72b39674b012e0288071034e156d7a')
|
self.assertEqual(rtn, '/a/c/f/x/y/z?p=1&p=3&q=2&_signature=5d01b982fd72b39674b012e0288071034e156d7a')
|
||||||
rtn = URL('a', 'c', 'f', args=['x', 'y', 'z'], vars={'p': (1, 3), 'q': 2}, hmac_key='key', hash_vars='p')
|
rtn = URL('a', 'c', 'f', args=['x', 'y', 'z'], vars={'p': (1, 3), 'q': 2}, hmac_key='key', hash_vars='p')
|
||||||
self.assertEqual(rtn, '/a/c/f/x/y/z?p=1&p=3&q=2&_signature=5d01b982fd72b39674b012e0288071034e156d7a')
|
self.assertEqual(rtn, '/a/c/f/x/y/z?p=1&p=3&q=2&_signature=5d01b982fd72b39674b012e0288071034e156d7a')
|
||||||
|
# test url_encode
|
||||||
|
rtn = URL('a', 'c', 'f', args=['x', 'y', 'z'], vars={'maï': (1, 3), 'lié': 2}, url_encode=False)
|
||||||
|
self.assertEqual(rtn, '/a/c/f/x/y/z?li\xc3\xa9=2&ma\xc3\xaf=1&ma\xc3\xaf=3')
|
||||||
|
rtn = URL('a', 'c', 'f', args=['x', 'y', 'z'], vars={'maï': (1, 3), 'lié': 2}, url_encode=True)
|
||||||
|
self.assertEqual(rtn, '/a/c/f/x/y/z?li%C3%A9=2&ma%C3%AF=1&ma%C3%AF=3')
|
||||||
# test CRLF detection
|
# test CRLF detection
|
||||||
self.assertRaises(SyntaxError, URL, *['a\n', 'c', 'f'])
|
self.assertRaises(SyntaxError, URL, *['a\n', 'c', 'f'])
|
||||||
self.assertRaises(SyntaxError, URL, *['a\r', 'c', 'f'])
|
self.assertRaises(SyntaxError, URL, *['a\r', 'c', 'f'])
|
||||||
@@ -394,18 +399,12 @@ class TestBareHelpers(unittest.TestCase):
|
|||||||
self.assertEqual(HR(_a='1', _b='2').xml(), '<hr a="1" b="2" />')
|
self.assertEqual(HR(_a='1', _b='2').xml(), '<hr a="1" b="2" />')
|
||||||
|
|
||||||
def test_A(self):
|
def test_A(self):
|
||||||
self.assertEqual(
|
self.assertEqual(A('<>', _a='1', _b='2').xml(),
|
||||||
A('<>', _a='1', _b='2').xml(),
|
'<a a="1" b="2"><></a>')
|
||||||
'<a a="1" b="2"><></a>'
|
self.assertEqual(A('a', cid='b').xml(),
|
||||||
)
|
'<a data-w2p_disable_with="default" data-w2p_method="GET" data-w2p_target="b">a</a>')
|
||||||
self.assertEqual(
|
self.assertEqual(A('a', callback='b', _id='c').xml(),
|
||||||
A('a', cid='b').xml(),
|
'<a data-w2p_disable_with="default" data-w2p_method="POST" href="b" id="c">a</a>')
|
||||||
'<a data-w2p_disable_with="default" data-w2p_method="GET" data-w2p_target="b">a</a>'
|
|
||||||
)
|
|
||||||
self.assertEqual(
|
|
||||||
A('a', callback='b', _id='c').xml(),
|
|
||||||
'<a data-w2p_disable_with="default" data-w2p_method="POST" href="b" id="c">a</a>'
|
|
||||||
)
|
|
||||||
# Callback with no id trigger web2py_uuid() call
|
# Callback with no id trigger web2py_uuid() call
|
||||||
from html import web2pyHTMLParser
|
from html import web2pyHTMLParser
|
||||||
a = A('a', callback='b').xml()
|
a = A('a', callback='b').xml()
|
||||||
@@ -413,38 +412,22 @@ class TestBareHelpers(unittest.TestCase):
|
|||||||
uuid_generated = tag.attributes['_id']
|
uuid_generated = tag.attributes['_id']
|
||||||
self.assertEqual(a,
|
self.assertEqual(a,
|
||||||
'<a data-w2p_disable_with="default" data-w2p_method="POST" href="b" id="{id}">a</a>'.format(id=uuid_generated))
|
'<a data-w2p_disable_with="default" data-w2p_method="POST" href="b" id="{id}">a</a>'.format(id=uuid_generated))
|
||||||
self.assertEqual(
|
self.assertEqual(A('a', delete='tr').xml(),
|
||||||
A('a', delete='tr').xml(),
|
'<a data-w2p_disable_with="default" data-w2p_remove="tr">a</a>')
|
||||||
'<a data-w2p_disable_with="default" data-w2p_remove="tr">a</a>'
|
self.assertEqual(A('a', _id='b', target='<self>').xml(),
|
||||||
)
|
'<a data-w2p_disable_with="default" data-w2p_target="b" id="b">a</a>')
|
||||||
self.assertEqual(
|
self.assertEqual(A('a', component='b').xml(),
|
||||||
A('a', _id='b', target='<self>').xml(),
|
'<a data-w2p_disable_with="default" data-w2p_method="GET" href="b">a</a>')
|
||||||
'<a data-w2p_disable_with="default" data-w2p_target="b" id="b">a</a>'
|
self.assertEqual(A('a', _id='b', callback='c', noconfirm=True).xml(),
|
||||||
)
|
'<a data-w2p_disable_with="default" data-w2p_method="POST" href="c" id="b">a</a>')
|
||||||
self.assertEqual(
|
self.assertEqual(A('a', cid='b').xml(),
|
||||||
A('a', component='b').xml(),
|
'<a data-w2p_disable_with="default" data-w2p_method="GET" data-w2p_target="b">a</a>')
|
||||||
'<a data-w2p_disable_with="default" data-w2p_method="GET" href="b">a</a>'
|
self.assertEqual(A('a', cid='b', _disable_with='processing...').xml(),
|
||||||
)
|
'<a data-w2p_disable_with="processing..." data-w2p_method="GET" data-w2p_target="b">a</a>')
|
||||||
self.assertEqual(
|
self.assertEqual(A('a', callback='b', delete='tr', noconfirm=True, _id='c').xml(),
|
||||||
A('a', _id='b', callback='c', noconfirm=True).xml(),
|
'<a data-w2p_disable_with="default" data-w2p_method="POST" data-w2p_remove="tr" href="b" id="c">a</a>')
|
||||||
'<a data-w2p_disable_with="default" data-w2p_method="POST" href="c" id="b">a</a>'
|
self.assertEqual(A('a', callback='b', delete='tr', confirm='Are you sure?', _id='c').xml(),
|
||||||
)
|
'<a data-w2p_confirm="Are you sure?" data-w2p_disable_with="default" data-w2p_method="POST" data-w2p_remove="tr" href="b" id="c">a</a>')
|
||||||
self.assertEqual(
|
|
||||||
A('a', cid='b').xml(),
|
|
||||||
'<a data-w2p_disable_with="default" data-w2p_method="GET" data-w2p_target="b">a</a>'
|
|
||||||
)
|
|
||||||
self.assertEqual(
|
|
||||||
A('a', cid='b', _disable_with='processing...').xml(),
|
|
||||||
'<a data-w2p_disable_with="processing..." data-w2p_method="GET" data-w2p_target="b">a</a>'
|
|
||||||
)
|
|
||||||
self.assertEqual(
|
|
||||||
A('a', callback='b', delete='tr', noconfirm=True, _id='c').xml(),
|
|
||||||
'<a data-w2p_disable_with="default" data-w2p_method="POST" data-w2p_remove="tr" href="b" id="c">a</a>'
|
|
||||||
)
|
|
||||||
self.assertEqual(
|
|
||||||
A('a', callback='b', delete='tr', confirm='Are you sure?', _id='c').xml(),
|
|
||||||
'<a data-w2p_confirm="Are you sure?" data-w2p_disable_with="default" data-w2p_method="POST" data-w2p_remove="tr" href="b" id="c">a</a>'
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_BUTTON(self):
|
def test_BUTTON(self):
|
||||||
self.assertEqual(BUTTON('test', _type='button').xml(),
|
self.assertEqual(BUTTON('test', _type='button').xml(),
|
||||||
|
|||||||
Reference in New Issue
Block a user