00001 <?php
00002
00014 class TKHTML extends TKComponent {
00015 const HTML_PLAIN = 0;
00016 const HTML_TK = 1;
00017
00023 public function __construct($html = "", $type = self::HTML_PLAIN) {
00024 parent::__construct();
00025
00026 $this->addProperty('content', self::PROP_STRING);
00027 $this->set('content', $html);
00028
00029 $this->addProperty('type', self::PROP_NUMERIC);
00030 $this->set('type', $type);
00031
00032 $this->removeProperty('class');
00033 $this->removeProperty('style');
00034 }
00035
00037 protected function renderComponent($class, $style, $events, $id) {
00038 if($this->get('type') == self::HTML_TK)
00039 $collect = self::parseTKHTML($this->get('content'));
00040 else
00041 $collect = $this->get('content');
00042 return $collect;
00043 }
00044
00046 private static function parseTag(&$xmlTree, $start, $ignoreTK = false) {
00047 $tag = $xmlTree[$start]['tag'];
00048 $pureValue = "";
00049 if($ignoreTK == false && substr($tag, 0, 2) == 'TK') {
00050 return self::parseTK($xmlTree, $start);
00051 }
00052 else {
00053 if(substr($tag, 0, 2) == 'TK') {
00054 if(!is_subclass_of($tag, "TKContainer"))
00055 $ignoreTK = false;
00056 }
00057 }
00058 $collect = "<$tag";
00059 $attrs = "";
00060 if(isset($xmlTree[$start]['attributes'])) {
00061 foreach($xmlTree[$start]['attributes'] as $a => $v)
00062 $attrs .= " $a=\"$v\"";
00063 $collect .= "$attrs";
00064 }
00065 if($xmlTree[$start]['type'] == 'complete') {
00066 if(isset($xmlTree[$start]['value']) && $xmlTree[$start]['value'] != '') {
00067 $val = $xmlTree[$start]['value'];
00068 $collect .= ">$val</$tag>";
00069 }
00070 else {
00071 $val = "";
00072 $collect .= " />";
00073 }
00074 $tagTree = array();
00075 $tagTree['tag'] = $tag;
00076 $tagTree['children'] = array();
00077 $tagTree['attrs'] = isset($xmlTree[$start]['attributes']) ? $xmlTree[$start]['attributes'] : array();
00078 $tagTree['value'] = $val;
00079 return array(1, $collect, $tagTree);
00080 }
00081 else
00082 $collect .= ">";
00083 $tagTree = array();
00084 $tagTree['tag'] = $tag;
00085 $tagTree['children'] = array();
00086 $tagTree['attrs'] = isset($xmlTree[$start]['attributes']) ? $xmlTree[$start]['attributes'] : array();
00087 $tagTree['value'] = "";
00088 if(isset($xmlTree[$start]['value'])) {
00089 $collect .= $xmlTree[$start]['value'];
00090 $tagTree['value'] .= $xmlTree[$start]['value'];
00091 }
00092 $level = $xmlTree[$start]['level'];
00093 $i = $start + 1;
00094 while($i < count($xmlTree)) {
00095 $node = $xmlTree[$i];
00096 if($node['type'] == 'cdata') {
00097 $collect .= $node['value'];
00098 $pureValue = $node['value'];
00099 $tagTree['value'] .= $node['value'];
00100 $i++;
00101 }
00102 else if($node['type'] == 'close') {
00103 $val = isset($node['value']) ? $node['value'] : "";
00104 $pureValue .= isset($node['value']) ? $node['value'] : "";
00105 $tagTree['value'] .= isset($node['value']) ? $node['value'] : "";
00106 $collect .= $val . "</$tag>";
00107 return array($i + 1 - $start, $collect, $tagTree);
00108 }
00109 else {
00110 list($cnt, $tmp, $subtree) = self::parseTag($xmlTree, $i, $ignoreTK);
00111 array_push($tagTree['children'], $subtree);
00112 $collect .= $tmp;
00113 $i += $cnt;
00114 }
00115 }
00116 return array($i - $start, $collect, $tagTree);
00117 }
00118
00120 private static function parseTK(&$xmlTree, $start) {
00121 $level = $xmlTree[$start]['level'];
00122 if(isset($xmlTree[$start]['attributes']))
00123 $attrs = $xmlTree[$start]['attributes'];
00124 else
00125 $attrs = array();
00126 $tag = $xmlTree[$start]['tag'];
00127 list($cnt, $value, $tagTree) = self::parseTag($xmlTree, $start, true);
00128 $tkclass = $tag;
00129 $obj = call_user_func(array($tkclass, 'createFromXML'), $attrs, $tagTree['value'], $tagTree);
00130 if($obj == null)
00131 $ret = "";
00132 else
00133 $ret = $obj->render();
00134 return array($cnt, $ret, $tagTree);
00135 }
00136
00138 public static function parseTKHTML($input) {
00139 $input = "<tkml>$input</tkml>";
00140
00141 $parser = xml_parser_create();
00142 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
00143 xml_parse_into_struct($parser, $input, $vals, $index);
00144 xml_parser_free($parser);
00145
00146 list($cnt, $output, $dummy) = self::parseTag($vals, 0);
00147 $output = substr($output, 6, strlen($output) - 13);
00148
00149 return $output;
00150 }
00151 }
00152
00153 ?>