r5320 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r5319‎ | r5320 | r5321 >
Date:06:17, 20 September 2004
Author:jeluf
Status:old
Tags:
Comment:
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
<!-- comment -->def
Modified paths:
  • /trunk/phase3/includes/Parser.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/Parser.php
@@ -1779,7 +1779,7 @@
17801780 $htmlattrs = $this->getHTMLattrs () ;
17811781
17821782 # Remove HTML comments
1783 - $text = preg_replace( '/(\\n *<!--.*--> *|<!--.*-->)/sU', '', $text );
 1783+ $text = $this->removeHTMLcomments( $text );
17841784
17851785 $bits = explode( '<', $text );
17861786 $text = array_shift( $bits );
@@ -1858,7 +1858,46 @@
18591859 return $text;
18601860 }
18611861
 1862+ # Remove '<!--', '-->', and everything between.
 1863+ # To avoid leaving blank lines, when a comment is both preceded
 1864+ # and followed by a newline (ignoring spaces), trim leading and
 1865+ # trailing spaces and one of the newlines.
 1866+ /* private */ function removeHTMLcomments( $text ) {
 1867+ $fname='Parser::removeHTMLcomments';
 1868+ wfProfileIn( $fname );
 1869+ while (($start = strpos($text, '<!--')) !== false) {
 1870+ $end = strpos($text, '-->', $start + 4);
 1871+ if ($end === false) {
 1872+ # Unterminated comment; bail out
 1873+ break;
 1874+ }
18621875
 1876+ $end += 3;
 1877+
 1878+ # Trim space and newline if the comment is both
 1879+ # preceded and followed by a newline
 1880+ $spaceStart = $start - 1;
 1881+ $spaceLen = $end - $spaceStart;
 1882+ while (substr($text, $spaceStart, 1) === ' ') {
 1883+ $spaceStart--;
 1884+ $spaceLen++;
 1885+ }
 1886+ while (substr($text, $spaceStart + $spaceLen, 1) === ' ')
 1887+ $spaceLen++;
 1888+ if (substr($text, $spaceStart, 1) === "\n" and substr($text, $spaceStart + $spaceLen, 1) === "\n") {
 1889+ # Remove the comment, leading and trailing
 1890+ # spaces, and leave only one newline.
 1891+ $text = substr_replace($text, "\n", $spaceStart, $spaceLen + 1);
 1892+ }
 1893+ else {
 1894+ # Remove just the comment.
 1895+ $text = substr_replace($text, '', $start, $end - $start);
 1896+ }
 1897+ }
 1898+ wfProfileOut( $fname );
 1899+ return $text;
 1900+ }
 1901+
18631902 # This function accomplishes several tasks:
18641903 # 1) Auto-number headings if that option is enabled
18651904 # 2) Add an [edit] link to sections for logged in users who have enabled the option

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r5234BUG#487 make regex not-so-greedy. If anchored to end-of-line, several lines a...jeluf05:53, 15 September 2004

Status & tagging log