* fixes for the merged Poem extension, per comments by Tim Starling on CodeReview...
authorRyan Schmidt <skizzerz@users.mediawiki.org>
Sat, 11 Oct 2008 21:53:44 +0000 (21:53 +0000)
committerRyan Schmidt <skizzerz@users.mediawiki.org>
Sat, 11 Oct 2008 21:53:44 +0000 (21:53 +0000)
* adding <poem> tests to the parser tests file

includes/parser/Parser.php
maintenance/parserTests.txt

index 427e4ff..d951a02 100644 (file)
@@ -4183,34 +4183,42 @@ class Parser
         * based on http://www.mediawiki.org/wiki/Extension:Poem
         */
 
-       function renderPoem( $in, $param=array() ) {
+       function renderPoem( $in, $param = array() ) {
 
                /* using newlines in the text will cause the parser to add <p> tags,
                * which may not be desired in some cases
                */
-               $nl = isset( $param['compact'] ) ? '' : "\n";
+               $nl = array_key_exists( 'compact', $param ) ? '' : "\n";
   
                $tag = $this->insertStripItem( "<br />", $this->mStripState );
-               $text = preg_replace(
-                       array( "/^\n/", "/\n$/D", "/\n/", "/^( +)/me" ),
-                       array( "", "", "$tag\n", "str_replace(' ','&nbsp;','\\1')" ),
-                       $in );
+               // Only strip the very first and very last \n (which trim cannot do)
+               $text = $in;
+               if( substr( $in, 0, 1 ) == "\n" )
+                       $text = substr( $in, 1 );
+               if( substr( $text, -1 ) == "\n" )
+                       $text = substr( $text, 0, -1 );
+               
+               $text = str_replace( "\n", "$tag\n", $text );
+               $text = preg_replace_callback(
+                       "/^( +)/m",
+                       create_function(
+                               '$matches',
+                               'return str_replace(" ", "&nbsp;", "$matches[0]");'
+                       ),
+                       $text );
                $text = $this->recursiveTagParse( $text );
 
                // Pass HTML attributes through to the output.
                $attribs = Sanitizer::validateTagAttributes( $param, 'div' );
 
                // Wrap output in a <div> with "poem" class.
-               if( isset( $attribs['class'] ) ) {
+               if( array_key_exists( 'class', $attribs ) ) {
                        $attribs['class'] = 'poem ' . $attribs['class'];
                } else {
                        $attribs['class'] = 'poem';
                }
 
-               return wfOpenElement( 'div', $attribs ) .
-                       $nl .
-                       trim( $text ) .
-                       "$nl</div>";
+               return XML::openElement( 'div', $attribs ) . $nl . trim( $text ) . $nl . XML::closeElement( 'div' );
        }
 
        function getImageParams( $handler ) {
index f397bdc..20876f2 100644 (file)
@@ -7148,6 +7148,120 @@ language=fa
 </p>
 !! end
 
+!!test
+<poem>
+!!input
+<poem>
+this
+is
+a
+test
+</poem>
+!!result
+<div class="poem">
+<p>this<br />
+is<br />
+a<br />
+test
+</p>
+</div>
+
+!!end
+
+!!test
+  <poem> with recursive parsing
+!!input
+<poem>
+this ''is'' a '''test'''
+</poem>
+!! result
+<div class="poem">
+<p>this <i>is</i> a <b>test</b>
+</p>
+</div>
+
+!!end
+
+
+!!test
+  <poem> with leading whitespace
+!!input
+<poem>
+
+   test
+
+</poem>
+!!result
+<div class="poem">
+<p><br />
+&nbsp;&nbsp;&nbsp;test<br />
+</p>
+</div>
+
+!!end
+
+!!test
+Horizontal rule
+!!input
+<poem>
+some
+-----
+text
+</poem>
+!!result
+<div class="poem">
+<p>some<br />
+</p>
+<hr /><br />
+<p>text
+</p>
+</div>
+
+!!end
+
+!!test
+  nested <poem><nowiki>
+!!input
+<poem><nowiki>
+this
+is
+a
+test
+</nowiki></poem>
+!!result
+<div class="poem">
+<p><br />
+this<br />
+is<br />
+a<br />
+test<br />
+
+</p>
+</div>
+
+!!end
+
+!!test
+  nested <poem><nowiki> with formatting
+!!input
+<poem><nowiki>
+this
+'''is'''
+a
+test
+</nowiki></poem>
+!!result
+<div class="poem">
+<p><br />
+this<br />
+'''is'''<br />
+a<br />
+test<br />
+
+</p>
+</div>
+
+!! end
 
 #
 #