Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

statementToCode return extra space character at the beginning

15 views
Skip to first unread message

Peter Cheung

unread,
Nov 26, 2024, 7:33:42 AM11/26/24
to Blockly
Hi
I created my own custom block to let user input some text. When i get the text in my own generator, some extra space char are at the beginning, why?

let className = javaGenerator.statementToCode(block, 'className')

let identifier = Blockly.common.createBlockDefinitionsFromJsonArray([
    {
        type: 'identifier',
        message0: '%1',
        // inputsInline: true,
        args0: [
            {
                type: 'field_input',
                name: 'name',
                text: 'default',
            }
        ],
        output: null,
        colour: 210,
        tooltip: '',
        helpUrl: '',
    }
]);
forBlock['identifier'] = function (block) {
    return block.getFieldValue('name');
};

Christopher Allen

unread,
Nov 26, 2024, 11:53:44 AM11/26/24
to blo...@googlegroups.com
Hi Peter,

I created my own custom block to let user input some text. When i get the text in my own generator, some extra space char are at the beginning, why?

 
forBlock['identifier'] = function (block) {
    return block.getFieldValue('name');
};

It appears that you are returning the user-entered value as if it were code, and the function that called your block generator function is duly indenting that code to match the surrounding code.

This is almost certainly not what you want; at the very least you should be ensuring that the value returned is a valid identifier—otherwise this field can be used by the user to enter arbitrary code to be evaluated, which is almost certainly not what you want.

Can you tell us a bit more about what the intense use of the identifier block is?  Normally I would expect that the identifier would be a part of a larger block that does something with the identifier—something more akin to the variables_get and variables_set blocks, for example.

The only reason why one would want to have an identifier block on its own that can immediately think of is if one was generating code for a highly dynamic language (e.g. Common Lisp) in which identifiers are first-class values (i.e., you can have a variable whose value is itself another variable).

Best wishes,

Christopher

Peter Cheung

unread,
Nov 26, 2024, 12:21:20 PM11/26/24
to Blockly
yes and than you, you are right, the getFieldValue() return indented string.
Reply all
Reply to author
Forward
0 new messages