public static function TocFilter::parseOptions in TOC filter 8.2
Parse options from an attributes string.
Parameters
string $text: A string of options.
Return value
array An associative array of parsed name/value pairs.
2 calls to TocFilter::parseOptions()
- TocFilter::process in src/
Plugin/ Filter/ TocFilter.php - Performs the filter processing.
- TocFilterOptionsTest::testParseOptions in tests/
src/ Unit/ TocFilterOptionsTest.php - Tests converting string of options with TocFilter::parseOptions().
File
- src/
Plugin/ Filter/ TocFilter.php, line 193 - Contains \Drupal\toc_filter\Plugin\Filter\TocFilter.
Class
- TocFilter
- Provides a filter to display a table of contents.
Namespace
Drupal\toc_filter\Plugin\FilterCode
public static function parseOptions($text) {
// Decode special characters.
$text = html_entity_decode($text);
// Convert decode to expected ASCII code 32 character.
// See: http://stackoverflow.com/questions/6275380
$text = str_replace("", ' ', $text);
// Create a DomElement so that we can parse its attributes as options.
$html = Html::load('<div ' . $text . ' />');
$dom_node = $html
->getElementsByTagName('div')
->item(0);
$options = [];
foreach ($dom_node->attributes as $name => $node) {
// Empty attribute values (ie name="") and attributes with no defined
// value (ie just name) both return empty strings.
// See: http://stackoverflow.com/questions/6232412
// So we are going to work-around this limitation and look at the
// actually attribute to see if is assigned a value, if not then set it to
// 'true'.
$value = $node->nodeValue ?: (preg_match('/' . preg_quote($name) . '\\s*=/i', $text) ? '' : 'true');
switch (strtolower($value)) {
case 'true':
case 'false':
$value = strtolower($value) === 'true';
break;
default:
if ($value !== '' && is_numeric($value)) {
$value = floatval($value);
}
break;
}
// Prefix h1-6 tags with 'headers.' to map it to the correction
// $option['headers'] array.
if (preg_match('/h[1-6]/', $name)) {
$name = 'headers.' . $name;
}
self::setOption($options, $name, $value);
}
return $options;
}