Fix link prefix/suffixes around Category and Language links (take 2).
authorC. Scott Ananian <cscott@cscott.net>
Sun, 13 Aug 2017 18:19:13 +0000 (14:19 -0400)
committerC. Scott Ananian <cscott@cscott.net>
Fri, 8 Sep 2017 20:12:21 +0000 (16:12 -0400)
Previous attempt was I943cd9bec0855d9a326b0b50739d686a29995370, reverted in
e687f2da3eb5be95855d9aafc53a7524dfaefa34 due to T174639.

There's still a weird behavior with newline stripping between links, which
I'll try to tackle in a follow-on patch (T175416).

Bug: T2087
Bug: T10897
Bug: T87753
Bug: T174639
Change-Id: I8228cdd3b80faf899000adb511a983edc454bc76

includes/parser/Parser.php
tests/parser/parserTests.txt

index 988e248..0d11d5a 100644 (file)
@@ -2296,8 +2296,13 @@ class Parser {
                                                $this->mOutput->addLanguageLink( $nt->getFullText() );
                                        }
 
+                                       /**
+                                        * Strip the whitespace interwiki links produce, see T10897
+                                        */
                                        $s = rtrim( $s . $prefix );
-                                       $s .= trim( $trail, "\n" ) == '' ? '' : $prefix . $trail;
+                                       // Special case: strip newlines when only thing between
+                                       // this link and next are newlines
+                                       $s .= trim( $trail, "\n" ) === '' ? '' : $trail;
                                        continue;
                                }
 
@@ -2322,7 +2327,13 @@ class Parser {
                                                continue;
                                        }
                                } elseif ( $ns == NS_CATEGORY ) {
-                                       $s = rtrim( $s . "\n" ); # T2087
+                                       /**
+                                        * Strip the whitespace Category links produce, see T2087
+                                        */
+                                       $s = rtrim( $s . $prefix ); # T2087, T87753
+                                       // Special case: strip newlines when only thing between
+                                       // this link and next are newlines
+                                       $s .= trim( $trail, "\n" ) === '' ? '' : $trail;
 
                                        if ( $wasblank ) {
                                                $sortkey = $this->getDefaultSort();
@@ -2334,11 +2345,6 @@ class Parser {
                                        $sortkey = $this->getConverterLanguage()->convertCategoryKey( $sortkey );
                                        $this->mOutput->addCategory( $nt->getDBkey(), $sortkey );
 
-                                       /**
-                                        * Strip the whitespace Category links produce, see T2087
-                                        */
-                                       $s .= trim( $prefix . $trail, "\n" ) == '' ? '' : $prefix . $trail;
-
                                        continue;
                                }
                        }
index 3f93793..ca16738 100644 (file)
@@ -16102,6 +16102,68 @@ parsoid=wt2html
 <link rel="mw:PageProp/Category" href="./Category:Baz" data-parsoid='{"stx":"simple","a":{"href":"./Category:Baz"},"sa":{"href":"Category:Baz"}}'/>
 !! end
 
+!! test
+9. Categories and newlines: should behave properly with linkprefix (T87753)
+!! options
+language=ar
+!! wikitext
+foo bar
+foo bar
+[[تصنيف:Foo]]
+[[تصنيف:Bar]]
+!! html/php
+<p>foo bar
+foo bar
+</p>
+!! html/parsoid
+<p>foo bar
+foo bar</p>
+<link rel="mw:PageProp/Category" href="./تصنيف:Foo"/>
+<link rel="mw:PageProp/Category" href="./تصنيف:Bar"/>
+!! end
+
+!! test
+10. No regressions on internal links following category (T174639)
+!! options
+parsoid=wt2html,html2html
+!! wikitext
+[[Category:Foo]]<div>a
+
+[[Foo]]</div>
+!! html/php
+<div>a
+<a href="/wiki/Foo" title="Foo">Foo</a></div>
+
+!! html/parsoid
+<link rel="mw:PageProp/Category" href="./Category:Foo"/><div>a
+
+<a rel="mw:WikiLink" href="./Foo" title="Foo">Foo</a></div>
+!! end
+
+# NOTE that Parsoid does not currently implement this special case
+# (T175416).  The plan is to remove this weird special-case in the PHP
+# parser to make it match the Parsoid output.
+!! test
+11. Special case where only newlines separate links
+!! options
+parsoid=wt2html,html2html
+!! wikitext
+[[Category:Foo]]
+
+[[Foo]][[es:Alimento]]
+
+[[Foo]]
+!! html/php
+<p><a href="/wiki/Foo" title="Foo">Foo</a><a href="/wiki/Foo" title="Foo">Foo</a>
+</p>
+!! html/parsoid
+<link rel="mw:PageProp/Category" href="./Category:Foo"/>
+
+<p><a rel="mw:WikiLink" href="./Foo" title="Foo">Foo</a></p><link rel="mw:PageProp/Language" href="http://es.wikipedia.org/wiki/Alimento"/>
+
+<p><a rel="mw:WikiLink" href="./Foo" title="Foo">Foo</a></p>
+!! end
+
 !! test
 Category links with multiple namespaces
 !! wikitext
@@ -16149,6 +16211,20 @@ x[[Category:Foo]]y
 <p>x<link rel="mw:PageProp/Category" href="./Flokkur:Foo" data-parsoid=""/>y</p>
 !! end
 
+!! test
+Link prefix/suffixes aren't applied to language links
+!! options
+parsoid=wt2html
+language=is
+!! wikitext
+x[[es:Foo]]y
+!! html/php
+<p>xy
+</p>
+!! html/parsoid
+<p>x<link rel="mw:PageProp/Language" href="http://es.wikipedia.org/wiki/Foo" data-parsoid=""/>y</p>
+!! end
+
 !! test
 Parsoid: Serialize link to file page with colon escape
 !! options