Merge "Don't break when reference names contain []"

This commit is contained in:
jenkins-bot 2017-11-21 21:59:22 +00:00 committed by Gerrit Code Review
commit 53e260e90a
2 changed files with 42 additions and 14 deletions

View file

@ -411,13 +411,13 @@ class Cite {
} }
if ( isset( $argv['name'] ) ) { if ( isset( $argv['name'] ) ) {
// Key given. // Key given.
$key = Sanitizer::escapeIdForAttribute( $argv['name'] ); $key = trim( $argv['name'] );
unset( $argv['name'] ); unset( $argv['name'] );
--$cnt; --$cnt;
} }
if ( isset( $argv['follow'] ) ) { if ( isset( $argv['follow'] ) ) {
// Follow given. // Follow given.
$follow = Sanitizer::escapeIdForAttribute( $argv['follow'] ); $follow = trim( $argv['follow'] );
unset( $argv['follow'] ); unset( $argv['follow'] );
--$cnt; --$cnt;
} }
@ -814,10 +814,10 @@ class Cite {
if ( !is_array( $val ) ) { if ( !is_array( $val ) ) {
return wfMessage( return wfMessage(
'cite_references_link_one', 'cite_references_link_one',
Sanitizer::safeEncodeAttribute( $this->normalizeKey(
self::getReferencesKey( $key ) self::getReferencesKey( $key )
), ),
Sanitizer::safeEncodeAttribute( $this->normalizeKey(
$this->refKey( $key ) $this->refKey( $key )
), ),
$this->referenceText( $key, $val ) $this->referenceText( $key, $val )
@ -827,7 +827,7 @@ class Cite {
if ( isset( $val['follow'] ) ) { if ( isset( $val['follow'] ) ) {
return wfMessage( return wfMessage(
'cite_references_no_link', 'cite_references_no_link',
Sanitizer::safeEncodeAttribute( $this->normalizeKey(
self::getReferencesKey( $val['follow'] ) self::getReferencesKey( $val['follow'] )
), ),
$text $text
@ -836,7 +836,7 @@ class Cite {
if ( !isset( $val['count'] ) ) { if ( !isset( $val['count'] ) ) {
// this handles the case of section preview for list-defined references // this handles the case of section preview for list-defined references
return wfMessage( 'cite_references_link_many', return wfMessage( 'cite_references_link_many',
Sanitizer::safeEncodeAttribute( $this->normalizeKey(
self::getReferencesKey( $key . "-" . ( isset( $val['key'] ) ? $val['key'] : '' ) ) self::getReferencesKey( $key . "-" . ( isset( $val['key'] ) ? $val['key'] : '' ) )
), ),
'', '',
@ -846,10 +846,10 @@ class Cite {
if ( $val['count'] < 0 ) { if ( $val['count'] < 0 ) {
return wfMessage( return wfMessage(
'cite_references_link_one', 'cite_references_link_one',
Sanitizer::safeEncodeAttribute( $this->normalizeKey(
self::getReferencesKey( $val['key'] ) self::getReferencesKey( $val['key'] )
), ),
Sanitizer::safeEncodeAttribute( $this->normalizeKey(
# $this->refKey( $val['key'], $val['count'] ) # $this->refKey( $val['key'], $val['count'] )
$this->refKey( $val['key'] ) $this->refKey( $val['key'] )
), ),
@ -863,10 +863,10 @@ class Cite {
if ( $val['count'] === 0 ) { if ( $val['count'] === 0 ) {
return wfMessage( return wfMessage(
'cite_references_link_one', 'cite_references_link_one',
Sanitizer::safeEncodeAttribute( $this->normalizeKey(
self::getReferencesKey( $key . "-" . $val['key'] ) self::getReferencesKey( $key . "-" . $val['key'] )
), ),
Sanitizer::safeEncodeAttribute( $this->normalizeKey(
# $this->refKey( $key, $val['count'] ), # $this->refKey( $key, $val['count'] ),
$this->refKey( $key, $val['key'] . "-" . $val['count'] ) $this->refKey( $key, $val['key'] . "-" . $val['count'] )
), ),
@ -879,7 +879,7 @@ class Cite {
for ( $i = 0; $i <= $val['count']; ++$i ) { for ( $i = 0; $i <= $val['count']; ++$i ) {
$links[] = wfMessage( $links[] = wfMessage(
'cite_references_link_many_format', 'cite_references_link_many_format',
Sanitizer::safeEncodeAttribute( $this->normalizeKey(
$this->refKey( $key, $val['key'] . "-$i" ) $this->refKey( $key, $val['key'] . "-$i" )
), ),
$this->referencesFormatEntryNumericBacklinkLabel( $val['number'], $i, $val['count'] ), $this->referencesFormatEntryNumericBacklinkLabel( $val['number'], $i, $val['count'] ),
@ -890,7 +890,7 @@ class Cite {
$list = $this->listToText( $links ); $list = $this->listToText( $links );
return wfMessage( 'cite_references_link_many', return wfMessage( 'cite_references_link_many',
Sanitizer::safeEncodeAttribute( $this->normalizeKey(
self::getReferencesKey( $key . "-" . $val['key'] ) self::getReferencesKey( $key . "-" . $val['key'] )
), ),
$list, $list,
@ -1049,10 +1049,10 @@ class Cite {
$this->mParser->recursiveTagParse( $this->mParser->recursiveTagParse(
wfMessage( wfMessage(
'cite_reference_link', 'cite_reference_link',
Sanitizer::safeEncodeAttribute( $this->normalizeKey(
$this->refKey( $key, $count ) $this->refKey( $key, $count )
), ),
Sanitizer::safeEncodeAttribute( $this->normalizeKey(
self::getReferencesKey( $key . $subkey ) self::getReferencesKey( $key . $subkey )
), ),
Sanitizer::safeEncodeAttribute( Sanitizer::safeEncodeAttribute(
@ -1063,6 +1063,19 @@ class Cite {
); );
} }
/**
* Normalizes and sanitizes a reference key
*
* @param string $key
* @return string
*/
private function normalizeKey( $key ) {
$key = Sanitizer::escapeIdForAttribute( $key );
$key = Sanitizer::safeEncodeAttribute( $key );
return $key;
}
/** /**
* This does approximately the same thing as * This does approximately the same thing as
* Language::listToText() but due to this being used for a * Language::listToText() but due to this being used for a

View file

@ -608,6 +608,21 @@ Multiple definition (inside {{#tag:references}})
!! end !! end
!! test
T29694 - [] in reference names in HTML5 fragment mode
!! config
wgFragmentMode=[ 'html5' ]
!! wikitext
<ref name="[#foo] {bar} <baz>">[bar]</ref>
!! html
<sup id="cite_ref-&#91;#foo&#93;_&#123;bar&#125;_&lt;baz_1-0" class="reference"><a href="#cite_note-[#foo]_{bar}_&lt;baz-1">&#91;1&#93;</a></sup><div class="mw-references-wrap"><ol class="references">
<li id="cite_note-&#91;#foo&#93;_&#123;bar&#125;_&lt;baz-1"><span class="mw-cite-backlink"><a href="#cite_ref-[#foo]_{bar}_&lt;baz_1-0">↑</a></span> <span class="reference-text">"&gt;[bar]</span>
</li>
</ol></div>
!! end
#### ---------------------------------------------------------------- #### ----------------------------------------------------------------
#### Parsoid-only testing of Parsoid's impl of <ref> and <references> #### Parsoid-only testing of Parsoid's impl of <ref> and <references>
#### tags. Parsoid's output for these tags differs from that of the #### tags. Parsoid's output for these tags differs from that of the