Skip to content

Commit

Permalink
Merge nested attributes parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
rnixx committed Nov 28, 2022
2 parents 15fc10c + b7bddb2 commit a151df5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ Changes
gets passed ``widget`` and ``data`` as of yafowil 3.0.0.
[rnix]

- Parse values of attributes (data-Attributes) if type() is dict.
HTML5 Data-Attributes with i18n or callables are possible now.
[2silver]

**Breaking changes:**

- Add ``python:`` expressions. Needed for cases where property callbacks not
Expand Down
13 changes: 11 additions & 2 deletions src/yafowil/yaml/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ def load_yaml(self, path):
def create_tree(self, data):
def call_factory(defs):
props = dict()
for k, v in defs.get('props', dict()).items():
props[k] = self.parse_definition_value(v)
props = self.parse_attribute(defs.get('props', dict()))
custom = dict()
for custom_key, custom_value in defs.get('custom', dict()).items():
custom_props = list()
Expand Down Expand Up @@ -171,6 +170,16 @@ def create_children(node, children_defs):
create_children(root, data.get('widgets', []))
return root

def parse_attribute(self, value):
if not isinstance(value, dict):
return self.parse_definition_value(value)
for k, v in value.items():
if isinstance(v, dict):
self.parse_attribute(v)
else:
value[k] = self.parse_definition_value(v)
return value

def parse_definition_value(self, value):
if not isinstance(value, STR_TYPE):
return value
Expand Down
26 changes: 22 additions & 4 deletions src/yafowil/yaml/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ def tearDown(self):
label: i18n:First Field
description: I am the description
required: I am required
data:
flat: I am a flat data-attribute
i18n: i18n:nested_firstfield:i18n Nested First Field
nested:
nested-i18n: i18n:nested_firstfield:i18n Nested First Field
- secondfield:
factory: field:label:*custom_stuff:error:select
value: ['a', 'b']
Expand Down Expand Up @@ -114,7 +119,14 @@ def test_load_yaml(self):
'props': {
'description': 'I am the description',
'label': 'i18n:First Field',
'required': 'I am required'
'required': 'I am required',
'data': {
'flat': 'I am a flat data-attribute',
'i18n': 'i18n:nested_firstfield:i18n Nested First Field',
'nested': {
'nested-i18n': 'i18n:nested_firstfield:i18n Nested First Field'
}
}
}
}
}, {
Expand Down Expand Up @@ -253,9 +265,15 @@ def test_parse_from_yaml(self):
id="form-demoform" method="post" novalidate="novalidate">
<div class="field" id="field-demoform-firstfield">
<label for="input-demoform-firstfield">First Field</label>
<input class="required text" id="input-demoform-firstfield"
name="demoform.firstfield" required="required"
type="text" value="First value"/>
<input class="required text"
data-flat="I am a flat data-attribute"
data-i18n="i18n Nested First Field"
data-nested="{&quot;nested-i18n&quot;: &quot;i18n Nested First Field&quot;}"
id="input-demoform-firstfield"
name="demoform.firstfield"
required="required"
type="text"
value="First value"/>
</div>
<div class="field" id="field-demoform-secondfield">
<label for="input-demoform-secondfield"
Expand Down

0 comments on commit a151df5

Please sign in to comment.