Sanitize underscores as core does, to not create broken links

Core sanitizes link targets and removes double spaces and underscores.
But the corresponding id="…" attributes are not sanitized the same
way. This results in broken links. This patch is not perfect (two
references with name="a_b" and name="a__b" will conflict), but the
best solution I can think of at the moment.

Bug: T184912
Change-Id: I9dbc916ad99269517d84c8ffb8581628d44a9f4e
This commit is contained in:
Thiemo Kreuz (WMDE) 2018-11-19 18:07:55 +01:00
parent 8b97f74dc9
commit 2b34dede6c
2 changed files with 16 additions and 0 deletions

View file

@ -1078,6 +1078,7 @@ class Cite {
*/
private function normalizeKey( $key ) {
$key = Sanitizer::escapeIdForAttribute( $key );
$key = preg_replace( '/__+/', '_', $key );
$key = Sanitizer::safeEncodeAttribute( $key );
return $key;

View file

@ -271,6 +271,21 @@ Ref names containing a number
!! end
!! test
T184912: Consistent normalization of consecutive underscores
!! wikitext
<ref name="x__x">example</ref>
<references />
!! html
<p><sup id="cite_ref-x_x_1-0" class="reference"><a href="#cite_note-x_x-1">&#91;1&#93;</a></sup>
</p>
<div class="mw-references-wrap"><ol class="references">
<li id="cite_note-x_x-1"><span class="mw-cite-backlink"><a href="#cite_ref-x_x_1-0">↑</a></span> <span class="reference-text">example</span>
</li>
</ol></div>
!! end
!! test
Erroneous refs
!! wikitext