mirror of
https://github.com/google/blockly.git
synced 2026-01-07 00:50:27 +01:00
feat: Parse newlines in JSON message as row separators. (#6944)
* feat: Parse message newlines as endOfRow dummies. * Fix the multilineinput field test. * Addressing PR feedback. * Addressing PR feedback. * Newline parsing now uses a new custom input. * npm run format * Added input_end_row to block factory. * Addres feedback, fix endrow after external value.
This commit is contained in:
@@ -92,6 +92,10 @@ suite('Block JSON initialization', function () {
|
||||
'Block "test": Message index %2 out of range.',
|
||||
);
|
||||
});
|
||||
|
||||
test('Newline tokens are valid', function () {
|
||||
this.assertNoError(['test', '\n', 'test'], 0);
|
||||
});
|
||||
});
|
||||
|
||||
suite('interpolateArguments_', function () {
|
||||
@@ -312,6 +316,70 @@ suite('Block JSON initialization', function () {
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
test('interpolation output includes end-row inputs', function () {
|
||||
this.assertInterpolation(
|
||||
['test1', {'type': 'input_end_row'}, 'test2'],
|
||||
[],
|
||||
undefined,
|
||||
[
|
||||
{
|
||||
'type': 'field_label',
|
||||
'text': 'test1',
|
||||
},
|
||||
{
|
||||
'type': 'input_end_row',
|
||||
},
|
||||
{
|
||||
'type': 'field_label',
|
||||
'text': 'test2',
|
||||
},
|
||||
{
|
||||
'type': 'input_dummy',
|
||||
},
|
||||
],
|
||||
);
|
||||
});
|
||||
|
||||
test('Newline is converted to end-row input', function () {
|
||||
this.assertInterpolation(['test1', '\n', 'test2'], [], undefined, [
|
||||
{
|
||||
'type': 'field_label',
|
||||
'text': 'test1',
|
||||
},
|
||||
{
|
||||
'type': 'input_end_row',
|
||||
},
|
||||
{
|
||||
'type': 'field_label',
|
||||
'text': 'test2',
|
||||
},
|
||||
{
|
||||
'type': 'input_dummy',
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
test('Newline converted to end-row aligned like last dummy', function () {
|
||||
this.assertInterpolation(['test1', '\n', 'test2'], [], 'CENTER', [
|
||||
{
|
||||
'type': 'field_label',
|
||||
'text': 'test1',
|
||||
},
|
||||
{
|
||||
'type': 'input_end_row',
|
||||
'align': 'CENTER',
|
||||
},
|
||||
{
|
||||
'type': 'field_label',
|
||||
'text': 'test2',
|
||||
},
|
||||
{
|
||||
'type': 'input_dummy',
|
||||
'align': 'CENTER',
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
suite('fieldFromJson_', function () {
|
||||
|
||||
@@ -10,6 +10,7 @@ import {ConnectionType} from '../../build/src/core/connection_type.js';
|
||||
import {createDeprecationWarningStub} from './test_helpers/warnings.js';
|
||||
import {createRenderedBlock} from './test_helpers/block_definitions.js';
|
||||
import * as eventUtils from '../../build/src/core/events/utils.js';
|
||||
import {EndRowInput} from '../../build/src/core/inputs/end_row_input.js';
|
||||
import {
|
||||
sharedTestSetup,
|
||||
sharedTestTeardown,
|
||||
@@ -2494,4 +2495,25 @@ suite('Blocks', function () {
|
||||
chai.assert.isTrue(initCalled, 'expected init function to be called');
|
||||
});
|
||||
});
|
||||
|
||||
suite('EndOfRow', function () {
|
||||
setup(function () {
|
||||
Blockly.defineBlocksWithJsonArray([
|
||||
{
|
||||
'type': 'end_row_test_block',
|
||||
'message0': 'Row1\nRow2',
|
||||
'inputsInline': true,
|
||||
},
|
||||
]);
|
||||
});
|
||||
test('Newline is converted to an end-row input', function () {
|
||||
const block = this.workspace.newBlock('end_row_test_block');
|
||||
chai.assert.equal(block.inputList[0].fieldRow[0].getValue(), 'Row1');
|
||||
chai.assert.isTrue(
|
||||
block.inputList[0] instanceof EndRowInput,
|
||||
'newline should be converted to an end-row input',
|
||||
);
|
||||
chai.assert.equal(block.inputList[1].fieldRow[0].getValue(), 'Row2');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -58,6 +58,13 @@ suite('Utils', function () {
|
||||
['Hello%World'],
|
||||
);
|
||||
});
|
||||
|
||||
test('Newlines are tokenized', function () {
|
||||
chai.assert.deepEqual(
|
||||
Blockly.utils.parsing.tokenizeInterpolation('Hello\nWorld'),
|
||||
['Hello', '\n', 'World'],
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
suite('Number interpolation', function () {
|
||||
@@ -231,6 +238,14 @@ suite('Utils', function () {
|
||||
'Unrecognized % escape code treated as literal',
|
||||
);
|
||||
|
||||
resultString =
|
||||
Blockly.utils.parsing.replaceMessageReferences('Hello\nWorld');
|
||||
chai.assert.equal(
|
||||
resultString,
|
||||
'Hello\nWorld',
|
||||
'Newlines are not tokenized',
|
||||
);
|
||||
|
||||
resultString = Blockly.utils.parsing.replaceMessageReferences('%1');
|
||||
chai.assert.equal(resultString, '%1', 'Interpolation tokens ignored.');
|
||||
resultString = Blockly.utils.parsing.replaceMessageReferences('%1 %2');
|
||||
|
||||
Reference in New Issue
Block a user