Parser =& $parser; $this->TagData = $tag_data; if ($tag_data != '') $this->ParseTagData($tag_data); $this->NP =& $this->NamedParams; } function CopyFrom(&$tag) { $this->Processor = $tag->Processor; $this->Tag = $tag->Tag; $this->TagData = $tag->TagData; $this->Params = $tag->Params; $this->NamedParams = $tag->NamedParams; $this->Parser =& $tag->Parser; } function GetFullTag() { return '<%'.$this->TagData.'%>'; } function RebuildTagData() { $res = $this->Processor.':'.$this->Tag.' '; foreach ($this->NamedParams as $name => $value) { $res .= "$name='$value' "; } return $res; } function ReplaceParams($tag_data) { //print_pre($this->Parser->Pattern, $tag_data); if (is_array($this->Parser->Params)) { $tag_data = preg_replace($this->Parser->Pattern, $this->Parser->Values, $tag_data); } //echo "got: $tag_data
"; return $tag_data; } function CmpParams($a, $b) { $a_len = strlen($a); $b_len = strlen($b); if ($a_len == $b_len) return 0; return $a_len > $b_len ? -1 : 1; } function ReplaceLanguageTags($tag_data) { preg_match_all("(!(la|lu)[^!]+!)", $tag_data, $res, PREG_PATTERN_ORDER); $language_tags = $res[0]; uasort($language_tags, array ("Tag", "CmpParams")); $values = Array(); $i = 0; foreach ($language_tags as $label) { array_push($values, $this->Application->Phrase($label)); $language_tags[$i] = '/' . $language_tags[$i] . '/'; $i++; } $tag_data = preg_replace($language_tags, $values, $tag_data); return $tag_data; } function ParseTagData($tag_data) { $tag_data = $this->ReplaceParams($tag_data) . ' '; $tag_data = $this->ReplaceLanguageTags($tag_data); list ($key_data, $params) = explode(' ', $tag_data, 2); list($this->Processor, $this->Tag) = explode(':', $key_data); if ($params != '') $this->ParseNamedParams($params); } function ParseNamedParams($params_str) { $params =& new Params($params_str); $this->NamedParams = $params->_Params; } function GetParam($param) { if (isset($this->NP[$param])) return $this->NP[$param]; else return false; } function Process() { if ($this->Processor == 'm' || $this->Processor == 'm_TagProcessor') { //if we are procssing Main tags if ($this->Tag == 'block') { $tag =& new BlockTag('', $this->Parser); $tag->CopyFrom($this); $tag->Process(); } elseif ($this->Parser->SkipMode == skip_tags) { return; } elseif ( $this->Tag == 'if' || $this->Tag == 'ifnot' || $this->Tag == 'else' || $this->Tag == 'elseif' ) { $tag =& new ConstructTag('', $this->Parser); $tag->CopyFrom($this); $tag->Process(); } elseif ($this->Tag == 'xml') { $tag =& new XMLTag('', $this->Parser); $tag->CopyFrom($this); $tag->Process(); } else { if ($this->Parser->SkipMode == skip) return; //if (!$this->ProcessMainTag()) //other main tags $this->ProcessTag(); } } else { //normal tags - processors other than main if ($this->Parser->SkipMode == skip_tags || $this->Parser->SkipMode == skip) return; //do not parse if we skipping tags $this->ProcessTag(); //$this->Parser->AppendOutput(''.$this->Tag.''); } } function DoProcessTag() { //$processor =& $this->Application->Processors->GetProcessor($this->Processor, $this); $processor =& $this->Application->recallObject($this->Processor); return $processor->ProcessTag($this); } function ProcessTag() { $o = $this->DoProcessTag(); if ($o !== false) { $this->Parser->AppendOutput($o); } else { echo "Warning: can't process tag ".$this->Tag."
"; } } } // ################### TESTS ############################ global $suite; if (isset($suite)) { class TestTags extends TestCase { function testTagParsing() { global $application; $tag_data = "m:test param1=\"123\""; $tag =& new Tag($tag_data, $application->Parser); $this->assertEquals('m', $tag->Processor); $this->assertEquals('test', $tag->Tag); $this->assertEquals('123', $tag->GetParam('param1')); $this->assertFalse($tag->GetParam('no_such_param')); } function testTagParamEscaping() { global $application; $tag_data = "m:test escape1='-\"-' \t\t \n \t\n escape2=\"+\\\"+\" \n escape3='*\'*' escape4='=\='"; //$tag_data = "m:test escape1='-\"-' escape2=\"+\\\"+\" escape3='*\'*' escape4='=\='"; $tag =& new Tag($tag_data, $application->Parser); $this->assertEquals('-"-', $tag->GetParam('escape1')); $this->assertEquals('+"+', $tag->GetParam('escape2')); $this->assertEquals("*'*", $tag->GetParam('escape3')); $this->assertEquals('=\=', $tag->GetParam('escape4')); } function testTagParamSubstitution() { global $application; $application->Parser->SetParams( Array( 'prefix' => 'a_prefix', 'tag' => 'a_tag', 'param_name' => 'a_param_name', 'param_value' => 'a_param_value' ) ); $tag_data = '$prefix:$tag $param_name="$param_value"'; $tag =& new Tag($tag_data, $application->Parser); $this->assertEquals('a_prefix', $tag->Processor); $this->assertEquals('a_tag', $tag->Tag); $this->assertEquals('a_param_value', $tag->GetParam('a_param_name')); } } $suite->addTest(new TestSuite("TestTags")); } ?>