Skip to content
This repository has been archived by the owner on Feb 6, 2023. It is now read-only.

Commit

Permalink
updated hastext method to not account for zero space width chars (#2163)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #2163

\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: 1b0c9142c54e9c45d19fde516167ef34d02e719d
  • Loading branch information
ajith23 authored and facebook-github-bot committed Aug 20, 2019
1 parent 3043573 commit 85aa3a3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/model/immutable/ContentState.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
7 changes: 7 additions & 0 deletions src/model/immutable/__tests__/ContentState-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 85aa3a3

Please sign in to comment.