1 | <?php |
||
5 | class Selecta |
||
6 | { |
||
7 | public static $single_tags = array('img', 'br', 'hr', 'input'); |
||
8 | public static $pseudo_classes = array('disabled', 'checked'); |
||
9 | public static $meta_map = array('.'=>'class', '#'=>'id'); |
||
10 | |||
11 | public static function build($selector, $contents='', $open_tags=true, $close_tags=true) |
||
22 | |||
23 | public static function wrap($selector, $contents='') |
||
27 | |||
28 | public static function open($selector) |
||
32 | |||
33 | public static function close($selector) |
||
37 | |||
38 | private static function tagify($selector='', $contents='', $open_tags=true, $close_tags=true) |
||
39 | { |
||
40 | $attrs = array(); |
||
41 | |||
42 | // replace dots within attribute selectors |
||
43 | $selector = self::sanitise_attribute_dots($selector); |
||
44 | |||
45 | $metas = '\.\#\[\:'; |
||
46 | $pattern = '/(['.$metas.'])([^'.$metas.']*)?/'; |
||
47 | preg_match_all($pattern, $selector, $matches, PREG_SET_ORDER); |
||
48 | |||
49 | if (count($matches)) { |
||
50 | foreach($matches as $match) { |
||
51 | $attrs = self::build_attributes($match[1], $match[2], $attrs); |
||
52 | } |
||
53 | |||
54 | // reduce selector to just tag name |
||
55 | $parts = preg_split('/['.$metas.']/', $selector); |
||
56 | $selector = $parts[0]; |
||
57 | } |
||
58 | |||
59 | return self::build_tag($selector, $attrs, $contents, $open_tags, $close_tags); |
||
60 | } |
||
61 | |||
62 | private static function build_attributes($meta_char, $value, $attrs) |
||
63 | { |
||
64 | $key = false; |
||
65 | if (isset(self::$meta_map[$meta_char])) { |
||
66 | $key = self::$meta_map[$meta_char]; |
||
67 | }else{ |
||
68 | switch ($meta_char) { |
||
69 | |||
70 | // Attribute selectors |
||
71 | case '[': |
||
72 | list($key, $value) = self::build_attribute_selector_attribute($value); |
||
73 | break; |
||
74 | |||
75 | // Pseudo-class selectors |
||
76 | case ':': |
||
77 | list($key, $value) = self::build_pseudo_class_attribute($value); |
||
78 | break; |
||
79 | } |
||
80 | } |
||
81 | |||
82 | if ($key){ |
||
83 | if (isset($attrs[$key])) { |
||
84 | $attrs[$key] .= ' '.$value; |
||
85 | }else{ |
||
86 | $attrs[$key] = $value; |
||
87 | } |
||
88 | } |
||
89 | |||
90 | return $attrs; |
||
91 | } |
||
92 | |||
93 | private static function build_attribute_selector_attribute($value) |
||
94 | { |
||
95 | $value = rtrim($value, ']'); |
||
96 | |||
97 | if (strpos($value, '=')) { |
||
98 | $parts = explode('=', $value, 2); |
||
99 | $key = $parts[0]; |
||
100 | $value = self::unsanitise_attribute_dots($parts[1]); |
||
101 | }else{ |
||
102 | $key = $value; |
||
103 | $value = false; |
||
104 | } |
||
105 | |||
106 | return array($key, $value); |
||
107 | } |
||
108 | |||
109 | private static function build_pseudo_class_attribute($pclass='') |
||
117 | |||
118 | private static function build_tag($name, $attributes=array(), $contents='', $open_tag=true, $close_tag=true) |
||
138 | |||
139 | private static function open_tag($name, $attributes=array()) |
||
158 | |||
159 | private static function close_tag($name) |
||
163 | |||
164 | private static function html($s, $quotes=false, $double_encode=false) |
||
165 | { |
||
166 | if ($s || (is_string($s) && strlen($s))) { |
||
171 | |||
172 | private static function sanitise_attribute_dots($selector) |
||
186 | |||
187 | private static function unsanitise_attribute_dots($string) |
||
191 | |||
192 | } |