- # Close all the prefixes which aren't shared.
- while ( $commonPrefixLength < $lastPrefixLength ) {
- $output .= $this->closeList( $lastPrefix[$lastPrefixLength-1] );
- --$lastPrefixLength;
- }
-
- # Continue the current prefix if appropriate.
- if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
- $output .= $this->nextItem( $prefix[$commonPrefixLength-1] );
- }
-
- # Open prefixes where appropriate.
- while ( $prefixLength > $commonPrefixLength ) {
- $char = substr( $prefix, $commonPrefixLength, 1 );
- $output .= $this->openList( $char );
-
- if ( ';' === $char ) {
- # FIXME: This is dupe of code above
- if ( $this->findColonNoLinks( $t, $term, $t2 ) !== false ) {
- $t = $t2;
- $output .= $term . $this->nextItem( ':' );
- }
- }
- ++$commonPrefixLength;
- }
- $lastPrefix = $prefix2;
- }
-
- # If we have no prefixes, go to paragraph mode.
- if ( 0 == $prefixLength ) {
- wfProfileIn( __METHOD__."-paragraph" );
- # No prefix (not in list)--go to paragraph mode
- # XXX: use a stack for nestable elements like span, table and div
- $openmatch = preg_match('/(?:<table|<blockquote|<h1|<h2|<h3|<h4|<h5|<h6|<pre|<tr|<p|<ul|<ol|<li|<\\/tr|<\\/td|<\\/th)/iS', $t );
- $closematch = preg_match(
- '/(?:<\\/table|<\\/blockquote|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6|'.
- '<td|<th|<\\/?div|<hr|<\\/pre|<\\/p|'.$this->mUniqPrefix.'-pre|<\\/li|<\\/ul|<\\/ol|<\\/?center)/iS', $t );
- if ( $openmatch or $closematch ) {
- $paragraphStack = false;
- # TODO bug 5718: paragraph closed
- $output .= $this->closeParagraph();
- if ( $preOpenMatch and !$preCloseMatch ) {
- $this->mInPre = true;
- }
- $inBlockElem = !$closematch;
- } elseif ( !$inBlockElem && !$this->mInPre ) {
- if ( ' ' == substr( $t, 0, 1 ) and ( $this->mLastSection === 'pre' || trim( $t ) != '' ) ) {
- # pre
- if ( $this->mLastSection !== 'pre' ) {
- $paragraphStack = false;
- $output .= $this->closeParagraph().'<pre>';
- $this->mLastSection = 'pre';
- }
- $t = substr( $t, 1 );
- } else {
- # paragraph
- if ( trim( $t ) === '' ) {
- if ( $paragraphStack ) {
- $output .= $paragraphStack.'<br />';
- $paragraphStack = false;
- $this->mLastSection = 'p';
- } else {
- if ( $this->mLastSection !== 'p' ) {
- $output .= $this->closeParagraph();
- $this->mLastSection = '';
- $paragraphStack = '<p>';
- } else {
- $paragraphStack = '</p><p>';
- }
- }
- } else {
- if ( $paragraphStack ) {
- $output .= $paragraphStack;
- $paragraphStack = false;
- $this->mLastSection = 'p';
- } elseif ( $this->mLastSection !== 'p' ) {
- $output .= $this->closeParagraph().'<p>';
- $this->mLastSection = 'p';
- }
- }
- }
- }
- wfProfileOut( __METHOD__."-paragraph" );
- }
- # somewhere above we forget to get out of pre block (bug 785)
- if ( $preCloseMatch && $this->mInPre ) {
- $this->mInPre = false;
- }
- if ( $paragraphStack === false ) {
- $output .= $t."\n";
- }
- }
- while ( $prefixLength ) {
- $output .= $this->closeList( $prefix2[$prefixLength-1] );
- --$prefixLength;
- }
- if ( $this->mLastSection != '' ) {
- $output .= '</' . $this->mLastSection . '>';
- $this->mLastSection = '';
- }
-
- wfProfileOut( __METHOD__ );
- return $output;
- }
-
- /**
- * Split up a string on ':', ignoring any occurences inside tags
- * to prevent illegal overlapping.
- *
- * @param $str String: the string to split
- * @param &$before String: set to everything before the ':'
- * @param &$after String: set to everything after the ':'
- * return String: the position of the ':', or false if none found
- */
- function findColonNoLinks( $str, &$before, &$after ) {
- wfProfileIn( __METHOD__ );
-
- $pos = strpos( $str, ':' );
- if ( $pos === false ) {
- # Nothing to find!
- wfProfileOut( __METHOD__ );
- return false;
- }
-
- $lt = strpos( $str, '<' );
- if ( $lt === false || $lt > $pos ) {
- # Easy; no tag nesting to worry about
- $before = substr( $str, 0, $pos );
- $after = substr( $str, $pos+1 );
- wfProfileOut( __METHOD__ );
- return $pos;
- }
-
- # Ugly state machine to walk through avoiding tags.
- $state = self::COLON_STATE_TEXT;
- $stack = 0;
- $len = strlen( $str );
- for( $i = 0; $i < $len; $i++ ) {
- $c = $str{$i};
-
- switch( $state ) {
- # (Using the number is a performance hack for common cases)
- case 0: # self::COLON_STATE_TEXT:
- switch( $c ) {
- case "<":
- # Could be either a <start> tag or an </end> tag
- $state = self::COLON_STATE_TAGSTART;
- break;
- case ":":
- if ( $stack == 0 ) {
- # We found it!
- $before = substr( $str, 0, $i );
- $after = substr( $str, $i + 1 );
- wfProfileOut( __METHOD__ );
- return $i;
- }
- # Embedded in a tag; don't break it.
- break;
- default:
- # Skip ahead looking for something interesting
- $colon = strpos( $str, ':', $i );
- if ( $colon === false ) {
- # Nothing else interesting
- wfProfileOut( __METHOD__ );
- return false;
- }
- $lt = strpos( $str, '<', $i );
- if ( $stack === 0 ) {
- if ( $lt === false || $colon < $lt ) {
- # We found it!
- $before = substr( $str, 0, $colon );
- $after = substr( $str, $colon + 1 );
- wfProfileOut( __METHOD__ );
- return $i;
- }
- }
- if ( $lt === false ) {
- # Nothing else interesting to find; abort!
- # We're nested, but there's no close tags left. Abort!
- break 2;
- }
- # Skip ahead to next tag start
- $i = $lt;
- $state = self::COLON_STATE_TAGSTART;
- }
- break;
- case 1: # self::COLON_STATE_TAG:
- # In a <tag>
- switch( $c ) {
- case ">":
- $stack++;
- $state = self::COLON_STATE_TEXT;
- break;
- case "/":
- # Slash may be followed by >?
- $state = self::COLON_STATE_TAGSLASH;
- break;
- default:
- # ignore
- }
- break;
- case 2: # self::COLON_STATE_TAGSTART:
- switch( $c ) {
- case "/":
- $state = self::COLON_STATE_CLOSETAG;
- break;
- case "!":
- $state = self::COLON_STATE_COMMENT;
- break;
- case ">":
- # Illegal early close? This shouldn't happen D:
- $state = self::COLON_STATE_TEXT;
- break;
- default:
- $state = self::COLON_STATE_TAG;
- }
- break;
- case 3: # self::COLON_STATE_CLOSETAG:
- # In a </tag>
- if ( $c === ">" ) {
- $stack--;
- if ( $stack < 0 ) {
- wfDebug( __METHOD__.": Invalid input; too many close tags\n" );
- wfProfileOut( __METHOD__ );
- return false;
- }
- $state = self::COLON_STATE_TEXT;
- }
- break;
- case self::COLON_STATE_TAGSLASH:
- if ( $c === ">" ) {
- # Yes, a self-closed tag <blah/>
- $state = self::COLON_STATE_TEXT;
- } else {
- # Probably we're jumping the gun, and this is an attribute
- $state = self::COLON_STATE_TAG;
- }
- break;
- case 5: # self::COLON_STATE_COMMENT:
- if ( $c === "-" ) {
- $state = self::COLON_STATE_COMMENTDASH;
- }
- break;
- case self::COLON_STATE_COMMENTDASH:
- if ( $c === "-" ) {
- $state = self::COLON_STATE_COMMENTDASHDASH;
- } else {
- $state = self::COLON_STATE_COMMENT;
- }
- break;
- case self::COLON_STATE_COMMENTDASHDASH:
- if ( $c === ">" ) {
- $state = self::COLON_STATE_TEXT;
- } else {
- $state = self::COLON_STATE_COMMENT;
- }
- break;
- default:
- throw new MWException( "State machine error in " . __METHOD__ );
- }
- }
- if ( $stack > 0 ) {
- wfDebug( __METHOD__.": Invalid input; not enough close tags (stack $stack, state $state)\n" );
- wfProfileOut( __METHOD__ );
- return false;
- }
- wfProfileOut( __METHOD__ );
- return false;