コード例 #1
0
 public static function get_proprieta($array_subset_selettori, $val, $e_original, $elem_radice)
 {
     global $selettori_appoggio;
     $valore_proprieta = null;
     $num_id = 0;
     $num_class = 0;
     $num_tag = 0;
     $num_regola = 0;
     // use the foreach to track the location of the rule priority associated with $elem_radice
     //lo uso nel foreach per tenere traccia della posizione della regola di priorita' maggiore associata a $elem_radice
     $spazio = "{_}";
     // used for cases in which a space between the two is significant. eg: "div.class" and "div .class"
     //serve per i casi in cui uno spazio tra due elementi è significativo. es: "div.class" e "div .class"
     foreach ($array_subset_selettori as $array_regole) {
         // Check if [$regalo]['regole'] contained the property' $val and store it in $valore_proprieta_new
         // use a case for special properties like margin and padding
         // for these properties' function BasicChecks: get_priority_prop consider what property has priority more
         // eg between margin and margin-top (that is, if one then overwrite the other)
         //verifico se in [$regola]["regole"] e' contenuta la proprieta' $val e la memorizzo in $valore_proprieta_new
         //uso un case per le proprietà particolari come margin e padding
         //per queste proprieta' la funzione BasicChecks::get_priority_prop valuta quale proprieta' ha la priorita' maggiore
         //ad es tra margin-top e margin (cioe' se una delle due "sovrascrive" l'altra)
         $num_id_new = 0;
         $num_class_new = 0;
         $num_tag_new = 0;
         $valore_proprieta_new = null;
         // NOTE: This switch may be included in a function also reused getElementStyleInline
         //NOTA: questo switch potrebbe essere incluso in una funzione riutilizzata anche da getElementStyleInline
         switch ($val) {
             case "margin-top":
                 if (isset($array_regole["regole"][$val]) || isset($array_regole["regole"]["margin"])) {
                     $valore_proprieta_new = BasicChecks::getTop(BasicChecks::get_priority_prop($array_regole["regole"][$val], $array_regole["regole"]["margin"]));
                 }
                 break;
             case "margin-bottom":
                 if (isset($array_regole["regole"][$val]) || isset($array_regole["regole"]["margin"])) {
                     $valore_proprieta_new = BasicChecks::getBottom(BasicChecks::get_priority_prop($array_regole["regole"][$val], $array_regole["regole"]["margin"]));
                 }
                 break;
             case "margin-left":
                 if (isset($array_regole["regole"][$val]) || isset($array_regole["regole"]["margin"])) {
                     $valore_proprieta_new = BasicChecks::getLeft(BasicChecks::get_priority_prop($array_regole["regole"][$val], $array_regole["regole"]["margin"]));
                 }
                 break;
             case "margin-right":
                 if (isset($array_regole["regole"][$val]) || isset($array_regole["regole"]["margin"])) {
                     $valore_proprieta_new = BasicChecks::getRight(BasicChecks::get_priority_prop($array_regole["regole"][$val], $array_regole["regole"]["margin"]));
                 }
                 break;
             case "padding-top":
                 if (isset($array_regole["regole"][$val]) || isset($array_regole["regole"]["padding"])) {
                     $valore_proprieta_new = BasicChecks::getTop(BasicChecks::get_priority_prop($array_regole["regole"][$val], $array_regole["regole"]["padding"]));
                 }
                 break;
             case "padding-bottom":
                 if (isset($array_regole["regole"][$val]) || isset($array_regole["regole"]["padding"])) {
                     $valore_proprieta_new = BasicChecks::getBottom(BasicChecks::get_priority_prop($array_regole["regole"][$val], $array_regole["regole"]["padding"]));
                 }
                 break;
             case "padding-left":
                 if (isset($array_regole["regole"][$val]) || isset($array_regole["regole"]["padding"])) {
                     $valore_proprieta_new = BasicChecks::getLeft(BasicChecks::get_priority_prop($array_regole["regole"][$val], $array_regole["regole"]["padding"]));
                 }
                 break;
             case "padding-right":
                 if (isset($array_regole["regole"][$val]) || isset($array_regole["regole"]["padding"])) {
                     $valore_proprieta_new = BasicChecks::getRight(BasicChecks::get_priority_prop($array_regole["regole"][$val], $array_regole["regole"]["padding"]));
                 }
                 break;
             case "background-color":
                 //verifico se c'e' un'immagine di sfondo, nel caso setto la proprietà a -1
                 if (isset($array_regole["regole"]["background-image"])) {
                     $valore_proprieta_new = "-1";
                 } elseif (isset($array_regole["regole"]["background"]) && stripos($array_regole["regole"]["background"]["val"], "url") !== false) {
                     $valore_proprieta_new = "-1";
                 } elseif (isset($array_regole["regole"][$val]) || isset($array_regole["regole"]["background"])) {
                     $valore_proprieta_new = BasicChecks::getBgColor(BasicChecks::get_priority_prop($array_regole["regole"][$val], $array_regole["regole"]["background"]));
                 }
                 break;
             default:
                 if (isset($array_regole["regole"][$val])) {
                     $valore_proprieta_new = $array_regole["regole"][$val]["val"];
                 }
                 break;
         }
         $ris = null;
         // if the value of a property  was found, confirm it can be applied to the element considered
         // se il valore di una proprieta è stato trovato verifico se puo' essere applicata all'elemento considerato
         if ($valore_proprieta_new != null) {
             if (stripos($elem_radice, "#") !== false) {
                 $num_id_new = 1;
             } elseif (stripos($elem_radice, ".") !== false) {
                 $num_class_new = 1;
             } else {
                 $num_tag_new = 1;
             }
             $size_of_regole = sizeof($array_regole["prev"]);
             if ($size_of_regole == 1) {
                 $ris = true;
             } else {
                 // verification takes into account that a compound rule takes precedence over a simple rule, even if it follows!
                 // eg: "div > p {}" & "{p}" => to <div><p></p></ div> wins over "div > p {}"
                 // check whether the item falls under the "compound"
                 // if so, I check if [$ rule] ['rules'] contained the $ val
                 //la verifica tiene conto che una regola composta ha priorità su una "semplice", anche se la semplice è successiva!
                 //es: "div > p{}" & "p{}" => per <div><p></p></div> vince "div > p{}"
                 //controllo se l'elemento rientra nella regola "composta"
                 //se si, verifico se in [$regola]["regole"] � contenuta la propriet� $val
                 $i = 1;
                 //start from the first parent of the current element
                 //inizio dal primo padre dell'elemento corrente
                 $e = $e_original;
                 while ($i < $size_of_regole && $ris !== false) {
                     // NOTE: This series of if / elseif and switch could be next
                     // be merged into a single set of if / else
                     // $ element can 'contain'> ',' + ', id, class, a tag
                     //NOTA: questa serie di if/elseif e lo switch successivo potrebbero
                     //essere unificati in un unica serie di if/else
                     //$elemento puo' contenere '>', '+', un id, una classe un tag
                     if ($array_regole["prev"][$i] == ">") {
                         $tipo = ">";
                     } elseif ($array_regole["prev"][$i] == "+") {
                         $tipo = "+";
                     } elseif ($array_regole["prev"][$i] == $spazio) {
                         $tipo = "spazio";
                     } elseif (stripos($array_regole["prev"][$i], ".") !== false) {
                         $tipo = "class";
                     } elseif (stripos($array_regole["prev"][$i], "#") !== false) {
                         $tipo = "id";
                     } else {
                         $tipo = "tag";
                     }
                     switch ($tipo) {
                         case ">":
                             //casi div > p, #id > p, .class > p
                             if (stripos($array_regole["prev"][$i + 1], "#") !== false) {
                                 $e = $e->parent();
                                 // id: the control that has the id of the predecessor rule
                                 // id: controllo che il predecessore abbia l'id della regola
                                 if ($e != null && $e->id == str_replace('#', '', $array_regole["prev"][$i + 1])) {
                                     $ris = true;
                                     $num_id_new++;
                                 } else {
                                     $ris = false;
                                 }
                             } elseif (stripos($array_regole["prev"][$i + 1], ".") !== false) {
                                 $e = $e->parent();
                                 // class: the control of a predecessor has the class rule
                                 //class: controllo che il predecessore abbia la class della regola
                                 if ($e != null && $e->class == str_replace('.', '', $array_regole["prev"][$i + 1])) {
                                     $ris = true;
                                     $num_class_new++;
                                 } else {
                                     $ris = false;
                                 }
                             } else {
                                 $e = $e->parent();
                                 // tag: check that the predecessor is the tag of the rule
                                 //tag: controllo che il predecessore sia il tag della regola
                                 if ($e != null && $e->tag == $array_regole["prev"][$i + 1]) {
                                     $ris = true;
                                     $num_tag_new++;
                                 } else {
                                     $ris = false;
                                 }
                             }
                             $i++;
                             break;
                         case "+":
                             if (stripos($array_regole["prev"][$i + 1], "#") !== false) {
                                 $e->prev_sibling();
                                 // id: the control that has the id of the predecessor rule
                                 //id: controllo che il predecessore abbia l'id della regola
                                 if ($e != null && $e->id == str_replace('#', '', $array_regole["prev"][$i + 1])) {
                                     $ris = true;
                                     $num_id_new++;
                                 } else {
                                     $ris = false;
                                 }
                             } elseif (stripos($array_regole["prev"][$i + 1], ".") !== false) {
                                 $e->prev_sibling();
                                 // class: the control that his predecessor has the class rule
                                 //class: controllo che il predecessore abbia la class della regola
                                 if ($e != null && $e->class == str_replace('.', '', $array_regole["prev"][$i + 1])) {
                                     $ris = true;
                                     $num_class_new++;
                                 } else {
                                     $ris = false;
                                 }
                             } else {
                                 $e->prev_sibling();
                                 // tag: check that the predecessor is the tag of the rule
                                 //tag: controllo che il predecessore sia il tag della regola
                                 if ($e != null && $e->tag == $array_regole["prev"][$i + 1]) {
                                     $ris = true;
                                     $num_tag_new++;
                                 } else {
                                     $ris = false;
                                 }
                             }
                             $i++;
                             break;
                         case "spazio":
                             //casi: div #id, #id #id, .class #id, div .class, #id .class, .class .class
                             if (stripos($array_regole["prev"][$i + 1], "#") !== false) {
                                 $e = $e->parent();
                                 while ($e != null && $e->id != str_replace('#', '', $array_regole["prev"][$i + 1])) {
                                     $e = $e->parent();
                                 }
                                 // id: the control that has the id of the predecessor rule
                                 //id: controllo che il predecessore abbia l'id della regola
                                 if ($e != null && $e->id == str_replace('#', '', $array_regole["prev"][$i + 1])) {
                                     $ris = true;
                                     $num_id_new++;
                                 } else {
                                     $ris = false;
                                 }
                             } elseif (stripos($array_regole["prev"][$i + 1], ".") !== false) {
                                 $e = $e->parent();
                                 while ($e != null && $e->class != str_replace('.', '', $array_regole["prev"][$i + 1])) {
                                     $e = $e->parent();
                                 }
                                 // class: the control that his predecessor has the class rule
                                 // class: controllo che il predecessore abbia la class della regola
                                 if ($e != null && $e->class == str_replace('.', '', $array_regole["prev"][$i + 1])) {
                                     $ris = true;
                                     $num_class_new++;
                                 } else {
                                     $ris = false;
                                 }
                             } else {
                                 $e = $e->parent();
                                 while ($e != null && $e->tag != $array_regole["prev"][$i + 1]) {
                                     $e = $e->parent();
                                 }
                                 // tag: check that the predecessor is the tag of the rule
                                 //tag: controllo che il predecessore sia il tag della regola
                                 if ($e != null && $e->tag == $array_regole["prev"][$i + 1]) {
                                     $ris = true;
                                     $num_tag_new++;
                                 } else {
                                     $ris = false;
                                 }
                             }
                             $i++;
                             break;
                         case "tag":
                             //casi: p.classe, p#id, div p
                             if (stripos($array_regole["prev"][$i - 1], ".") !== false) {
                                 if ($e->tag == $array_regole["prev"][$i]) {
                                     $ris = true;
                                     $num_tag_new++;
                                 } else {
                                     $ris = false;
                                 }
                             } elseif (stripos($array_regole["prev"][$i - 1], "#") !== false) {
                                 if ($e->tag == $array_regole["prev"][$i]) {
                                     $ris = true;
                                     $num_tag_new++;
                                 } else {
                                     $ris = false;
                                 }
                             } else {
                                 //$e=$e->parent();
                                 while ($e != null && $e->tag != $array_regole["prev"][$i]) {
                                     $e = $e->parent();
                                 }
                                 // tag: check that the predecessor is the tag of the rule
                                 //tag: controllo che il predecessore sia il tag della regola
                                 if ($e != null) {
                                     $ris = true;
                                     $num_tag_new++;
                                 } else {
                                     $ris = false;
                                 }
                             }
                             break;
                         case "id":
                             //casi: #id p, #id .class, #id #id
                             //$e=$e->parent();
                             while ($e != null && $e->id != str_replace('#', '', $array_regole["prev"][$i])) {
                                 $e = $e->parent();
                             }
                             // tag: check that the predecessor is the tag of the rule
                             //tag: controllo che il predecessore sia il tag della regola
                             if ($e != null) {
                                 $ris = true;
                                 $num_tag_new++;
                             } else {
                                 $ris = false;
                             }
                             break;
                         case "class":
                             //casi: .id p, .id .class, .id #id
                             //$e=$e->parent();
                             while ($e != null && $e->class != str_replace('.', '', $array_regole["prev"][$i])) {
                                 $e = $e->parent();
                             }
                             // tag: check that the predecessor is the tag of the rul
                             //tag: controllo che il predecessore sia il tag della regola
                             //if($e != null && $e->tag == $array_regole["prev"][$i])
                             if ($e != null) {
                                 $ris = true;
                                 $num_tag_new++;
                             } else {
                                 $ris = false;
                             }
                             break;
                     }
                     //end case
                     $i++;
                 }
                 //end while
             }
             //end else regola composta - compound rule
             if ($ris == true) {
                 // analyze and apply the new rule
                 // check if the priority of the new greater than previous
                 //la nuova regola analizzata è applicabile
                 //controllo se la priorita della nuova supera quella della precedente
                 if (stripos($valore_proprieta_new, "!important") !== false && stripos($valore_proprieta, "!important") === false) {
                     // $proprieta is not !important while $proprietà_new is, then override $proprieta
                     //$proprieta non è !important mentre $proprietà_new si, quindi sovrascrivo $proprieta
                     $valore_proprieta = $valore_proprieta_new;
                     $num_id = $num_id_new;
                     $num_class = $num_class_new;
                     $num_tag = $num_tag_new;
                     $num_regola_best = $num_regola;
                 } elseif (stripos($valore_proprieta_new, "!important") === false && stripos($valore_proprieta, "!important") === false || stripos($valore_proprieta_new, "!important") !== false && stripos($valore_proprieta, "!important") !== false) {
                     if ($num_id_new > $num_id) {
                         $valore_proprieta = $valore_proprieta_new;
                         $num_id = $num_id_new;
                         $num_class = $num_class_new;
                         $num_tag = $num_tag_new;
                         $num_regola_best = $num_regola;
                     } elseif ($num_id_new == $num_id) {
                         // same ID number, control the number of class
                         //stesso numero di id, controllo il numero di class
                         if ($num_class_new > $num_class) {
                             $valore_proprieta = $valore_proprieta_new;
                             $num_id = $num_id_new;
                             $num_class = $num_class_new;
                             $num_tag = $num_tag_new;
                             $num_regola_best = $num_regola;
                         } elseif ($num_class_new == $num_class) {
                             // same id and class number, check number of tags
                             //stesso numero di id e class, controllo in numero di tag
                             if ($num_tag_new >= $num_tag) {
                                 // same or greater number of id, class and tags: is the priority of the new rule  //stesso o maggiore numero di id, class e tag: la priorità è della nuova regola
                                 $valore_proprieta = $valore_proprieta_new;
                                 $num_id = $num_id_new;
                                 $num_class = $num_class_new;
                                 $num_tag = $num_tag_new;
                                 $num_regola_best = $num_regola;
                             }
                         }
                     }
                 }
                 $valore_proprieta_new = null;
             }
         }
         $num_regola++;
     }
     if ($valore_proprieta == null) {
         return null;
     }
     // create the structure info_proprieta
     // store the id number, class and tag  necessary to verify the priority
     // rules are starting from an id, a class or a tag
     // it is not always a rule that has the selectors as the last (or only) a descendant of id or a class that takes
     // ends with a tag
     //creo la struttura info_proprieta'
     //memorizzare il numero di id, class e tag è necessario per verificare la priorita'
     //delle regole trovate partendo da un id, una class o un tag
     // non sempre infatti una regola che nei selettori ha come ultimo (o unico) discendente un id o class batte una che
     //termina con un tag
     //es:
     // "#id2 p{}" batte "#id1{}" (se p contiene id="id1")
     $info_proprieta = array("valore" => $valore_proprieta, "num_id" => $num_id, "num_class" => $num_class, "num_tag" => $num_tag, "css_rule" => $array_subset_selettori[$num_regola_best]);
     //echo("<p>regola per ". $elem_radice."</p>");
     //print_r($array_subset_selettori[$num_regola-1]);
     return $info_proprieta;
 }