Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugs in using escape characters #1940

Open
microbit-mark opened this issue Mar 27, 2019 · 2 comments
Open

Bugs in using escape characters #1940

microbit-mark opened this issue Mar 27, 2019 · 2 comments

Comments

@microbit-mark
Copy link
Contributor

micro:bit-support: 21346
Describe the bug
In blocks entering within Serial Write Line, Serial Write String on in a general string assignment, entering a character escape sequence such as “\r” adds extra “\”
If the character required is \”, then two extra \ are added.
ecape

Entering a character escape sequence in Javascript gives an unrendered block sometimes.
A round trip from JS to blocks back to JS results in a null being added at the front i.e if the string is “\r” then the round trip results in “” + “\r”
escape2

Entering hex number as a character escape crashes program and all work is lost e.g. for control-Z “\0x1a”
escape3

There is a documented block https://makecode.microbit.org/reference/math/from-char-code to enter a character code that renders if you use eg let ctrlZ = String.fromCharCode(26) and switched to bklocks, but this doesn't appear in the Text Block menu. Can this be added?
escape4

@nhzandi
Copy link

nhzandi commented Apr 25, 2019

Considering escape characters, in the Blockly API it is handled like this:

string = block.getFieldValue('TEXT');
string = string.replace(/\\/g, '\\\\')
                 .replace(/\n/g, '\\\n')
                 .replace(/'/g, '\\\'');

So Blockly is intentionally adding an extra \ to escape the characters, maybe assuming that children using it are not familiar with the concept, and since it makes problem when communicating with a serial port and they don't have this problem. A quick fix to this would be to handle it in this function:

function compileText(e: Environment, b: Blockly.Block, comments: string[]): JsNode {
     return H.mkStringLiteral(b.getFieldValue("TEXT")); /* <-- HERE*/
}

A fix for this could be sth like this:

b.getFieldValue("TEXT").replace('\\r', '\r')  /* for other characters as well*/

or could have special characters as separate blocks and use join_text to join them together.

So if everyone agrees with that.

@microbit-mark
Copy link
Contributor Author

^^ @jwunderl @abchatra

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants