[ Index ] |
|
Code source de CMS made simple 1.0.5 |
1 <?php /** 2 * Class to parse css information. 3 * 4 * See the readme file : http://www.phpclasses.org/browse/file/4685.html 5 * 6 * $Id: cssparser.inc.php,v 1.2 2005/03/30 23:42:15 yalnifj dead $ 7 * 8 * @author http://www.phpclasses.org/browse/package/1289.html 9 * @package PhpGedView 10 * @subpackage Charts 11 */ 12 class cssparser { 13 var $css; 14 var $html; 15 16 function cssparser($html = true) { 17 // Register "destructor" 18 register_shutdown_function(array(&$this, "finalize")); 19 $this->html = ($html != false); 20 $this->Clear(); 21 } 22 23 function finalize() { 24 unset($this->css); 25 } 26 27 function Clear() { 28 unset($this->css); 29 $this->css = array(); 30 if($this->html) { 31 $this->Add("ADDRESS", ""); 32 $this->Add("APPLET", ""); 33 $this->Add("AREA", ""); 34 $this->Add("A", "text-decoration : underline; color : Blue;"); 35 $this->Add("A:visited", "color : Purple;"); 36 $this->Add("BASE", ""); 37 $this->Add("BASEFONT", ""); 38 $this->Add("BIG", ""); 39 $this->Add("BLOCKQUOTE", ""); 40 $this->Add("BODY", ""); 41 $this->Add("BR", ""); 42 $this->Add("B", "font-weight: bold;"); 43 $this->Add("CAPTION", ""); 44 $this->Add("CENTER", ""); 45 $this->Add("CITE", ""); 46 $this->Add("CODE", ""); 47 $this->Add("DD", ""); 48 $this->Add("DFN", ""); 49 $this->Add("DIR", ""); 50 $this->Add("DIV", ""); 51 $this->Add("DL", ""); 52 $this->Add("DT", ""); 53 $this->Add("EM", ""); 54 $this->Add("FONT", ""); 55 $this->Add("FORM", ""); 56 $this->Add("H1", ""); 57 $this->Add("H2", ""); 58 $this->Add("H3", ""); 59 $this->Add("H4", ""); 60 $this->Add("H5", ""); 61 $this->Add("H6", ""); 62 $this->Add("HEAD", ""); 63 $this->Add("HR", ""); 64 $this->Add("HTML", ""); 65 $this->Add("IMG", ""); 66 $this->Add("INPUT", ""); 67 $this->Add("ISINDEX", ""); 68 $this->Add("I", "font-style: italic;"); 69 $this->Add("KBD", ""); 70 $this->Add("LINK", ""); 71 $this->Add("LI", ""); 72 $this->Add("MAP", ""); 73 $this->Add("MENU", ""); 74 $this->Add("META", ""); 75 $this->Add("OL", ""); 76 $this->Add("OPTION", ""); 77 $this->Add("PARAM", ""); 78 $this->Add("PRE", ""); 79 $this->Add("P", ""); 80 $this->Add("SAMP", ""); 81 $this->Add("SCRIPT", ""); 82 $this->Add("SELECT", ""); 83 $this->Add("SMALL", ""); 84 $this->Add("STRIKE", ""); 85 $this->Add("STRONG", ""); 86 $this->Add("STYLE", ""); 87 $this->Add("SUB", ""); 88 $this->Add("SUP", ""); 89 $this->Add("TABLE", ""); 90 $this->Add("TD", ""); 91 $this->Add("TEXTAREA", ""); 92 $this->Add("TH", ""); 93 $this->Add("TITLE", ""); 94 $this->Add("TR", ""); 95 $this->Add("TT", ""); 96 $this->Add("UL", ""); 97 $this->Add("U", "text-decoration : underline;"); 98 $this->Add("VAR", ""); 99 } 100 } 101 102 function SetHTML($html) { 103 $this->html = ($html != false); 104 } 105 106 function Add($key, $codestr) { 107 $key = strtolower($key); 108 $codestr = strtolower($codestr); 109 if(!isset($this->css[$key])) { 110 $this->css[$key] = array(); 111 } 112 $codes = explode(";",$codestr); 113 if(count($codes) > 0) { 114 foreach($codes as $code) { 115 $code = trim($code); 116 if (strlen($code)>0) { 117 $splode = explode(":",$code,2); 118 if (count($splode) == 2) 119 { 120 list($codekey, $codevalue) = $splode; 121 if(strlen($codekey) > 0) { 122 $this->css[$key][trim($codekey)] = trim($codevalue); 123 } 124 } 125 } 126 } 127 } 128 } 129 130 function Get($key, $property) { 131 $key = strtolower($key); 132 $property = strtolower($property); 133 134 list($tag, $subtag) = explode(":",$key); 135 list($tag, $class) = explode(".",$tag); 136 list($tag, $id) = explode("#",$tag); 137 $result = ""; 138 foreach($this->css as $_tag => $value) { 139 list($_tag, $_subtag) = explode(":",$_tag); 140 list($_tag, $_class) = explode(".",$_tag); 141 list($_tag, $_id) = explode("#",$_tag); 142 143 $tagmatch = (strcmp($tag, $_tag) == 0) | (strlen($_tag) == 0); 144 $subtagmatch = (strcmp($subtag, $_subtag) == 0) | (strlen($_subtag) == 0); 145 $classmatch = (strcmp($class, $_class) == 0) | (strlen($_class) == 0); 146 $idmatch = (strcmp($id, $_id) == 0); 147 148 if($tagmatch & $subtagmatch & $classmatch & $idmatch) { 149 $temp = $_tag; 150 if((strlen($temp) > 0) & (strlen($_class) > 0)) { 151 $temp .= ".".$_class; 152 } elseif(strlen($temp) == 0) { 153 $temp = ".".$_class; 154 } 155 if((strlen($temp) > 0) & (strlen($_subtag) > 0)) { 156 $temp .= ":".$_subtag; 157 } elseif(strlen($temp) == 0) { 158 $temp = ":".$_subtag; 159 } 160 if(isset($this->css[$temp][$property])) { 161 $result = $this->css[$temp][$property]; 162 } 163 } 164 } 165 return $result; 166 } 167 168 function GetSection($key) { 169 $key = strtolower($key); 170 171 list($tag, $subtag) = explode(":",$key); 172 list($tag, $class) = explode(".",$tag); 173 list($tag, $id) = explode("#",$tag); 174 $result = array(); 175 foreach($this->css as $_tag => $value) { 176 list($_tag, $_subtag) = explode(":",$_tag); 177 list($_tag, $_class) = explode(".",$_tag); 178 list($_tag, $_id) = explode("#",$_tag); 179 180 $tagmatch = (strcmp($tag, $_tag) == 0) | (strlen($_tag) == 0); 181 $subtagmatch = (strcmp($subtag, $_subtag) == 0) | (strlen($_subtag) == 0); 182 $classmatch = (strcmp($class, $_class) == 0) | (strlen($_class) == 0); 183 $idmatch = (strcmp($id, $_id) == 0); 184 185 if($tagmatch & $subtagmatch & $classmatch & $idmatch) { 186 $temp = $_tag; 187 if((strlen($temp) > 0) & (strlen($_class) > 0)) { 188 $temp .= ".".$_class; 189 } elseif(strlen($temp) == 0) { 190 $temp = ".".$_class; 191 } 192 if((strlen($temp) > 0) & (strlen($_subtag) > 0)) { 193 $temp .= ":".$_subtag; 194 } elseif(strlen($temp) == 0) { 195 $temp = ":".$_subtag; 196 } 197 foreach($this->css[$temp] as $property => $value) { 198 $result[$property] = $value; 199 } 200 } 201 } 202 return $result; 203 } 204 205 function ParseStr($str) { 206 $this->Clear(); 207 // Remove comments 208 $str = preg_replace("/\/\*(.*)?\*\//Usi", "", $str); 209 // Parse this damn csscode 210 $parts = explode("}",$str); 211 if(count($parts) > 0) { 212 foreach($parts as $part) { 213 $part=trim($part); 214 if (strlen($part)>0) { 215 $splode = explode("{",$part, 2); 216 if (count($splode) == 2) 217 { 218 list($keystr,$codestr) = $splode; 219 $keys = explode(",",trim($keystr)); 220 if(count($keys) > 0) { 221 foreach($keys as $key) { 222 if(strlen($key) > 0) { 223 $key = str_replace("\n", "", $key); 224 $key = str_replace("\\", "", $key); 225 $this->Add($key, trim($codestr)); 226 } 227 } 228 } 229 } 230 } 231 } 232 } 233 // 234 return (count($this->css) > 0); 235 } 236 237 function Parse($filename) { 238 $this->Clear(); 239 if(file_exists($filename)) { 240 return $this->ParseStr(file_get_contents($filename)); 241 } else { 242 return false; 243 } 244 } 245 246 function GetCSS() { 247 $result = ""; 248 foreach($this->css as $key => $values) { 249 if (count($values) == 0) 250 continue; 251 $result .= $key." {\n"; 252 foreach($values as $key => $value) { 253 $result .= " $key: $value;\n"; 254 } 255 $result .= "}\n\n"; 256 } 257 return $result; 258 } 259 } 260 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Tue Apr 3 18:50:37 2007 | par Balluche grâce à PHPXref 0.7 |