mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-26 06:15:19 +08:00
Merge branch '6043-html-arrays'
Conflicts: framework/CHANGELOG.md
This commit is contained in:
@ -1756,6 +1756,16 @@ class BaseHtml
|
||||
$html .= " $name-$n=\"" . static::encode($v) . '"';
|
||||
}
|
||||
}
|
||||
} elseif ($name === 'class') {
|
||||
if (empty($value)) {
|
||||
continue;
|
||||
}
|
||||
$html .= " $name=\"" . static::encode(implode(' ', $value)) . '"';
|
||||
} elseif ($name === 'style') {
|
||||
if (empty($value)) {
|
||||
continue;
|
||||
}
|
||||
$html .= " $name=\"" . static::encode(static::cssStyleFromArray($value)) . '"';
|
||||
} else {
|
||||
$html .= " $name='" . Json::htmlEncode($value) . "'";
|
||||
}
|
||||
@ -1768,39 +1778,76 @@ class BaseHtml
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a CSS class to the specified options.
|
||||
* Adds a CSS class (or several classes) to the specified options.
|
||||
* If the CSS class is already in the options, it will not be added again.
|
||||
* If class specification at given options is an array, and some class placed there with the named (string) key,
|
||||
* overriding of such key will have no effect. For example:
|
||||
*
|
||||
* ~~~php
|
||||
* $options = ['class' => ['persistent' => 'initial']];
|
||||
* Html::addCssClass($options, ['persistent' => 'override']);
|
||||
* var_dump($options['class']); // outputs: array('persistent' => 'initial');
|
||||
* ~~~
|
||||
*
|
||||
* @param array $options the options to be modified.
|
||||
* @param string $class the CSS class to be added
|
||||
* @param string|array $class the CSS class(es) to be added
|
||||
*/
|
||||
public static function addCssClass(&$options, $class)
|
||||
{
|
||||
if (isset($options['class'])) {
|
||||
$classes = ' ' . $options['class'] . ' ';
|
||||
if (strpos($classes, ' ' . $class . ' ') === false) {
|
||||
$options['class'] .= ' ' . $class;
|
||||
if (is_array($options['class'])) {
|
||||
$options['class'] = self::mergeCssClasses($options['class'], (array)$class);
|
||||
} else {
|
||||
$classes = preg_split('/\s+/', $options['class'], -1, PREG_SPLIT_NO_EMPTY);
|
||||
$options['class'] = implode(' ', self::mergeCssClasses($classes, (array)$class));
|
||||
}
|
||||
} else {
|
||||
$options['class'] = $class;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Merges already existing CSS classes with new one.
|
||||
* This method provides the priority for named existing classes over additional.
|
||||
* @param array $existingClasses already existing CSS classes.
|
||||
* @param array $additionalClasses CSS classes to be added.
|
||||
* @return array merge result.
|
||||
*/
|
||||
private static function mergeCssClasses(array $existingClasses, array $additionalClasses)
|
||||
{
|
||||
foreach ($additionalClasses as $key => $class) {
|
||||
if (is_int($key) && !in_array($class, $existingClasses)) {
|
||||
$existingClasses[] = $class;
|
||||
} elseif (!isset($existingClasses[$key])) {
|
||||
$existingClasses[$key] = $class;
|
||||
}
|
||||
}
|
||||
return array_unique($existingClasses);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a CSS class from the specified options.
|
||||
* @param array $options the options to be modified.
|
||||
* @param string $class the CSS class to be removed
|
||||
* @param string|array $class the CSS class(es) to be removed
|
||||
*/
|
||||
public static function removeCssClass(&$options, $class)
|
||||
{
|
||||
if (isset($options['class'])) {
|
||||
$classes = array_unique(preg_split('/\s+/', $options['class'] . ' ' . $class, -1, PREG_SPLIT_NO_EMPTY));
|
||||
if (($index = array_search($class, $classes)) !== false) {
|
||||
unset($classes[$index]);
|
||||
}
|
||||
if (empty($classes)) {
|
||||
unset($options['class']);
|
||||
if (is_array($options['class'])) {
|
||||
$classes = array_diff($options['class'], (array)$class);
|
||||
if (empty($classes)) {
|
||||
unset($options['class']);
|
||||
} else {
|
||||
$options['class'] = $classes;
|
||||
}
|
||||
} else {
|
||||
$options['class'] = implode(' ', $classes);
|
||||
$classes = preg_split('/\s+/', $options['class'], -1, PREG_SPLIT_NO_EMPTY);
|
||||
$classes = array_diff($classes, (array)$class);
|
||||
if (empty($classes)) {
|
||||
unset($options['class']);
|
||||
} else {
|
||||
$options['class'] = implode(' ', $classes);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1830,7 +1877,7 @@ class BaseHtml
|
||||
public static function addCssStyle(&$options, $style, $overwrite = true)
|
||||
{
|
||||
if (!empty($options['style'])) {
|
||||
$oldStyle = static::cssStyleToArray($options['style']);
|
||||
$oldStyle = is_array($options['style']) ? $options['style'] : static::cssStyleToArray($options['style']);
|
||||
$newStyle = is_array($style) ? $style : static::cssStyleToArray($style);
|
||||
if (!$overwrite) {
|
||||
foreach ($newStyle as $property => $value) {
|
||||
@ -1861,7 +1908,7 @@ class BaseHtml
|
||||
public static function removeCssStyle(&$options, $properties)
|
||||
{
|
||||
if (!empty($options['style'])) {
|
||||
$style = static::cssStyleToArray($options['style']);
|
||||
$style = is_array($options['style']) ? $options['style'] : static::cssStyleToArray($options['style']);
|
||||
foreach ((array) $properties as $property) {
|
||||
unset($style[$property]);
|
||||
}
|
||||
|
Reference in New Issue
Block a user