mirror of
https://github.com/processwire/processwire.git
synced 2025-08-14 10:45:54 +02:00
Add 2 new methods to $sanitizer: htmlClass() and htmlClasses(), for sanitizing HTML class attribute values.
This commit is contained in:
@@ -263,6 +263,8 @@ class Sanitizer extends Wire {
|
|||||||
'filename' => 's',
|
'filename' => 's',
|
||||||
'flatArray' => 'a',
|
'flatArray' => 'a',
|
||||||
'float' => 'f',
|
'float' => 'f',
|
||||||
|
'htmlClass' => 's',
|
||||||
|
'htmlClasses' => 's',
|
||||||
'httpUrl' => 's',
|
'httpUrl' => 's',
|
||||||
'hyphenCase' => 's',
|
'hyphenCase' => 's',
|
||||||
'int' => 'i',
|
'int' => 'i',
|
||||||
@@ -591,6 +593,54 @@ class Sanitizer extends Wire {
|
|||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sanitize string to ASCII-only HTML class attribute value
|
||||||
|
*
|
||||||
|
* Note that this does not support all possible characters in an HTML class attribute
|
||||||
|
* and instead focuses on the most commonly used ones. Characters allowed in HTML class
|
||||||
|
* attributes from this method include: `-_:@a-zA-Z0-9`. This method does not allow
|
||||||
|
* values that have no letters or digits.
|
||||||
|
*
|
||||||
|
* @param string $value
|
||||||
|
* @return string
|
||||||
|
* @since 3.0.212
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public function htmlClass($value) {
|
||||||
|
$value = trim("$value");
|
||||||
|
if(empty($value)) return '';
|
||||||
|
$extras = array('-', '_', ':', '@');
|
||||||
|
$value = $this->nameFilter($value, $extras, '-');
|
||||||
|
$value = ltrim($value, '0123456789'); // cannot begin with digit
|
||||||
|
if(trim($value, implode('', $extras)) === '') $value = ''; // do not allow extras-only class
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sanitize string to ASCII-only space-separated HTML class attribute values with no duplicates
|
||||||
|
*
|
||||||
|
* See additional notes in `Sanitizer::htmlClass()` method.
|
||||||
|
*
|
||||||
|
* @param string|array $value
|
||||||
|
* @param bool $getArray Get array rather than string? (default=false)
|
||||||
|
* @return string|array
|
||||||
|
* @since 3.0.212
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public function htmlClasses($value, $getArray = false) {
|
||||||
|
if(is_array($value)) $value = implode(' ', $value);
|
||||||
|
$value = str_replace(array("\n", "\r", "\t", ",", "."), ' ', $value);
|
||||||
|
$value = trim("$value");
|
||||||
|
if(empty($value)) return $getArray ? array() : '';
|
||||||
|
$a = array();
|
||||||
|
foreach(explode(' ', $value) as $c) {
|
||||||
|
$c = $this->htmlClass($c);
|
||||||
|
if(!empty($c)) $a[$c] = $c;
|
||||||
|
}
|
||||||
|
if($getArray) return array_values($a);
|
||||||
|
return count($a) ? implode(' ', $a) : '';
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sanitize consistent with names used by ProcessWire fields and/or PHP variables
|
* Sanitize consistent with names used by ProcessWire fields and/or PHP variables
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user