mirror of
https://github.com/EGroupware/egroupware.git
synced 2025-02-17 02:41:02 +01:00
use method to parse and serialize attributes
This commit is contained in:
parent
737311d829
commit
9fe7b22d1f
@ -137,8 +137,7 @@ function send_template()
|
|||||||
$str = preg_replace_callback('#<split([^>]*?)>(.*)</split>#su', static function ($matches)
|
$str = preg_replace_callback('#<split([^>]*?)>(.*)</split>#su', static function ($matches)
|
||||||
{
|
{
|
||||||
$tag = 'et2-split';
|
$tag = 'et2-split';
|
||||||
preg_match_all('/(^| )([a-z0-9_-]+)="([^"]+)"/i', $matches[1], $attrs, PREG_PATTERN_ORDER);
|
$attrs = parseAttrs($matches[1]);
|
||||||
$attrs = array_combine($attrs[2], $attrs[3]);
|
|
||||||
|
|
||||||
$attrs['vertical'] = $attrs['orientation'] === 'h' ? "true" : "false";
|
$attrs['vertical'] = $attrs['orientation'] === 'h' ? "true" : "false";
|
||||||
if (str_contains($attrs['dock_side'], 'top') || str_contains($attrs['dock_side'], 'left'))
|
if (str_contains($attrs['dock_side'], 'top') || str_contains($attrs['dock_side'], 'left'))
|
||||||
@ -151,10 +150,7 @@ function send_template()
|
|||||||
}
|
}
|
||||||
unset($attrs['dock_side']);
|
unset($attrs['dock_side']);
|
||||||
|
|
||||||
return "<$tag " . implode(' ', array_map(function ($name, $value) {
|
return "<$tag " . stringAttrs($attrs) . '>' . $matches[2] . "</$tag>";
|
||||||
return $name . '="' . $value . '"';
|
|
||||||
}, array_keys($attrs), $attrs)
|
|
||||||
) . '>' . $matches[2] . "</$tag>";
|
|
||||||
}, $str);
|
}, $str);
|
||||||
|
|
||||||
// modify <(image|description) expose_view="true" --> <et2-*-expose
|
// modify <(image|description) expose_view="true" --> <et2-*-expose
|
||||||
@ -187,8 +183,7 @@ function send_template()
|
|||||||
$str = preg_replace_callback('#<et2-link(-[a-z]+)?([^>]*?)></et2-link(-[a-z]+)?>#su', static function ($matches)
|
$str = preg_replace_callback('#<et2-link(-[a-z]+)?([^>]*?)></et2-link(-[a-z]+)?>#su', static function ($matches)
|
||||||
{
|
{
|
||||||
$tag = 'et2-link'.$matches[1];
|
$tag = 'et2-link'.$matches[1];
|
||||||
preg_match_all('/(^| )([a-z0-9_-]+)="([^"]+)"/i', $matches[2], $attrs, PREG_PATTERN_ORDER);
|
$attrs = parseAttrs($matches[2]);
|
||||||
$attrs = array_combine($attrs[2], $attrs[3]);
|
|
||||||
|
|
||||||
if ($tag === 'et2-link-entry' && !empty($attrs['readonly']) || $tag === 'et2-link')
|
if ($tag === 'et2-link-entry' && !empty($attrs['readonly']) || $tag === 'et2-link')
|
||||||
{
|
{
|
||||||
@ -196,16 +191,13 @@ function send_template()
|
|||||||
$attrs['app'] = $attrs['only_app'];
|
$attrs['app'] = $attrs['only_app'];
|
||||||
unset($attrs['only_app'], $attrs['readonly']);
|
unset($attrs['only_app'], $attrs['readonly']);
|
||||||
}
|
}
|
||||||
return "<$tag " . implode(' ', array_map(function ($name, $value) {
|
return "<$tag " . stringAttrs($attrs) . "></$tag>";
|
||||||
return $name . '="' . $value . '"';
|
|
||||||
}, array_keys($attrs), $attrs)
|
|
||||||
) . "></$tag>";
|
|
||||||
}, $str);
|
}, $str);
|
||||||
|
|
||||||
// handling of select and taglist widget, incl. removing of type attribute
|
// handling of select and taglist widget, incl. removing of type attribute
|
||||||
$str = preg_replace_callback('#<(select|taglist|listbox)(-[^ ]+)? ([^>]+?)(/|>(.*?)</select)>#s', static function (array $matches) {
|
$str = preg_replace_callback('#<(select|taglist|listbox)(-[^ ]+)? ([^>]+?)(/|>(.*?)</select)>#s', static function (array $matches)
|
||||||
preg_match_all('/(^|\s)([a-z0-9_-]+)="([^"]*)"/i', $matches[3], $attrs, PREG_PATTERN_ORDER);
|
{
|
||||||
$attrs = array_combine($attrs[2], $attrs[3]);
|
$attrs = parseAttrs($matches[3]);
|
||||||
|
|
||||||
// set multiple for old tags attribute or taglist without maxSelection="1"
|
// set multiple for old tags attribute or taglist without maxSelection="1"
|
||||||
if (isset($attrs['tags']) || $matches['1'] === 'taglist' && (empty($attrs['maxSelection']) || $attrs['maxSelection'] > 1))
|
if (isset($attrs['tags']) || $matches['1'] === 'taglist' && (empty($attrs['maxSelection']) || $attrs['maxSelection'] > 1))
|
||||||
@ -244,18 +236,13 @@ 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']);
|
||||||
}
|
}
|
||||||
$replace = '<et2-select' . $matches[2] . ' ' . implode(' ', array_map(static function($attr, $val)
|
return '<et2-select' . $matches[2] . ' ' . stringAttrs($attrs) . '>'.$matches[5].'</et2-select' . $matches[2] . '>';
|
||||||
{
|
|
||||||
return $attr.'="'.$val.'"';
|
|
||||||
}, array_keys($attrs), $attrs)) . '>'.$matches[5].'</et2-select' . $matches[2] . '>';
|
|
||||||
return $replace;
|
|
||||||
}, $str);
|
}, $str);
|
||||||
|
|
||||||
// nextmatch headers
|
// nextmatch headers
|
||||||
$str = preg_replace_callback('#<(nextmatch-)([^ ]+)(header|filter) ([^>]+?)/>#s', static function (array $matches)
|
$str = preg_replace_callback('#<(nextmatch-)([^ ]+)(header|filter) ([^>]+?)/>#s', static function (array $matches)
|
||||||
{
|
{
|
||||||
preg_match_all('/(^|\s)([a-z0-9_-]+)="([^"]*)"/i', $matches[4], $attrs, PREG_PATTERN_ORDER);
|
$attrs = parseAttrs($matches[4]);
|
||||||
$attrs = array_combine($attrs[2], $attrs[3]);
|
|
||||||
|
|
||||||
if ($matches[2] === 'custom')
|
if ($matches[2] === 'custom')
|
||||||
{
|
{
|
||||||
@ -273,13 +260,7 @@ function send_template()
|
|||||||
$matches[2] = "filter";
|
$matches[2] = "filter";
|
||||||
}
|
}
|
||||||
|
|
||||||
$replace = '<et2-nextmatch-header-' . $matches[2] . ' ' .
|
return '<et2-nextmatch-header-' . $matches[2] . ' ' . stringAttrs($attrs) . '/>';
|
||||||
implode(' ', array_map(static function ($attr, $val)
|
|
||||||
{
|
|
||||||
return $attr . '="' . $val . '"';
|
|
||||||
}, array_keys($attrs), $attrs)
|
|
||||||
) . '/>';
|
|
||||||
return $replace;
|
|
||||||
}, $str);
|
}, $str);
|
||||||
|
|
||||||
$str = preg_replace('#<passwd ([^/>]+)/>#', '<et2-password $1></et2-password>', $str);
|
$str = preg_replace('#<passwd ([^/>]+)/>#', '<et2-password $1></et2-password>', $str);
|
||||||
@ -300,8 +281,7 @@ function send_template()
|
|||||||
// fix <button(only)?.../> --> <et2-button(-image)? noSubmit="true".../>
|
// fix <button(only)?.../> --> <et2-button(-image)? noSubmit="true".../>
|
||||||
$str = preg_replace_callback('#<button(only)?\s(.*?)/>#s', function ($matches) use ($name) {
|
$str = preg_replace_callback('#<button(only)?\s(.*?)/>#s', function ($matches) use ($name) {
|
||||||
$tag = 'et2-button';
|
$tag = 'et2-button';
|
||||||
preg_match_all('/(^|\s)([a-z0-9_-]+)="([^"]+)"/i', $matches[2], $attrs, PREG_PATTERN_ORDER);
|
$attrs = parseAttrs($matches[2]);
|
||||||
$attrs = array_combine($attrs[2], $attrs[3]);
|
|
||||||
// replace buttononly tag with noSubmit="true" attribute
|
// replace buttononly tag with noSubmit="true" attribute
|
||||||
if (!empty($matches[1]))
|
if (!empty($matches[1]))
|
||||||
{
|
{
|
||||||
@ -327,9 +307,7 @@ function send_template()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
unset($attrs['background_image']);
|
unset($attrs['background_image']);
|
||||||
return "<$tag " . implode(' ', array_map(function ($name, $value) {
|
return "<$tag " . stringAttrs($attrs) . '></' . $tag . '>';
|
||||||
return $name . '="' . $value . '"';
|
|
||||||
}, array_keys($attrs), $attrs)) . '></' . $tag . '>';
|
|
||||||
}, $str);
|
}, $str);
|
||||||
|
|
||||||
$str = preg_replace_callback(ADD_ET2_PREFIX_REGEXP, static function (array $matches) {
|
$str = preg_replace_callback(ADD_ET2_PREFIX_REGEXP, static function (array $matches) {
|
||||||
@ -343,8 +321,7 @@ function send_template()
|
|||||||
// change all attribute-names of new et2-* widgets to camelCase, and other attribute modifications for all web-components
|
// change all attribute-names of new et2-* widgets to camelCase, and other attribute modifications for all web-components
|
||||||
$str = preg_replace_callback('/<(et2|records)-([a-z-]+)\s([^>]+)>/', static function(array $matches)
|
$str = preg_replace_callback('/<(et2|records)-([a-z-]+)\s([^>]+)>/', static function(array $matches)
|
||||||
{
|
{
|
||||||
preg_match_all('/(^| )([a-z\d_-]+)="([^"]+)"/i', $matches[3], $attrs, PREG_PATTERN_ORDER);
|
$attrs = parseAttrs($matches[3]);
|
||||||
$attrs = array_combine($attrs[2], $attrs[3]);
|
|
||||||
|
|
||||||
// fix deprecated attributes: needed, blur, ...
|
// fix deprecated attributes: needed, blur, ...
|
||||||
static $deprecated = [
|
static $deprecated = [
|
||||||
@ -379,10 +356,7 @@ function send_template()
|
|||||||
unset($attrs['size']);
|
unset($attrs['size']);
|
||||||
}
|
}
|
||||||
|
|
||||||
$ret = str_replace($matches[3], implode(' ', array_map(static function ($name, $value) {
|
return str_replace($matches[3], stringAttrs($attrs).(substr($matches[3], -1) === '/' ? '/' : ''), $matches[0]);
|
||||||
return $name . '="' . $value . '"';
|
|
||||||
}, array_keys($attrs), $attrs)).(substr($matches[3], -1) === '/' ? '/' : ''), $matches[0]);
|
|
||||||
return $ret;
|
|
||||||
}, $str);
|
}, $str);
|
||||||
|
|
||||||
$processing = microtime(true);
|
$processing = microtime(true);
|
||||||
@ -432,3 +406,31 @@ function send_template()
|
|||||||
|
|
||||||
exit; // stop further processing eg. redirect to login
|
exit; // stop further processing eg. redirect to login
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse attributes in an array
|
||||||
|
*
|
||||||
|
* @param string $str
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
function parseAttrs($str)
|
||||||
|
{
|
||||||
|
if (!preg_match_all('/(^|\s)([a-z\d_-]+)="([^"]+)"/i', $str, $attrs, PREG_PATTERN_ORDER))
|
||||||
|
{
|
||||||
|
throw new Exception("Can NOT parse attributes from '$str'");
|
||||||
|
}
|
||||||
|
return array_combine($attrs[2], $attrs[3]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Combine attribute array into a string
|
||||||
|
*
|
||||||
|
* @param array $attrs
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
function stringAttrs(array $attrs)
|
||||||
|
{
|
||||||
|
return implode(' ', array_map(static function ($name, $value) {
|
||||||
|
return $name . '="' . $value . '"';
|
||||||
|
}, array_keys($attrs), $attrs));
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user