This repository has been 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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatically call transferAttributes as needed.
- split out XHPBaseHTMLHelpers to just implement the ID and class helpers without transferAttributes - added tests - XHPHelpers should probably be renamed to XHPHTMLHelpers in XHP-Lib 3, and the bulk of the transfer/copyattribtues functiontionality split to XHPTransferAttributes or something like that. Keeping like this for now to reduce BC breakage - Allows implementation of non-HTML helpers; until the above modification happens this will involve copypasta, but better than the current situation. - I /think/ this gets rid of the render()/compose() pattern. fixes #130 closes #133
- Loading branch information
1 parent
d7b472f
commit 3923d04
Showing
8 changed files
with
287 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?hh // strict | ||
/* | ||
* Copyright (c) 2015, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
*/ | ||
|
||
/** | ||
* Indicates that any attributes set on an element should be transferred to the | ||
* element returned from ::render() or ::asyncRender(). This is automatically | ||
* invoked by :x:element. | ||
*/ | ||
interface XHPHasTransferAttributes { | ||
require extends :x:element; | ||
public function transferAttributesToRenderedRoot( | ||
:x:composable-element $root, | ||
): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?hh // strict | ||
/* | ||
* Copyright (c) 2015, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
*/ | ||
|
||
interface HasXHPBaseHTMLHelpers { | ||
require extends :x:composable-element; | ||
|
||
public function addClass(string $class): this; | ||
public function conditionClass(bool $cond, string $class): this; | ||
public function requireUniqueID(): string; | ||
public function getID(): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?hh // strict | ||
|
||
/* | ||
* Copyright (c) 2015, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
*/ | ||
|
||
trait XHPBaseHTMLHelpers implements HasXHPBaseHTMLHelpers { | ||
require extends :x:composable-element; | ||
|
||
/* | ||
* Appends a string to the "class" attribute (space separated). | ||
*/ | ||
public function addClass(string $class): this { | ||
try { | ||
$current_class = /* UNSAFE_EXPR */ $this->:class; | ||
return $this->setAttribute('class', trim($current_class.' '.$class)); | ||
} catch (XHPInvalidAttributeException $error) { | ||
throw new XHPException( | ||
'You are trying to add an HTML class to a(n) '. | ||
:xhp::class2element(static::class).' element, but it does not support '. | ||
'the "class" attribute. The best way to do this is to inherit '. | ||
'the HTML attributes from the element your component will render into.', | ||
); | ||
} | ||
} | ||
|
||
/* | ||
* Conditionally adds a class to the "class" attribute. | ||
*/ | ||
public function conditionClass(bool $cond, string $class): this { | ||
return $cond ? $this->addClass($class) : $this; | ||
} | ||
|
||
/* | ||
* Generates a unique ID (and sets it) on the "id" attribute. A unique ID | ||
* will only be generated if one has not already been set. | ||
*/ | ||
public function requireUniqueID(): string { | ||
$id = /* UNSAFE_EXPR */ $this->:id; | ||
if ($id === null || $id === '') { | ||
try { | ||
$this->setAttribute('id', $id = substr(md5(mt_rand(0, 100000)), 0, 10)); | ||
} catch (XHPInvalidAttributeException $error) { | ||
throw new XHPException( | ||
'You are trying to add an HTML id to a(n) '. | ||
:xhp::class2element(static::class).' element, but it does not '. | ||
'support the "id" attribute. The best way to do this is to inherit '. | ||
'the HTML attributes from the element your component will render '. | ||
'into.', | ||
); | ||
} | ||
} | ||
return (string)$id; | ||
} | ||
|
||
/* | ||
* Fetches the "id" attribute, will generate a unique value if not set. | ||
*/ | ||
final public function getID(): string { | ||
return $this->requireUniqueID(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.