From 87c7ccd9bcada35f5645e727f1524f69d0345c23 Mon Sep 17 00:00:00 2001 From: Subramanya Sastry Date: Tue, 20 Mar 2018 20:01:55 -0500 Subject: [PATCH] Fix whitespace trimming in headings * b3dd3881 was trimming whitespace in wikitext as well as HTML headings whereas the whitespace-trimming proposal was going to leave HTML tags untouched. * 30495ea1 missed this because coincidentally, the test I added there for HTML headings had a typo and used

...

instead of

...

which caused the test to magically pass. * This patch trims whitespace in doHeadings (which deals with wikitext headings) instead of formatHeadings (which deals with all headings). * Updated parser tests to account for this. Change-Id: I854f20b4c39a0a8e03d70155b269de77acf02cae --- includes/parser/Parser.php | 9 ++++++--- tests/parser/parserTests.txt | 14 ++++++++------ 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/includes/parser/Parser.php b/includes/parser/Parser.php index e54588750b..d34257fa5b 100644 --- a/includes/parser/Parser.php +++ b/includes/parser/Parser.php @@ -1623,7 +1623,9 @@ class Parser { public function doHeadings( $text ) { for ( $i = 6; $i >= 1; --$i ) { $h = str_repeat( '=', $i ); - $text = preg_replace( "/^$h(.+)$h\\s*$/m", "\\1", $text ); + // Trim non-newline whitespace from headings + // Using \s* will break for: "==\n===\n" and parse as

=

+ $text = preg_replace( "/^(?:$h)[ \\t]*(.+?)[ \\t]*(?:$h)\\s*$/m", "\\1", $text ); } return $text; } @@ -4056,10 +4058,11 @@ class Parser { # Get all headlines for numbering them and adding funky stuff like [edit] # links - this is for later, but we need the number of headlines right now - # This regexp also trims whitespace in the heading's content + # NOTE: white space in headings have been trimmed in doHeadings. They shouldn't + # be trimmed here since whitespace in HTML headings is significant. $matches = []; $numMatches = preg_match_all( - '/[1-6])(?P.*?>)\s*(?P
[\s\S]*?)\s*<\/H[1-6] *>/i', + '/[1-6])(?P.*?>)(?P
[\s\S]*?)<\/H[1-6] *>/i', $text, $matches ); diff --git a/tests/parser/parserTests.txt b/tests/parser/parserTests.txt index 3f4ecadc9e..9a4c2735f1 100644 --- a/tests/parser/parserTests.txt +++ b/tests/parser/parserTests.txt @@ -16941,7 +16941,8 @@ __NOEDITSECTION__

Header 1

Header 1.1

Header 1.2

-

Header 2

+

Header 2 +

Header 2.1

Header 2.2

@@ -30485,7 +30486,8 @@ headings, and cells. HTML versions of the same should preserve whitespace. Trim whitespace in wikitext headings, list items, table captions, headings, and cells !! wikitext __NOTOC__ -== Heading == +== Spaces == +== Tabs == * List item ; term : definition {| @@ -30503,7 +30505,8 @@ __NOTOC__ | Table Cell 1 || Table Cell 2 |} foo !! html/php+tidy -

Heading[edit]

+

Spaces[edit]

+

Tabs[edit]

  • List item
term 
definition
@@ -30537,20 +30540,19 @@ __NOTOC__ Do not trim whitespace in HTML headings, list items, table captions, headings, and cells !! wikitext __NOTOC__ -

Heading

+

Heading

  • List item
Table Heading
Table Cell
!! html/php+tidy -

Heading

+

Heading

  • List item
Table Heading
Table Cell
- !! end !! test -- 2.20.1