Exemplo n.º 1
0
 public function test3()
 {
     $this->getFlInstance();
     $tokens = $this->getTokens('3.text');
     $tokens = $tokens[0];
     $score = Fl_Css_Static::getSelectorSpecificity($tokens);
     //print_r($score);
 }
Exemplo n.º 2
0
 /**
  * 
  * collection selector
  * @param array $token
  * @param int $selectorPos
  */
 public function collectionSelector($token, $selectorPos = 0)
 {
     $attrs = $this->getSelectorProperties();
     if ($this->options['remove_empty_selector'] && empty($attrs)) {
         return true;
     }
     $detail = array('attrs' => $attrs, 'score' => 0, 'equal' => array(), 'pos' => $selectorPos++);
     $result = $this->getInstance('Fl_Css_SelectorToken', $token['value'])->run();
     if ($this->removeUnusedClass) {
         $removeResult = array();
         foreach ($result as $item) {
             $last = $item[count($item) - 1];
             if ($last['type'] === FL_TOKEN_CSS_SELECTOR_CLASS) {
                 $classValue = substr($last['value'], 1);
                 if (in_array($classValue, $this->useClassList)) {
                     $removeResult[] = $item;
                 }
             } else {
                 $removeResult[] = $item;
             }
         }
         if (empty($removeResult)) {
             return true;
         }
         $result = $removeResult;
     }
     if (count($result) === 1) {
         //only a tag
         if (!$this->inKeyframes && count($result[0]) === 1 && $result[0][0]['type'] === FL_TOKEN_CSS_SELECTOR_TYPE) {
             $value = strtolower($result[0][0]['value']);
             $se = array_merge($detail, array("selector" => $value, "score" => 1));
             if (!isset($this->tagMultis[$value])) {
                 if (isset($this->selectors[$value])) {
                     $attrs = Fl_Css_Static::mergeProperties($this->selectors[$value]['attrs'], $attrs);
                     $this->selectors[$value]['attrs'] = $attrs;
                 } else {
                     /*$this->selectors = array_merge ( array (
                     			$value => $se 
                     		), $this->selectors );*/
                     $this->selectors[$value] = $se;
                 }
             } else {
                 $this->selectors[$value . "%" . $selectorPos++] = $se;
             }
         } else {
             $selector = Fl_Css_Static::selectorTokenToText($result[0], false);
             $detail['selector'] = $selector;
             $detail['score'] = Fl_Css_Static::getSelectorSpecificity($result[0], true);
             $this->selectors[$selector . "%" . $selectorPos++] = $detail;
         }
     } else {
         $every = true;
         $sts = array();
         foreach ($result as $item) {
             if (count($item) === 1 && $item[0]['type'] === FL_TOKEN_CSS_SELECTOR_TYPE) {
                 $sts[strtolower($item[0]['value'])] = 1;
             } else {
                 $every = false;
             }
         }
         $selectors = array();
         $score = -1;
         $same = true;
         foreach ($result as $item) {
             $selectors[] = Fl_Css_Static::selectorTokenToText($item, false);
             $s = Fl_Css_Static::getSelectorSpecificity($item, true);
             if ($score != -1 && $score !== $s) {
                 $same = false;
             }
             if ($score === -1) {
                 $score = $s;
             }
         }
         $selector = join(",", $selectors);
         if ($every) {
             if (isset($this->selectors[$selector])) {
                 $attrs = Fl_Css_Static::mergeProperties($this->selectors[$selector]['attrs'], $attrs);
                 $this->selectors[$selector]['attrs'] = $attrs;
             } else {
                 $detail['selector'] = array_shift($selectors);
                 $detail['score'] = 1;
                 $detail['same_score'] = true;
                 $detail['equal'] = $selectors;
                 /*$this->selectors = array_merge ( array (
                 			$selector => $detail 
                 		), $this->selectors );*/
                 $this->selectors[$selector] = $detail;
             }
         } else {
             $this->tagMultis = array_merge($this->tagMultis, $sts);
             $detail['selector'] = array_shift($selectors);
             $detail['equal'] = $selectors;
             $detail['same_score'] = $same;
             $detail['score'] = $score;
             $this->selectors[$selector . "%" . $selectorPos++] = $detail;
         }
     }
 }
Exemplo n.º 3
0
 /**
  * 
  * Calculating a selector's specificity
  * see more: http://www.w3.org/TR/selectors/#specificity
  * @param array $selectorTokens
  */
 public static function getSelectorSpecificity($selectorTokens = array())
 {
     if (!is_array($selectorTokens)) {
         $selectorTokens = self::getSelectorTokens($selectorTokens);
     }
     $score = array(0, 0, 0);
     $notPattern = '/^\\:not\\(/ies';
     foreach ($selectorTokens as $item) {
         $type = $item['type'];
         switch ($type) {
             case FL_TOKEN_CSS_SELECTOR_ID:
                 $score[0]++;
                 break;
             case FL_TOKEN_CSS_SELECTOR_TYPE:
             case FL_TOKEN_CSS_SELECTOR_PSEUDO_ELEMENT:
                 $score[2]++;
                 break;
             case FL_TOKEN_CSS_SELECTOR_CLASS:
             case FL_TOKEN_CSS_SELECTOR_ATTRIBUTES:
                 $score[1]++;
                 break;
             case FL_TOKEN_CSS_SELECTOR_PSEUDO_CLASS:
                 $value = $item['value'];
                 //:not(xxx)
                 if (preg_match($notPattern, $value)) {
                     $value = trim(preg_replace($notPattern, "", $value));
                     $value = substr($value, 0, strlen($value) - 1);
                     Fl::loadClass('Fl_Css_SelectorToken');
                     $instance = new Fl_Css_SelectorToken($value);
                     $tokens = $instance->run();
                     $notScore = Fl_Css_Static::getSelectorSpecificity($tokens[0]);
                     $score[0] += $notScore[0];
                     $score[1] += $notScore[1];
                     $score[2] += $notScore[2];
                 } else {
                     $score[1]++;
                 }
                 break;
         }
     }
     return $score;
 }