1. Matthew Schinckel
  2. werkzeug-main

Commits

mitsuhiko  committed 2af27f3

changed behavior or `Href` a bit (trailing slash handling) and added unittest.

  • Participants
  • Parent commits 795153e
  • Branches trunk

Comments (0)

Files changed (2)

File tests/test_utils.py

View file
            'http://de.wikipedia.org/wiki/Elf%20%28Begriffskl%C3%A4rung%29'
 
 
+test_href_tool = '>>> from werkzeug import Href\n\n' + Href.__doc__
+
+
 def test_escape():
     assert escape(None) == ''
     assert escape(42) == '42'
 def test_find_modules():
     assert list(find_modules('werkzeug.debug')) == ['werkzeug.debug.render',
                                                     'werkzeug.debug.util']
+
+
+def test_html_builder():
+    assert html.p('Hello World') == '<p>Hello World</p>'
+    assert html.a('Test', href='#') == '<a href="#">Test</a>'
+    assert html.br() == '<br>'
+    assert xhtml.br() == '<br />'
+    assert html.img(src='foo') == '<img src="foo">'
+    assert xhtml.img(src='foo') == '<img src="foo" />'
+    assert html.html(
+        html.head(
+            html.title('foo'),
+            html.script(type='text/javascript')
+        )
+    ) == '<html><head><title>foo</title><script type="text/javascript">' \
+         '</script></head></html>'

File werkzeug/utils.py

View file
 
     >>> bar_href = href.bar
     >>> bar_href("blub")
-    /foo/bar/blub
+    '/foo/bar/blub'
     """
 
     def __init__(self, base='./', charset='utf-8'):
         if not base:
             base = './'
-        elif not base.endswith('/'):
-            base += '/'
         self.base = base
         self.charset = charset
 
     def __getattr__(self, name):
         if name[:2] == '__':
             raise AttributeError(name)
-        return Href(urlparse.urljoin(self.base, name), self.charset)
+        base = self.base
+        if base[-1:] != '/':
+            base += '/'
+        return Href(urlparse.urljoin(base, name), self.charset)
 
     def __call__(self, *path, **query):
-        if not query:
+        if query:
             if path and isinstance(path[-1], dict):
                 query, path = path[-1], path[:-1]
             else:
                               for k, v in query.items()])
         path = '/'.join([url_quote(x, self.charset) for x in path
                          if x is not None]).lstrip('/')
+        rv = self.base
+        if path:
+            if not rv.endswith('/'):
+                rv += '/'
+            rv = urlparse.urljoin(rv, path)
         if query:
-            path += '?' + query
-        return urlparse.urljoin(self.base, path)
+            rv += '?' + url_encode(query, self.charset)
+        return str(rv)
 
 
 class cached_property(object):
     """
     if isinstance(s, unicode):
         s = s.encode(charset)
+    elif not isinstance(s, str):
+        s = str(s)
     return urllib.quote(s, safe=safe)
 
 
     """
     if isinstance(s, unicode):
         s = s.encode(charset)
+    elif not isinstance(s, str):
+        s = str(s)
     return urllib.quote_plus(s, safe=safe)