| 35 | ==== `Href` with an empty base ^[source:trunk/trac/web/tests/href.py@8551:61-62,76-78#L57 (0.12)] [source:branches/0.11-stable/trac/web/tests/href.py@8551:63-64,79-81#L57 (0.11)]^ ==== #Href |
| 36 | The `Href` class has been changed to ensure that it always generates valid URLs, even with an empty base. In 0.11, the following uses all return an empty string: |
| 37 | {{{ |
| 38 | #!python |
| 39 | # 0.11 |
| 40 | >>> href = Href('') # Also applies to Href('/') |
| 41 | >>> href() |
| 42 | '' |
| 43 | >>> href('/') |
| 44 | '' |
| 45 | }}} |
| 46 | In 0.12, the same expressions return a valid relative URL: |
| 47 | {{{ |
| 48 | #!python |
| 49 | # 0.12 |
| 50 | >>> href = Href('') |
| 51 | >>> href() |
| 52 | '/' |
| 53 | >>> href('/') |
| 54 | '/' |
| 55 | }}} |
| 56 | This change will break plugins that use the following idiom to concatenate the base URL with a path starting with a slash: |
| 57 | {{{ |
| 58 | #!python |
| 59 | # 0.12 |
| 60 | >>> href = Href('') |
| 61 | >>> path = '/path/to/page' |
| 62 | >>> href() + path # Broken |
| 63 | '//path/to/page' |
| 64 | }}} |
| 65 | For this specific use case, a new syntax has been added to avoid doubled slashes: |
| 66 | {{{ |
| 67 | #!python |
| 68 | # 0.12 and 0.11.6 |
| 69 | >>> href = Href('') |
| 70 | >>> path = '/path/to/page' |
| 71 | >>> href + path # New syntax |
| 72 | '/path/to/page' |
| 73 | }}} |
| 74 | The new syntax has been backported to 0.11-stable in 0.11.6 to facilitate compatibility of plugins with both 0.11.6 and 0.12. If compatibility with older releases of the 0.11.x branch is required, the following code can be used: |
| 75 | {{{ |
| 76 | #!python |
| 77 | # 0.12 and 0.11.x |
| 78 | >>> href = Href('') |
| 79 | >>> path = '/path/to/page' |
| 80 | >>> href().rstrip('/') + path # Compatibility |
| 81 | '/path/to/page' |
| 82 | }}} |
| 83 | |
| 84 | See also #8159. |
| 85 | |