From a1fb46b7a3e44f5a669a9df6fec882fa088234cf Mon Sep 17 00:00:00 2001 From: Ajith V Date: Tue, 20 Aug 2019 14:28:35 -0700 Subject: [PATCH] updated hastext method to not account for zero space width chars Summary: \u200B (zero space width chars) is put into the blockmap while inserting tokens. Therfore hastext returns true even when there is no text in the input. Updated hastext method to not account for zero space width chars. Reviewed By: claudiopro Differential Revision: D16868475 fbshipit-source-id: ebbc392dbddddedfd13fd6ed3d54d6ba476542ae --- src/model/immutable/ContentState.js | 6 +++++- src/model/immutable/__tests__/ContentState-test.js | 7 +++++++ .../__tests__/__snapshots__/ContentState-test.js.snap | 6 ++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/model/immutable/ContentState.js b/src/model/immutable/ContentState.js index c303002027..a7889ebdd0 100644 --- a/src/model/immutable/ContentState.js +++ b/src/model/immutable/ContentState.js @@ -127,7 +127,11 @@ class ContentState extends ContentStateRecord { hasText(): boolean { const blockMap = this.getBlockMap(); - return blockMap.size > 1 || blockMap.first().getLength() > 0; + return ( + blockMap.size > 1 || + // make sure that there are no zero width space chars + escape(blockMap.first().getText()).replace(/%u200B/g, '').length > 0 + ); } createEntity( diff --git a/src/model/immutable/__tests__/ContentState-test.js b/src/model/immutable/__tests__/ContentState-test.js index 580741193f..153d0b92e5 100644 --- a/src/model/immutable/__tests__/ContentState-test.js +++ b/src/model/immutable/__tests__/ContentState-test.js @@ -26,6 +26,7 @@ const MULTI_BLOCK = [ {text: 'Four score', key: 'b'}, {text: 'and seven', key: 'c'}, ]; +const ZERO_WIDTH_CHAR_BLOCK = [{text: unescape('%u200B%u200B'), key: 'a'}]; const SelectionState = require('SelectionState'); @@ -84,6 +85,12 @@ test('block fetching must retrieve or fail fetching block for key', () => { expect(state.getBlockForKey('x')).toMatchSnapshot(); }); +test('must not include zero width chars for has text', () => { + expect(getSample(ZERO_WIDTH_CHAR_BLOCK).hasText()).toMatchSnapshot(); + expect(getSample(SINGLE_BLOCK).hasText()).toMatchSnapshot(); + expect(getSample(MULTI_BLOCK).hasText()).toMatchSnapshot(); +}); + test('must create entities instances', () => { const contentState = createLink(); expect(typeof contentState.getLastCreatedEntityKey()).toMatchSnapshot(); diff --git a/src/model/immutable/__tests__/__snapshots__/ContentState-test.js.snap b/src/model/immutable/__tests__/__snapshots__/ContentState-test.js.snap index 27d4ba0067..4123619761 100644 --- a/src/model/immutable/__tests__/__snapshots__/ContentState-test.js.snap +++ b/src/model/immutable/__tests__/__snapshots__/ContentState-test.js.snap @@ -40,6 +40,12 @@ Object { } `; +exports[`must not include zero width chars for has text 1`] = `false`; + +exports[`must not include zero width chars for has text 2`] = `true`; + +exports[`must not include zero width chars for has text 3`] = `true`; + exports[`must replace entities data 1`] = ` Object { "newProp": "baz",