This repository was archived by the owner on Apr 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathNode.hack
More file actions
824 lines (758 loc) · 24.9 KB
/
Node.hack
File metadata and controls
824 lines (758 loc) · 24.9 KB
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
/*
* Copyright (c) 2004-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
namespace Facebook\XHP\Core;
use namespace HH\Lib\{C, Dict, Keyset, Str, Vec};
use type Facebook\XHP\{
ReflectionXHPAttribute,
ReflectionXHPChildrenDeclaration,
ReflectionXHPChildrenExpression,
XHPChildrenConstraintType,
XHPChildrenDeclarationType,
XHPChildrenExpressionType,
};
<<__Sealed(primitive::class, element::class)>>
abstract xhp class node implements \XHPChild {
// Must be kept in sync with compiler
const string SPREAD_PREFIX = '...$';
protected bool $__isRendered = false;
private dict<string, mixed> $attributes = dict[];
private vec<\XHPChild> $children = vec[];
private dict<string, mixed> $context = dict[];
public ?string $source;
protected function init(): void {
}
/**
* A new :x:node 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
* @param $debug_info will in the source when childValidation is enabled
*/
final public function __construct(
KeyedTraversable<string, mixed> $attributes,
Traversable<?\XHPChild> $children,
dynamic ...$debug_info
) {
invariant(
$this->__xhpChildrenDeclaration() ===
self::__NO_LEGACY_CHILDREN_DECLARATION,
'The `children` keyword is no longer supported',
);
invariant(
$this->__xhpCategoryDeclaration() ===
self::__NO_LEGACY_CATEGORY_DECLARATION,
'The `category` keyword is no longer supported',
);
foreach ($children as $child) {
$this->appendChild($child);
}
foreach ($attributes as $key => $value) {
if (self::isSpreadKey($key)) {
invariant(
$value is node,
'Only XHP can be used with an attribute spread operator',
);
$this->spreadElementImpl($value);
} else {
$this->setAttribute($key, $value);
}
}
if (\Facebook\XHP\ChildValidation\is_enabled()) {
if (C\count($debug_info) >= 2) {
$this->source = (string)$debug_info[0].':'.(string)$debug_info[1];
} else {
$this->source =
'You have child validation on, but debug information is not being '.
'passed to XHP objects correctly. Ensure xhp.include_debug is on '.
'in your server configuration. Without this option enabled, '.
'validation errors will be painful to debug at best.';
}
}
$this->init();
}
abstract public function toStringAsync(): Awaitable<string>;
/**
* Adds a child to the end of this node. If you give a Traversable to this method
* then it will behave like a DocumentFragment.
*
* @param $child single child or a Traversable of children
* @throws UseAfterRenderException
*/
final public function appendChild(mixed $child): this {
if ($this->__isRendered) {
throw new UseAfterRenderException(
Str\format("Can't %s after render", __FUNCTION__),
);
}
if ($child is Traversable<_>) {
foreach ($child as $c) {
$this->appendChild($c);
}
} else if ($child is frag) {
foreach ($child->getChildren() as $new_child) {
$this->children[] = $new_child;
}
} else if ($child !== null) {
$this->children[] = $child as \XHPChild;
}
return $this;
}
/**
* Replaces all children in this node.
*
* @param $children single child or a Traversable of children
* @throws UseAfterRenderException
*/
final public function replaceChildren(\XHPChild ...$children): this {
if ($this->__isRendered) {
throw new UseAfterRenderException(
Str\format("Can't %s after render", __FUNCTION__),
);
}
// This function has been micro-optimized
$new_children = vec[];
foreach ($children as $xhp) {
if ($xhp is frag) {
foreach ($xhp->children as $child) {
$new_children[] = $child;
}
} else if (!($xhp is Traversable<_>)) {
$new_children[] = $xhp;
} else {
foreach ($xhp as $element) {
if ($element is frag) {
foreach ($element->children as $child) {
$new_children[] = $child;
}
} else if ($element !== null) {
$new_children[] = $element as \XHPChild;
}
}
}
}
$this->children = $new_children;
return $this;
}
/**
* Fetches all direct children of this element.
*/
final public function getChildren(): vec<\XHPChild> {
return $this->children;
}
/**
* Fetches all direct children of this element of the given type.
*/
public function getChildrenOfType<<<__Enforceable>> reify T as \XHPChild>(
): vec<T> {
$children = vec[];
foreach ($this->children as $child) {
if ($child is T) {
$children[] = $child;
}
}
return $children;
}
/**
* Fetches the first direct child of the element, if any.
*/
final public function getFirstChild(): ?\XHPChild {
return $this->children[0] ?? null;
}
/**
* Fetch the first direct child of the element.
*
* An exception is thrown if the element has no children.
*/
public function getFirstChildx(): \XHPChild {
$child = $this->getFirstChild();
if ($child is nonnull) {
return $child;
}
invariant_violation('%s called on element with no children', __FUNCTION__);
}
/**
* Fetch the first direct child of a given type, if any.
*
* If no matching child is present, returns `null`.
*/
public function getFirstChildOfType<<<__Enforceable>> reify T as \XHPChild>(
): ?T {
foreach ($this->children as $child) {
if ($child is T) {
return $child;
}
}
return null;
}
/**
* Fetch the first direct child of a given type.
*
* If no matching child is present, an exception is thrown.
*/
public function getFirstChildOfTypex<<<__Enforceable>> reify T as \XHPChild>(
): T {
$child = $this->getFirstChildOfType<T>();
invariant(
$child is nonnull,
'%s called with no matching child',
__FUNCTION__,
);
return $child;
}
/**
* Fetches the last direct child of the element, if any.
*
* If the element has no children, `null` is returned.
*/
final public function getLastChild(): ?\XHPChild {
return C\last($this->getChildren());
}
/**
* Fetches the last direct child of the element.
*
* If the element has no children, an exception is thrown.
*/
public function getLastChildx(): \XHPChild {
$child = $this->getLastChild();
if ($child is nonnull) {
return $child;
}
invariant_violation('%s called on element with no children', __FUNCTION__);
}
/**
* Fetch the last direct child of the element of a given type, if any.
*
* If the element has no matching children, `null` is returned.
*/
public function getLastChildOfType<<<__Enforceable>> reify T as \XHPChild>(
): ?T {
for ($i = C\count($this->children) - 1; $i >= 0; --$i) {
$child = $this->children[$i];
if ($child is T) {
return $child;
}
}
return null;
}
/**
* Fetch the last direct child of the element of a given type.
*
* If the element has no matching children, an exception is thrown.
*/
public function getLastChildOfTypex<<<__Enforceable>> reify T as \XHPChild>(
): T {
$child = $this->getLastChildOfType<T>();
invariant(
$child is nonnull,
'%s called with no matching child',
__FUNCTION__,
);
return $child;
}
/**
* 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(string $attr): mixed {
// Return the attribute if it's there
if (C\contains_key($this->attributes, $attr)) {
return $this->attributes[$attr];
}
// Get the declaration
$decl = static::__xhpReflectionAttribute($attr);
if ($decl === null) {
if (!ReflectionXHPAttribute::isSpecial($attr)) {
throw new \Facebook\XHP\AttributeNotSupportedException($this, $attr);
}
} else if ($decl->isRequired()) {
throw new \Facebook\XHP\AttributeRequiredException($this, $attr);
} else {
return $decl->getDefaultValue();
}
return null;
}
final public static function __xhpReflectionAttribute(
string $attr,
): ?ReflectionXHPAttribute {
return static::__xhpReflectionAttributes()[$attr] ?? null;
}
<<__MemoizeLSB>>
final public static function __xhpReflectionAttributes(
): dict<string, ReflectionXHPAttribute> {
$decl = static::__xhpAttributeDeclaration();
return Dict\map_with_key(
$decl,
($name, $attr_decl) ==> new ReflectionXHPAttribute($name, $attr_decl),
);
}
protected static function __legacySerializedXHPChildrenDeclaration(): mixed {
invariant(
self::emptyInstance()->__xhpChildrenDeclaration() ===
self::__NO_LEGACY_CHILDREN_DECLARATION,
'Legacy XHP children declaration syntax is no longer supported',
);
return 1; // any children
}
<<__MemoizeLSB>>
final public static function __xhpReflectionChildrenDeclaration(
): ReflectionXHPChildrenDeclaration {
return new ReflectionXHPChildrenDeclaration(
static::class,
static::__legacySerializedXHPChildrenDeclaration(),
);
}
final public static function __xhpReflectionCategoryDeclaration(
): keyset<string> {
return Keyset\keys(self::emptyInstance()->__xhpCategoryDeclaration());
}
// Work-around to call methods that should be static without a real
// instance.
<<__MemoizeLSB>>
private static function emptyInstance(): this {
return
(new \ReflectionClass(static::class))->newInstanceWithoutConstructor();
}
final public function getAttributes(): dict<string, mixed> {
return $this->attributes;
}
/**
* Determines if a given XHP attribute "key" represents an XHP spread operator
* in the constructor.
*/
private static function isSpreadKey(string $key): bool {
return Str\starts_with($key, self::SPREAD_PREFIX);
}
/**
* Implements the XHP spread operator in expressions like:
* <foo attr1="bar" {...$xhp} />
*
* This will only copy defined attributes on $xhp to when they are also
* defined on $this, or if they are "special" data-/aria- attributes.
*
* Defaults from $xhp are copied as well, if they are present.
*/
protected final function spreadElementImpl(node $element): void {
$attrs = $element::__xhpReflectionAttributes()
|> Dict\filter($$, $attr ==> $attr->hasDefaultValue())
|> Dict\map($$, $attr ==> $attr->getDefaultValue())
|> Dict\merge($$, $element->getAttributes());
foreach ($attrs as $attr_name => $value) {
if (
$value === null ||
static::__xhpReflectionAttribute($attr_name) === null &&
!ReflectionXHPAttribute::isSpecial($attr_name)
) {
continue;
}
// If the receiving class has the same attribute and we had a value or
// a default, then copy it over.
$this->setAttribute($attr_name, $value);
}
}
/**
* Sets an attribute in this element's attribute store.
*
* @param $attr attribute to set
* @param $val value
* @throws UseAfterRenderException
*/
final public function setAttribute(string $attr, mixed $value): this {
if ($this->__isRendered) {
throw new UseAfterRenderException(
Str\format("Can't %s after render", __FUNCTION__),
);
}
$this->attributes[$attr] = $value;
return $this;
}
/**
* Takes a KeyedContainer and adds each as an attribute.
*
* @param $attrs KeyedContainer of attributes
*/
final public function setAttributes(
KeyedTraversable<string, mixed> $attrs,
): this {
foreach ($attrs as $key => $value) {
$this->setAttribute($key, $value);
}
return $this;
}
/**
* Whether the attribute has been explicitly set in either the XhpOpenTag
* or using setAttribute(). An attribute with a default value is not
* automatically considered set. Explicitly setting a value, which may be
* the same as the default value, will cause this method to return true.
*
* @param $attr attribute to check
*/
final public function isAttributeSet(string $attr): bool {
return C\contains_key($this->attributes, $attr);
}
/**
* Removes an attribute from this element's attribute store.
*
* @param $attr attribute to remove
* @throws UseAfterRenderException
*/
final public function removeAttribute(string $attr): this {
if ($this->__isRendered) {
throw new UseAfterRenderException(
Str\format("Can't %s after render", __FUNCTION__),
);
}
unset($this->attributes[$attr]);
return $this;
}
/**
* @deprecated This functionality will be removed in a future release.
*
* This has not yet been removed as it is currently the only way to
* set an `UnsafeAttributeValue_DEPRECATED`.
*
* Sets an attribute in this element's attribute store. Always foregoes
* validation.
*
* @param $attr attribute to set
* @param $val value
* @throws UseAfterRenderException
*/
final public function forceAttribute_DEPRECATED(
string $attr,
mixed $value,
): this {
if ($this->__isRendered) {
throw new UseAfterRenderException(
Str\format("Can't %s after render", __FUNCTION__),
);
}
$this->attributes[$attr] = $value;
return $this;
}
/**
* Returns all contexts currently set.
*
* @return All contexts
*/
final public function getAllContexts(): dict<string, mixed> {
return $this->context;
}
/**
* Returns a specific context value. Can include a default if not set.
*
* @param $key The context key
* @param $default The value to return if not set (optional)
* @return The context value or $default
*/
final public function getContext(string $key, mixed $default = null): mixed {
// You can't use ?? here, since the context may contain nulls.
if (C\contains_key($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 $key The key
* @param $default The value to set
* @return $this
* @throws UseAfterRenderException
*/
final public function setContext(string $key, mixed $value): this {
if ($this->__isRendered) {
throw new UseAfterRenderException(
Str\format("Can't %s after render", __FUNCTION__),
);
}
$this->context[$key] = $value;
return $this;
}
/**
* 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 KeyedContainer $context A map of key/value pairs
* @return $this
* @throws UseAfterRenderException
*/
final public function addContextMap(
KeyedContainer<string, mixed> $context,
): this {
if ($this->__isRendered) {
throw new UseAfterRenderException(
Str\format("Can't %s after render", __FUNCTION__),
);
}
$this->context = Dict\merge($this->context, $context);
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 $parentContext The context to transfer
*/
final protected function __transferContext(
KeyedContainer<string, mixed> $parentContext,
): void {
foreach ($parentContext as $key => $value) {
// You can't use ??= here, since context may contain nulls.
if (!C\contains_key($this->context, $key)) {
$this->context[$key] = $value;
}
}
}
abstract protected function __flushSubtree(): Awaitable<primitive>;
/**
* 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(
): dict<string, vec<mixed>> {
return dict[];
}
/**
* 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(): dict<string, int> {
return self::__NO_LEGACY_CATEGORY_DECLARATION;
}
const int __NO_LEGACY_CHILDREN_DECLARATION = -31337;
const dict<string, int> __NO_LEGACY_CATEGORY_DECLARATION =
dict["\0INVALID\0" => 0];
/**
* 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(): mixed {
return self::__NO_LEGACY_CHILDREN_DECLARATION;
}
/**
* Validates that this element's children match its children descriptor, and
* throws an exception if that's not the case.
*/
protected function validateChildren(): void {
$decl = self::__xhpReflectionChildrenDeclaration();
$type = $decl->getType();
if ($type === XHPChildrenDeclarationType::ANY_CHILDREN) {
return;
}
if ($type === XHPChildrenDeclarationType::NO_CHILDREN) {
if ($this->children) {
throw new \Facebook\XHP\InvalidChildrenException($this, 0);
} else {
return;
}
}
list($ret, $ii) =
$this->validateChildrenExpression($decl->getExpression(), 0);
if (!$ret || $ii < C\count($this->children)) {
if (($this->children[$ii] ?? null) is \Facebook\XHP\AlwaysValidChild) {
return;
}
throw new \Facebook\XHP\InvalidChildrenException($this, $ii);
}
}
private function validateChildrenExpression(
ReflectionXHPChildrenExpression $expr,
int $index,
): (bool, int) {
switch ($expr->getType()) {
case XHPChildrenExpressionType::SINGLE:
// Exactly once -- :fb_thing
return $this->validateChildrenRule($expr, $index);
case XHPChildrenExpressionType::ANY_NUMBER:
// Zero or more times -- :fb_thing*
do {
list($ret, $index) = $this->validateChildrenRule($expr, $index);
} while ($ret);
return tuple(true, $index);
case XHPChildrenExpressionType::ZERO_OR_ONE:
// Zero or one times -- :fb_thing?
list($_, $index) = $this->validateChildrenRule($expr, $index);
return tuple(true, $index);
case XHPChildrenExpressionType::ONE_OR_MORE:
// One or more times -- :fb_thing+
list($ret, $index) = $this->validateChildrenRule($expr, $index);
if (!$ret) {
return tuple(false, $index);
}
do {
list($ret, $index) = $this->validateChildrenRule($expr, $index);
} while ($ret);
return tuple(true, $index);
case XHPChildrenExpressionType::SUB_EXPR_SEQUENCE:
// Specific order -- :fb_thing, :fb_other_thing
$oindex = $index;
list($sub_expr_1, $sub_expr_2) = $expr->getSubExpressions();
list($ret, $index) =
$this->validateChildrenExpression($sub_expr_1, $index);
if ($ret) {
list($ret, $index) =
$this->validateChildrenExpression($sub_expr_2, $index);
}
if ($ret) {
return tuple(true, $index);
}
return tuple(false, $oindex);
case XHPChildrenExpressionType::SUB_EXPR_DISJUNCTION:
// Either or -- :fb_thing | :fb_other_thing
$oindex = $index;
list($sub_expr_1, $sub_expr_2) = $expr->getSubExpressions();
list($ret, $index) =
$this->validateChildrenExpression($sub_expr_1, $index);
if (!$ret) {
list($ret, $index) =
$this->validateChildrenExpression($sub_expr_2, $index);
}
if ($ret) {
return tuple(true, $index);
}
return tuple(false, $oindex);
}
}
private function validateChildrenRule(
ReflectionXHPChildrenExpression $expr,
int $index,
): (bool, int) {
switch ($expr->getConstraintType()) {
case XHPChildrenConstraintType::ANY:
if (C\contains_key($this->children, $index)) {
return tuple(true, $index + 1);
}
return tuple(false, $index);
case XHPChildrenConstraintType::PCDATA:
if (
C\contains_key($this->children, $index) &&
!($this->children[$index] is node)
) {
return tuple(true, $index + 1);
}
return tuple(false, $index);
case XHPChildrenConstraintType::ELEMENT:
$class = $expr->getConstraintString();
if (
C\contains_key($this->children, $index) &&
\is_a($this->children[$index], $class, true)
) {
return tuple(true, $index + 1);
}
return tuple(false, $index);
case XHPChildrenConstraintType::CATEGORY:
if (!C\contains_key($this->children, $index)) {
return tuple(false, $index);
}
$child = $this->children[$index];
if (!$child is node) {
return tuple(false, $index);
}
$category = $expr->getConstraintString()
|> Str\replace($$, '__', ':')
|> Str\replace($$, '_', '-');
$categories = $child->__xhpCategoryDeclaration();
if (($categories[$category] ?? 0) === 0) {
return tuple(false, $index);
}
return tuple(true, $index + 1);
case XHPChildrenConstraintType::SUB_EXPR:
return
$this->validateChildrenExpression($expr->getSubExpression(), $index);
}
}
/**
* Returns the human-readable `children` declaration as seen in this class's
* source code.
*
* Keeping this wrapper around reflection, as it fits well with
* __getChildrenDescription.
*/
public function __getChildrenDeclaration(): string {
return self::__xhpReflectionChildrenDeclaration()->__toString();
}
/**
* 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(): string {
$desc = vec[];
foreach ($this->children as $child) {
if ($child is node) {
$tmp = '\\'.\get_class($child);
$categories = $child->__xhpCategoryDeclaration();
if (C\count($categories) > 0) {
$tmp .= '[%'.Str\join(Vec\keys($categories), ',%').']';
}
$desc[] = $tmp;
} else {
$desc[] = 'pcdata';
}
}
return Str\join($desc, ',');
}
final public function __getSourcePositionWithErrorDefaultForNull(): string {
return $this->source ?? '!!! SOURCE POSITION NOT KNOWN !!!';
}
final public function categoryOf(string $c): bool {
$categories = $this->__xhpCategoryDeclaration();
if ($categories[$c] ?? null !== null) {
return true;
}
// XHP parses the category string
$c = \str_replace(vec[':', '-'], vec['__', '_'], $c);
return ($categories[$c] ?? null) !== null;
}
final protected static async function renderChildAsync(
\XHPChild $child,
): Awaitable<string> {
if ($child is node) {
return await $child->toStringAsync();
}
if ($child is \Facebook\XHP\UnsafeRenderable) {
return await $child->toHTMLStringAsync();
}
if ($child is Traversable<_>) {
throw
new \Facebook\XHP\RenderArrayException('Can not render traversables!');
}
/* HH_FIXME[4281] stringish migration */
return \htmlspecialchars((string)$child);
}
}