Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added support for anchor link generation.

  • Loading branch information...
commit 1759d8e4d80df3c3dc709c9bace2eb07d5685596 1 parent d04c9a6
@mitsuhiko authored
Showing with 22 additions and 1 deletion.
  1. +3 −0  CHANGES
  2. +10 −1 flask/helpers.py
  3. +9 −0 flask/testsuite/helpers.py
View
3  CHANGES
@@ -8,6 +8,9 @@ Version 0.9
Relase date to be decided, codename to be chosen.
+- The :func:`flask.url_for` function now can generate anchors to the
+ generated links.
+
Version 0.8.1
-------------
View
11 flask/helpers.py
@@ -18,6 +18,7 @@
from time import time
from zlib import adler32
from threading import RLock
+from werkzeug.urls import url_quote
# try to load the best simplejson implementation available. If JSON
# is not installed, we add a failing class.
@@ -184,9 +185,13 @@ def url_for(endpoint, **values):
For more information, head over to the :ref:`Quickstart <url-building>`.
+ .. versionadded:: 0.9
+ The `_anchor` parameter was added.
+
:param endpoint: the endpoint of the URL (name of the function)
:param values: the variable arguments of the URL rule
:param _external: if set to `True`, an absolute URL is generated.
+ :param _anchor: if provided this is added as anchor to the URL.
"""
ctx = _request_ctx_stack.top
blueprint_name = request.blueprint
@@ -204,8 +209,12 @@ def url_for(endpoint, **values):
elif endpoint.startswith('.'):
endpoint = endpoint[1:]
external = values.pop('_external', False)
+ anchor = values.pop('_anchor', None)
ctx.app.inject_url_defaults(endpoint, values)
- return ctx.url_adapter.build(endpoint, values, force_external=external)
+ rv = ctx.url_adapter.build(endpoint, values, force_external=external)
+ if anchor is not None:
+ rv += '#' + url_quote(anchor)
+ return rv
def get_template_attribute(template_name, attribute):
View
9 flask/testsuite/helpers.py
@@ -288,6 +288,15 @@ def internal_server_error(e):
self.assert_equal(rv.status_code, 500)
self.assert_equal(rv.data, 'Hello Server Error')
+ def test_url_for_with_anchor(self):
+ app = flask.Flask(__name__)
+ @app.route('/')
+ def index():
+ return '42'
+ with app.test_request_context():
+ self.assert_equal(flask.url_for('index', _anchor='x y'),
+ '/#x%20y')
+
def suite():
suite = unittest.TestSuite()
Please sign in to comment.
Something went wrong with that request. Please try again.