From 351a08d1b79d7817c634597178dc35a49239a741 Mon Sep 17 00:00:00 2001 From: Max Semenik Date: Thu, 9 Nov 2017 17:21:54 -0800 Subject: [PATCH] Don't break when reference names contain [] Bug: T29694 Bug: T179544 Depends-On: I189bdefbc9034cf8d221a89d7158195de1c0fa6c Change-Id: Iec3439f76ecc2a3543b30b35f8735c92b0cfb711 --- includes/Cite.php | 41 +++++++++++++++++++++----------- tests/parser/citeParserTests.txt | 15 ++++++++++++ 2 files changed, 42 insertions(+), 14 deletions(-) diff --git a/includes/Cite.php b/includes/Cite.php index 126380e23..e75eb2dc0 100644 --- a/includes/Cite.php +++ b/includes/Cite.php @@ -411,13 +411,13 @@ class Cite { } if ( isset( $argv['name'] ) ) { // Key given. - $key = Sanitizer::escapeIdForAttribute( $argv['name'] ); + $key = trim( $argv['name'] ); unset( $argv['name'] ); --$cnt; } if ( isset( $argv['follow'] ) ) { // Follow given. - $follow = Sanitizer::escapeIdForAttribute( $argv['follow'] ); + $follow = trim( $argv['follow'] ); unset( $argv['follow'] ); --$cnt; } @@ -814,10 +814,10 @@ class Cite { if ( !is_array( $val ) ) { return wfMessage( 'cite_references_link_one', - Sanitizer::safeEncodeAttribute( + $this->normalizeKey( self::getReferencesKey( $key ) ), - Sanitizer::safeEncodeAttribute( + $this->normalizeKey( $this->refKey( $key ) ), $this->referenceText( $key, $val ) @@ -827,7 +827,7 @@ class Cite { if ( isset( $val['follow'] ) ) { return wfMessage( 'cite_references_no_link', - Sanitizer::safeEncodeAttribute( + $this->normalizeKey( self::getReferencesKey( $val['follow'] ) ), $text @@ -836,7 +836,7 @@ class Cite { if ( !isset( $val['count'] ) ) { // this handles the case of section preview for list-defined references return wfMessage( 'cite_references_link_many', - Sanitizer::safeEncodeAttribute( + $this->normalizeKey( self::getReferencesKey( $key . "-" . ( isset( $val['key'] ) ? $val['key'] : '' ) ) ), '', @@ -846,10 +846,10 @@ class Cite { if ( $val['count'] < 0 ) { return wfMessage( 'cite_references_link_one', - Sanitizer::safeEncodeAttribute( + $this->normalizeKey( self::getReferencesKey( $val['key'] ) ), - Sanitizer::safeEncodeAttribute( + $this->normalizeKey( # $this->refKey( $val['key'], $val['count'] ) $this->refKey( $val['key'] ) ), @@ -863,10 +863,10 @@ class Cite { if ( $val['count'] === 0 ) { return wfMessage( 'cite_references_link_one', - Sanitizer::safeEncodeAttribute( + $this->normalizeKey( self::getReferencesKey( $key . "-" . $val['key'] ) ), - Sanitizer::safeEncodeAttribute( + $this->normalizeKey( # $this->refKey( $key, $val['count'] ), $this->refKey( $key, $val['key'] . "-" . $val['count'] ) ), @@ -879,7 +879,7 @@ class Cite { for ( $i = 0; $i <= $val['count']; ++$i ) { $links[] = wfMessage( 'cite_references_link_many_format', - Sanitizer::safeEncodeAttribute( + $this->normalizeKey( $this->refKey( $key, $val['key'] . "-$i" ) ), $this->referencesFormatEntryNumericBacklinkLabel( $val['number'], $i, $val['count'] ), @@ -890,7 +890,7 @@ class Cite { $list = $this->listToText( $links ); return wfMessage( 'cite_references_link_many', - Sanitizer::safeEncodeAttribute( + $this->normalizeKey( self::getReferencesKey( $key . "-" . $val['key'] ) ), $list, @@ -1049,10 +1049,10 @@ class Cite { $this->mParser->recursiveTagParse( wfMessage( 'cite_reference_link', - Sanitizer::safeEncodeAttribute( + $this->normalizeKey( $this->refKey( $key, $count ) ), - Sanitizer::safeEncodeAttribute( + $this->normalizeKey( self::getReferencesKey( $key . $subkey ) ), 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 * Language::listToText() but due to this being used for a diff --git a/tests/parser/citeParserTests.txt b/tests/parser/citeParserTests.txt index a5ad0eea2..c3f97a79f 100644 --- a/tests/parser/citeParserTests.txt +++ b/tests/parser/citeParserTests.txt @@ -608,6 +608,21 @@ Multiple definition (inside {{#tag:references}}) !! end +!! test +T29694 - [] in reference names in HTML5 fragment mode +!! config +wgFragmentMode=[ 'html5' ] +!! wikitext +[bar] +!! html +[1]
    +
  1. ">[bar] +
  2. +
+ +!! end + + #### ---------------------------------------------------------------- #### Parsoid-only testing of Parsoid's impl of and #### tags. Parsoid's output for these tags differs from that of the