fix error "Can not parse attributes ''" in smallpart.course

thought preprocessor can currently not kope with nested details tags!
This commit is contained in:
ralf 2022-12-08 12:27:43 +01:00
parent d7f8f7d997
commit 06cb34974a

View File

@ -136,7 +136,7 @@ function send_template()
return $matches[0]; return $matches[0];
}, $str); }, $str);
// Change details title --> summary // Change details title --> summary (This can currently not kope with nested details like in smallpart.curse.xet
$str = preg_replace_callback('#<details([^>]*?)>(.*?)</details>#su', static function ($matches) $str = preg_replace_callback('#<details([^>]*?)>(.*?)</details>#su', static function ($matches)
{ {
$attrs = parseAttrs($matches[1]); $attrs = parseAttrs($matches[1]);
@ -292,7 +292,7 @@ function send_template()
$matches[2] = preg_replace('/^(select|taglist)/', '', $attrs['type']); $matches[2] = preg_replace('/^(select|taglist)/', '', $attrs['type']);
unset($attrs['type']); unset($attrs['type']);
} }
return '<et2-select' . $matches[2] . ' ' . stringAttrs($attrs) . '>'.$matches[5].'</et2-select' . $matches[2] . '>'; return '<et2-select' . $matches[2] . stringAttrs($attrs) . '>'.$matches[5].'</et2-select' . $matches[2] . '>';
}, $str); }, $str);
// nextmatch headers // nextmatch headers
@ -316,7 +316,7 @@ function send_template()
$matches[2] = "filter"; $matches[2] = "filter";
} }
return '<et2-nextmatch-header-' . $matches[2] . ' ' . stringAttrs($attrs) . '/>'; return '<et2-nextmatch-header-' . $matches[2] . stringAttrs($attrs) . '/>';
}, $str); }, $str);
// fix <(button|buttononly|timestamper).../> --> <et2-(button|image|button-timestamp) (noSubmit="true")?.../> // fix <(button|buttononly|timestamper).../> --> <et2-(button|image|button-timestamp) (noSubmit="true")?.../>
@ -499,6 +499,10 @@ function send_template()
*/ */
function parseAttrs($str) function parseAttrs($str)
{ {
if (empty($str) || !trim($str))
{
return [];
}
if (!preg_match_all('/(^|\s)([a-z\d_-]+)="([^"]*)"/i', $str, $attrs, PREG_PATTERN_ORDER)) if (!preg_match_all('/(^|\s)([a-z\d_-]+)="([^"]*)"/i', $str, $attrs, PREG_PATTERN_ORDER))
{ {
throw new Exception("Can NOT parse attributes from '$str'"); throw new Exception("Can NOT parse attributes from '$str'");
@ -509,12 +513,18 @@ function parseAttrs($str)
/** /**
* Combine attribute array into a string * Combine attribute array into a string
* *
* If there are attributes the returned string is prefixed with a single space, otherwise an empty string is returned.
*
* @param array $attrs * @param array $attrs
* @return string * @return string
*/ */
function stringAttrs(array $attrs) function stringAttrs(array $attrs)
{ {
return implode(' ', array_map(static function ($name, $value) { if (!$attrs)
{
return '';
}
return ' '.implode(' ', array_map(static function ($name, $value) {
return $name . '="' . $value . '"'; return $name . '="' . $value . '"';
}, array_keys($attrs), $attrs)); }, array_keys($attrs), $attrs));
} }