Fixes for IE7 (alignment of checkboxes due to double-margin bug on floated elements...
[mediawiki.git] / includes / parser / StripState.php
blob0bf9e17acee515943c9a3eb205f0119bbf0deadc
1 <?php
3 /**
4 * @todo document, briefly.
5 * @ingroup Parser
6 */
7 class StripState {
8 protected $prefix;
9 protected $data;
10 protected $regex;
12 protected $tempType, $tempMergePrefix;
14 function __construct( $prefix ) {
15 $this->prefix = $prefix;
16 $this->data = array(
17 'nowiki' => array(),
18 'general' => array()
20 $this->regex = "/{$this->prefix}([^\x7f]+)" . Parser::MARKER_SUFFIX . '/';
23 /**
24 * Add a nowiki strip item
26 function addNoWiki( $marker, $value ) {
27 $this->addItem( 'nowiki', $marker, $value );
30 function addGeneral( $marker, $value ) {
31 $this->addItem( 'general', $marker, $value );
34 protected function addItem( $type, $marker, $value ) {
35 if ( !preg_match( $this->regex, $marker, $m ) ) {
36 throw new MWException( "Invalid marker: $marker" );
39 $this->data[$type][$m[1]] = $value;
42 function unstripGeneral( $text ) {
43 return $this->unstripType( 'general', $text );
46 function unstripNoWiki( $text ) {
47 return $this->unstripType( 'nowiki', $text );
50 function unstripBoth( $text ) {
51 $text = $this->unstripType( 'general', $text );
52 $text = $this->unstripType( 'nowiki', $text );
53 return $text;
56 protected function unstripType( $type, $text ) {
57 // Shortcut
58 if ( !count( $this->data[$type] ) ) {
59 return $text;
62 wfProfileIn( __METHOD__ );
63 $this->tempType = $type;
64 $out = preg_replace_callback( $this->regex, array( $this, 'unstripCallback' ), $text );
65 $this->tempType = null;
66 wfProfileOut( __METHOD__ );
67 return $out;
70 protected function unstripCallback( $m ) {
71 if ( isset( $this->data[$this->tempType][$m[1]] ) ) {
72 return $this->data[$this->tempType][$m[1]];
73 } else {
74 return $m[0];
78 /**
79 * Get a StripState object which is sufficient to unstrip the given text.
80 * It will contain the minimum subset of strip items necessary.
82 function getSubState( $text ) {
83 $subState = new StripState( $this->prefix );
84 $pos = 0;
85 while ( true ) {
86 $startPos = strpos( $text, $this->prefix, $pos );
87 $endPos = strpos( $text, Parser::MARKER_SUFFIX, $pos );
88 if ( $startPos === false || $endPos === false ) {
89 break;
92 $endPos += strlen( Parser::MARKER_SUFFIX );
93 $marker = substr( $text, $startPos, $endPos - $startPos );
94 if ( !preg_match( $this->regex, $marker, $m ) ) {
95 continue;
98 $key = $m[1];
99 if ( isset( $this->data['nowiki'][$key] ) ) {
100 $subState->data['nowiki'][$key] = $this->data['nowiki'][$key];
101 } elseif ( isset( $this->data['general'][$key] ) ) {
102 $subState->data['general'][$key] = $this->data['general'][$key];
104 $pos = $endPos;
106 return $subState;
110 * Merge another StripState object into this one. The strip marker keys
111 * will not be preserved. The strings in the $texts array will have their
112 * strip markers rewritten, the resulting array of strings will be returned.
114 * @param $otherState StripState
115 * @param $texts Array
116 * @return Array
118 function merge( $otherState, $texts ) {
119 $mergePrefix = Parser::getRandomString();
121 foreach ( $otherState->data as $type => $items ) {
122 foreach ( $items as $key => $value ) {
123 $this->data[$type]["$mergePrefix-$key"] = $value;
127 $this->tempMergePrefix = $mergePrefix;
128 $texts = preg_replace_callback( $otherState->regex, array( $this, 'mergeCallback' ), $texts );
129 $this->tempMergePrefix = null;
130 return $texts;
133 protected function mergeCallback( $m ) {
134 $key = $m[1];
135 return "{$this->prefix}{$this->tempMergePrefix}-$key" . Parser::MARKER_SUFFIX;