From a38fd104b07003f9ce75c4958b709e8fddfc61c5 Mon Sep 17 00:00:00 2001 From: Jens Frank Date: Mon, 20 Sep 2004 06:17:04 +0000 Subject: [PATCH] BUG#487 Use Wil's custom function to replace HTML comments instead of a regular expression. It is much more robust for different test cases, e.g. *abc def --- includes/Parser.php | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/includes/Parser.php b/includes/Parser.php index 3d3f80633a..ba915f8887 100644 --- a/includes/Parser.php +++ b/includes/Parser.php @@ -1779,7 +1779,7 @@ class Parser $htmlattrs = $this->getHTMLattrs () ; # Remove HTML comments - $text = preg_replace( '/(\\n * *|)/sU', '', $text ); + $text = $this->removeHTMLcomments( $text ); $bits = explode( '<', $text ); $text = array_shift( $bits ); @@ -1858,6 +1858,45 @@ class Parser return $text; } + # Remove '', and everything between. + # To avoid leaving blank lines, when a comment is both preceded + # and followed by a newline (ignoring spaces), trim leading and + # trailing spaces and one of the newlines. + /* private */ function removeHTMLcomments( $text ) { + $fname='Parser::removeHTMLcomments'; + wfProfileIn( $fname ); + while (($start = strpos($text, '', $start + 4); + if ($end === false) { + # Unterminated comment; bail out + break; + } + + $end += 3; + + # Trim space and newline if the comment is both + # preceded and followed by a newline + $spaceStart = $start - 1; + $spaceLen = $end - $spaceStart; + while (substr($text, $spaceStart, 1) === ' ') { + $spaceStart--; + $spaceLen++; + } + while (substr($text, $spaceStart + $spaceLen, 1) === ' ') + $spaceLen++; + if (substr($text, $spaceStart, 1) === "\n" and substr($text, $spaceStart + $spaceLen, 1) === "\n") { + # Remove the comment, leading and trailing + # spaces, and leave only one newline. + $text = substr_replace($text, "\n", $spaceStart, $spaceLen + 1); + } + else { + # Remove just the comment. + $text = substr_replace($text, '', $start, $end - $start); + } + } + wfProfileOut( $fname ); + return $text; + } # This function accomplishes several tasks: # 1) Auto-number headings if that option is enabled -- 2.20.1