From 6c7480e5f03eabbea79059b70b83fe9b681a4ab9 Mon Sep 17 00:00:00 2001 From: Vivek Ghaisas Date: Tue, 17 Feb 2015 21:54:26 +0530 Subject: [PATCH] Add clickable link for section headers Changed the Linker::makeHeadline function to additionally generate an anchor that uses a section symbol. Created mediawiki.sectionAnchor module to style the anchors and included it in SkinTemplate so that other skins automatically get those styles. Bug: T18691 Change-Id: I562e437ec0bd337c9db0406f5dcab504dfa37034 --- RELEASE-NOTES-1.25 | 2 + includes/Linker.php | 8 + includes/skins/SkinTemplate.php | 3 +- languages/i18n/en.json | 4 +- resources/Resources.php | 11 + .../src/mediawiki/mediawiki.sectionAnchor.css | 59 ++++ .../mediawiki.sectionAnchor.hide.css | 4 + tests/parser/parserTests.txt | 260 +++++++++--------- 8 files changed, 219 insertions(+), 132 deletions(-) create mode 100644 resources/src/mediawiki/mediawiki.sectionAnchor.css create mode 100644 resources/src/mediawiki/mediawiki.sectionAnchor.hide.css diff --git a/RELEASE-NOTES-1.25 b/RELEASE-NOTES-1.25 index 8992ce0ce9..10e49ae1e0 100644 --- a/RELEASE-NOTES-1.25 +++ b/RELEASE-NOTES-1.25 @@ -99,6 +99,8 @@ production. tags. * Added 'ChangeTagsListActive' hook, to separate the concepts of "defined" and "active" formerly conflated by the 'ListDefinedTags' hook. +* Clickable anchors for each section heading in the content are now generated + and appear in the gutter on hovering over the heading. ==== External libraries ==== * MediaWiki now requires certain external libraries to be installed. In the past diff --git a/includes/Linker.php b/includes/Linker.php index 238bb5348a..ae8695bbd1 100644 --- a/includes/Linker.php +++ b/includes/Linker.php @@ -1751,6 +1751,14 @@ class Linker { $link, $legacyAnchor = false ) { $ret = " '#' . $anchor, + 'class' => 'mw-headline-anchor', + 'title' => wfMessage( 'headline-anchor-title' )->text() + ), + wfMessage( 'section-symbol' )->text() + ) . "$html" . $link . ""; diff --git a/includes/skins/SkinTemplate.php b/includes/skins/SkinTemplate.php index 8136d8e9dc..98a1b03d7f 100644 --- a/includes/skins/SkinTemplate.php +++ b/includes/skins/SkinTemplate.php @@ -54,7 +54,8 @@ class SkinTemplate extends Skin { $out->addModuleStyles( array( 'mediawiki.legacy.shared', 'mediawiki.legacy.commonPrint', - 'mediawiki.ui.button' + 'mediawiki.ui.button', + 'mediawiki.sectionAnchor' ) ); } diff --git a/languages/i18n/en.json b/languages/i18n/en.json index cff74b4e64..7bc5a9db07 100644 --- a/languages/i18n/en.json +++ b/languages/i18n/en.json @@ -3659,5 +3659,7 @@ "json-error-utf8": "Malformed UTF-8 characters, possibly incorrectly encoded", "json-error-recursion": "One or more recursive references in the value to be encoded", "json-error-inf-or-nan": "One or more NAN or INF values in the value to be encoded", - "json-error-unsupported-type": "A value of a type that cannot be encoded was given" + "json-error-unsupported-type": "A value of a type that cannot be encoded was given", + "headline-anchor-title": "Link to this section", + "section-symbol": "§" } diff --git a/resources/Resources.php b/resources/Resources.php index 486dd36171..2168899257 100644 --- a/resources/Resources.php +++ b/resources/Resources.php @@ -956,6 +956,17 @@ return array( 'mediawiki.api', ), ), + 'mediawiki.sectionAnchor' => array( + 'skinStyles' => array( + 'default' => array( + 'resources/src/mediawiki/mediawiki.sectionAnchor.css' => + array( 'media' => 'screen'), + 'resources/src/mediawiki/mediawiki.sectionAnchor.hide.css' => + array( 'media' => 'print' ), + ), + ), + 'targets' => array( 'desktop', 'mobile' ), + ), 'mediawiki.Title' => array( 'scripts' => 'resources/src/mediawiki/mediawiki.Title.js', 'dependencies' => array( diff --git a/resources/src/mediawiki/mediawiki.sectionAnchor.css b/resources/src/mediawiki/mediawiki.sectionAnchor.css new file mode 100644 index 0000000000..1a8fe0a3cb --- /dev/null +++ b/resources/src/mediawiki/mediawiki.sectionAnchor.css @@ -0,0 +1,59 @@ +/* Anchors for section headings */ +.mw-headline-anchor { + font-weight: normal; + margin-left: -14px; + width: 14px; + filter: alpha(opacity=0); + opacity: 0; + position: absolute; + text-align: center; + text-decoration: none; + -moz-user-select: none; + -webkit-user-select: none; + user-select: none; +} + +/* @noflip */ +.mw-content-ltr .mw-headline-anchor, +.mw-content-rtl .mw-content-ltr .mw-headline-anchor { + margin-left: -14px; + margin-right: 0; +} + +/* @noflip */ +.mw-content-rtl .mw-headline-anchor, +.mw-content-ltr .mw-content-rtl .mw-headline-anchor { + margin-left: 0; + margin-right: -14px; +} + +.mw-headline-anchor, +.mw-headline-anchor:visited { + color: #888; +} + +.mw-headline-anchor:hover, +.mw-headline-anchor:focus { + color: #555; + text-decoration: none; +} + +.mw-headline-anchor:active { + color: #000; +} + +.mw-body h1:hover .mw-headline-anchor, +.mw-body h2:hover .mw-headline-anchor, +.mw-body h3:hover .mw-headline-anchor, +.mw-body h4:hover .mw-headline-anchor, +.mw-body h5:hover .mw-headline-anchor, +.mw-body h6:hover .mw-headline-anchor, +.mw-body h1:focus .mw-headline-anchor, +.mw-body h2:focus .mw-headline-anchor, +.mw-body h3:focus .mw-headline-anchor, +.mw-body h4:focus .mw-headline-anchor, +.mw-body h5:focus .mw-headline-anchor, +.mw-body h6:focus .mw-headline-anchor { + filter: alpha(opacity=100); + opacity: 1; +} diff --git a/resources/src/mediawiki/mediawiki.sectionAnchor.hide.css b/resources/src/mediawiki/mediawiki.sectionAnchor.hide.css new file mode 100644 index 0000000000..a7695ccdc0 --- /dev/null +++ b/resources/src/mediawiki/mediawiki.sectionAnchor.hide.css @@ -0,0 +1,4 @@ +/* Hide section anchor from print media */ +.mw-headline-anchor { + display: none; +} diff --git a/tests/parser/parserTests.txt b/tests/parser/parserTests.txt index cc662808f5..e525cac08f 100644 --- a/tests/parser/parserTests.txt +++ b/tests/parser/parserTests.txt @@ -411,11 +411,11 @@ a

a


-

b[edit]

+

§b[edit]

a


-

b[edit]

+

§b[edit]

!! end @@ -428,7 +428,7 @@ Extra newlines between heading and content are swallowed [[a]] !! html -

b[edit]

+

§b[edit]

a

!! end @@ -8004,7 +8004,7 @@ List interrupted by empty line or heading !! html -

A heading[edit]

+

§A heading[edit]

!!end @@ -9706,8 +9706,8 @@ Bug 6563: Edit link generation for section shown by !! wikitext {{includeonly section}} !! html -

Includeonly section[edit]

-

Section T-1[edit]

+

§Includeonly section[edit]

+

§Section T-1[edit]

!! end @@ -9733,7 +9733,7 @@ Bug 6563: Edit link generation for section suppressed by ==Section 1== !! html -

Section 1[edit]

+

§Section 1[edit]

!! end @@ -9770,12 +9770,12 @@ some can have stuff=== here === !! html/php -

hu

+

§hu

some

  • stuff
  • here
-

here

+

§here

!! html/parsoid

hu

@@ -13179,13 +13179,13 @@ More ===Smaller headline=== Blah blah !! html -

Headline 1[edit]

+

§Headline 1[edit]

Some text

-

Headline 2[edit]

+

§Headline 2[edit]

More

-

Smaller headline[edit]

+

§Smaller headline[edit]

Blah blah

!! end @@ -13225,14 +13225,14 @@ Some text -

Headline 1[edit]

-

Subheadline 1[edit]

-
Skipping a level[edit]
-
Skipping a level[edit]
-

Headline 2[edit]

+

§Headline 1[edit]

+

§Subheadline 1[edit]

+
§Skipping a level[edit]
+
§Skipping a level[edit]
+

§Headline 2[edit]

Some text

-

Another headline[edit]

+

§Another headline[edit]

!! end @@ -13254,10 +13254,10 @@ __FORCETOC__ -

Headline 2[edit]

-

Headline[edit]

-

Headline 2[edit]

-

Headline[edit]

+

§Headline 2[edit]

+

§Headline[edit]

+

§Headline 2[edit]

+

§Headline[edit]

!! end @@ -13306,16 +13306,16 @@ Handling of sections up to level 6 and beyond -

Level 1 Heading[edit]

-

Level 2 Heading[edit]

-

Level 3 Heading[edit]

-

Level 4 Heading[edit]

-
Level 5 Heading[edit]
-
Level 6 Heading[edit]
-
= Level 7 Heading=[edit]
-
== Level 8 Heading==[edit]
-
=== Level 9 Heading===[edit]
-
==== Level 10 Heading====[edit]
+

§Level 1 Heading[edit]

+

§Level 2 Heading[edit]

+

§Level 3 Heading[edit]

+

§Level 4 Heading[edit]

+
§Level 5 Heading[edit]
+
§Level 6 Heading[edit]
+
§= Level 7 Heading=[edit]
+
§== Level 8 Heading==[edit]
+
§=== Level 9 Heading===[edit]
+
§==== Level 10 Heading====[edit]
!! end @@ -13349,12 +13349,12 @@ TOC regression (bug 9764) -

title 1[edit]

-

title 1.1[edit]

-

title 1.1.1[edit]

-

title 1.2[edit]

-

title 2[edit]

-

title 2.1[edit]

+

§title 1[edit]

+

§title 1.1[edit]

+

§title 1.1.1[edit]

+

§title 1.2[edit]

+

§title 2[edit]

+

§title 2.1[edit]

!! end @@ -13386,12 +13386,12 @@ wgMaxTocLevel=3 -

title 1[edit]

-

title 1.1[edit]

-

title 1.1.1[edit]

-

title 1.2[edit]

-

title 2[edit]

-

title 2.1[edit]

+

§title 1[edit]

+

§title 1.1[edit]

+

§title 1.1.1[edit]

+

§title 1.2[edit]

+

§title 2[edit]

+

§title 2.1[edit]

!! end @@ -13417,11 +13417,11 @@ wgMaxTocLevel=3 -

Section 1[edit]

-

Section 1.1[edit]

-

Section 1.1.1[edit]

-

Section 1.1.1.1[edit]

-

Section 2[edit]

+

§Section 1[edit]

+

§Section 1.1[edit]

+

§Section 1.1.1[edit]

+

§Section 1.1.1.1[edit]

+

§Section 2[edit]

!! end @@ -13432,8 +13432,8 @@ Resolving duplicate section names == Foo bar == == Foo bar == !! html -

Foo bar[edit]

-

Foo bar[edit]

+

§Foo bar[edit]

+

§Foo bar[edit]

!! end @@ -13443,8 +13443,8 @@ Resolving duplicate section names with differing case (bug 10721) == Foo bar == == Foo Bar == !! html -

Foo bar[edit]

-

Foo Bar[edit]

+

§Foo bar[edit]

+

§Foo Bar[edit]

!! end @@ -13463,10 +13463,10 @@ __NOTOC__ {{sections}} ==Section 4== !! html -

Section 0[edit]

-

Section 1[edit]

-

Section 2[edit]

-

Section 4[edit]

+

§Section 0[edit]

+

§Section 1[edit]

+

§Section 2[edit]

+

§Section 4[edit]

!! end @@ -13477,8 +13477,8 @@ __NOEDITSECTION__ ==Section 1== ==Section 2== !! html -

Section 1

-

Section 2

+

§Section 1

+

§Section 2

!! end @@ -13487,7 +13487,7 @@ Link inside a section heading !! wikitext ==Section with a [[Main Page|link]] in it== !! html -

Section with a link in it[edit]

+

§Section with a link in it[edit]

!! end @@ -13510,9 +13510,9 @@ __TOC__ -

title 1[edit]

-

title 1.1[edit]

-

title 2[edit]

+

§title 1[edit]

+

§title 1.1[edit]

+

§title 2[edit]

!! end @@ -13536,10 +13536,10 @@ The line above must have a trailing space! --> But just in case it doesn't... !! html -

=[edit]

+

§=[edit]

The line above must have a trailing space!

-

=[edit]

+

§=[edit]

But just in case it doesn't...

!! end @@ -13576,19 +13576,19 @@ section 5 -

text > text[edit]

+

§text > text[edit]

section 1

-

text < text[edit]

+

§text < text[edit]

section 2

-

text & text[edit]

+

§text & text[edit]

section 3

-

text ' text[edit]

+

§text ' text[edit]

section 4

-

text " text[edit]

+

§text " text[edit]

section 5

!! end @@ -13636,22 +13636,22 @@ section 6 -

Space between Text[edit]

+

§Space between Text[edit]

section 1

-

Space-Entity between Text[edit]

+

§Space-Entity between Text[edit]

section 2

-

Plus+between+Text[edit]

+

§Plus+between+Text[edit]

section 3

-

Plus-Entity+between+Text[edit]

+

§Plus-Entity+between+Text[edit]

section 4

-

Underscore_between_Text[edit]

+

§Underscore_between_Text[edit]

section 5

-

Underscore-Entity_between_Text[edit]

+

§Underscore-Entity_between_Text[edit]

section 6

#Space between Text #Space-Entity between Text @@ -13680,10 +13680,10 @@ Headers with excess '=' characters -

foo=[edit]

-

=foo[edit]

-

italic heading=[edit]

-

=italic heading[edit]

+

§foo=[edit]

+

§=foo[edit]

+

§italic heading=[edit]

+

§=italic heading[edit]

!! end @@ -13718,12 +13718,12 @@ __NOEDITSECTION__ -

Header 1

-

Header 1.1

-

Header 1.2

-

Header 2

-

Header 2.1

-

Header 2.2

+

§Header 1

+

§Header 1.1

+

§Header 1.2

+

§Header 2

+

§Header 2.1

+

§Header 2.2

!! end @@ -13738,9 +13738,9 @@ parsoid=wt2html,wt2wt c2 c3--> !! html -

foo

-

bar

-

baz

+

§foo

+

§bar

+

§baz

!! end @@ -15321,7 +15321,7 @@ Fuzz testing: Parser14 == onmouseover= == http://__TOC__ !! html -

onmouseover=[edit]

+

§onmouseover=[edit]

http://

Contents

  • 1 onmouseover=
  • @@ -15330,7 +15330,7 @@ http://

    Contents

    !! html+tidy -

    onmouseover=[edit]

    +

    §onmouseover=[edit]

    http://

    @@ -15348,13 +15348,13 @@ Fuzz testing: Parser14-table ==a== {| STYLE=__TOC__ !! html -

    a[edit]

    +

    §a[edit]

    !! html+tidy -

    a[edit]

    +

    §a[edit]

    @@ -17363,7 +17363,7 @@ Inclusion of !userCanEdit() content !! wikitext {{MediaWiki:Fake}} !! html -

    header[edit]

    +

    §header[edit]

    !! end @@ -17395,12 +17395,12 @@ Out-of-order TOC heading levels -

    2[edit]

    -
    6[edit]
    -

    3[edit]

    -

    1[edit]

    -
    5[edit]
    -

    2[edit]

    +

    §2[edit]

    +
    §6[edit]
    +

    §3[edit]

    +

    §1[edit]

    +
    §5[edit]
    +

    §2[edit]

    !! end @@ -17507,7 +17507,7 @@ anchorencode encodes like the TOC generator: (bug 18431) {{anchorencode: _ +:.3A%3A&&]] }} __NOEDITSECTION__ !! html -

    _ +:.3A%3A&&]]

    +

    §_ +:.3A%3A&&]]

    .2B:.3A.253A.26.26.5D.5D

    !! end @@ -17757,7 +17757,7 @@ language=sr variant=sr-ec !! wikitext == -{Naslov}- == !! html -

    Naslov[уреди]

    +

    §Naslov[уреди]

    !! end @@ -18147,7 +18147,7 @@ Morwen/13: Unclosed link followed by heading !! html

    [[link

    -

    heading[edit]

    +

    §heading[edit]

    !! end @@ -18159,7 +18159,7 @@ HHP2.1: Heuristics for headings in preprocessor parenthetical structures !! html

    {{foo|

    -

    heading

    +

    §heading

    !! end @@ -18171,7 +18171,7 @@ HHP2.2: Heuristics for headings in preprocessor parenthetical structures !! html

    {{foo|

    -

    heading[edit]

    +

    §heading[edit]

    !! end @@ -19055,7 +19055,7 @@ __TOC__ -

    Lost episodes[edit]

    +

    §Lost episodes[edit]

    !! end @@ -19073,7 +19073,7 @@ __TOC__ -

    should be bold then normal text[edit]

    +

    §should be bold then normal text[edit]

    !! end @@ -19091,7 +19091,7 @@ __TOC__ -

    Image Foobar.jpg[edit]

    +

    §Image Foobar.jpg[edit]

    !! end @@ -19109,7 +19109,7 @@ __TOC__ -

    Quote
    [edit]

    +

    §
    Quote
    [edit]

    !! html+tidy
    @@ -19120,7 +19120,7 @@ __TOC__
  • 1 Quote
  • -

    +

    §

    Quote

    @@ -19143,7 +19143,7 @@ QED -

    Proof: 2 < 3[edit]

    +

    §Proof: 2 < 3[edit]

    Hanc marginis exiguitas non caperet. QED

    @@ -19164,8 +19164,8 @@ __TOC__ -

    Foo Bar[edit]

    -

    Foo
    Bar
    [edit]

    +

    §Foo Bar[edit]

    +

    §Foo
    Bar
    [edit]

    !! html+tidy
    @@ -19177,8 +19177,8 @@ __TOC__
  • 2 Foo Bar
  • -

    Foo Bar[edit]

    -

    Foo

    +

    §Foo Bar[edit]

    +

    §Foo

    Bar

    @@ -19200,8 +19200,8 @@ __TOC__ -

    Hello[edit]

    -

    b">Evilbye[edit]

    +

    §Hello[edit]

    +

    § b">Evilbye[edit]

    !! end @@ -19229,11 +19229,11 @@ __TOC__ -

    C++[edit]

    -

    זבנג![edit]

    -

    The attributes on these span tags must be deleted from the TOC[edit]

    -

    All attributes on these span tags must be deleted from the TOC[edit]

    -

    Attributes after dir on these span tags must be deleted from the TOC[edit]

    +

    §C++[edit]

    +

    §זבנג![edit]

    +

    §The attributes on these span tags must be deleted from the TOC[edit]

    +

    §All attributes on these span tags must be deleted from the TOC[edit]

    +

    §Attributes after dir on these span tags must be deleted from the TOC[edit]

    !! end @@ -19249,7 +19249,7 @@ __TOC__ -

    test[edit]

    +

    §test[edit]

    !! end @@ -19266,7 +19266,7 @@ title=[[Main Page]] !! wikitext {{int:Bug32057}} !! html -

    Headline text[edit]

    +

    §Headline text[edit]

    !! end @@ -19381,7 +19381,7 @@ nowiki inside link inside heading (bug 18295) !! wikitext ==[[foo|xyz]]== !! html -

    xyz[edit]

    +

    §xyz[edit]

    !! end @@ -19566,11 +19566,11 @@ Lead

    Lead

    -

    Section 1[edit]

    -

    Section 2[edit]

    -

    Section 3[edit]

    -

    Section 4[edit]

    -

    Section 5[edit]

    +

    §Section 1[edit]

    +

    §Section 2[edit]

    +

    §Section 3[edit]

    +

    §Section 4[edit]

    +

    §Section 5[edit]

    !! end -- 2.20.1