Skip to content
This repository
Browse code

Added support for anchor link generation.

  • Loading branch information...
commit 1759d8e4d80df3c3dc709c9bace2eb07d5685596 1 parent d04c9a6
Armin Ronacher authored October 06, 2011
3  CHANGES
@@ -8,6 +8,9 @@ Version 0.9
8 8
 
9 9
 Relase date to be decided, codename to be chosen.
10 10
 
  11
+- The :func:`flask.url_for` function now can generate anchors to the
  12
+  generated links.
  13
+
11 14
 Version 0.8.1
12 15
 -------------
13 16
 
11  flask/helpers.py
@@ -18,6 +18,7 @@
18 18
 from time import time
19 19
 from zlib import adler32
20 20
 from threading import RLock
  21
+from werkzeug.urls import url_quote
21 22
 
22 23
 # try to load the best simplejson implementation available.  If JSON
23 24
 # is not installed, we add a failing class.
@@ -184,9 +185,13 @@ def url_for(endpoint, **values):
184 185
 
185 186
     For more information, head over to the :ref:`Quickstart <url-building>`.
186 187
 
  188
+    .. versionadded:: 0.9
  189
+       The `_anchor` parameter was added.
  190
+
187 191
     :param endpoint: the endpoint of the URL (name of the function)
188 192
     :param values: the variable arguments of the URL rule
189 193
     :param _external: if set to `True`, an absolute URL is generated.
  194
+    :param _anchor: if provided this is added as anchor to the URL.
190 195
     """
191 196
     ctx = _request_ctx_stack.top
192 197
     blueprint_name = request.blueprint
@@ -204,8 +209,12 @@ def url_for(endpoint, **values):
204 209
         elif endpoint.startswith('.'):
205 210
             endpoint = endpoint[1:]
206 211
     external = values.pop('_external', False)
  212
+    anchor = values.pop('_anchor', None)
207 213
     ctx.app.inject_url_defaults(endpoint, values)
208  
-    return ctx.url_adapter.build(endpoint, values, force_external=external)
  214
+    rv = ctx.url_adapter.build(endpoint, values, force_external=external)
  215
+    if anchor is not None:
  216
+        rv += '#' + url_quote(anchor)
  217
+    return rv
209 218
 
210 219
 
211 220
 def get_template_attribute(template_name, attribute):
9  flask/testsuite/helpers.py
@@ -288,6 +288,15 @@ def internal_server_error(e):
288 288
             self.assert_equal(rv.status_code, 500)
289 289
             self.assert_equal(rv.data, 'Hello Server Error')
290 290
 
  291
+    def test_url_for_with_anchor(self):
  292
+        app = flask.Flask(__name__)
  293
+        @app.route('/')
  294
+        def index():
  295
+            return '42'
  296
+        with app.test_request_context():
  297
+            self.assert_equal(flask.url_for('index', _anchor='x y'),
  298
+                              '/#x%20y')
  299
+
291 300
 
292 301
 def suite():
293 302
     suite = unittest.TestSuite()

0 notes on commit 1759d8e

Please sign in to comment.
Something went wrong with that request. Please try again.