Skip to content

Commit e5140c4

Browse files
committed
Better enforce Flow types
1 parent b82f74b commit e5140c4

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

packages/draft-js-import-element/src/stateFromElement.js

+24-9
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ export type CustomBlockFn = (
5858
element: DOMElement,
5959
) => ?PartialBlock;
6060

61-
type EntityData = {[key: string]: mixed};
6261
type EntityMutability = 'IMMUTABLE' | 'MUTABLE' | 'SEGMENTED';
6362

6463
type CustomStyle = {
@@ -75,7 +74,7 @@ export type CustomInlineFn = (
7574
element: DOMElement,
7675
creators: {
7776
Style: (style: string) => CustomStyle;
78-
Entity: (type: string, data: EntityData, mutability?: EntityMutability) => CustomEntity;
77+
Entity: (type: string, data: DataMap<mixed>, mutability?: EntityMutability) => CustomEntity;
7978
}
8079
) => ?(CustomStyle | CustomEntity);
8180

@@ -85,6 +84,7 @@ type Options = {
8584
customBlockFn?: CustomBlockFn;
8685
customInlineFn?: CustomInlineFn;
8786
};
87+
type DataMap<T> = {[key: string]: T};
8888

8989
const NO_STYLE = OrderedSet();
9090
const NO_ENTITY = null;
@@ -112,17 +112,18 @@ const ELEM_ATTR_MAP = {
112112
};
113113

114114
const getEntityData = (tagName: string, element: DOMElement) => {
115-
const data = {};
115+
const data: DataMap<string> = {};
116116
if (ELEM_ATTR_MAP.hasOwnProperty(tagName)) {
117117
const attrMap = ELEM_ATTR_MAP[tagName];
118118
for (let i = 0; i < element.attributes.length; i++) {
119119
const {name, value} = element.attributes[i];
120-
if (value != null) {
120+
if (typeof value === 'string') {
121+
let strVal = value;
121122
if (attrMap.hasOwnProperty(name)) {
122123
const newName = attrMap[name];
123-
data[newName] = value;
124+
data[newName] = strVal;
124125
} else if (DATA_ATTRIBUTE.test(name)) {
125-
data[name] = value;
126+
data[name] = strVal;
126127
}
127128
}
128129
}
@@ -166,9 +167,9 @@ class ContentGenerator {
166167
// to return a Style() or Entity().
167168
inlineCreators = {
168169
Style: (style: Style) => ({type: 'STYLE', style}),
169-
Entity: (type: string, data: EntityData, mutability: EntityMutability = 'MUTABLE') => ({
170+
Entity: (type: string, data: DataMap<mixed>, mutability: EntityMutability = 'MUTABLE') => ({
170171
type: 'ENTITY',
171-
entityKey: this.createEntity(type, data, mutability),
172+
entityKey: this.createEntity(type, toStringMap(data), mutability),
172173
}),
173174
};
174175

@@ -408,7 +409,7 @@ class ContentGenerator {
408409
}
409410
}
410411

411-
createEntity(type: string, data: EntityData, mutability: EntityMutability = 'MUTABLE') {
412+
createEntity(type: string, data: DataMap<string>, mutability: EntityMutability = 'MUTABLE') {
412413
this.contentStateForEntities = this.contentStateForEntities.createEntity(
413414
type,
414415
mutability,
@@ -543,6 +544,20 @@ function hasSemanticMeaning(blockType: string) {
543544
return blockType !== BLOCK_TYPE.UNSTYLED;
544545
}
545546

547+
function toStringMap(input: mixed) {
548+
let result: DataMap<string> = {};
549+
if (input !== null && typeof input === 'object' && !Array.isArray(input)) {
550+
let obj = input;
551+
for (let key of Object.keys(obj)) {
552+
let value = obj[key];
553+
if (typeof value === 'string') {
554+
result[key] = value;
555+
}
556+
}
557+
}
558+
return result;
559+
}
560+
546561
export function stateFromElement(
547562
element: DOMElement,
548563
options?: Options,

packages/draft-js-import-markdown/src/MarkdownParser.js

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
* Use ES6 classes
66
* Add flow annotations
77
*/
8-
/* eslint-disable no-spaced-func */
98

109
import {TextNode, ElementNode, FragmentNode, SELF_CLOSING} from 'synthetic-dom';
1110

0 commit comments

Comments
 (0)