From e90ae88a20e51b2619791b883f85eccfcbd63937 Mon Sep 17 00:00:00 2001 From: ralf Date: Fri, 22 Jul 2022 11:08:55 +0200 Subject: [PATCH] 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 --- api/etemplate.php | 8 ++++---- api/js/etemplate/Et2Widget/Et2Widget.ts | 9 +++++++++ api/src/Etemplate/Widget/Transformer.php | 13 +++++++++++-- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/api/etemplate.php b/api/etemplate.php index 5df2b06674..bb9a045c64 100644 --- a/api/etemplate.php +++ b/api/etemplate.php @@ -354,9 +354,9 @@ function send_template() }, $str); } // change all attribute-names of new et2-* widgets to camelCase - $str = preg_replace_callback('/]+)>/', 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); diff --git a/api/js/etemplate/Et2Widget/Et2Widget.ts b/api/js/etemplate/Et2Widget/Et2Widget.ts index 8cbe6babfb..3503e84f0c 100644 --- a/api/js/etemplate/Et2Widget/Et2Widget.ts +++ b/api/js/etemplate/Et2Widget/Et2Widget.ts @@ -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) diff --git a/api/src/Etemplate/Widget/Transformer.php b/api/src/Etemplate/Widget/Transformer.php index b7e9ea6d89..efd637f385 100644 --- a/api/src/Etemplate/Widget/Transformer.php +++ b/api/src/Etemplate/Widget/Transformer.php @@ -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!'); } } -} +} \ No newline at end of file