Skip to content
This repository was archived by the owner on Apr 1, 2024. It is now read-only.

Commit aff764e

Browse files
committed
Disable attribute validation by default
1 parent 9d64497 commit aff764e

File tree

8 files changed

+52
-8
lines changed

8 files changed

+52
-8
lines changed

src/core/ComposableElement.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ final public function __construct(
4242
$this->appendChild($child);
4343
}
4444
$this->setAttributes($attributes);
45-
if (:xhp::$ENABLE_VALIDATION) {
45+
if (:xhp::isChildValidationEnabled()) {
4646
// There is some cost to having defaulted unused arguments on a function
4747
// so we leave these out and get them with func_get_args().
4848
$args = func_get_args();
4949
if (isset($args[2])) {
5050
$this->source = "$args[2]:$args[3]";
5151
} else {
5252
$this->source =
53-
'You have ENABLE_VALIDATION on, but debug information is not being '.
53+
'You have child validation on, but debug information is not being '.
5454
'passed to XHP objects correctly. Ensure xhp.include_debug is on '.
5555
'in your PHP configuration. Without this option enabled, '.
5656
'validation errors will be painful to debug at best.';
@@ -304,7 +304,9 @@ final public function getAttributes(): Map<string, mixed> {
304304
*/
305305
final public function setAttribute(string $attr, mixed $value): this {
306306
if (!ReflectionXHPAttribute::IsSpecial($attr)) {
307-
$value = $this->validateAttributeValue($attr, $value);
307+
if (:xhp::isAttributeValidationEnabled()) {
308+
$value = $this->validateAttributeValue($attr, $value);
309+
}
308310
} else {
309311
$value = (string)$value;
310312
}
@@ -345,7 +347,9 @@ final public function isAttributeSet(string $attr): bool {
345347
*/
346348
final public function removeAttribute(string $attr): this {
347349
if (!ReflectionXHPAttribute::IsSpecial($attr)) {
348-
$value = $this->validateAttributeValue($attr, null);
350+
if (:xhp::isAttributeValidationEnabled()) {
351+
$value = $this->validateAttributeValue($attr, null);
352+
}
349353
}
350354
$this->attributes->removeKey($attr);
351355
return $this;

src/core/Element.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ final public function toString(): string {
3535
}
3636

3737
protected async function __renderAndProcess(): Awaitable<XHPRoot> {
38-
if (:xhp::$ENABLE_VALIDATION) {
38+
if (:xhp::isChildValidationEnabled()) {
3939
$this->validateChildren();
4040
}
4141

src/core/Primitive.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ final public function toString(): string {
4747

4848
final protected async function __flushSubtree(): Awaitable<:x:primitive> {
4949
await $this->__flushElementChildren();
50-
if (:xhp::$ENABLE_VALIDATION) {
50+
if (:xhp::isChildValidationEnabled()) {
5151
$this->validateChildren();
5252
}
5353
return $this;

src/core/XHP.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,32 @@ protected static function &__xhpAttributeDeclaration(
5252
* production. You should still leave it on while developing new features,
5353
* though.
5454
*/
55-
public static bool $ENABLE_VALIDATION = true;
55+
private static bool $validateChildren = true;
56+
private static bool $validateAttributes = false;
57+
58+
public static function disableChildValidation(): void {
59+
self::$validateChildren = false;
60+
}
61+
62+
public static function enableChildValidation(): void {
63+
self::$validateChildren = true;
64+
}
65+
66+
public static function isChildValidationEnabled(): bool {
67+
return self::$validateChildren;
68+
}
69+
70+
public static function disableAttributeValidation(): void {
71+
self::$validateAttributes = false;
72+
}
73+
74+
public static function enableAttributeValidation(): void {
75+
self::$validateAttributes = true;
76+
}
77+
78+
public static function isAttributeValidationEnabled(): bool {
79+
return self::$validateAttributes;
80+
}
5681

5782
final public function __toString(): string {
5883
return $this->toString();

src/html/XHPHelpers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ protected function getAttributeNamesThatAppendValuesOnTransfer(
146146
final public function transferAttributesToRenderedRoot(
147147
:x:composable-element $root,
148148
): void {
149-
if (:xhp::$ENABLE_VALIDATION && $root instanceof :x:element) {
149+
if (:xhp::isAttributeValidationEnabled() && $root instanceof :x:element) {
150150
if (!($root instanceof HasXHPHelpers)) {
151151
throw new XHPClassException(
152152
$this,

tests/AttributesCoercionModeTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ class AttributesCoercionModeTest extends PHPUnit_Framework_TestCase {
2121
public function setUp(): void {
2222
$this->coercionMode = XHPAttributeCoercion::GetMode();
2323
$this->errorReporting = error_reporting();
24+
:xhp::enableAttributeValidation();
2425
}
2526

2627
public function tearDown(): void {
2728
$mode = $this->coercionMode;
2829
invariant($mode !== null, 'did not save coercion mode');
2930
XHPAttributeCoercion::SetMode($mode);
3031
error_reporting($this->errorReporting);
32+
:xhp::disableAttributeValidation();
3133
}
3234

3335
public function testNoCoercion(): void {

tests/AttributesTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ public function __toString() {
6060
class AttributesTest extends PHPUnit_Framework_TestCase {
6161
public function setUp(): void {
6262
XHPAttributeCoercion::SetMode(XHPAttributeCoercionMode::SILENT);
63+
:xhp::enableAttributeValidation();
64+
}
65+
66+
public function tearDown(): void {
67+
:xhp::disableAttributeValidation();
6368
}
6469

6570
public function testValidTypes(): void {

tests/HackEnumAttributeTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ protected function render(): XHPRoot {
1414
}
1515

1616
class HackEnumAttributesTest extends PHPUnit_Framework_TestCase {
17+
public function setUp(): void {
18+
:xhp::enableAttributeValidation();
19+
}
20+
21+
public function tearDown(): void {
22+
:xhp::disableAttributeValidation();
23+
}
24+
1725
public function testValidValues(): void {
1826
$x = <test:hack-enum-attribute foo={TestEnum::HERP} />;
1927
$this->assertSame('<div>HERP</div>', $x->toString());

0 commit comments

Comments
 (0)