1 | <?php |
---|
2 | |
---|
3 | |
---|
4 | include_spip('inc/plugin'); |
---|
5 | include_spip('inc/texte'); |
---|
6 | include_spip('base/create'); |
---|
7 | include_spip('base/abstract_sql'); |
---|
8 | include_spip('base/facteur'); |
---|
9 | include_spip('inc/facteur_classes'); |
---|
10 | |
---|
11 | |
---|
12 | /** |
---|
13 | * facteur_addstyle |
---|
14 | * |
---|
15 | * @author Eric Dols |
---|
16 | **/ |
---|
17 | function facteur_addstyle($matches) { |
---|
18 | |
---|
19 | // $matches[1]=tag, $matches[2]=tag attributes (if any), $matches[3]=xhtml closing (if any) |
---|
20 | |
---|
21 | // variables values set in calling function |
---|
22 | global $styledefinition, $styletag, $styleclass; |
---|
23 | |
---|
24 | // convert the style definition to a one-liner |
---|
25 | $styledefinition = preg_replace ("!\s+!mi", " ", $styledefinition ); |
---|
26 | // convert all double-quotes to single-quotes |
---|
27 | $styledefinition = preg_replace ('/"/','\'', $styledefinition ); |
---|
28 | |
---|
29 | if (preg_match ("/style\=/i", $matches[2])) { |
---|
30 | // add styles to existing style attribute if any already in the tag |
---|
31 | $pattern = "!(.* style\=)[\"]([^\"]*)[\"](.*)!mi"; |
---|
32 | $replacement = "\$1".'"'."\$2 ".$styledefinition.'"'."\$3"; |
---|
33 | $attributes = preg_replace ($pattern, $replacement , $matches[2]); |
---|
34 | } else { |
---|
35 | // otherwise add new style attribute to tag (none was present) |
---|
36 | $attributes = $matches[2].' style="'.$styledefinition.'"'; |
---|
37 | } |
---|
38 | |
---|
39 | if ($styleclass!="") { |
---|
40 | // if we were injecting a class style, remove the now useless class attribute from the html tag |
---|
41 | |
---|
42 | // Single class in tag case (class="classname"): remove class attribute altogether |
---|
43 | $pattern = "!(.*) class\=['\"]".$styleclass."['\"](.*)!mi"; |
---|
44 | $replacement = "\$1\$2"; |
---|
45 | $attributes = preg_replace ( $pattern, $replacement, $attributes); |
---|
46 | |
---|
47 | // Multiple classes in tag case (class="classname anotherclass..."): remove class name from class attribute. |
---|
48 | // classes are injected inline and removed by order of appearance in <head> stylesheet |
---|
49 | // exact same behavior as where last declared class attributes in <style> take over (IE6 tested only) |
---|
50 | $pattern = "!(.* class\=['\"][^\"]*)(".$styleclass." | ".$styleclass.")([^\"]*['\"].*)!mi"; |
---|
51 | $replacement = "\$1\$3"; |
---|
52 | $attributes = preg_replace ( $pattern, $replacement, $attributes); |
---|
53 | |
---|
54 | } |
---|
55 | |
---|
56 | return "<".$matches[1].$attributes.$matches[3].">"; |
---|
57 | } |
---|
58 | |
---|
59 | |
---|
60 | ?> |
---|