zauberzeug / nicegui

Create web-based user interfaces with Python. The nice way.
https://nicegui.io
MIT License
9.8k stars 581 forks source link

Dynamic properties starting with a line break are ignored #2845

Closed falkoschindler closed 6 months ago

falkoschindler commented 6 months ago

Description

Here is a minimum example of a dynamic property controlling the row height depending on the length of the name:

ui.aggrid({
    'columnDefs': [{'field': 'name'}],
    'rowData': [{'name': 'Alice'}, {'name': 'Bob'}],
    ':getRowHeight': 'params => params.data.name.length * 10',
})

The same property is ignored when wrapped with line breaks:

ui.aggrid({
    'columnDefs': [{'field': 'name'}],
    'rowData': [{'name': 'Alice'}, {'name': 'Bob'}],
    ':getRowHeight': '''
        params => params.data.name.length * 10
    ''',
})
falkoschindler commented 6 months ago

The way we converted strings to JavaScript objects couldn't handle leading line breaks:

obj[attr.slice(1)] = new Function("return " + value)();

The function would just return undefined because the line ends after "return". I fixed it by adding brackets:

obj[attr.slice(1)] = new Function(`return (${value})`)();