From b33b5d5840fe0c59d776e78755b0c8a5e712a805 Mon Sep 17 00:00:00 2001
From: Gabriel Wicke
Date: Tue, 20 May 2014 09:14:21 -0700
Subject: [PATCH] Update list item newline handling to follow Parsoid's model
This improves on commit 34bd573144883af9ace7ead32d3be8f692431718 by matching
Parsoid's newline handling in the PHP parser. It is the outcome of a
discussion with Erwin, where we agreed that
* foo
* bar
should produce
See the discussion in https://gerrit.wikimedia.org/r/#/c/94443/
The original rendering issue this tried to address is no longer present after
a change to the template. The pure CSS solution is now working.
Bug: 39617
Bug: 56809
Change-Id: Ib7aa9449bbd994cb23b83b3f23cff944b1cddadf
---
includes/parser/Parser.php | 32 +-
tests/parser/parserTests.txt | 1104 ++++++-------------------
tests/phpunit/includes/StatusTest.php | 4 +-
3 files changed, 265 insertions(+), 875 deletions(-)
diff --git a/includes/parser/Parser.php b/includes/parser/Parser.php
index d2a20df971..4385dd661c 100644
--- a/includes/parser/Parser.php
+++ b/includes/parser/Parser.php
@@ -2327,13 +2327,13 @@ class Parser {
$result = $this->closeParagraph();
if ( '*' === $char ) {
- $result .= "\n- ";
+ $result .= "
- ";
} elseif ( '#' === $char ) {
- $result .= "
\n- ";
+ $result .= "
- ";
} elseif ( ':' === $char ) {
- $result .= "
\n- ";
+ $result .= "
- ";
} elseif ( ';' === $char ) {
- $result .= "
\n- ";
+ $result .= "
- ";
$this->mDTopen = true;
} else {
$result = '';
@@ -2377,20 +2377,20 @@ class Parser {
*/
function closeList( $char ) {
if ( '*' === $char ) {
- $text = "
\n
";
+ $text = "
";
} elseif ( '#' === $char ) {
- $text = "\n";
+ $text = "";
} elseif ( ':' === $char ) {
if ( $this->mDTopen ) {
$this->mDTopen = false;
- $text = "\n";
+ $text = "";
} else {
- $text = "\n";
+ $text = "";
}
} else {
return '';
}
- return $text . "\n";
+ return $text;
}
/**#@-*/
@@ -2488,6 +2488,9 @@ class Parser {
}
# Open prefixes where appropriate.
+ if ( $lastPrefix && $prefixLength > $commonPrefixLength ) {
+ $output .= "\n";
+ }
while ( $prefixLength > $commonPrefixLength ) {
$char = substr( $prefix, $commonPrefixLength, 1 );
$output .= $this->openList( $char );
@@ -2501,6 +2504,9 @@ class Parser {
}
++$commonPrefixLength;
}
+ if ( !$prefixLength && $lastPrefix ) {
+ $output .= "\n";
+ }
$lastPrefix = $prefix2;
}
@@ -2582,12 +2588,18 @@ class Parser {
$this->mInPre = false;
}
if ( $paragraphStack === false ) {
- $output .= $t . "\n";
+ $output .= $t;
+ if ( $prefixLength === 0 ) {
+ $output .= "\n";
+ }
}
}
while ( $prefixLength ) {
$output .= $this->closeList( $prefix2[$prefixLength - 1] );
--$prefixLength;
+ if ( !$prefixLength ) {
+ $output .= "\n";
+ }
}
if ( $this->mLastSection != '' ) {
$output .= '' . $this->mLastSection . '>';
diff --git a/tests/parser/parserTests.txt b/tests/parser/parserTests.txt
index 569c165321..941a6a0bfa 100644
--- a/tests/parser/parserTests.txt
+++ b/tests/parser/parserTests.txt
@@ -413,12 +413,8 @@ Simple list
* Item 1
* Item 2
!! html
-
+
!! end
@@ -441,38 +437,21 @@ Italics and bold
* plain l'''italic''plain
* plain l''''bold''' plain
!! html
-
-- plain
-
-- plainitalicplain
-
-- plainitalicplainitalicplain
-
-- plainboldplain
-
-- plainboldplainboldplain
-
-- plainitalicplainboldplain
-
-- plainboldplainitalicplain
-
-- plainitalicbold-italicitalicplain
-
-- plainboldbold-italicboldplain
-
-- plainbold-italicitalicplain
-
-- plainbold-italicboldplain
-
-- plainitalicbold-italicplain
-
-- plainboldbold-italicplain
-
-- plain l'italicplain
-
-- plain l'bold plain
-
-
+- plain
+- plainitalicplain
+- plainitalicplainitalicplain
+- plainboldplain
+- plainboldplainboldplain
+- plainitalicplainboldplain
+- plainboldplainitalicplain
+- plainitalicbold-italicitalicplain
+- plainboldbold-italicboldplain
+- plainbold-italicitalicplain
+- plainbold-italicboldplain
+- plainitalicbold-italicplain
+- plainboldbold-italicplain
+- plain l'italicplain
+- plain l'bold plain
!! end
@@ -1215,24 +1194,12 @@ nowiki 3
*There is not nowiki.
*There is nowiki.
!! html
-
-- There is not nowiki.
-
-- There is nowiki.
-
-
-
-- There is not nowiki.
-
-- There is nowiki.
-
-
-
-- There is not nowiki.
-
-- There is nowiki.
-
-
+- There is not nowiki.
+- There is nowiki.
+- There is not nowiki.
+- There is nowiki.
+- There is not nowiki.
+- There is nowiki.
!! end
@@ -2085,10 +2052,7 @@ Templates: Strip leading and trailing whitespace from named-param values
b
c
-
+
!! end
@@ -2129,10 +2093,7 @@ Templates: Don't strip whitespace from positional-param values
-
+
g
@@ -2733,11 +2694,8 @@ Simple definition
!! wikitext
; name : Definition
!! html
-
-- name
-- Definition
-
-
+- name
+- Definition
!! end
@@ -2746,10 +2704,7 @@ Definition list for indentation only
!! wikitext
: Indented text
!! html
-
-- Indented text
-
-
+- Indented text
!! end
@@ -2758,11 +2713,8 @@ Definition list with no space
!! wikitext
;name:Definition
!! html
-
-- name
-- Definition
-
-
+- name
+- Definition
!!end
@@ -2771,11 +2723,8 @@ Definition list with URL link
!! wikitext
; http://example.com/ : definition
!! html
-
-- http://example.com/
-- definition
-
-
+- http://example.com/
+- definition
!! end
@@ -2784,11 +2733,8 @@ Definition list with bracketed URL link
!! wikitext
;[http://www.example.com/ Example]:Something about it
!! html
-
-- Example
-- Something about it
-
-
+- Example
+- Something about it
!! end
@@ -2797,11 +2743,8 @@ Definition list with wikilink containing colon
!! wikitext
; [[Help:FAQ]]: The least-read page on Wikipedia
!! html
-
-- Help:FAQ
-- The least-read page on Wikipedia
-
-
+- Help:FAQ
+- The least-read page on Wikipedia
!! end
@@ -2811,11 +2754,8 @@ Definition list with news link containing colon
!! wikitext
; news:alt.wikipedia.rox: This isn't even a real newsgroup!
!! html
-
-- news:alt.wikipedia.rox
-- This isn't even a real newsgroup!
-
-
+- news:alt.wikipedia.rox
+- This isn't even a real newsgroup!
!! end
@@ -2824,10 +2764,7 @@ Malformed definition list with colon
!! wikitext
; news:alt.wikipedia.rox -- don't crash or enter an infinite loop
!! html
-
-- news:alt.wikipedia.rox -- don't crash or enter an infinite loop
-
-
+- news:alt.wikipedia.rox -- don't crash or enter an infinite loop
!! end
@@ -2836,11 +2773,8 @@ Definition lists: colon in external link text
!! wikitext
; [http://www.wikipedia2.org/ Wikipedia : The Next Generation]: OK, I made that up
!! html
-
-- Wikipedia : The Next Generation
-- OK, I made that up
-
-
+- Wikipedia : The Next Generation
+- OK, I made that up
!! end
@@ -2849,10 +2783,7 @@ Definition lists: colon in HTML attribute
!! wikitext
;bold
!! html
-
-- bold
-
-
+- bold
!! end
@@ -2861,11 +2792,8 @@ Definition lists: self-closed tag
!! wikitext
;one
two : two-line fun
!! html
-
-- one
two
-- two-line fun
-
-
+- one
two
+- two-line fun
!! end
@@ -2897,15 +2825,9 @@ Definition and unordered list using wiki syntax nested in unordered list using h
!! html
-
-
-- term
-- description
-
-
-
+- term
+- description
+
!! end
@@ -2917,11 +2839,8 @@ Definition list with empty definition and following paragraph
; term:
Paragraph text
!! html
-
-- term
--
-
-
+- term
+
Paragraph text
!! end
@@ -2929,17 +2848,9 @@ Paragraph text
!! test
Nested definition lists using html syntax
!! wikitext
--
-
-- Foo
-
-
-!! html
--
-
-- Foo
-
-
+- x
+- a
+- b
!! end
@@ -2950,14 +2861,9 @@ Definition Lists: No nesting: Multiple dd's
:a
:b
!! html
-
-- x
-
-- a
-
-- b
-
-
+- x
+- a
+- b
!! end
@@ -2968,18 +2874,9 @@ Definition Lists: Indentation: Regular
::i2
:::i3
!! html
-
-- i1
-
-- i2
-
-- i3
-
-
-
-
-
-
+- i1
+
- i2
+
- i3
!! end
@@ -2989,17 +2886,8 @@ Definition Lists: Indentation: Missing 1st level
::i2
:::i3
!! html
-
-
-- i2
-
-- i3
-
-
-
-
-
-
+- i2
+
- i3
!! end
@@ -3008,16 +2896,7 @@ Definition Lists: Indentation: Multi-level indent
!! wikitext
:::i3
!! html
-
-
-
-- i3
-
-
-
-
-
-
+- i3
!! end
@@ -3139,17 +3018,11 @@ Table / list interaction: indented table with lists in table contents
a
-
+
|
c
-
+
|
!! end
@@ -3172,27 +3045,15 @@ Table / list interaction: lists nested in tables nested in indented lists
-
+
!!end
@@ -3280,18 +3141,8 @@ Definition Lists: Nesting: Test 4
::;t3
:::d3
!! html
-
-
-
-- t3
-
-- d3
-
-
-
-
-
-
+- t3
+- d3
!! end
@@ -3306,22 +3157,9 @@ Definition Lists: Mixed Lists: Test 1
::* bar
:; baz
!! html/php
-
-
--
-
-
-
-- baz
-
-
-
-
+
+- baz
!! html/parsoid
@@ -3345,15 +3183,8 @@ Definition Lists: Mixed Lists: Test 2
*: d1
*: d2
!! html
-
+
!! end
@@ -3364,21 +3195,8 @@ Definition Lists: Mixed Lists: Test 3
*::: d1
*::: d2
!! html
-
-
-
-
-- d1
-
-- d2
-
-
-
-
-
-
-
-
+
!! end
@@ -3389,17 +3207,10 @@ Definition Lists: Mixed Lists: Test 4
*;d1 :d2
*;d3 :d4
!! html
-
!! end
@@ -3410,17 +3221,8 @@ Definition Lists: Mixed Lists: Test 5
*:d1
*:: d2
!! html
-
+
!! end
@@ -3431,23 +3233,8 @@ Definition Lists: Mixed Lists: Test 6
#*:d1
#*::: d3
!! html
-
-
-
-- d1
-
-
-- d3
-
-
-
-
-
-
-
-
-
-
+
!! end
@@ -3458,15 +3245,8 @@ Definition Lists: Mixed Lists: Test 7
:* d1
:* d2
!! html
-
--
-
-
+
!! end
@@ -3477,20 +3257,8 @@ Definition Lists: Mixed Lists: Test 8
:* d1
::* d2
!! html
-
--
-
--
-
-
-
-
+-
+
!! end
@@ -3500,14 +3268,8 @@ Definition Lists: Mixed Lists: Test 9
!! wikitext
*;foo :bar
!! html
-
+
!! end
@@ -3517,17 +3279,8 @@ Definition Lists: Mixed Lists: Test 10
!! wikitext
*#;foo :bar
!! html
-
-
-
-- foo
-- bar
-
-
-
-
-
-
+
!! end
@@ -3542,37 +3295,10 @@ Definition Lists: Mixed Lists: Test 11
*#*#;*;;foo :bar
*#*#;boo :baz
!! html/php
-
-
-- boo
-- baz
-
-
-
-
-
-
-
-
-
-
+
!! html/parsoid
@@ -3610,32 +3336,8 @@ Definition Lists: Weird Ones: Test 1
!! wikitext
*#;*::;; foo : bar (who uses this?)
!! html/php
-
-
-
-- foo
-
-
-
-
-
-- bar (who uses this?)
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
!! html/parsoid
@@ -6186,12 +5888,8 @@ Interwiki link encoding conversion (bug 1636)
*[[Wikipedia:ro:Olteniţa]]
*[[Wikipedia:ro:Olteniţa]]
!! html
-
+
!! end
@@ -6705,14 +6403,9 @@ Common list
* item 2
*item 3
!! html
-
-- Common list
-
-- item 2
-
-- item 3
-
-
+- Common list
+- item 2
+- item 3
!! end
@@ -6723,14 +6416,9 @@ Numbered list
#item 2
# item 3
!! html
-
-- Numbered list
-
-- item 2
-
-- item 3
-
-
+- Numbered list
+- item 2
+- item 3
!! end
@@ -6753,67 +6441,21 @@ Mixed list
*** Level 3
#** Level 3, but ordered
!! html
-
-- Mixed list
-
-- with numbers
-
-
-
-
-- and numbers
-
-
-
+- Mixed list
+
- with numbers
+
+- and numbers
- bullets again
-
+- bullet level 2
+
- bullet level 3
+
- Number on level 4
- bullet level 2
-
-- bullet level 3
-
-- Number on level 4
-
-
-
-
-
-- bullet level 2
-
-- Number on level 3
-
-- Number on level 3
-
-
-
-
-
-- number level 2
-
-
-
+- Number on level 3
+- Number on level 3
+- number level 2
- Level 1
-
-
-
-
-
-
-- Level 3, but ordered
-
-
-
-
-
-
+
+
!! end
@@ -6823,14 +6465,8 @@ Nested lists 1
*foo
**bar
!! html
-
+
!! end
@@ -6840,32 +6476,19 @@ Nested lists 2
**foo
*bar
!! html
-
+
!! end
-!! test
-Nested lists 3 (first element empty)
-!! wikitext
-*
-**bar
-!! html
-
+!! test
+Nested lists 3 (first element empty)
+!! wikitext
+*
+**bar
+!! html
+
!! end
@@ -6875,15 +6498,8 @@ Nested lists 4 (first element empty)
**
*bar
!! html
-
+
!! end
@@ -6893,15 +6509,8 @@ Nested lists 5 (both elements empty)
**
*
!! html
-
+
!! end
@@ -6911,14 +6520,8 @@ Nested lists 6 (both elements empty)
*
**
!! html
-
+
!! end
@@ -6927,16 +6530,7 @@ Nested lists 7 (skip initial nesting levels)
!! wikitext
*** foo
!! html
-
+
!! end
@@ -6948,21 +6542,10 @@ Nested lists 8 (multiple nesting transitions)
** baz
* boo
!! html
-
+
!! end
@@ -6973,14 +6556,9 @@ Nested lists 8 (multiple nesting transitions)
*bar
*baz
!! html
-
+
!! end
@@ -6990,12 +6568,8 @@ Nested lists 8 (multiple nesting transitions)
*foo {{echo|bar
}}*baz
!! html
-
+
!! end
@@ -7006,14 +6580,9 @@ List items are not parsed correctly following a block (bug 785)
* bar
* zar
!! html
-
--
foo
-
--
bar
-
-- zar
-
-
+
!! end
@@ -7032,30 +6601,15 @@ List items from template
* notSOL{{inner list}}
* item 2
!! html
-
-
-- item 0
-
-- item 1
-
-- item 2
-
-
-
-- item 0
-
-- notSOL
-
-- item 1
-
-- item 2
-
-
+
+
+- item 0
+- notSOL
+- item 1
+- item 2
!! end
@@ -7068,22 +6622,10 @@ List interrupted by empty line or heading
== A heading ==
* Another list item
!! html
-
-
+
+
A heading[edit]
-
+
!!end
@@ -7113,16 +6655,10 @@ Single-comment whitespace lines dont break lists, and neither do multi-comment w
*d
!! html
-
+
!!end
@@ -7138,16 +6674,10 @@ Replacing whitespace with tabs still doesn't break the list (gerrit 78327)
either-->
*d
!! html
-
+
!!end
@@ -7465,36 +6995,20 @@ Magic Words LOCAL (UTC)
* {{LOCALDOW}}
* {{LOCALTIMESTAMP}}
!! html
-
-- 01
-
-- 1
-
-- January
-
-- January
-
-- Jan
-
-- 1
-
-- 01
-
-- Thursday
-
-- 1970
-
-- 00:02
-
-- 00
-
-- 1
-
-- 4
-
-- 19700101000203
-
-
+- 01
+- 1
+- January
+- January
+- Jan
+- 1
+- 01
+- Thursday
+- 1970
+- 00:02
+- 00
+- 1
+- 4
+- 19700101000203
!! end
@@ -9362,12 +8876,8 @@ unused}}}}
*{{echo|b {{nonexistent|
unused}}}}
!! html
-
+
!!end
@@ -15078,13 +14588,7 @@ Handling of
in URLs
!! wikitext
** irc://
a
!! html/php
-
+
!! html/parsoid
+
!! html/parsoid
@@ -15186,52 +14684,28 @@ title=[[Parser test]]
* {{SUBJECTSPACEE}}
* {{Dynamic|{{NUMBEROFUSERS}}|{{NUMBEROFPAGES}}|{{CURRENTVERSION}}|{{CONTENTLANGUAGE}}|{{DIRECTIONMARK}}|{{CURRENTTIMESTAMP}}|{{NUMBEROFARTICLES}}}}
!! html
-
-- Parser test
-
-- Parser_test
-
-- Parser test
-
-- Parser_test
-
-- Parser test
-
-- Parser_test
-
-- Parser test
-
-- Parser_test
-
-- Parser test
-
-- Parser_test
-
-- Talk:Parser test
-
-- Talk:Parser_test
-
-- Parser test
-
-- Parser_test
-
--
-
--
-
-- 0
-
-- Talk
-
-- Talk
-
--
-
--
-
-- Template:Dynamic
-
-
+- Parser test
+- Parser_test
+- Parser test
+- Parser_test
+- Parser test
+- Parser_test
+- Parser test
+- Parser_test
+- Parser test
+- Parser_test
+- Talk:Parser test
+- Talk:Parser_test
+- Parser test
+- Parser_test
+-
+-
+- 0
+- Talk
+- Talk
+-
+-
+- Template:Dynamic
!! end
### Note: Above tests excludes the "{{NUMBEROFADMINS}}" magic word because it generates a MySQL error when included.
@@ -15819,17 +15293,12 @@ Definition list code coverage
; title : def
;title: def
!! html
-
-- title
-- def
-
+- title
+- def
- title
-- def
-
+- def
- title
-- def
-
-
+- def
!! end
@@ -16565,12 +16034,8 @@ Bug 529: Uncovered bullet
!! wikitext
* Foo {{bullet}}
!! html
-
+
!! end
@@ -16585,30 +16050,8 @@ Bug 529: Uncovered bullet leaving empty list, normally removed by tidy
!! wikitext
******* Foo {{bullet}}
!! html
-
+
!! end
@@ -16640,12 +16083,8 @@ Bug 529: Uncovered bullet in parser function result
!! wikitext
* Foo {{lc:{{bullet}} }}
!! html
-
+
!! end
@@ -18821,38 +18260,14 @@ Lists: 1. Nested inside html
#;foo
!! html
-
-
-
-
-
-- *foo
-
-
-
-- #foo
-
-
-
-- :foo
-
-
-
-- ;foo
-
-
+
+
+
+
+- *foo
+- #foo
+- :foo
+- ;foo
!!end
@@ -18868,24 +18283,11 @@ Lists: 2. Inside definition lists
::foo
!! html
-
-- ;foo
-
-
-
-- :foo
-
-
-
-- :foo
-
-- bar
-
-
-
-- :foo
-
-
+- ;foo
+- :foo
+- :foo
+- bar
+- :foo
!!end
@@ -18896,14 +18298,8 @@ Lists: 3. Only bullets at start of text should be escaped
**foo''it''*bar
!! html
-
-
+
+
!!end
@@ -18953,30 +18349,12 @@ Lists: 5. No unnecessary escapes
* : a
!! html
-
-
-
-
-
-
+
+
+
+
+
+
!!end
diff --git a/tests/phpunit/includes/StatusTest.php b/tests/phpunit/includes/StatusTest.php
index 1a6cfe6faf..3829f904ff 100644
--- a/tests/phpunit/includes/StatusTest.php
+++ b/tests/phpunit/includes/StatusTest.php
@@ -355,7 +355,7 @@ class StatusTest extends MediaWikiLangTestCase {
$testCases[ '2StringWarnings' ] = array(
$status,
"* \n* \n",
- "\n- <fooBar!>\n
\n- <fooBar2!>\n
\n
\n",
+ "\n",
);
$status = new Status();
@@ -372,7 +372,7 @@ class StatusTest extends MediaWikiLangTestCase {
$testCases[ '2MessageWarnings' ] = array(
$status,
"* \n* \n",
- "\n- <fooBar!>\n
\n- <fooBar2!>\n
\n
\n",
+ "\n",
);
return $testCases;
--
2.20.1