Index: trunk/phase3/includes/Parser.php |
— | — | @@ -1779,7 +1779,7 @@ |
1780 | 1780 | $htmlattrs = $this->getHTMLattrs () ; |
1781 | 1781 | |
1782 | 1782 | # Remove HTML comments |
1783 | | - $text = preg_replace( '/(\\n *<!--.*--> *|<!--.*-->)/sU', '', $text ); |
| 1783 | + $text = $this->removeHTMLcomments( $text ); |
1784 | 1784 | |
1785 | 1785 | $bits = explode( '<', $text ); |
1786 | 1786 | $text = array_shift( $bits ); |
— | — | @@ -1858,7 +1858,46 @@ |
1859 | 1859 | return $text; |
1860 | 1860 | } |
1861 | 1861 | |
| 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 | + } |
1862 | 1875 | |
| 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 | + |
1863 | 1902 | # This function accomplishes several tasks: |
1864 | 1903 | # 1) Auto-number headings if that option is enabled |
1865 | 1904 | # 2) Add an [edit] link to sections for logged in users who have enabled the option |