示例#1
0
 /**
  * Parses inbound CSS, storing it as an internal representation of keys and code
  * @param string $str A valid CSS string
  * @return int the number of CSS keys stored
  */
 public function parseString($str, $clear = false)
 {
     if ($clear) {
         $this->clear();
     }
     $keys = '';
     $Instr = $str;
     // Remove comments
     $str = preg_replace("/\\/\\*(.*)?\\*\\//Usi", "", $str);
     $str = str_replace("\t", "", $str);
     $str = str_replace("}\\", '[TANTEK]', $str);
     // find nested stylesheets
     preg_match_all('/(\\@[^\\{]*\\{)([^\\{^\\}]*(\\{[^\\@^\\{^\\}]*\\}[^\\{^\\}]*)*?)\\}/', $str, $nested);
     $nestIndex = sizeof($this->nestedItems);
     if (sizeof($nested[0]) && isset($nested[1]) && is_array($nested[1]) && sizeof($nested[1])) {
         foreach ($nested[1] as $nestNum => $nestSel) {
             if (!empty($nestSel) && isset($nested[2]) && is_array($nested[2]) && isset($nested[2][$nestNum])) {
                 // handle imported stylesheets separately
                 if (stristr($nestSel, '@import') !== false) {
                     continue;
                 }
                 $subSheet = new CSS_Magic($this->processImports, $this->baseUrl, $this->basePath);
                 $subSheet->set_filename($this->filename);
                 $this->totalItems = $this->totalItems + $subSheet->parseString($nested[2][$nestNum]);
                 $this->nestedItems[$nestIndex] = array('selector' => $nestSel, 'content' => $subSheet);
                 $str = str_replace($nested[0][$nestNum], '[WPU_NESTED] {' . $nestIndex . '}', $str);
                 $nestIndex++;
             }
         }
     }
     // Other nested stylesheets:
     if ($this->processImports) {
         preg_match_all('/\\@import\\s(url\\()?[\'"]?([^\'^"^\\)]*)[\'"]?\\)?;/', $str, $imported);
         $importIndex = sizeof($this->importedItems);
         if (sizeof($imported[0]) && isset($imported[2]) && is_array($imported[2]) && sizeof($imported[2])) {
             foreach ($imported[2] as $importNum => $importUrl) {
                 $this->totalItems = $this->totalItems + 1;
                 $subSheet = new CSS_Magic($this->processImports, $this->baseUrl, $this->basePath);
                 $this->importedItems[$importIndex] = array('obj' => $subSheet, 'orig' => $imported[0][$importNum], 'url' => $imported[2][$importNum]);
                 $str = str_replace($imported[0][$importNum], '[WPU_NESTED_IMPORT] {' . $importIndex . '}', $str);
                 $importIndex++;
             }
         }
         // Now process the nested imports:
         $this->process_imports($subUrl, $subPath);
     }
     $parts = explode("}", $str);
     if (count($parts) > 0) {
         foreach ($parts as $part) {
             if (strpos($part, '{') !== FALSE) {
                 list($keys, $cssCode) = explode('{', $part);
                 // store full selector
                 if (strlen($keys) > 0) {
                     $keys = str_replace("\n", "", $keys);
                     $keys = str_replace("\r", "", $keys);
                     $keys = str_replace("\\", "", $keys);
                     $this->addSelector($keys, trim($cssCode));
                 }
             }
         }
     }
     // process nested stylesheets too
     $this->totalItems = $this->totalItems + count($this->css);
     return $this->totalItems;
 }