Added support for anchor link generation.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent d04c9a6 · Browse files · 3 files·+22 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+22 added−1 removed
- other:+22 added−1 removed
Provenance
- Commit
1759d8e- Tree
3a293fe- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
d04c9a6
3 files changed+22 added−1 removed
modified:flask/testsuite/helpers.py+9 added
286 unmodified lines287 rv = app.test_client().get('/')288 self.assert_equal(rv.status_code, 500)289 self.assert_equal(rv.data, 'Hello Server Error')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 300+301 def suite():302 suite = unittest.TestSuite()303 if flask.json_available:304 suite.addTest(unittest.makeSuite(JSONTestCase))3 unmodified linesmodified:flask/helpers.py+10 added-1 removed
16 unmodified lines17 import mimetypes18 from time import time19 from zlib import adler3220 from threading import RLock21+from werkzeug.urls import url_quote22 23 # try to load the best simplejson implementation available. If JSON24 # is not installed, we add a failing class.25 json_available = True158 unmodified lines184 url_for('.index')185 186 For more information, head over to the :ref:`Quickstart <url-building>`.187 188+ .. versionadded:: 0.9189+ The `_anchor` parameter was added.190+191 :param endpoint: the endpoint of the URL (name of the function)192 :param values: the variable arguments of the URL rule193 :param _external: if set to `True`, an absolute URL is generated.194+ :param _anchor: if provided this is added as anchor to the URL.195 """196 ctx = _request_ctx_stack.top197 blueprint_name = request.blueprint198 if not ctx.request._is_old_module:9 unmodified lines208 endpoint = blueprint_name + '.' + endpoint209 elif endpoint.startswith('.'):210 endpoint = endpoint[1:]211 external = values.pop('_external', False)212+ anchor = values.pop('_anchor', None)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 rv218 219 220 def get_template_attribute(template_name, attribute):221 """Loads a macro (or variable) a template exports. This can be used to437 unmodified linesmodified:CHANGES+3 added
6 unmodified lines7 -----------8 9 Relase date to be decided, codename to be chosen.10 11+- The :func:`flask.url_for` function now can generate anchors to the12+ generated links.13+14 Version 0.8.115 -------------16 17 Bugfix release, release date to be decided293 unmodified lines