From 1bf5a652d69871e2820d1156250fa75a3adbdd0b Mon Sep 17 00:00:00 2001 From: Ed Sanders Date: Wed, 6 Jul 2016 22:05:24 +0100 Subject: [PATCH] Use classes instead of IDs for TOC collapsing One may way to have multiple TOC's on the page (e.g. in VisualEditor). Change-Id: I19701c4037b653b2944e407752e50f444861f883 --- includes/Linker.php | 2 +- resources/src/mediawiki/mediawiki.toc.js | 90 ++++++++++--------- tests/parser/parserTests.txt | 58 ++++++------ .../resources/mediawiki/mediawiki.toc.test.js | 4 +- 4 files changed, 79 insertions(+), 75 deletions(-) diff --git a/includes/Linker.php b/includes/Linker.php index 816271f483..bed9957f00 100644 --- a/includes/Linker.php +++ b/includes/Linker.php @@ -1561,7 +1561,7 @@ class Linker { $title = wfMessage( 'toc' )->inLanguage( $lang )->escaped(); return '
' - . '

' . $title . "

\n" + . '

' . $title . "

\n" . $toc . "\n
\n"; } diff --git a/resources/src/mediawiki/mediawiki.toc.js b/resources/src/mediawiki/mediawiki.toc.js index 0955c23f06..9ef6c4384a 100644 --- a/resources/src/mediawiki/mediawiki.toc.js +++ b/resources/src/mediawiki/mediawiki.toc.js @@ -3,56 +3,60 @@ // Table of contents toggle mw.hook( 'wikipage.content' ).add( function ( $content ) { - var $toc, $tocTitle, $tocToggleLink, $tocList, hideToc; - $toc = $content.find( '#toc' ); - $tocTitle = $content.find( '#toctitle' ); - $tocToggleLink = $content.find( '#togglelink' ); - $tocList = $toc.find( 'ul' ).eq( 0 ); + $content.find( '.toc' ).addBack( '.toc' ).each( function () { + var hideToc, + $this = $( this ), + // .toctitle is new so may not exist in HTML caches for a few weeks, + // so keep checking for #toctitle for now + $tocTitle = $this.find( '.toctitle, #toctitle' ), + $tocToggleLink = $this.find( '.togglelink' ), + $tocList = $this.find( 'ul' ).eq( 0 ); - // Hide/show the table of contents element - function toggleToc() { - if ( $tocList.is( ':hidden' ) ) { - $tocList.slideDown( 'fast' ); - $tocToggleLink.text( mw.msg( 'hidetoc' ) ); - $toc.removeClass( 'tochidden' ); - mw.cookie.set( 'hidetoc', null ); - } else { - $tocList.slideUp( 'fast' ); - $tocToggleLink.text( mw.msg( 'showtoc' ) ); - $toc.addClass( 'tochidden' ); - mw.cookie.set( 'hidetoc', '1' ); + // Hide/show the table of contents element + function toggleToc() { + if ( $tocList.is( ':hidden' ) ) { + $tocList.slideDown( 'fast' ); + $tocToggleLink.text( mw.msg( 'hidetoc' ) ); + $this.removeClass( 'tochidden' ); + mw.cookie.set( 'hidetoc', null ); + } else { + $tocList.slideUp( 'fast' ); + $tocToggleLink.text( mw.msg( 'showtoc' ) ); + $this.addClass( 'tochidden' ); + mw.cookie.set( 'hidetoc', '1' ); + } } - } - // Only add it if there is a complete TOC and it doesn't - // have a toggle added already - if ( $toc.length && $tocTitle.length && $tocList.length && !$tocToggleLink.length ) { - hideToc = mw.cookie.get( 'hidetoc' ) === '1'; + // Only add it if there is a complete TOC and it doesn't + // have a toggle added already + if ( $tocTitle.length && $tocList.length && !$tocToggleLink.length ) { + hideToc = mw.cookie.get( 'hidetoc' ) === '1'; - $tocToggleLink = $( '' ) - .text( mw.msg( hideToc ? 'showtoc' : 'hidetoc' ) ) - .on( 'click keypress', function ( e ) { - if ( - e.type === 'click' || - e.type === 'keypress' && e.which === 13 - ) { - toggleToc(); - } - } ); + $tocToggleLink = $( '' ) + .text( mw.msg( hideToc ? 'showtoc' : 'hidetoc' ) ) + .on( 'click keypress', function ( e ) { + if ( + e.type === 'click' || + e.type === 'keypress' && e.which === 13 + ) { + toggleToc(); + } + } ); - $tocTitle.append( - $tocToggleLink - .wrap( '' ) - .parent() - .prepend( ' [' ) - .append( '] ' ) - ); + $tocTitle.append( + $tocToggleLink + .wrap( '' ) + .parent() + .prepend( ' [' ) + .append( '] ' ) + ); - if ( hideToc ) { - $tocList.hide(); - $toc.addClass( 'tochidden' ); + if ( hideToc ) { + $tocList.hide(); + $this.addClass( 'tochidden' ); + } } - } + } ); } ); }( mediaWiki, jQuery ) ); diff --git a/tests/parser/parserTests.txt b/tests/parser/parserTests.txt index 7bf2ce2199..ee0d30f694 100644 --- a/tests/parser/parserTests.txt +++ b/tests/parser/parserTests.txt @@ -15458,7 +15458,7 @@ Section headings with TOC Some text ===Another headline=== !! html -

Contents

+

Contents

  • 1 Headline 1
      @@ -15501,7 +15501,7 @@ __FORCETOC__ == Headline 2 == == Headline == !! html/php -

      Contents

      +

      Contents

      • 1 Headline 2
      • 2 Headline
      • @@ -15532,7 +15532,7 @@ Handling of sections up to level 6 and beyond ========= Level 9 Heading========= ========== Level 10 Heading========== !! html -

        Contents

        +

        Contents

        • 1 Level 1 Heading
            @@ -15585,7 +15585,7 @@ TOC regression (T11764) == title 2 == === title 2.1 === !! html -

            Contents

            +

            Contents

            • 1 title 1
                @@ -15620,7 +15620,7 @@ TOC for heading containing (T96153) __FORCETOC__ ==New title== !! html/php -

                Contents

                +

                Contents

                @@ -15642,7 +15642,7 @@ wgMaxTocLevel=3 == title 2 == === title 2.1 === !! html -

                Contents

                +

                Contents

                • 1 title 1
                    @@ -15678,7 +15678,7 @@ wgMaxTocLevel=3 ====Section 1.1.1.1==== ==Section 2== !! html -

                    Contents

                    +

                    Contents

                    • 1 Section 1
                        @@ -15771,7 +15771,7 @@ __TOC__ === title 1.1 === == title 2 == !! html -

                        Contents

                        +

                        Contents

                        • 1 title 1
                            @@ -15838,7 +15838,7 @@ section 5 !! html

                            The tooltips shall not show entities to the user (ie. be double escaped)

                            -

                            Contents

                            +

                            Contents

                            • 1 text > text
                            • 2 text < text
                            • @@ -15897,7 +15897,7 @@ section 6 !! html

                              Id should not contain + for spaces

                              -

                              Contents

                              +

                              Contents

                              • 1 Space between Text
                              • 2 Space-Entity between Text
                              • @@ -15943,7 +15943,7 @@ Headers with excess '=' characters =''italic'' heading== ==''italic'' heading= !! html -

                                Contents

                                +

                                Contents

                                • 1 foo=
                                • 2 =foo
                                • @@ -15973,7 +15973,7 @@ HTML headers vs TOC (T25393) == Header 2.2 == __NOEDITSECTION__ !! html -

                                  Contents

                                  +

                                  Contents

                                  • 1 Header 1
                                      @@ -17751,7 +17751,7 @@ Fuzz testing: Parser14 http://__TOC__ !! html

                                      onmouseover=[edit]

                                      -http://

                                      Contents

                                      +http://

                                      Contents

                                      @@ -17762,7 +17762,7 @@ http://

                                      Contents

                                      onmouseover=[edit]

                                      http://

                                      -
                                      +

                                      Contents

                                        @@ -20216,7 +20216,7 @@ Out-of-order TOC heading levels =====5===== ==2== !! html -

                                        Contents

                                        +

                                        Contents

                                        • 1 2
                                            @@ -22198,7 +22198,7 @@ title=[[Main Page]] __TOC__ == ''Lost'' episodes == !! html -

                                            Contents

                                            +

                                            Contents

                                            @@ -22216,7 +22216,7 @@ title=[[Main Page]] __TOC__ == '''should be bold''' then normal text == !! html -

                                            Contents

                                            +

                                            Contents

                                            @@ -22234,7 +22234,7 @@ title=[[Main Page]] __TOC__ == Image [[Image:foobar.jpg]] == !! html -

                                            Contents

                                            +

                                            Contents

                                            @@ -22252,7 +22252,7 @@ title=[[Main Page]] __TOC__ ==
                                            Quote
                                            == !! html -

                                            Contents

                                            +

                                            Contents

                                            @@ -22263,7 +22263,7 @@ __TOC__ !! html+tidy

                                            -
                                            +

                                            Contents

                                              @@ -22288,7 +22288,7 @@ __TOC__ Hanc marginis exiguitas non caperet. QED !! html -

                                              Contents

                                              +

                                              Contents

                                              @@ -22308,7 +22308,7 @@ __TOC__ == Foo
                                              Bar
                                              == !! html -

                                              Contents

                                              +

                                              Contents

                                              • 1 Foo Bar
                                              • 2 Foo Bar
                                              • @@ -22321,7 +22321,7 @@ __TOC__ !! html+tidy

                                                -
                                                +

                                                Contents

                                                  @@ -22350,7 +22350,7 @@ __TOC__ == Evilbye == !! html/php -

                                                  Contents

                                                  +

                                                  Contents

                                                  • 1 Hello
                                                  • 2 b">Evilbye
                                                  • @@ -22381,7 +22381,7 @@ __TOC__ == Attributes after dir on these span tags must be deleted from the TOC == !! html -

                                                    Contents

                                                    +

                                                    Contents

                                                    • 1 C++
                                                    • 2 זבנג!
                                                    • @@ -22405,7 +22405,7 @@ T74884: bdi element in ToC __TOC__ == test == !! html -

                                                      Contents

                                                      +

                                                      Contents

                                                      @@ -22421,7 +22421,7 @@ T35715: s/strike element in ToC __TOC__ == test test test == !! html -

                                                      Contents

                                                      +

                                                      Contents

                                                      @@ -22440,7 +22440,7 @@ Empty

                                                      tag in TOC, removed by Sanitizer (T92892) __TOC__ == x == !! html -

                                                      Contents

                                                      +

                                                      Contents

                                                      @@ -22451,7 +22451,7 @@ __TOC__ !! html+tidy

                                                      -
                                                      +

                                                      Contents

                                                        diff --git a/tests/qunit/suites/resources/mediawiki/mediawiki.toc.test.js b/tests/qunit/suites/resources/mediawiki/mediawiki.toc.test.js index 297f0fdb65..70a7e35e52 100644 --- a/tests/qunit/suites/resources/mediawiki/mediawiki.toc.test.js +++ b/tests/qunit/suites/resources/mediawiki/mediawiki.toc.test.js @@ -12,7 +12,7 @@ assert.strictEqual( $( '#toc' ).length, 0, 'There is no table of contents on the page at the beginning' ); tocHtml = '
                                                        ' + - '
                                                        ' + + '
                                                        ' + '

                                                        Contents

                                                        ' + '
                                                        ' + '
                                                        ' + @@ -21,7 +21,7 @@ mw.hook( 'wikipage.content' ).fire( $( '#qunit-fixture' ) ); $tocList = $( '#toc ul:first' ); - $toggleLink = $( '#togglelink' ); + $toggleLink = $( '#toc .togglelink' ); assert.strictEqual( $toggleLink.length, 1, 'Toggle link is added to the table of contents' ); -- 2.20.1