New Parser::setTransparentTagHook for parser extension and template compatibility
authorJens Frank <jeluf@users.mediawiki.org>
Sun, 15 Jul 2007 11:14:53 +0000 (11:14 +0000)
committerJens Frank <jeluf@users.mediawiki.org>
Sun, 15 Jul 2007 11:14:53 +0000 (11:14 +0000)
RELEASE-NOTES
includes/Parser.php
includes/Sanitizer.php

index c81da17..6904322 100644 (file)
@@ -138,6 +138,8 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN
   additional explanation in Special:Contributions
 * (bug 10520) Preview licences during upload via AJAX (toggle with
   $wgAjaxLicencePreview)
+* New Parser::setTransparentTagHook for parser extension and template
+  compatibility
 
 == Bugfixes since 1.10 ==
 
index 04cd9ed..11e9ee2 100644 (file)
@@ -97,7 +97,7 @@ class Parser
         * @private
         */
        # Persistent:
-       var $mTagHooks, $mFunctionHooks, $mFunctionSynonyms, $mVariables;
+       var $mTagHooks, $mTransparentTagHooks, $mFunctionHooks, $mFunctionSynonyms, $mVariables;
        
        # Cleared with clearState():
        var $mOutput, $mAutonumber, $mDTopen, $mStripState;
@@ -128,6 +128,7 @@ class Parser
         */
        function Parser() {
                $this->mTagHooks = array();
+               $this->mTransparentTagHooks = array();
                $this->mFunctionHooks = array();
                $this->mFunctionSynonyms = array( 0 => array(), 1 => array() );
                $this->mFirstCall = true;
@@ -329,6 +330,33 @@ class Parser
 
                wfRunHooks( 'ParserBeforeTidy', array( &$this, &$text ) );
 
+//!JF Move to its own function
+
+               $uniq_prefix = $this->mUniqPrefix;
+                $matches = array();
+               $elements = array_keys( $this->mTransparentTagHooks );
+                $text = Parser::extractTagsAndParams( $elements, $text, $matches, $uniq_prefix );
+
+                foreach( $matches as $marker => $data ) {
+                        list( $element, $content, $params, $tag ) = $data;
+                        $tagName = strtolower( $element );
+                        if( isset( $this->mTransparentTagHooks[$tagName] ) ) {
+                                $output = call_user_func_array( $this->mTransparentTagHooks[$tagName],
+                                        array( $content, $params, $this ) );
+                        } else {
+                               $output = $tag;
+                       }
+                       $this->mStripState->general->setPair( $marker, $output );
+               }
+               $text = $this->mStripState->unstripGeneral( $text );
+
+
+
+
+
+
+
+
                $text = Sanitizer::normalizeCharReferences( $text );
 
                if (($wgUseTidy and $this->mOptions->mTidy) or $wgAlwaysUseTidy) {
@@ -1006,7 +1034,7 @@ class Parser
                $text = strtr( $text, array( '<noinclude>' => '', '</noinclude>' => '') );
                $text = StringUtils::delimiterReplace( '<includeonly>', '</includeonly>', '', $text );
 
-               $text = Sanitizer::removeHTMLtags( $text, array( &$this, 'attributeStripCallback' ) );
+               $text = Sanitizer::removeHTMLtags( $text, array( &$this, 'attributeStripCallback' ), array(), array_keys( $this->mTransparentTagHooks ) );
 
                $text = $this->replaceVariables( $text, $args );
                wfRunHooks( 'InternalParseBeforeLinks', array( &$this, &$text, &$this->mStripState ) );
@@ -3963,6 +3991,14 @@ class Parser
                return $oldVal;
        }
 
+       function setTransparentTagHook( $tag, $callback ) {
+               $tag = strtolower( $tag );
+               $oldVal = isset( $this->mTransparentTagHooks[$tag] ) ? $this->mTransparentTagHooks[$tag] : null;
+               $this->mTransparentTagHooks[$tag] = $callback;
+
+               return $oldVal;
+       }
+
        /**
         * Create a function, e.g. {{sum:1|2|3}}
         * The callback function should have the form:
@@ -4601,7 +4637,7 @@ class Parser
        /**#@+
         * Accessor
         */
-       function getTags() { return array_keys( $this->mTagHooks ); }
+       function getTags() { return array_merge( array_keys($this->mTransparentTagHooks), array_keys( $this->mTagHooks ) ); }
        /**#@-*/
 
 
index ffe10f9..e2f0f27 100644 (file)
@@ -339,7 +339,7 @@ class Sanitizer {
         * @param array $args for the processing callback
         * @return string
         */
-       static function removeHTMLtags( $text, $processCallback = null, $args = array() ) {
+       static function removeHTMLtags( $text, $processCallback = null, $args = array(), $extratags = array() ) {
                global $wgUseTidy;
 
                static $htmlpairs, $htmlsingle, $htmlsingleonly, $htmlnest, $tabletags,
@@ -349,13 +349,13 @@ class Sanitizer {
 
                if ( !$staticInitialised ) {
 
-                       $htmlpairs = array( # Tags that must be closed
+                       $htmlpairs = array_merge( $extratags, array( # Tags that must be closed
                                'b', 'del', 'i', 'ins', 'u', 'font', 'big', 'small', 'sub', 'sup', 'h1',
                                'h2', 'h3', 'h4', 'h5', 'h6', 'cite', 'code', 'em', 's',
                                'strike', 'strong', 'tt', 'var', 'div', 'center',
                                'blockquote', 'ol', 'ul', 'dl', 'table', 'caption', 'pre',
                                'ruby', 'rt' , 'rb' , 'rp', 'p', 'span', 'u'
-                       );
+                       ) );
                        $htmlsingle = array(
                                'br', 'hr', 'li', 'dt', 'dd'
                        );