| @@ -1,248 +1,274 @@ | ||
| 1 | 1 | <?php |
| 2 | +namespace MaxButtons; | |
| 3 | +defined('ABSPATH') or die('No direct access permitted'); | |
| 2 | 4 | /* Class to Parse CSS. Load as array with diffent pseudo-types, |
| 3 | - ability to add nested and new root blocks | |
| 5 | + ability to add nested and new root blocks | |
| 4 | 6 | parses via scss |
| 5 | 7 | ability to use complicated css stuff via scss mixins parsing like gradient |
| 6 | - auto-discovery for -unit field types to set units (like px, or %) | |
| 8 | + auto-discovery for -unit field types to set units (like px, or %) | |
| 7 | 9 | auto-discovery of fields via domobj. |
| 8 | - | |
| 10 | + | |
| 9 | 11 | */ |
| 12 | + | |
| 13 | +use \Exception as Exception; | |
| 14 | + | |
| 10 | 15 | class maxCSSParser |
| 11 | 16 | { |
| 12 | - protected $struct = array(); | |
| 17 | + protected $struct = array(); | |
| 13 | 18 | protected $domObj = ''; |
| 14 | - protected $pseudo = array("hover","active","responsive"); | |
| 15 | - | |
| 16 | - protected $data; | |
| 19 | + protected $pseudo = array("hover","active","responsive"); | |
| 20 | + | |
| 21 | + protected $data; | |
| 17 | 22 | protected $output_css; |
| 18 | - | |
| 23 | + | |
| 19 | 24 | protected $inline = array(); |
| 20 | 25 | protected $responsive = array(); |
| 21 | - | |
| 26 | + | |
| 27 | + public $anchor_class = '.maxbutton'; // used for matching buttons in parse_part | |
| 28 | + | |
| 22 | 29 | // settings |
| 23 | 30 | protected $elements_ignore_zero = array( |
| 24 | 31 | 'text-shadow-left', |
| 25 | - 'text_shadow-top', | |
| 32 | + 'text_shadow-top', | |
| 26 | 33 | 'text-shadow-width', |
| 27 | 34 | 'box-shadow-offset-left', |
| 28 | - 'box-shadow-offset-top', | |
| 35 | + 'box-shadow-offset-top', | |
| 29 | 36 | 'box-shadow-width', |
| 30 | - 'box-shadow-spread', | |
| 31 | - ); // items to ignore if value is zero, otherwise they become unremovable ( where 0 is still something on display) | |
| 32 | - | |
| 37 | + 'box-shadow-spread', | |
| 38 | + ); // items to ignore if value is zero, otherwise they become unremovable ( where 0 is still something on display) | |
| 39 | + | |
| 33 | 40 | protected $important = false; |
| 34 | - | |
| 41 | + | |
| 35 | 42 | // log possible problems and incidents for debugging; |
| 36 | 43 | protected $parse_log = array(); |
| 37 | - | |
| 44 | + | |
| 38 | 45 | function __construct() |
| 39 | 46 | { |
| 40 | - //$root[] = array("a" => array("hover","active","responsive")); | |
| 47 | + //$root[] = array("a" => array("hover","active","responsive")); | |
| 41 | 48 | |
| 42 | 49 | } |
| 43 | - | |
| 50 | + | |
| 44 | 51 | function loadDom($domObj) |
| 45 | - { | |
| 52 | + { | |
| 46 | 53 | $this->domObj = $domObj; |
| 47 | 54 | |
| 48 | 55 | $root = $domObj->find(0,0); |
| 49 | - $struct[$root->tag] = array(); | |
| 50 | - | |
| 56 | + $struct[$root->tag] = array(); | |
| 57 | + | |
| 51 | 58 | $children = $root->children(); |
| 52 | - | |
| 53 | - if (count($children) > 0) | |
| 59 | + | |
| 60 | + if (count($children) > 0) | |
| 54 | 61 | $struct[$root->tag] = $this->loadRecursive(array(), $children); |
| 55 | - | |
| 62 | + | |
| 63 | + | |
| 56 | 64 | $this->struct = $struct; |
| 57 | 65 | |
| 58 | 66 | |
| 59 | 67 | } |
| 60 | - | |
| 68 | + | |
| 61 | 69 | function loadRecursive($struct, $children) |
| 62 | 70 | { |
| 63 | 71 | foreach($children as $domChild) |
| 64 | 72 | { |
| 65 | 73 | |
| 66 | - $class = $domChild->class; | |
| 67 | - $class = str_replace(" ",".", $class); // combine seperate classes | |
| 68 | - | |
| 69 | - $struct[$class]["tag"] = $domChild->tag; | |
| 70 | - | |
| 71 | - $child_children = $domChild->children(); | |
| 74 | + $class = $domChild->class; | |
| 75 | + $class = str_replace(" ",".", $class); // combine seperate classes | |
| 76 | + $struct[$class]["tag"] = $domChild->tag; | |
| 72 | 77 | |
| 73 | - if (count($child_children) > 0) | |
| 78 | + $child_children = $domChild->children(); | |
| 79 | + | |
| 80 | + if (count($child_children) > 0) | |
| 74 | 81 | { |
| 75 | - | |
| 82 | + | |
| 76 | 83 | $struct[$class]["children"] = $this->loadRecursive(array(), $child_children); |
| 77 | 84 | } |
| 78 | 85 | } |
| 79 | - | |
| 86 | + | |
| 80 | 87 | return $struct; |
| 81 | 88 | } |
| 82 | - | |
| 83 | - | |
| 89 | + | |
| 90 | + | |
| 84 | 91 | function parse($data) |
| 85 | - { | |
| 86 | - $this->clear(); | |
| 87 | - | |
| 88 | - $struct = $this->struct; | |
| 92 | + { | |
| 93 | + $this->clear(); | |
| 89 | 94 | |
| 90 | - $this->data = $data; | |
| 95 | + $struct = $this->struct; | |
| 91 | 96 | |
| 92 | - | |
| 97 | + $this->data = $data; | |
| 98 | + | |
| 99 | + | |
| 93 | 100 | if (isset($data["settings"])) // room for settings in parser |
| 94 | 101 | { |
| 95 | 102 | $settings = $data["settings"]; |
| 96 | - $this->important = (isset($settings["important"])) ? $settings["important"] : false; | |
| 97 | - | |
| 98 | - unset($this->data["settings"]); | |
| 103 | + $this->important = (isset($settings["important"])) ? $settings["important"] : false; | |
| 104 | + | |
| 105 | + unset($this->data["settings"]); | |
| 99 | 106 | } |
| 100 | 107 | |
| 108 | + $elements = array_shift($struct); // first element is a 'stub' root. | |
| 101 | 109 | |
| 102 | - $elements = array_shift($struct); // first element is a 'stub' root. | |
| 110 | + if ( is_null($elements) ) | |
| 111 | + return; | |
| 103 | 112 | |
| 113 | + | |
| 104 | 114 | foreach($elements as $el => $el_data) |
| 105 | 115 | { |
| 106 | 116 | |
| 107 | - $this->parse_part($el,$el_data); | |
| 117 | + $this->parse_part($el,$el_data); | |
| 108 | 118 | } |
| 109 | 119 | |
| 110 | 120 | |
| 111 | 121 | $this->parse_responsive(); |
| 112 | - | |
| 122 | + | |
| 113 | 123 | maxUtils::startTime('compile CSS'); |
| 114 | 124 | $css = $this->compile($this->output_css); |
| 125 | + | |
| 115 | 126 | maxUtils::endTime('compile CSS'); |
| 116 | 127 | |
| 117 | 128 | |
| 118 | 129 | return $css; |
| 119 | - | |
| 130 | + | |
| 120 | 131 | } |
| 121 | - | |
| 122 | - // reset output values. | |
| 123 | - protected function clear() | |
| 132 | + | |
| 133 | + // reset output values. | |
| 134 | + protected function clear() | |
| 124 | 135 | { |
| 125 | - $this->data = ''; | |
| 126 | - $this->output_css = ''; | |
| 136 | + $this->data = ''; | |
| 137 | + $this->output_css = ''; | |
| 127 | 138 | $this->inline = array(); |
| 128 | 139 | //$this->struct = array(); |
| 129 | 140 | $this->responsive = array(); |
| 130 | - | |
| 141 | + | |
| 131 | 142 | } |
| 132 | - | |
| 143 | + | |
| 133 | 144 | protected function compile($css) |
| 134 | 145 | { |
| 135 | - $scss = new scssc(); | |
| 146 | + $scss = new \Leafo\ScssPhp\Compiler(); | |
| 136 | 147 | $scss->setImportPaths(MB()->get_plugin_path() . "assets/scss"); |
| 137 | - | |
| 138 | - $minify = get_option("maxbuttons_minify", 1); | |
| 148 | + | |
| 149 | + $minify = get_option("maxbuttons_minify", 1); | |
| 139 | 150 | if ($minify == 1) |
| 140 | - $scss->setFormatter('scss_formatter_compressed'); | |
| 141 | - | |
| 151 | + $scss->setFormatter('\Leafo\ScssPhp\Formatter\Compressed'); | |
| 152 | + | |
| 142 | 153 | $compile = " @import '_mixins.scss'; " . $css; |
| 143 | 154 | //maxUtils::addTime("CSSParser: Compile start "); |
| 144 | - | |
| 155 | + | |
| 145 | 156 | try |
| 146 | 157 | { |
| 147 | 158 | $css = $scss->compile($compile); |
| 148 | - } catch (Exception $e) { | |
| 149 | - | |
| 150 | - $css = $this->output_css; | |
| 151 | - } | |
| 159 | + } catch (Exception $e) { | |
| 152 | 160 | |
| 161 | + $css = $this->output_css; | |
| 162 | + } | |
| 163 | + | |
| 153 | 164 | //maxUtils::addTime("CSSParser: Compile end "); |
| 154 | 165 | |
| 155 | 166 | return $css; |
| 156 | 167 | } |
| 157 | - | |
| 168 | + | |
| 158 | 169 | function parse_part($element, $el_data, $el_add = '') |
| 159 | 170 | { |
| 160 | 171 | maxUtils::addTime("CSSParser: Parse $element "); |
| 161 | - | |
| 162 | - $tag = $el_data["tag"]; | |
| 172 | + | |
| 173 | + | |
| 174 | + $tag = $el_data["tag"]; | |
| 163 | 175 | $element_data = $this->findData($this->data, $element); |
| 164 | 176 | |
| 165 | - // not using scss selectors here since there is trouble w/ the :pseudo selector, which should be put on the maxbutton / a tag. | |
| 177 | + // not using scss selectors here since there is trouble w/ the :pseudo selector, which should be put on the maxbutton / a tag. | |
| 166 | 178 | if ($element != '') |
| 167 | 179 | { $el_add .= " ." . $element; |
| 168 | - | |
| 180 | + | |
| 169 | 181 | } |
| 170 | 182 | |
| 171 | - | |
| 172 | - if (isset($element_data["responsive"])) | |
| 183 | + if (isset($element_data["responsive"])) | |
| 173 | 184 | { |
| 174 | 185 | $responsive = $element_data["responsive"]; // doing that at the end |
| 175 | - unset($element_data["responsive"]); | |
| 186 | + unset($element_data["responsive"]); | |
| 176 | 187 | |
| 177 | 188 | $this->responsive[$el_add] = $responsive; |
| 178 | 189 | } |
| 179 | - | |
| 190 | + | |
| 180 | 191 | foreach($element_data as $pseudo => $values) |
| 181 | 192 | { |
| 182 | - | |
| 183 | - if ($pseudo != 'normal') | |
| 193 | + | |
| 194 | + if ($pseudo != 'normal') | |
| 184 | 195 | { |
| 185 | - // select the maxbutton case, ending with either space or next class -dot. | |
| 186 | - $ps_selector = preg_replace('/.maxbutton$|.maxbutton([.| ])/i',".maxbutton:$pseudo\$1",$el_add); | |
| 187 | - | |
| 188 | - $this->output_css .= " $ps_selector { "; | |
| 196 | + // select the maxbutton case, ending with either space or next class -dot. | |
| 197 | + // Anchor class in default situation should be .maxbutton | |
| 198 | + $anchor_class = $this->anchor_class; | |
| 199 | + | |
| 200 | + $count = 0; | |
| 201 | + | |
| 202 | + /* If PS Selector replacement doesn't match anchor class selector this probably means the parse is done in a higher level | |
| 203 | + e.g. container level, so no proper will be set. In case 0 count replacement, just put it on current */ | |
| 204 | + $ps_selector = preg_replace('/' . $anchor_class . '$|' . $anchor_class . '([.| ])/i',"$anchor_class:$pseudo\$1",$el_add, -1, $count); | |
| 205 | + | |
| 206 | + | |
| 207 | + if ($count === 0) | |
| 208 | + { | |
| 209 | + $ps_selector = $el_add . ":" . $pseudo; | |
| 210 | + } | |
| 211 | + | |
| 212 | + | |
| 213 | + $this->output_css .= " $ps_selector { "; | |
| 189 | 214 | } |
| 190 | 215 | else { |
| 191 | 216 | $this->output_css .= " $el_add { "; |
| 192 | 217 | } |
| 193 | - | |
| 218 | + | |
| 194 | 219 | $values = $this->doMixins($values); |
| 195 | 220 | |
| 196 | 221 | foreach($values as $cssTag => $cssVal) |
| 197 | 222 | { |
| 198 | - | |
| 223 | + | |
| 199 | 224 | $statement = $this->parse_cssline($values, $cssTag,$cssVal); ///"$cssTag $css_sep $cssVal$unit$css_end "; |
| 225 | + | |
| 200 | 226 | if ($statement) |
| 201 | 227 | { |
| 202 | 228 | $this->output_css .= $statement ; |
| 203 | - | |
| 229 | + | |
| 204 | 230 | if (! isset($this->inline[$pseudo][$element])) $this->inline[$pseudo][$element] = ''; |
| 205 | 231 | $this->inline[$pseudo][$element] .= $statement; |
| 206 | - } | |
| 232 | + } | |
| 207 | 233 | } |
| 208 | 234 | |
| 209 | - $this->output_css .= " } "; | |
| 235 | + $this->output_css .= " } "; | |
| 210 | 236 | } |
| 211 | - if (isset($el_data["children"])) | |
| 237 | + if (isset($el_data["children"])) | |
| 212 | 238 | { |
| 213 | 239 | foreach($el_data["children"] as $child_id => $child_data) |
| 214 | 240 | { |
| 215 | - | |
| 241 | + | |
| 216 | 242 | $this->parse_part($child_id, $child_data, $el_add); |
| 217 | 243 | } |
| 218 | 244 | } |
| 219 | 245 | |
| 220 | 246 | } |
| 221 | - | |
| 247 | + | |
| 222 | 248 | function parse_cssline($values, $cssTag, $cssVal, $css_end = ';') |
| 223 | 249 | { |
| 224 | 250 | |
| 225 | 251 | // unit check - two ways; either unitable items is first or unit declaration. |
| 226 | - if (isset($values[$cssTag . "_unit"])) | |
| 252 | + if (isset($values[$cssTag . "_unit"])) | |
| 227 | 253 | { |
| 228 | - $unit = $values[$cssTag . "_unit"]; | |
| 254 | + $unit = $values[$cssTag . "_unit"]; | |
| 229 | 255 | } |
| 230 | 256 | elseif(strpos($cssTag, "_unit") !== false) |
| 231 | 257 | { |
| 232 | - return false; // no print, should be found under first def. | |
| 258 | + return false; // no print, should be found under first def. | |
| 233 | 259 | } |
| 234 | 260 | else $unit = ''; |
| 235 | - | |
| 236 | 261 | |
| 237 | - $important = ($this->is_important()) ? " !important" : ""; | |
| 238 | - $important = ($cssTag == '@include') ? "" : $important; // mixin's problem, no checking here. | |
| 239 | - | |
| 262 | + | |
| 263 | + $important = ($this->is_important()) ? " !important" : ""; | |
| 264 | + $important = ($cssTag == '@include') ? "" : $important; // mixin's problem, no checking here. | |
| 265 | + | |
| 240 | 266 | $css_sep = ($cssTag == '@include') ? $css_sep = '' : ':'; |
| 241 | - | |
| 267 | + | |
| 242 | 268 | if ($cssVal == 0 && in_array($cssTag, $this->elements_ignore_zero)) |
| 243 | - return false; | |
| 244 | - | |
| 269 | + return false; | |
| 270 | + | |
| 245 | 271 | if($cssVal !== '' && $cssTag !== '') |
| 246 | 272 | { |
| 247 | 273 | $statement = "$cssTag $css_sep $cssVal$unit$important$css_end "; |
| 248 | 274 | return $statement; |
| @@ -247,119 +273,119 @@ | ||
| 247 | 273 | $statement = "$cssTag $css_sep $cssVal$unit$important$css_end "; |
| 248 | 274 | return $statement; |
| 249 | 275 | } |
| 250 | 276 | return false; |
| 251 | - | |
| 277 | + | |
| 252 | 278 | } |
| 253 | - | |
| 279 | + | |
| 254 | 280 | function parse_responsive() |
| 255 | 281 | { |
| 256 | 282 | |
| 257 | - $responsive = $this->responsive; | |
| 258 | - if (! is_array($responsive) || count($responsive) == 0) | |
| 259 | - return; | |
| 283 | + $responsive = $this->responsive; | |
| 284 | + if (! is_array($responsive) || count($responsive) == 0) | |
| 285 | + return; | |
| 260 | 286 | |
| 261 | 287 | $media_queries = maxUtils::get_media_query(2); // query names |
| 262 | - | |
| 263 | - $output = ''; | |
| 264 | - | |
| 265 | - $query_array = array(); | |
| 266 | 288 | |
| 289 | + $output = ''; | |
| 267 | 290 | |
| 291 | + $query_array = array(); | |
| 292 | + | |
| 293 | + | |
| 268 | 294 | foreach($responsive as $element => $queries) |
| 269 | 295 | { |
| 270 | 296 | foreach($queries as $query => $qdata) |
| 271 | 297 | foreach($qdata as $index => $data) |
| 272 | 298 | {{ |
| 273 | - $query_array[$query][$index][$element] = $data; | |
| 274 | - | |
| 299 | + $query_array[$query][$index][$element] = $data; | |
| 300 | + | |
| 275 | 301 | }} |
| 276 | 302 | } |
| 277 | - | |
| 278 | 303 | |
| 279 | - | |
| 304 | + | |
| 305 | + | |
| 280 | 306 | foreach($query_array as $query => $vdata): |
| 281 | 307 | |
| 282 | - if ($query == 'custom') | |
| 308 | + if ($query == 'custom') | |
| 283 | 309 | { |
| 284 | 310 | |
| 285 | - // first discover the custom size properties. | |
| 311 | + // first discover the custom size properties. | |
| 286 | 312 | foreach($vdata as $index => $data): |
| 287 | 313 | foreach($data as $element => $values): |
| 288 | 314 | |
| 289 | - if (isset($values["custom_maxwidth"]) || isset($values["custom_minwidth"]) ) | |
| 315 | + if (isset($values["custom_maxwidth"]) || isset($values["custom_minwidth"]) ) | |
| 290 | 316 | { |
| 291 | - | |
| 317 | + | |
| 292 | 318 | $minwidth = (isset($values["custom_minwidth"])) ? intval($values["custom_minwidth"]) : -1; |
| 293 | - $maxwidth = (isset($values["custom_maxwidth"])) ? intval($values["custom_maxwidth"]) : -1; | |
| 294 | - | |
| 295 | - | |
| 296 | - unset($vdata[$index][$element]["custom_minwidth"]); | |
| 319 | + $maxwidth = (isset($values["custom_maxwidth"])) ? intval($values["custom_maxwidth"]) : -1; | |
| 320 | + | |
| 321 | + | |
| 322 | + unset($vdata[$index][$element]["custom_minwidth"]); | |
| 297 | 323 | unset($vdata[$index][$element]["custom_maxwidth"]); |
| 298 | - | |
| 299 | - | |
| 300 | - // make it always an integer | |
| 301 | - if ($minwidth == '') $minwidth = 0; | |
| 302 | - if ($maxwidth == '') $maxwidth = 0; | |
| 303 | - | |
| 304 | - // if minwidth is not set and maxwidth zero or not set - ignore since it would result in an empty media line. | |
| 305 | - //if ($minwidth <= 0 && $maxwidth <= 0) | |
| 324 | + | |
| 325 | + | |
| 326 | + // make it always an integer | |
| 327 | + if ($minwidth == '') $minwidth = 0; | |
| 328 | + if ($maxwidth == '') $maxwidth = 0; | |
| 329 | + | |
| 330 | + // if minwidth is not set and maxwidth zero or not set - ignore since it would result in an empty media line. | |
| 331 | + //if ($minwidth <= 0 && $maxwidth <= 0) | |
| 306 | 332 | //{ |
| 307 | 333 | //continue; |
| 308 | 334 | //} |
| 309 | - | |
| 310 | - if ($minwidth > 0 && $maxwidth > 0) | |
| 311 | - $qdef = "only screen and (min-width: $minwidth" . "px) and (max-width: $maxwidth" . "px)"; | |
| 312 | - if ($minwidth >= 0 && $maxwidth <= 0) | |
| 313 | - $qdef = "only screen and (min-width: $minwidth" . "px) "; | |
| 314 | - if ($minwidth <= 0 && $maxwidth > 0) | |
| 315 | - $qdef = "only screen and (max-width: $maxwidth" . "px) "; | |
| 316 | - | |
| 335 | + | |
| 336 | + if ($minwidth > 0 && $maxwidth > 0) | |
| 337 | + $qdef = "only screen and (min-width: $minwidth" . "px) and (max-width: $maxwidth" . "px)"; | |
| 338 | + if ($minwidth >= 0 && $maxwidth <= 0) | |
| 339 | + $qdef = "only screen and (min-width: $minwidth" . "px) "; | |
| 340 | + if ($minwidth <= 0 && $maxwidth > 0) | |
| 341 | + $qdef = "only screen and (max-width: $maxwidth" . "px) "; | |
| 342 | + | |
| 317 | 343 | //break; |
| 318 | 344 | |
| 319 | - } | |
| 320 | - endforeach; //foreach data | |
| 321 | - | |
| 322 | - // The problem is that every 'custom' query needs to run with a different qdef unlike other queries. | |
| 323 | - | |
| 345 | + } | |
| 346 | + endforeach; //foreach data | |
| 347 | + | |
| 348 | + // The problem is that every 'custom' query needs to run with a different qdef unlike other queries. | |
| 349 | + | |
| 324 | 350 | $qdata = array($vdata[$index]); |
| 325 | - | |
| 326 | - $output = $this->parse_responsive_definition($output, $qdef, $qdata); | |
| 327 | - endforeach; // foreach vdata | |
| 351 | + | |
| 352 | + $output = $this->parse_responsive_definition($output, $qdef, $qdata); | |
| 353 | + endforeach; // foreach vdata | |
| 328 | 354 | } |
| 329 | 355 | else |
| 330 | 356 | { |
| 331 | - | |
| 332 | - $qdef = $media_queries[$query]; | |
| 357 | + | |
| 358 | + $qdef = $media_queries[$query]; | |
| 333 | 359 | $output = $this->parse_responsive_definition($output, $qdef, $vdata); |
| 334 | 360 | } |
| 335 | 361 | |
| 336 | 362 | endforeach; |
| 337 | - | |
| 338 | - | |
| 363 | + | |
| 364 | + | |
| 339 | 365 | $this->output_css .= $output; |
| 340 | 366 | } |
| 341 | - | |
| 367 | + | |
| 342 | 368 | protected function parse_responsive_definition($output, $qdef, $vdata) |
| 343 | 369 | { |
| 344 | - | |
| 370 | + | |
| 345 | 371 | if (! isset($qdef) || $qdef == '') { |
| 346 | - | |
| 347 | - return; // no definition. | |
| 372 | + | |
| 373 | + return; // no definition. | |
| 348 | 374 | } |
| 349 | 375 | |
| 350 | - | |
| 351 | - $output .= "@media ". $qdef . " { "; | |
| 352 | 376 | |
| 353 | - | |
| 377 | + $output .= "@media ". $qdef . " { "; | |
| 378 | + | |
| 379 | + | |
| 354 | 380 | foreach($vdata as $index => $data) |
| 355 | 381 | { |
| 356 | - | |
| 382 | + | |
| 357 | 383 | foreach($data as $element => $values) { |
| 358 | 384 | //foreach($vdat as $index => $values): |
| 359 | - $output .= $element . " { "; | |
| 385 | + $output .= $element . " { "; | |
| 360 | 386 | $css_end = ';'; |
| 361 | - | |
| 387 | + | |
| 362 | 388 | // same as parse part, maybe merge in future |
| 363 | 389 | foreach($values as $cssTag => $cssVal) |
| 364 | 390 | { |
| 365 | 391 | // unit check - two ways; either unitable items is first or unit declaration. |
| @@ -365,207 +391,216 @@ | ||
| 365 | 391 | // unit check - two ways; either unitable items is first or unit declaration. |
| 366 | 392 | $statement = $this->parse_cssline($values, $cssTag,$cssVal); |
| 367 | 393 | if($statement) |
| 368 | 394 | $output .= $statement; |
| 369 | - | |
| 395 | + | |
| 370 | 396 | } |
| 371 | 397 | |
| 372 | - $output .= " } "; | |
| 398 | + $output .= " } "; | |
| 373 | 399 | // endforeach; |
| 374 | 400 | } |
| 375 | 401 | |
| 376 | - | |
| 402 | + | |
| 377 | 403 | } |
| 378 | - $output .= " } "; | |
| 379 | - | |
| 380 | - //endforeach; | |
| 381 | - | |
| 404 | + $output .= " } "; | |
| 405 | + | |
| 406 | + //endforeach; | |
| 407 | + | |
| 382 | 408 | return $output; |
| 383 | 409 | } |
| 384 | - | |
| 410 | + | |
| 385 | 411 | private function is_important() |
| 386 | 412 | { |
| 387 | 413 | |
| 388 | 414 | if ($this->important == 1) |
| 389 | 415 | return true; |
| 390 | - else | |
| 416 | + else | |
| 391 | 417 | return false; |
| 392 | 418 | } |
| 393 | - | |
| 419 | + | |
| 394 | 420 | function findData($data, $el) |
| 395 | 421 | { |
| 396 | - $classes = explode(".", $el); | |
| 422 | + $classes = explode(".", $el); | |
| 397 | 423 | |
| 398 | - foreach($data as $part => $values) | |
| 424 | + foreach($data as $part => $values) | |
| 399 | 425 | { |
| 400 | 426 | if (in_array($part, $classes)) |
| 427 | + { | |
| 401 | 428 | return $data[$part]; |
| 429 | + } | |
| 402 | 430 | } |
| 403 | 431 | return array(); |
| 404 | 432 | } |
| 405 | - | |
| 433 | + | |
| 406 | 434 | function doMixins($values) |
| 407 | 435 | { |
| 408 | 436 | $mixins = array("gradient", "box-shadow", "text-shadow"); |
| 409 | - | |
| 410 | - foreach($mixins as $mixin) | |
| 437 | + | |
| 438 | + foreach($mixins as $mixin) | |
| 411 | 439 | { |
| 412 | 440 | |
| 413 | 441 | $results = preg_grep("/^$mixin/i",array_keys($values) ); |
| 442 | + if (count($results) === 0) | |
| 443 | + continue; // no mixins. | |
| 444 | + | |
| 414 | 445 | $mixin_array = array(); |
| 415 | 446 | foreach($results as $result) |
| 416 | 447 | { |
| 417 | - $mixin_array[$result] = $values[$result]; | |
| 448 | + $mixin_array[$result] = $values[$result]; | |
| 418 | 449 | } |
| 419 | - | |
| 450 | + | |
| 420 | 451 | if (count($mixin_array) > 0) |
| 421 | 452 | { |
| 422 | 453 | switch($mixin) |
| 423 | 454 | { |
| 424 | - case "gradient": | |
| 455 | + case "gradient": | |
| 425 | 456 | $values = $this->mixin_gradient($mixin_array, $values); |
| 426 | - | |
| 457 | + | |
| 427 | 458 | break; |
| 428 | - case "box-shadow": | |
| 459 | + case "box-shadow": | |
| 429 | 460 | $values = $this->mixin_boxshadow($mixin_array, $values); |
| 430 | - break; | |
| 431 | - case "text-shadow": | |
| 461 | + break; | |
| 462 | + case "text-shadow": | |
| 432 | 463 | $values = $this->mixin_textshadow($mixin_array, $values); |
| 433 | 464 | break; |
| 434 | 465 | } |
| 435 | 466 | } |
| 436 | 467 | |
| 437 | - | |
| 438 | - } | |
| 468 | + | |
| 469 | + } | |
| 439 | 470 | return $values; |
| 440 | 471 | } |
| 441 | - | |
| 472 | + | |
| 442 | 473 | function mixin_gradient($results, $values) |
| 443 | 474 | { |
| 444 | 475 | |
| 445 | 476 | $start = isset($results["gradient-start-color"]) ? $results["gradient-start-color"] : ''; |
| 446 | - $end = isset( $results["gradient-end-color"] ) ? $results["gradient-end-color"] : ''; | |
| 447 | - $start_opacity = isset( $results["gradient-start-opacity"] ) ? $results["gradient-start-opacity"] : ''; | |
| 448 | - $end_opacity = isset( $results["gradient-end-opacity"] ) ? $results["gradient-end-opacity"] : ''; | |
| 449 | - $stop = (isset( $results["gradient-stop"]) && $results["gradient-stop"] != '') ? $results["gradient-stop"] . "%" : '45%'; | |
| 477 | + $end = isset( $results["gradient-end-color"] ) ? $results["gradient-end-color"] : ''; | |
| 478 | + $start_opacity = isset( $results["gradient-start-opacity"] ) ? $results["gradient-start-opacity"] : ''; | |
| 479 | + $end_opacity = isset( $results["gradient-end-opacity"] ) ? $results["gradient-end-opacity"] : ''; | |
| 480 | + $stop = (isset( $results["gradient-stop"]) && $results["gradient-stop"] != '') ? $results["gradient-stop"] . "%" : '45%'; | |
| 450 | 481 | // default to use ( old situation ) |
| 451 | 482 | $use_gradient = (isset($results['gradient-use-gradient']) && $results['gradient-use-gradient'] != '') ? $results['gradient-use-gradient'] : 1; |
| 452 | - | |
| 483 | + | |
| 453 | 484 | $start = maxUtils::hex2rgba($start, $start_opacity); |
| 454 | - $end = maxUtils::hex2rgba($end, $end_opacity); | |
| 455 | - | |
| 485 | + $end = maxUtils::hex2rgba($end, $end_opacity); | |
| 486 | + | |
| 456 | 487 | $important = ($this->is_important()) ? "!important" : ""; |
| 457 | - //$values = $this->add_include($values, "linear-gradient($start,$end,$stop,$important)"); | |
| 488 | + //$values = $this->add_include($values, "linear-gradient($start,$end,$stop,$important)"); | |
| 458 | 489 | |
| 459 | - if ($use_gradient == 1) | |
| 490 | + if ($use_gradient == 1) | |
| 460 | 491 | { |
| 461 | - $values = $this->add_include($values, "linear-gradient($start,$end,$stop,$important)"); | |
| 492 | + $values = $this->add_include($values, "linear-gradient($start,$end,$stop,$important)"); | |
| 462 | 493 | } |
| 463 | 494 | else { |
| 464 | - $values['background-color'] = $start; | |
| 495 | + $values['background-color'] = $start; | |
| 465 | 496 | } |
| 466 | - // remove the non-css keys from the value array ( field names ) | |
| 497 | + | |
| 498 | + // remove the non-css keys from the value array ( field names ) | |
| 467 | 499 | $values = array_diff_key($values, $results); |
| 468 | - | |
| 500 | + | |
| 469 | 501 | return $values; |
| 470 | - | |
| 502 | + | |
| 471 | 503 | } |
| 472 | - | |
| 504 | + | |
| 473 | 505 | function mixin_boxshadow($results, $values) |
| 474 | 506 | { |
| 475 | - $width = $results["box-shadow-width"]; | |
| 476 | - $left = $results["box-shadow-offset-left"]; | |
| 477 | - $top = $results["box-shadow-offset-top"]; | |
| 478 | - $spread = isset($results['box-shadow-spread']) ? $results['box-shadow-spread'] : 0; | |
| 479 | - $color = isset($results["box-shadow-color"]) ? $results["box-shadow-color"] : ''; | |
| 480 | - | |
| 481 | - $important = ($this->is_important()) ? "!important" : ""; | |
| 482 | - | |
| 483 | - if ($width == 0 && $left == 0 && $top == 0) | |
| 484 | - return $values; | |
| 485 | - | |
| 486 | - $values = $this->add_include($values, "box-shadow($left, $top, $width, $color,$spread, false, $important) "); | |
| 507 | + $width = isset($results["box-shadow-width"]) ? $results["box-shadow-width"] : 0; | |
| 508 | + $left = isset($results["box-shadow-offset-left"]) ? $results["box-shadow-offset-left"] : 0; | |
| 509 | + $top = isset($results["box-shadow-offset-top"]) ? $results["box-shadow-offset-top"] : 0; | |
| 510 | + $spread = isset($results['box-shadow-spread']) ? $results['box-shadow-spread'] : 0; | |
| 511 | + $color = isset($results["box-shadow-color"]) ? $results["box-shadow-color"] : ''; | |
| 512 | + | |
| 513 | + $important = ($this->is_important()) ? "!important" : ""; | |
| 514 | + | |
| 515 | + if ($width == 0 && $left == 0 && $top == 0) | |
| 516 | + return $values; | |
| 517 | + | |
| 518 | + $values = $this->add_include($values, "box-shadow($left, $top, $width, $color,$spread, false, $important) "); | |
| 487 | 519 | $values = array_diff_key($values, $results); |
| 488 | 520 | |
| 489 | - return $values; | |
| 521 | + return $values; | |
| 490 | 522 | } |
| 491 | - | |
| 523 | + | |
| 492 | 524 | function mixin_textshadow($results, $values) |
| 493 | 525 | { |
| 494 | - $width = isset($results["text-shadow-width"]) ? $results["text-shadow-width"] : 0; | |
| 495 | - $left = isset($results["text-shadow-left"]) ? $results["text-shadow-left"] : 0; | |
| 496 | - $top = isset($results["text-shadow-top"]) ? $results["text-shadow-top"] : 0; | |
| 497 | - $color = isset($results["text-shadow-color"]) ? $results["text-shadow-color"] : ''; | |
| 498 | - $important = ($this->is_important()) ? "!important" : ""; | |
| 499 | - | |
| 500 | - if ($width == 0 && $left == 0 && $top == 0) | |
| 501 | - return $values; | |
| 502 | - | |
| 503 | - $values = $this->add_include($values, "text-shadow ($left,$top,$width,$color $important)"); | |
| 504 | - | |
| 526 | + $width = isset($results["text-shadow-width"]) ? $results["text-shadow-width"] : 0; | |
| 527 | + $left = isset($results["text-shadow-left"]) ? $results["text-shadow-left"] : 0; | |
| 528 | + $top = isset($results["text-shadow-top"]) ? $results["text-shadow-top"] : 0; | |
| 529 | + $color = isset($results["text-shadow-color"]) ? $results["text-shadow-color"] : ''; | |
| 530 | + $important = ($this->is_important()) ? "!important" : ""; | |
| 531 | + | |
| 532 | + if ($width == 0 && $left == 0 && $top == 0) | |
| 533 | + { | |
| 534 | + $values = array_diff_key($values, $results); // remove them from the values, prevent incorrect output. | |
| 535 | + return $values; | |
| 536 | + } | |
| 537 | + | |
| 538 | + $values = $this->add_include($values, "text-shadow ($left,$top,$width,$color $important)"); | |
| 539 | + | |
| 505 | 540 | $values = array_diff_key($values, $results); |
| 506 | - | |
| 541 | + | |
| 507 | 542 | return $values; |
| 508 | 543 | } |
| 509 | - | |
| 544 | + | |
| 510 | 545 | private function add_include($values, $include) |
| 511 | 546 | { |
| 512 | - if (isset($values["@include"])) | |
| 513 | - $values["@include"] .= "; @include " . $include; | |
| 547 | + if (isset($values["@include"])) | |
| 548 | + $values["@include"] .= "; @include " . $include; | |
| 514 | 549 | else |
| 515 | - $values["@include"] = $include; | |
| 516 | - return $values; | |
| 550 | + $values["@include"] = $include; | |
| 551 | + return $values; | |
| 517 | 552 | } |
| 518 | - | |
| 553 | + | |
| 519 | 554 | function outputInline($domObj, $pseudo = 'normal') |
| 520 | 555 | { |
| 521 | 556 | $domObj = $domObj->load($domObj->save()); |
| 522 | - | |
| 557 | + | |
| 523 | 558 | $inline = $this->inline; |
| 524 | - | |
| 525 | - // ISSUE #43 Sometimes this breaks | |
| 526 | - if (! isset($inline[$pseudo])) | |
| 559 | + | |
| 560 | + // ISSUE #43 Sometimes this breaks | |
| 561 | + if (! isset($inline[$pseudo])) | |
| 527 | 562 | return $domObj; |
| 528 | 563 | |
| 529 | 564 | $elements = array_keys($inline[$pseudo]); |
| 530 | 565 | if ($pseudo != 'normal') // gather all elements |
| 531 | 566 | $elements = array_merge($elements, array_keys($inline["normal"])); |
| 532 | - | |
| 567 | + | |
| 533 | 568 | foreach($elements as $element ) |
| 534 | 569 | { |
| 535 | - $styles = isset($inline[$pseudo][$element]) ? $inline[$pseudo][$element] : ''; | |
| 536 | - | |
| 570 | + $styles = isset($inline[$pseudo][$element]) ? $inline[$pseudo][$element] : ''; | |
| 571 | + | |
| 537 | 572 | if ($pseudo != 'normal') // parse all possible missing styles from pseudo el. |
| 538 | - $normstyle = $this->compile($inline['normal'][$element]); | |
| 539 | - | |
| 540 | - $normstyle = ''; | |
| 573 | + $normstyle = $this->compile($inline['normal'][$element]); | |
| 574 | + | |
| 575 | + $normstyle = ''; | |
| 541 | 576 | if ($pseudo != 'normal') // parse all possible missing styles from pseudo el. |
| 542 | - $normstyle = $this->compile($inline['normal'][$element]); | |
| 543 | - | |
| 577 | + $normstyle = $this->compile($inline['normal'][$element]); | |
| 578 | + | |
| 544 | 579 | maxUtils::addTime("CSSParser: Parse inline done"); |
| 545 | - | |
| 580 | + | |
| 546 | 581 | $styles = $normstyle . $this->compile($styles); |
| 547 | - | |
| 548 | - $element = trim(str_replace("."," ", $element)); // molten css class, seperator. | |
| 549 | - | |
| 582 | + | |
| 583 | + $element = trim(str_replace("."," ", $element)); // molten css class, seperator. | |
| 584 | + | |
| 550 | 585 | $el = $domObj->find('[class*="' . $element . '"]', 0); |
| 551 | - if (is_null($el)) echo "NULL"; | |
| 586 | + | |
| 552 | 587 | $el->style = $styles; |
| 553 | - | |
| 588 | + | |
| 554 | 589 | } |
| 555 | - | |
| 590 | + | |
| 556 | 591 | return $domObj; |
| 557 | - | |
| 558 | - } | |
| 559 | - | |
| 560 | - function output() | |
| 592 | + | |
| 593 | + } | |
| 594 | + | |
| 595 | + function output() | |
| 561 | 596 | { |
| 562 | - | |
| 563 | - | |
| 597 | + | |
| 598 | + | |
| 564 | 599 | } |
| 565 | - | |
| 600 | + | |
| 566 | 601 | } |
| 567 | 602 | |
| 568 | -class compileException extends Exception { | |
| 603 | +class compileException extends Exception { | |
| 569 | 604 | protected $code = -1; |
| 570 | 605 | |
| 571 | 606 | } |