From 8c3822b9b7074ff9d4f7692dd5f5b4fea678c475 Mon Sep 17 00:00:00 2001 From: Jens Frank Date: Sun, 15 Jul 2007 11:14:53 +0000 Subject: [PATCH] New Parser::setTransparentTagHook for parser extension and template compatibility --- RELEASE-NOTES | 2 ++ includes/Parser.php | 42 +++++++++++++++++++++++++++++++++++++++--- includes/Sanitizer.php | 6 +++--- 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/RELEASE-NOTES b/RELEASE-NOTES index c81da17fdc..690432286f 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -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 == diff --git a/includes/Parser.php b/includes/Parser.php index 04cd9ed741..11e9ee2a57 100644 --- a/includes/Parser.php +++ b/includes/Parser.php @@ -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( '' => '', '' => '') ); $text = StringUtils::delimiterReplace( '', '', '', $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 ) ); } /**#@-*/ diff --git a/includes/Sanitizer.php b/includes/Sanitizer.php index ffe10f9c48..e2f0f272a8 100644 --- a/includes/Sanitizer.php +++ b/includes/Sanitizer.php @@ -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' ); -- 2.20.1