fix preprocessor and transformer can't know if application widget is a web-component or a legacy widget

- white-list now records-* like et2-* widgets for camelCase attribute names
- server-side transformer also transforms attribute-names to camelCase for widget-type et2-*
- client-side transformAttributes() also transforms attribute-names to camelCase for widget-type et2-*
--> hopefully this can be dropped, once als widgets are web-components
This commit is contained in:
ralf 2022-07-22 11:08:55 +02:00
parent ec8c38b882
commit e90ae88a20
3 changed files with 24 additions and 6 deletions

View File

@ -354,9 +354,9 @@ function send_template()
}, $str);
}
// change all attribute-names of new et2-* widgets to camelCase
$str = preg_replace_callback('/<et2-([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[2], $attrs, PREG_PATTERN_ORDER);
preg_match_all('/(^| )([a-z\d_-]+)="([^"]+)"/i', $matches[3], $attrs, PREG_PATTERN_ORDER);
$attrs = array_combine($attrs[2], $attrs[3]);
foreach($attrs as $name => $value)
@ -368,9 +368,9 @@ function send_template()
unset($attrs[$name]);
}
}
$ret = str_replace($matches[2], implode(' ', array_map(static function ($name, $value) {
$ret = str_replace($matches[3], implode(' ', array_map(static function ($name, $value) {
return $name . '="' . $value . '"';
}, array_keys($attrs), $attrs)).(substr($matches[2], -1) === '/' ? '/' : ''), $matches[0]);
}, array_keys($attrs), $attrs)).(substr($matches[3], -1) === '/' ? '/' : ''), $matches[0]);
return $ret;
}, $str);

View File

@ -1369,6 +1369,15 @@ function transformAttributes(widget, mgr : et2_arrayMgr, attributes)
continue;
}
// preprocessor and transformer can't know if application widget is a web-component or a legacy one
// translate attribute names to camelCase (only do it for used underscore, to not require a regexp)
if (attribute !== 'select_options' && attribute.indexOf('_') !== -1)
{
let parts = attribute.split('_');
if (attribute === 'parent_node') parts[1] = 'Id';
attribute = parts.shift()+parts.map(part => part[0].toUpperCase()+part.substring(1));
}
const property = widget_class.getPropertyOptions(attribute);
switch(typeof property === "object" ? property.type : property)

View File

@ -115,7 +115,7 @@ abstract class Transformer extends Etemplate\Widget
if($val == 'template')
{
// If the widget has been transformed into a template, we
// also need to try and instanciate & parse the template too
// also need to try and instantiate & parse the template too
$transformed_template = Template::instance($attrs['template']);
if($transformed_template)
{
@ -125,6 +125,15 @@ abstract class Transformer extends Etemplate\Widget
$type_changed = false;
}
default:
// transform attributes for web-components to camelCase
if (substr($attrs['type'], 0, 4) === 'et2-')
{
if (count($parts = preg_split('/[_-]/', $attr)) > 1)
{
if ($attr === 'parent_node') $parts[1] = 'Id'; // we can not use DOM property parentNode --> parentId
$attr = array_shift($parts).implode('', array_map('ucfirst', $parts));
}
}
self::setElementAttribute($form_name, $attr, $val);
break;
}
@ -230,4 +239,4 @@ abstract class Transformer extends Etemplate\Widget
throw new Api\Exception\WrongParameter(__METHOD__."(attr='$attr', action=".array2string($action).', attrs='.array2string($attrs).') wrong datatype for action!');
}
}
}
}