Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4b80b885e0 | ||
|
|
e2b9fc0392 |
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "htmx",
|
||||
"version": "0.1.1",
|
||||
"version": "0.2.0",
|
||||
"description": "Motor de vistas basado en componentes y HTMX",
|
||||
"core": {
|
||||
"requires": ">=0.1",
|
||||
|
||||
@@ -13,29 +13,6 @@ namespace Libs;
|
||||
*/
|
||||
class HTMX extends HTMLComponent
|
||||
{
|
||||
/**
|
||||
* Valid HTMX swap styles.
|
||||
*/
|
||||
public const SWAP_STYLES = [
|
||||
'innerHTML', 'outerHTML', 'outerSync',
|
||||
'beforebegin', 'before',
|
||||
'afterbegin', 'prepend',
|
||||
'beforeend', 'append',
|
||||
'afterend', 'after',
|
||||
'delete', 'none',
|
||||
'innerMorph', 'outerMorph',
|
||||
'textContent',
|
||||
];
|
||||
|
||||
/**
|
||||
* Valid HTMX swap option keys (used as key:value after the style).
|
||||
*/
|
||||
public const SWAP_OPTIONS = [
|
||||
'swap', 'settle', 'transition', 'ignoreTitle', 'strip',
|
||||
'focusScroll', 'swapEmpty', 'scroll', 'show', 'showTarget',
|
||||
'scrollTarget', 'target',
|
||||
];
|
||||
|
||||
/**
|
||||
* Renders a component
|
||||
*
|
||||
@@ -266,8 +243,6 @@ class HTMX extends HTMLComponent
|
||||
* @param bool $excludeBoosted Exclude if the request is boosted (default: false)
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @throws \InvalidArgumentException On an unknown style or option.
|
||||
*/
|
||||
public static function swapOob(string $value = 'true', bool $excludeBoosted = false): string
|
||||
{
|
||||
@@ -275,10 +250,6 @@ class HTMX extends HTMLComponent
|
||||
return '';
|
||||
}
|
||||
|
||||
if ($value !== 'true') {
|
||||
$value = static::validateSwap($value);
|
||||
}
|
||||
|
||||
return static::isHtmx() ? 'hx-swap-oob="' . $value . '"' : '';
|
||||
}
|
||||
|
||||
@@ -318,8 +289,6 @@ class HTMX extends HTMLComponent
|
||||
return '';
|
||||
}
|
||||
|
||||
$swap = static::validateSwap($swap);
|
||||
|
||||
return '<hx-partial hx-target="' . htmlspecialchars($target)
|
||||
. '" hx-swap="' . htmlspecialchars($swap) . '">'
|
||||
. $content
|
||||
@@ -381,52 +350,10 @@ class HTMX extends HTMLComponent
|
||||
* @param string $rule
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws \InvalidArgumentException On an unknown style or option.
|
||||
*/
|
||||
public static function reswap(string $rule = 'innerHTML'): void
|
||||
{
|
||||
header('HX-Reswap: ' . static::validateSwap($rule));
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates a HTMX swap value ("style [key:value]...").
|
||||
*
|
||||
* @param string $value
|
||||
*
|
||||
* @return string The normalized value.
|
||||
*
|
||||
* @throws \InvalidArgumentException On an unknown style or option.
|
||||
*/
|
||||
public static function validateSwap(string $value): string
|
||||
{
|
||||
$parts = preg_split(
|
||||
'/\s+/',
|
||||
trim($value),
|
||||
-1,
|
||||
PREG_SPLIT_NO_EMPTY
|
||||
);
|
||||
|
||||
if (empty($parts)) {
|
||||
throw new \InvalidArgumentException('Empty swap value.');
|
||||
}
|
||||
|
||||
if (!in_array($parts[0], static::SWAP_STYLES, true)) {
|
||||
throw new \InvalidArgumentException(
|
||||
'Invalid swap style: ' . $parts[0]
|
||||
);
|
||||
}
|
||||
|
||||
foreach (array_slice($parts, 1) as $option) {
|
||||
$key = explode(':', $option)[0];
|
||||
if (!in_array($key, static::SWAP_OPTIONS, true)) {
|
||||
throw new \InvalidArgumentException(
|
||||
'Invalid swap option: ' . $option
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return implode(' ', $parts);
|
||||
header('HX-Reswap: ' . $rule);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user