1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12:
13:
14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27:
28: final class TexyModifier extends TexyObject
29: {
30:
31: public $id;
32:
33:
34: public $classes = array();
35:
36:
37: public $styles = array();
38:
39:
40: public $attrs = array();
41:
42:
43: public $hAlign;
44:
45:
46: public $vAlign;
47:
48:
49: public $title;
50:
51:
52: public $cite;
53:
54:
55: public static $elAttrs = array(
56: 'abbr'=>1,'accesskey'=>1,'align'=>1,'alt'=>1,'archive'=>1,'axis'=>1,'bgcolor'=>1,'cellpadding'=>1,
57: 'cellspacing'=>1,'char'=>1,'charoff'=>1,'charset'=>1,'cite'=>1,'classid'=>1,'codebase'=>1,'codetype'=>1,
58: 'colspan'=>1,'compact'=>1,'coords'=>1,'data'=>1,'datetime'=>1,'declare'=>1,'dir'=>1,'face'=>1,'frame'=>1,
59: 'headers'=>1,'href'=>1,'hreflang'=>1,'hspace'=>1,'ismap'=>1,'lang'=>1,'longdesc'=>1,'name'=>1,
60: 'noshade'=>1,'nowrap'=>1,'onblur'=>1,'onclick'=>1,'ondblclick'=>1,'onkeydown'=>1,'onkeypress'=>1,
61: 'onkeyup'=>1,'onmousedown'=>1,'onmousemove'=>1,'onmouseout'=>1,'onmouseover'=>1,'onmouseup'=>1,'rel'=>1,
62: 'rev'=>1,'rowspan'=>1,'rules'=>1,'scope'=>1,'shape'=>1,'size'=>1,'span'=>1,'src'=>1,'standby'=>1,
63: 'start'=>1,'summary'=>1,'tabindex'=>1,'target'=>1,'title'=>1,'type'=>1,'usemap'=>1,'valign'=>1,
64: 'value'=>1,'vspace'=>1,
65: );
66:
67:
68:
69: 70: 71:
72: public function __construct($mod = NULL)
73: {
74: $this->setProperties($mod);
75: }
76:
77:
78:
79: public function setProperties($mod)
80: {
81: if (!$mod) return;
82:
83: $p = 0;
84: $len = strlen($mod);
85:
86: while ($p < $len)
87: {
88: $ch = $mod[$p];
89:
90: if ($ch === '(') { 91: $a = strpos($mod, ')', $p) + 1;
92: $this->title = Texy::unescapeHtml(trim(substr($mod, $p + 1, $a - $p - 2)));
93: $p = $a;
94:
95: } elseif ($ch === '{') { 96: $a = strpos($mod, '}', $p) + 1;
97: foreach (explode(';', substr($mod, $p + 1, $a - $p - 2)) as $value) {
98: $pair = explode(':', $value, 2);
99: $prop = strtolower(trim($pair[0]));
100: if ($prop === '' || !isset($pair[1])) continue;
101: $value = trim($pair[1]);
102:
103: if (isset(self::$elAttrs[$prop])) 104: $this->attrs[$prop] = $value;
105: elseif ($value !== '') 106: $this->styles[$prop] = $value;
107: }
108: $p = $a;
109:
110: } elseif ($ch === '[') { 111: $a = strpos($mod, ']', $p) + 1;
112: $s = str_replace('#', ' #', substr($mod, $p + 1, $a - $p - 2));
113: foreach (explode(' ', $s) as $value) {
114: if ($value === '') continue;
115:
116: if ($value{0} === '#')
117: $this->id = substr($value, 1);
118: else
119: $this->classes[$value] = TRUE;
120: }
121: $p = $a;
122: }
123: 124: elseif ($ch === '^') { $this->vAlign = 'top'; $p++; }
125: elseif ($ch === '-') { $this->vAlign = 'middle'; $p++; }
126: elseif ($ch === '_') { $this->vAlign = 'bottom'; $p++; }
127: elseif ($ch === '=') { $this->hAlign = 'justify'; $p++; }
128: elseif ($ch === '>') { $this->hAlign = 'right'; $p++; }
129: elseif (substr($mod, $p, 2) === '<>') { $this->hAlign = 'center'; $p+=2; }
130: elseif ($ch === '<') { $this->hAlign = 'left'; $p++; }
131: else { break; }
132: }
133: }
134:
135:
136:
137: 138: 139: 140: 141: 142:
143: public function decorate($texy, $el)
144: {
145: $elAttrs = & $el->attrs;
146:
147: 148: $tmp = $texy->allowedTags; 149: if (!$this->attrs) {
150:
151: } elseif ($tmp === Texy::ALL) {
152: $elAttrs = $this->attrs;
153: $el->validateAttrs($texy->dtd);
154:
155: } elseif (is_array($tmp) && isset($tmp[$el->getName()])) {
156: $tmp = $tmp[$el->getName()];
157:
158: if ($tmp === Texy::ALL) {
159: $elAttrs = $this->attrs;
160:
161: } elseif (is_array($tmp) && count($tmp)) {
162: $tmp = array_flip($tmp);
163: foreach ($this->attrs as $key => $value)
164: if (isset($tmp[$key])) $el->attrs[$key] = $value;
165: }
166: $el->validateAttrs($texy->dtd);
167: }
168:
169: 170: if ($this->title !== NULL)
171: $elAttrs['title'] = $texy->typographyModule->postLine($this->title);
172:
173: 174: if ($this->classes || $this->id !== NULL) {
175: $tmp = $texy->_classes; 176: if ($tmp === Texy::ALL) {
177: foreach ($this->classes as $value => $foo) $elAttrs['class'][] = $value;
178: $elAttrs['id'] = $this->id;
179: } elseif (is_array($tmp)) {
180: foreach ($this->classes as $value => $foo)
181: if (isset($tmp[$value])) $elAttrs['class'][] = $value;
182:
183: if (isset($tmp['#' . $this->id])) $elAttrs['id'] = $this->id;
184: }
185: }
186:
187: 188: if ($this->styles) {
189: $tmp = $texy->_styles; 190: if ($tmp === Texy::ALL) {
191: foreach ($this->styles as $prop => $value) $elAttrs['style'][$prop] = $value;
192: } elseif (is_array($tmp)) {
193: foreach ($this->styles as $prop => $value)
194: if (isset($tmp[$prop])) $elAttrs['style'][$prop] = $value;
195: }
196: }
197:
198: 199: if ($this->hAlign) {
200: if (empty($texy->alignClasses[$this->hAlign])) {
201: $elAttrs['style']['text-align'] = $this->hAlign;
202: } else {
203: $elAttrs['class'][] = $texy->alignClasses[$this->hAlign];
204: }
205: }
206:
207: 208: if ($this->vAlign) {
209: if (empty($texy->alignClasses[$this->vAlign])) {
210: $elAttrs['style']['vertical-align'] = $this->vAlign;
211: } else {
212: $elAttrs['class'][] = $texy->alignClasses[$this->vAlign];
213: }
214: }
215:
216: return $el;
217: }
218:
219: }
220: