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

Commit 85aa3a3

Browse files
ajith23facebook-github-bot
authored andcommitted
updated hastext method to not account for zero space width chars (#2163)
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
1 parent 3043573 commit 85aa3a3

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

src/model/immutable/ContentState.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,11 @@ class ContentState extends ContentStateRecord {
127127

128128
hasText(): boolean {
129129
const blockMap = this.getBlockMap();
130-
return blockMap.size > 1 || blockMap.first().getLength() > 0;
130+
return (
131+
blockMap.size > 1 ||
132+
// make sure that there are no zero width space chars
133+
escape(blockMap.first().getText()).replace(/%u200B/g, '').length > 0
134+
);
131135
}
132136

133137
createEntity(

src/model/immutable/__tests__/ContentState-test.js

+7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const MULTI_BLOCK = [
2626
{text: 'Four score', key: 'b'},
2727
{text: 'and seven', key: 'c'},
2828
];
29+
const ZERO_WIDTH_CHAR_BLOCK = [{text: unescape('%u200B%u200B'), key: 'a'}];
2930

3031
const SelectionState = require('SelectionState');
3132

@@ -84,6 +85,12 @@ test('block fetching must retrieve or fail fetching block for key', () => {
8485
expect(state.getBlockForKey('x')).toMatchSnapshot();
8586
});
8687

88+
test('must not include zero width chars for has text', () => {
89+
expect(getSample(ZERO_WIDTH_CHAR_BLOCK).hasText()).toMatchSnapshot();
90+
expect(getSample(SINGLE_BLOCK).hasText()).toMatchSnapshot();
91+
expect(getSample(MULTI_BLOCK).hasText()).toMatchSnapshot();
92+
});
93+
8794
test('must create entities instances', () => {
8895
const contentState = createLink();
8996
expect(typeof contentState.getLastCreatedEntityKey()).toMatchSnapshot();

src/model/immutable/__tests__/__snapshots__/ContentState-test.js.snap

+6
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ Object {
4040
}
4141
`;
4242

43+
exports[`must not include zero width chars for has text 1`] = `false`;
44+
45+
exports[`must not include zero width chars for has text 2`] = `true`;
46+
47+
exports[`must not include zero width chars for has text 3`] = `true`;
48+
4349
exports[`must replace entities data 1`] = `
4450
Object {
4551
"newProp": "baz",

0 commit comments

Comments
 (0)