-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcore.php
1040 lines (947 loc) · 31.2 KB
/
core.php
1
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
+----------------------------------------------------------------------+
| XHP |
+----------------------------------------------------------------------+
| Copyright (c) 2009 - 2016 Facebook, Inc. (http://www.facebook.com) |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE.PHP, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
*/
abstract class :xhp {
abstract public function __construct($attributes, $children);
abstract public function appendChild($child);
abstract public function prependChild($child);
abstract public function replaceChildren();
abstract public function getChildren($selector = null);
abstract public function getFirstChild($selector = null);
abstract public function getLastChild($selector = null);
abstract public function getAttribute($attr);
abstract public function getAttributes();
abstract public function setAttribute($attr, $val);
abstract public function setAttributes(array $attrs);
abstract public function isAttributeSet($attr);
abstract public function removeAttribute($attr);
abstract public function categoryOf($cat);
abstract public function __toString();
abstract protected function &__xhpCategoryDeclaration();
abstract protected function &__xhpChildrenDeclaration();
protected static function &__xhpAttributeDeclaration() {}
/**
* Enabling validation will give you stricter documents; you won't be able to
* do many things that violate the XHTML 1.0 Strict spec. It is recommend that
* you leave this on because otherwise things like the `children` keyword will
* do nothing. This validation comes at some CPU cost, however, so if you are
* running a high-traffic site you will probably want to disable this in
* production. You should still leave it on while developing new features,
* though.
*/
public static $ENABLE_VALIDATION = true;
final protected static function renderChild($child) {
if ($child instanceof :xhp) {
return $child->__toString();
} else if (is_array($child)) {
throw new XHPRenderArrayException('Can not render array!');
} else {
return htmlspecialchars((string)$child);
}
}
public static function element2class($element) {
return 'xhp_'.str_replace(array(':', '-'), array('__', '_'), $element);
}
public static function class2element($class) {
return str_replace(array('__', '_'), array(':', '-'), preg_replace('#^xhp_#i', '', $class));
}
}
/**
* For backwards compatibility only -- this class really shouldn't exist. Use
* :xhp or :x:composable-element as appropriate, but never :x:base.
*/
abstract class :x:base extends :xhp {}
abstract class :x:composable-element extends :x:base {
private
$attributes = array(),
$children = array(),
$context = array();
private static $specialAttributes = array('data' => true, 'aria' => true);
// Private constants indicating the declared types of attributes
const TYPE_STRING = 1;
const TYPE_BOOL = 2;
const TYPE_NUMBER = 3;
const TYPE_ARRAY = 4;
const TYPE_OBJECT = 5;
const TYPE_VAR = 6;
const TYPE_ENUM = 7;
const TYPE_FLOAT = 8;
const TYPE_CALLABLE = 9;
protected function init() {}
/**
* A new :x:composable-element is instantiated for every literal tag
* expression in the script.
*
* The following code:
* $foo = <foo attr="val">bar</foo>;
*
* will execute something like:
* $foo = new xhp_foo(array('attr' => 'val'), array('bar'));
*
* @param $attributes map of attributes to values
* @param $children list of children
*/
final public function __construct($attributes, $children) {
foreach ($children as $child) {
$this->appendChild($child);
}
$this->setAttributes($attributes);
if (:xhp::$ENABLE_VALIDATION) {
// There is some cost to having defaulted unused arguments on a function
// so we leave these out and get them with func_get_args().
$args = func_get_args();
if (isset($args[2])) {
$this->source = "$args[2]:$args[3]";
} else {
$this->source =
'You have ENABLE_VALIDATION on, but debug information is not being ' .
'passed to XHP objects correctly. Ensure xhp.include_debug is on ' .
'in your PHP configuration. Without this option enabled, ' .
'validation errors will be painful to debug at best.';
}
}
$this->init();
}
/**
* Adds a child to the end of this node. If you give an array to this method
* then it will behave like a DocumentFragment.
*
* @param $child single child or array of children
*/
final public function appendChild($child) {
if ($child instanceof Traversable) {
foreach ($child as $c) {
$this->appendChild($c);
}
} else if ($child instanceof :x:frag) {
$this->children = array_merge($this->children, $child->children);
} else if ($child !== null) {
$this->children[] = $child;
}
return $this;
}
/**
* Adds a child to the beginning of this node. If you give an array to this
* method then it will behave like a DocumentFragment.
*
* @param $child single child or array of children
*/
final public function prependChild($child) {
if ($child instanceof Traversable) {
foreach (array_reverse($child) as $c) {
$this->prependChild($c);
}
} else if ($child instanceof :x:frag) {
$this->children = array_merge($child->children, $this->children);
} else if ($child !== null) {
array_unshift($this->children, $child);
}
return $this;
}
/**
* Replaces all children in this node. You may pass a single array or
* multiple parameters.
*
* @param $children Single child or array of children
*/
final public function replaceChildren(/* ... */) {
// This function has been micro-optimized
$args = func_get_args();
$new_children = array();
foreach ($args as $xhp) {
if ($xhp) {
if ($xhp instanceof :x:frag) {
foreach ($xhp->children as $child) {
$new_children[] = $child;
}
} else if (!$xhp instanceof Traversable) {
$new_children[] = $xhp;
} else {
foreach ($xhp as $element) {
if ($element instanceof :x:frag) {
foreach ($element->children as $child) {
$new_children[] = $child;
}
} else if ($element !== null) {
$new_children[] = $element;
}
}
}
}
}
$this->children = $new_children;
return $this;
}
/**
* Fetches all direct children of this element that match a particular tag
* name or category (or all children if none is given)
*
* @param $selector tag name or category (optional)
* @return array
*/
final public function getChildren($selector = null) {
if (!$selector) {
return $this->children;
}
$result = array();
if ($selector[0] == '%') {
$selector = substr($selector, 1);
foreach ($this->children as $child) {
if ($child instanceof :xhp && $child->categoryOf($selector)) {
$result[] = $child;
}
}
} else {
$selector = :xhp::element2class($selector);
foreach ($this->children as $child) {
if ($child instanceof $selector) {
$result[] = $child;
}
}
}
return $result;
}
/**
* Fetches the first direct child of the element, or the first child that
* matches the tag if one is given
*
* @param $selector string tag name or category (optional)
* @return element the first child node (with the given selector),
* false if there are no (matching) children
*/
final public function getFirstChild($selector = null) {
if (!$selector) {
return reset($this->children);
} else if ($selector[0] == '%') {
$selector = substr($selector, 1);
foreach ($this->children as $child) {
if ($child instanceof :xhp && $child->categoryOf($selector)) {
return $child;
}
}
} else {
$selector = :xhp::element2class($selector);
foreach ($this->children as $child) {
if ($child instanceof $selector) {
return $child;
}
}
}
return null;
}
/**
* Fetches the last direct child of the element, or the last child that
* matches the tag or category if one is given
*
* @param $selector string tag name or category (optional)
* @return element the last child node (with the given selector),
* false if there are no (matching) children
*/
final public function getLastChild($selector = null) {
$temp = $this->getChildren($selector);
return end($temp);
}
/**
* Returns true if the attribute is a data- or aria- attribute.
*
* @param $attr attribute to fetch
* @return bool
*/
final private static function isAttributeSpecial($attr) {
// Must be at least 6 characters, with a '-' in the 5th position
return
isset($attr[5])
&& $attr[4] == '-'
&& isset(self::$specialAttributes[substr($attr, 0, 4)]);
}
/**
* Fetches an attribute from this elements attribute store. If $attr is not
* defined in the store and is not a data- or aria- attribute an exception
* will be thrown. An exception will also be thrown if $attr is required and
* not set.
*
* @param $attr attribute to fetch
* @return value
*/
final public function getAttribute($attr) {
// Return the attribute if it's there
if (isset($this->attributes[$attr])) {
return $this->attributes[$attr];
}
if (!self::isAttributeSpecial($attr)) {
// Get the declaration
$decl = $this->__xhpAttributeDeclaration();
if (!isset($decl[$attr])) {
throw new XHPAttributeNotSupportedException($this, $attr);
} else if (!empty($decl[$attr][3])) {
throw new XHPAttributeRequiredException($this, $attr);
} else {
return $decl[$attr][2];
}
} else {
return null;
}
}
final public function getAttributes() {
return $this->attributes;
}
/**
* Sets an attribute in this element's attribute store. If the attribute is
* not defined in the store and is not a data- or aria- attribute an
* exception will be thrown. An exception will also be thrown if the
* attribute value is invalid.
*
* @param $attr attribute to set
* @param $val value
*/
final public function setAttribute($attr, $value) {
if (!self::isAttributeSpecial($attr)) {
$this->validateAttributeValue($attr, $value);
} else {
$value = (string)$value;
}
$this->attributes[$attr] = $value;
return $this;
}
/**
* Takes an array of key/value pairs and adds each as an attribute.
*
* @param $attrs array of attributes
*/
final public function setAttributes(array $attrs) {
foreach ($attrs as $key => $value) {
$this->setAttribute($key, $value);
}
return $this;
}
/**
* Whether the attribute has been explicitly set to a non-null value by the
* caller (vs. using the default set by "attribute" in the class definition).
*
* @param $attr attribute to check
*/
final public function isAttributeSet($attr) {
return isset($this->attributes[$attr]);
}
/**
* Removes an attribute from this element's attribute store. An exception
* will be thrown if $attr is not supported.
*
* @param $attr attribute to remove
* @param $val value
*/
final public function removeAttribute($attr) {
if (!self::isAttributeSpecial($attr)) {
$this->validateAttributeValue($attr, $value = null);
}
unset($this->attributes[$attr]);
return $this;
}
/**
* Sets an attribute in this element's attribute store. Always foregoes
* validation.
*
* @param $attr attribute to set
* @param $val value
*/
final public function forceAttribute($attr, $value) {
$this->attributes[$attr] = $value;
return $this;
}
/**
* Returns all contexts currently set.
*
* @return array All contexts
*/
final public function getAllContexts() {
return $this->context;
}
/**
* Returns a specific context value. Can include a default if not set.
*
* @param string $key The context key
* @param mixed $default The value to return if not set (optional)
* @return mixed The context value or $default
*/
final public function getContext($key, $default = null) {
if (isset($this->context[$key])) {
return $this->context[$key];
}
return $default;
}
/**
* Sets a value that will be automatically passed down through a render chain
* and can be referenced by children and composed elements. For instance, if
* a root element sets a context of "admin_mode" = true, then all elements
* that are rendered as children of that root element will receive this
* context WHEN RENDERED. The context will not be available before render.
*
* @param mixed $key Either a key, or an array of key/value pairs
* @param mixed $default if $key is a string, the value to set
* @return :xhp $this
*/
final public function setContext($keyOrArray, $value = null) {
if ($keyOrArray instanceof Traversable) {
$this->context = $keyOrArray + $this->context;
} else {
$this->context[$keyOrArray] = $value;
}
return $this;
}
/**
* Transfers the context but will not overwrite anything. This is done only
* for rendering because we don't want a parent's context to replace a
* child's context if they have the same key.
*
* @param array $parentContext The context to transfer
*/
final private function transferContext(array $parentContext) {
$this->context += $parentContext;
}
final protected function __flushElementChildren() {
// Flush all :xhp elements to x:primitive's
$ln = count($this->children);
for ($ii = 0; $ii < $ln; ++$ii) {
$child = $this->children[$ii];
if ($child instanceof :x:composable-element) {
$child->transferContext($this->context);
}
if ($child instanceof :x:element) {
$child = $child->__flushRenderedRootElement();
if ($child instanceof :x:frag) {
array_splice($this->children, $ii, 1, $child->children);
$ln = count($this->children);
--$ii;
} else {
$this->children[$ii] = $child;
}
}
}
}
final protected function __flushRenderedRootElement() {
$that = $this;
// Flush root elements returned from render() to an :x:primitive
while (($composed = $that->render()) instanceof :x:element) {
if (:xhp::$ENABLE_VALIDATION) {
$composed->validateChildren();
}
$composed->transferContext($that->context);
$that = $composed;
}
if ($composed instanceof :x:composable-element) {
$composed->transferContext($that->context);
} else if (:xhp::$ENABLE_VALIDATION) {
// render() must always return XHPPrimitives
throw new XHPCoreRenderException($this, $that);
}
return $composed;
}
/**
* Defined in elements by the `attribute` keyword. The declaration is simple.
* There is a keyed array, with each key being an attribute. Each value is
* an array with 4 elements. The first is the attribute type. The second is
* meta-data about the attribute. The third is a default value (null for
* none). And the fourth is whether or not this value is required.
*
* Attribute types are suggested by the TYPE_* constants.
*/
protected static function &__xhpAttributeDeclaration() {
static $_ = array();
return $_;
}
/**
* Defined in elements by the `category` keyword. This is just a list of all
* categories an element belongs to. Each category is a key with value 1.
*/
protected function &__xhpCategoryDeclaration() {
static $_ = array();
return $_;
}
/**
* Defined in elements by the `children` keyword. This returns a pattern of
* allowed children. The return value is potentially very complicated. The
* two simplest are 0 and 1 which mean no children and any children,
* respectively. Otherwise you're dealing with an array which is just the
* biggest mess you've ever seen.
*/
protected function &__xhpChildrenDeclaration() {
static $_ = 1;
return $_;
}
/**
* Throws an exception if $val is not a valid value for the attribute $attr
* on this element.
*/
final protected function validateAttributeValue($attr, &$val) {
$decl = $this->__xhpAttributeDeclaration();
if (!isset($decl[$attr])) {
throw new XHPAttributeNotSupportedException($this, $attr);
}
if ($val === null) {
return;
}
switch ($decl[$attr][0]) {
case self::TYPE_STRING:
$val = (string)$val;
return;
case self::TYPE_BOOL:
if (!is_bool($val)) {
if ($val === "false") {
$val = false;
} else {
$val = (bool)$val;
}
}
return;
case self::TYPE_NUMBER:
if (!is_int($val)) {
$val = (int)$val;
}
return;
case self::TYPE_FLOAT:
if (!is_numeric($val)) {
$val = (float)$val;
}
return;
case self::TYPE_CALLABLE:
if (!is_callable($val)) {
throw new XHPInvalidAttributeException($this, 'callable', $attr, $val);
}
return;
case self::TYPE_ARRAY:
if (!is_array($val)) {
throw new XHPInvalidAttributeException($this, 'array', $attr, $val);
}
if ($decl[$attr][1]) {
$this->validateArrayAttributeValue($decl[$attr][1], $attr, $val);
}
return;
case self::TYPE_OBJECT:
if (!($val instanceof $decl[$attr][1])) {
throw new XHPInvalidAttributeException(
$this, $decl[$attr][1], $attr, $val
);
}
return;
// case self::TYPE_VAR: `var` (any type)
case self::TYPE_ENUM:
foreach ($decl[$attr][1] as $enum) {
if ($enum === $val) {
return;
}
}
$enums = 'enum("' . implode('","', $decl[$attr][1]) . '")';
throw new XHPInvalidAttributeException($this, $enums, $attr, $val);
}
}
final private function validateArrayAttributeValue(array $decl, $attr, &$val) {
if ($decl[0]) { // Key declaration
if ($decl[0] == self::TYPE_STRING) {
$type = 'string';
$func = 'is_string';
} else {
$type = 'int';
$func = 'is_int';
}
if (count($val) != count(array_filter(array_keys($val), $func))) {
$bad = $type == 'string' ? 'int' : 'string';
throw new XHPInvalidArrayKeyAttributeException(
$this,
$type,
$attr,
$bad
);
}
}
switch ($decl[1]) { // Value declaration
case self::TYPE_STRING:
$type = 'string';
$func = 'is_string';
break;
case self::TYPE_BOOL:
$type = 'bool';
$func = 'is_bool';
break;
case self::TYPE_NUMBER:
$type = 'int';
$func = 'is_int';
break;
case self::TYPE_FLOAT:
$type = 'float';
$func = 'is_numeric';
break;
case self::TYPE_CALLABLE:
$type = 'callable';
$func = 'is_callable';
return;
case self::TYPE_ARRAY:
$type = 'array';
$func = 'is_array';
break;
case self::TYPE_OBJECT:
$type = $decl[2];
$func = function($item) use ($type) {
return $item instanceof $type;
};
break;
}
$filtered = array_filter($val, $func);
if (count($val) != count($filtered)) {
$bad = array_diff($val, $filtered);
throw new XHPInvalidArrayAttributeException(
$this,
$type,
$attr,
reset($bad)
);
}
if (isset($decl[2]) && $decl[1] == self::TYPE_ARRAY) {
foreach ($val as &$arrayVal) {
$this->validateArrayAttributeValue($decl[2], $attr, $arrayVal);
}
}
}
/**
* Validates that this element's children match its children descriptor, and
* throws an exception if that's not the case.
*/
final protected function validateChildren() {
$decl = $this->__xhpChildrenDeclaration();
if ($decl === 1) { // Any children allowed
return;
}
if ($decl === 0) { // No children allowed
if ($this->children) {
throw new XHPInvalidChildrenException($this, 0);
} else {
return;
}
}
$ii = 0;
if (!$this->validateChildrenExpression($decl, $ii) ||
$ii < count($this->children)) {
throw new XHPInvalidChildrenException($this, $ii);
}
}
final private function validateChildrenExpression($decl, &$index) {
switch ($decl[0]) {
case 0: // Exactly once -- :fb-thing
if ($this->validateChildrenRule($decl[1], $decl[2], $index)) {
return true;
}
return false;
case 1: // Zero or more times -- :fb-thing*
while ($this->validateChildrenRule($decl[1], $decl[2], $index));
return true;
case 2: // Zero or one times -- :fb-thing?
if ($this->validateChildrenRule($decl[1], $decl[2], $index));
return true;
case 3: // One or more times -- :fb-thing+
if (!$this->validateChildrenRule($decl[1], $decl[2], $index)) {
return false;
}
while ($this->validateChildrenRule($decl[1], $decl[2], $index));
return true;
case 4: // Specific order -- :fb-thing, :fb-other-thing
$oindex = $index;
if ($this->validateChildrenExpression($decl[1], $index) &&
$this->validateChildrenExpression($decl[2], $index)) {
return true;
}
$index = $oindex;
return false;
case 5: // Either or -- :fb-thing | :fb-other-thing
if ($this->validateChildrenExpression($decl[1], $index) ||
$this->validateChildrenExpression($decl[2], $index)) {
return true;
}
return false;
}
}
final private function validateChildrenRule($type, $rule, &$index) {
switch ($type) {
case 1: // any element -- any
if (isset($this->children[$index])) {
++$index;
return true;
}
return false;
case 2: // pcdata -- pcdata
if (isset($this->children[$index]) &&
!($this->children[$index] instanceof :xhp)) {
++$index;
return true;
}
return false;
case 3: // specific element -- :fb-thing
if (isset($this->children[$index]) &&
$this->children[$index] instanceof $rule) {
++$index;
return true;
}
return false;
case 4: // element category -- %block
if (!isset($this->children[$index]) ||
!($this->children[$index] instanceof :xhp)) {
return false;
}
$categories = $this->children[$index]->__xhpCategoryDeclaration();
if (empty($categories[$rule])) {
return false;
}
++$index;
return true;
case 5: // nested rule -- ((:fb-thing, :fb-other-thing)*, :fb:thing-footer)
return $this->validateChildrenExpression($rule, $index);
}
}
/**
* Returns the human-readable `children` declaration as seen in this class's
* source code.
*/
final public function __getChildrenDeclaration() {
$decl = $this->__xhpChildrenDeclaration();
if ($decl === 1) {
return 'any';
}
if ($decl === 0) {
return 'empty';
}
return $this->renderChildrenDeclaration($decl);
}
final private function renderChildrenDeclaration($decl) {
switch ($decl[0]) {
case 0:
return $this->renderChildrenRule($decl[1], $decl[2]);
case 1:
return $this->renderChildrenRule($decl[1], $decl[2]) . '*';
case 2:
return $this->renderChildrenRule($decl[1], $decl[2]) . '?';
case 3:
return $this->renderChildrenRule($decl[1], $decl[2]) . '+';
case 4:
return $this->renderChildrenDeclaration($decl[1]) . ',' .
$this->renderChildrenDeclaration($decl[2]);
case 5:
return $this->renderChildrenDeclaration($decl[1]) . '|' .
$this->renderChildrenDeclaration($decl[2]);
}
}
final private function renderChildrenRule($type, $rule) {
switch ($type) {
case 1:
return 'any';
case 2:
return 'pcdata';
case 3:
return ':' . :xhp::class2element($rule);
case 4:
return '%' . $rule;
case 5:
return '(' . $this->renderChildrenDeclaration($rule) . ')';
}
}
/**
* Returns a description of the current children in this element. Maybe
* something like this:
* <div><span>foo</span>bar</div> ->
* :span[%inline],pcdata
*/
final public function __getChildrenDescription() {
$desc = array();
foreach ($this->children as $child) {
if ($child instanceof :xhp) {
$tmp = ':' . :xhp::class2element(get_class($child));
if ($categories = $child->__xhpCategoryDeclaration()) {
$tmp .= '[%'. implode(',%', array_keys($categories)) . ']';
}
$desc[] = $tmp;
} else {
$desc[] = 'pcdata';
}
}
return implode(',', $desc);
}
final public function categoryOf($c) {
$categories = $this->__xhpCategoryDeclaration();
if (isset($categories[$c])) {
return true;
}
// XHP parses the category string
$c = str_replace(array(':', '-'), array('__', '_'), $c);
return isset($categories[$c]);
}
}
/**
* :x:primitive lays down the foundation for very low-level elements. You
* should directly :x:primitive only if you are creating a core element that
* needs to directly implement stringify(). All other elements should subclass
* from :x:element.
*/
abstract class :x:primitive extends :x:composable-element {
abstract protected function stringify();
final public function __toString() {
try {
// Validate our children
$this->__flushElementChildren();
if (:xhp::$ENABLE_VALIDATION) {
$this->validateChildren();
}
// Render to string
return $this->stringify();
} catch (Exception $error) {
trigger_error($error->getMessage(), E_USER_ERROR);
}
}
}
/**
* :x:element defines an interface that all user-land elements should subclass
* from. The main difference between :x:element and :x:primitive is that
* subclasses of :x:element should implement `render()` instead of `stringify`.
* This is important because most elements should not be dealing with strings
* of markup.
*/
abstract class :x:element extends :x:composable-element {
final public function __toString() {
$that = $this;
try {
if (:xhp::$ENABLE_VALIDATION) {
$that->validateChildren();
}
$that = $that->__flushRenderedRootElement();
return $that->__toString();
} catch (Exception $error) {
trigger_error($error->getMessage(), E_USER_ERROR);
}
}
}
/**
* An <x:frag /> is a transparent wrapper around any number of elements. When
* you render it just the children will be rendered. When you append it to an
* element the <x:frag /> will disappear and each child will be sequentially
* appended to the element.
*/
class :x:frag extends :x:primitive {
protected function stringify() {
$buf = '';
foreach ($this->getChildren() as $child) {
$buf .= :xhp::renderChild($child);
}
return $buf;
}
}
/**
* Exceptions are neat.
*/
class XHPException extends Exception {
protected static function getElementName($that) {
$name = get_class($that);
if (substr($name, 0, 4) !== 'xhp_') {
return $name;
} else {
return :xhp::class2element($name);
}
}
}
class XHPClassException extends XHPException {
public function __construct($that, $msg) {
parent::__construct(
'Exception in class `' . XHPException::getElementName($that) . "`\n\n".
"$that->source\n\n".
$msg
);
}
}
class XHPCoreRenderException extends XHPException {
public function __construct($that, $rend) {
parent::__construct(
':x:element::render must reduce an object to an :x:primitive, but `'.
:xhp::class2element(get_class($that)).'` reduced into `'.gettype($rend)."`.\n\n".
$that->source
);
}
}
class XHPRenderArrayException extends XHPException {
}
class XHPInvalidArrayAttributeException extends XHPException {
public function __construct($that, $type, $attr, $val) {
if (is_object($val)) {
$val_type = get_class($val);
} else {
$val_type = gettype($val);
}
parent::__construct(
"Invalid attribute `$attr` of type array<`$val_type`> supplied to element `".
:xhp::class2element(get_class($that))."`, expected array<`$type`>.\n\n".
$that->source
);
}
}
class XHPInvalidArrayKeyAttributeException extends XHPException {
public function __construct($that, $type, $attr, $val_type) {
parent::__construct(
"Invalid key in attribute `$attr` of type array<$val_type => ?> supplied to element `".
:xhp::class2element(get_class($that))."`, expected array<$type => ?>.\n\n".
$that->source
);
}
}
class XHPAttributeNotSupportedException extends XHPException {
public function __construct($that, $attr) {
parent::__construct(
'Attribute "'.$attr.'" is not supported in class '.
'"'.XHPException::getElementName($that).'"'.
"\n\n".$that->source."\n\n".
'Please check for typos in your attribute. If you are creating a new '.
'attribute on this element define it with the "attribute" keyword'."\n\n"
);