From 4c3250e0214b90f8352b2250c674a0f143c30c20 Mon Sep 17 00:00:00 2001 From: Daniel Kraus Date: Wed, 30 Aug 2017 22:23:03 +0200 Subject: [PATCH] Add tests for headings. --- includes/Splitter.php | 17 ++++++++++++----- tests/phpunit/SplitterTest.php | 20 ++++++++++++++++++++ 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/includes/Splitter.php b/includes/Splitter.php index 8b8d2c8..c13952c 100644 --- a/includes/Splitter.php +++ b/includes/Splitter.php @@ -114,14 +114,21 @@ class Splitter { // wiki headings ("= ... =", "== ... ==" etc.), // urls ("http://example.com", "[http://example.com]", "[http://example.com Description]", // and email addresses ("mail@example.com"). - // Since there is a user option to skip headings, we make this part of the expression - // optional. Note that in order to use preg_split(), it is important to have only one - // capturing subpattern (which precludes the use of conditional subpatterns). - ( $this->config->parseHeadings ) ? $delimiter = '' : $delimiter = '^=+.+?=+|'; + + // Match WikiText headings. + // Since there is a user option to skip headings, we make this part of the + // expression optional. Note that in order to use preg_split(), it is + // important to have only one capturing subpattern (which precludes the use + // of conditional subpatterns). + // Caveat: This regex pattern should be improved to deal with balanced '='s + // only. However, this would require grouping in the pattern which does not + // agree with preg_split. + $headingsDelimiter = $this->config->parseHeadings ? '' : '^=+[^=]+=+$|'; + $urlPattern = '[a-z]+?\:\/\/(?:\S+\.)+\S+(?:\/.*)?'; $this->splitter = '/(' . // exclude from linking: '\[\[.*?\]\]|' . // links - $delimiter . // titles (if requested) + $headingsDelimiter . // headings (if requested) $templatesDelimiter . // templates (if requested) '^ .+?\n|\n .+?\n|\n .+?$|^ .+?$|' . // preformatted text '.*?<.nowiki>|.*?<\/code>|' . // nowiki/code diff --git a/tests/phpunit/SplitterTest.php b/tests/phpunit/SplitterTest.php index 2ac2450..c503b73 100644 --- a/tests/phpunit/SplitterTest.php +++ b/tests/phpunit/SplitterTest.php @@ -73,6 +73,26 @@ class SplitterTest extends MediaWikiTestCase { 'With skipTemplates = true, this may be linked {{mytemplate|param={{transcluded}}}}', [ 'With skipTemplates = true, this may be linked ', '{{mytemplate|param={{transcluded}}}}', '' ] ], + [ + true, // skipTemplates + true, // parseHeadings + "With parseHeadings = true,\n==a heading may be linked==\n", + [ "With parseHeadings = true,\n==a heading may be linked==\n" ] + ], + [ + true, // skipTemplates + false, // parseHeadings + // no trailing newline in the following string because it would be swallowed + "With parseHeadings = false,\n==a heading may not be linked==", + [ "With parseHeadings = false,\n", "==a heading may not be linked==", '' ] + ], + // Improperly formatted headings cannot be dealt with appropriately for now + // [ + // true, // skipTemplates + // false, // parseHeadings + // "With parseHeadings = false,\n==an improperly formatted heading may be linked=\n", + // [ "With parseHeadings = false,\n==an improperly formatted heading may be linked=\n" ] + // ], ]; } }