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

Commit dcd41c6

Browse files
committed
Replace instanceof checks with nodeType checks
1 parent be8f251 commit dcd41c6

File tree

9 files changed

+86
-89
lines changed

9 files changed

+86
-89
lines changed

src/component/base/DraftEditor.react.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -374,15 +374,15 @@ class DraftEditor extends React.Component<DraftEditorProps, State> {
374374

375375
const scrollParent = Style.getScrollParent(editorNode);
376376
const {x, y} = scrollPosition || getScrollPosition(scrollParent);
377-
const {defaultView} = editorNode.ownerDocument;
378377

379378
invariant(
380-
editorNode instanceof HTMLElement,
381-
'editorNode is not an HTMLElement',
379+
editorNode.nodeType === 1,
380+
'editorNode is not an Element',
382381
);
383382
editorNode.focus();
384383

385384
// Restore scroll position
385+
const {defaultView} = editorNode.ownerDocument;
386386
if (scrollParent === defaultView) {
387387
defaultView.scrollTo(x, y);
388388
} else {
@@ -406,8 +406,8 @@ class DraftEditor extends React.Component<DraftEditorProps, State> {
406406
_blur(): void {
407407
const editorNode = ReactDOM.findDOMNode(this.refs.editor);
408408
invariant(
409-
editorNode instanceof HTMLElement,
410-
'editorNode is not an HTMLElement',
409+
editorNode.nodeType === 1,
410+
'editorNode is not an Element',
411411
);
412412
editorNode.blur();
413413
}

src/component/contents/DraftEditorBlock.react.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ class DraftEditorBlock extends React.Component<Props> {
113113
}
114114
} else {
115115
invariant(
116-
blockNode instanceof HTMLElement,
117-
'blockNode is not an HTMLElement',
116+
blockNode.nodeType === 1,
117+
'blockNode is not an Element',
118118
);
119119
var blockBottom = blockNode.offsetHeight + blockNode.offsetTop;
120120
var scrollBottom = scrollParent.offsetHeight + scrollPosition.y;

src/component/contents/DraftEditorTextNode.react.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class DraftEditorTextNode extends React.Component<Props> {
7171
shouldComponentUpdate(nextProps: Props): boolean {
7272
const node = ReactDOM.findDOMNode(this);
7373
const shouldBeNewline = nextProps.children === '';
74-
invariant(node instanceof Element, 'node is not an Element');
74+
invariant(node.nodeType === 1, 'node is not an Element');
7575
if (shouldBeNewline) {
7676
return !isNewline(node);
7777
}

src/component/handlers/edit/editOnSelect.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ function editOnSelect(editor: DraftEditor): void {
3030
const editorNode = ReactDOM.findDOMNode(editor.refs.editorContainer);
3131
invariant(editorNode, 'Missing editorNode');
3232
invariant(
33-
editorNode.firstChild instanceof HTMLElement,
34-
'editorNode.firstChild is not an HTMLElement',
33+
editorNode.firstChild.nodeType === 1,
34+
'editorNode.firstChild is not an Element',
3535
);
3636
var documentSelection = getDraftEditorSelection(
3737
editorState,

src/component/selection/findAncestorOffsetKey.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var getSelectionOffsetKeyForNode = require('getSelectionOffsetKeyForNode');
2020
*/
2121
function findAncestorOffsetKey(node: Node): ?string {
2222
let searchNode = node;
23-
while (searchNode && searchNode !== searchNode.ownerDocument.documentElement) {
23+
while (searchNode && searchNode.nodeName !== 'HTML') {
2424
var key = getSelectionOffsetKeyForNode(searchNode);
2525
if (key != null) {
2626
return key;

src/component/selection/getDraftEditorSelectionWithNodes.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,9 @@ function getPointForNonTextNode(
156156
// wrapper.
157157
if (editorRoot === node) {
158158
node = node.firstChild;
159-
var win = node && node.ownerDocument.defaultView;
160159
invariant(
161-
win && node instanceof win.Element && node.getAttribute('data-contents') === 'true',
160+
node && node.nodeType === 1 && typeof node.getAttribute === 'function' &&
161+
node.getAttribute('data-contents') === 'true',
162162
'Invalid DraftEditorContents structure.',
163163
);
164164
if (childOffset > 0) {

src/component/selection/getSelectionOffsetKeyForNode.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818
* found on the DOM tree of given node.
1919
*/
2020
function getSelectionOffsetKeyForNode(node: Node): ?string {
21-
var win = node.ownerDocument.defaultView;
22-
if (win && node instanceof win.Element) {
23-
var offsetKey = node.getAttribute('data-offset-key');
21+
if (node.nodeType === 1) {
22+
var element: Element = (node: any);
23+
var offsetKey = element.getAttribute('data-offset-key');
2424
if (offsetKey) {
2525
return offsetKey;
2626
}
27-
for (var ii = 0; ii < node.childNodes.length; ii++) {
28-
var childOffsetKey = getSelectionOffsetKeyForNode(node.childNodes[ii]);
27+
for (var ii = 0; ii < element.childNodes.length; ii++) {
28+
var childOffsetKey = getSelectionOffsetKeyForNode(element.childNodes[ii]);
2929
if (childOffsetKey) {
3030
return childOffsetKey;
3131
}

src/component/selection/setDraftEditorSelection.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ function getAnonymizedDOM(
3636
}
3737

3838
invariant(
39-
anonymized instanceof Element,
39+
anonymized.nodeType === Node.ELEMENT_NODE,
4040
'Node must be an Element if it is not a text node.',
4141
);
42-
return anonymized.outerHTML;
42+
var element: Element = (anonymized: any);
43+
return element.outerHTML;
4344
}
4445

4546
function anonymizeTextWithin(
@@ -77,15 +78,14 @@ function getAnonymizedEditorDOM(
7778
// grabbing the DOM content of the Draft editor
7879
let currentNode = node;
7980
while (currentNode) {
80-
if (
81-
currentNode instanceof Element &&
82-
currentNode.hasAttribute('contenteditable')
83-
) {
84-
// found the Draft editor container
85-
return getAnonymizedDOM(currentNode, getNodeLabels);
86-
} else {
87-
currentNode = currentNode.parentNode;
81+
if (currentNode.nodeType === 1) {
82+
const currentElement: Element = (currentNode: any);
83+
if (currentElement.hasAttribute('contenteditable')) {
84+
// found the Draft editor container
85+
return getAnonymizedDOM(currentNode, getNodeLabels);
86+
}
8887
}
88+
currentNode = currentNode.parentNode;
8989
}
9090
return 'Could not find contentEditable parent of node';
9191
}

src/model/encoding/convertFromHTMLToContentBlocks.js

Lines changed: 58 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,10 @@ function processInlineTag(
230230
currentStyle: DraftInlineStyle,
231231
): DraftInlineStyle {
232232
var styleToCheck = inlineTags[tag];
233-
var win = node.ownerDocument.defaultView || window;
234233
if (styleToCheck) {
235234
currentStyle = currentStyle.add(styleToCheck).toOrderedSet();
236-
} else if (node instanceof win.HTMLElement) {
237-
const htmlElement = node;
235+
} else if (node.nodeType === 1) {
236+
const htmlElement: HTMLElement = (node: any);
238237
currentStyle = currentStyle.withMutations(style => {
239238
const fontWeight = htmlElement.style.fontWeight;
240239
const fontStyle = htmlElement.style.fontStyle;
@@ -317,16 +316,16 @@ function containsSemanticBlockMarkup(
317316
}
318317

319318
function hasValidLinkText(link: Node): boolean {
320-
const win = link.ownerDocument.defaultView || window;
321319
invariant(
322-
link instanceof win.HTMLAnchorElement,
320+
link.nodeName === 'A',
323321
'Link must be an HTMLAnchorElement.',
324322
);
325-
var protocol = link.protocol;
323+
324+
const element: HTMLAnchorElement = (link: any);
326325
return (
327-
protocol === 'http:' ||
328-
protocol === 'https:' ||
329-
protocol === 'mailto:'
326+
element.protocol === 'http:' ||
327+
element.protocol === 'https:' ||
328+
element.protocol === 'mailto:'
330329
);
331330
}
332331

@@ -389,37 +388,35 @@ function genFragment(
389388
return {chunk: getSoftNewlineChunk(), entityMap};
390389
}
391390

392-
const win = node.ownerDocument.defaultView || window;
393-
394391
// IMG tags
395-
if (
396-
nodeName === 'img' &&
397-
node instanceof win.HTMLImageElement &&
398-
node.attributes.getNamedItem('src') &&
399-
node.attributes.getNamedItem('src').value
400-
) {
401-
const image: HTMLImageElement = node;
402-
const entityConfig = {};
392+
if (nodeName === 'img') {
393+
const image: HTMLImageElement = (node: any);
394+
if (
395+
image.attributes.getNamedItem('src') &&
396+
image.attributes.getNamedItem('src').value
397+
) {
398+
const entityConfig = {};
403399

404-
imgAttr.forEach((attr) => {
405-
const imageAttribute = image.getAttribute(attr);
406-
if (imageAttribute) {
407-
entityConfig[attr] = imageAttribute;
408-
}
409-
});
410-
// Forcing this node to have children because otherwise no entity will be
411-
// created for this node.
412-
// The child text node cannot just have a space or return as content -
413-
// we strip those out.
414-
// See https://github.com/facebook/draft-js/issues/231 for some context.
415-
node.textContent = '\ud83d\udcf7';
416-
417-
// TODO: update this when we remove DraftEntity entirely
418-
inEntity = DraftEntity.__create(
419-
'IMAGE',
420-
'MUTABLE',
421-
entityConfig || {},
422-
);
400+
imgAttr.forEach((attr) => {
401+
const imageAttribute = image.getAttribute(attr);
402+
if (imageAttribute) {
403+
entityConfig[attr] = imageAttribute;
404+
}
405+
});
406+
// Forcing this node to have children because otherwise no entity will be
407+
// created for this node.
408+
// The child text node cannot just have a space or return as content -
409+
// we strip those out.
410+
// See https://github.com/facebook/draft-js/issues/231 for some context.
411+
node.textContent = '\ud83d\udcf7';
412+
413+
// TODO: update this when we remove DraftEntity entirely
414+
inEntity = DraftEntity.__create(
415+
'IMAGE',
416+
'MUTABLE',
417+
entityConfig || {},
418+
);
419+
}
423420
}
424421

425422
var chunk = getEmptyChunk();
@@ -465,30 +462,30 @@ function genFragment(
465462
var entityId: ?string = null;
466463

467464
while (child) {
468-
if (
469-
child instanceof win.HTMLAnchorElement &&
470-
child.href &&
471-
hasValidLinkText(child)
472-
) {
473-
const anchor: HTMLAnchorElement = child;
474-
const entityConfig = {};
475-
476-
anchorAttr.forEach((attr) => {
477-
const anchorAttribute = anchor.getAttribute(attr);
478-
if (anchorAttribute) {
479-
entityConfig[attr] = anchorAttribute;
480-
}
481-
});
465+
entityId = null;
466+
if (nodeName === 'a') {
467+
const anchor: HTMLAnchorElement = (child: any);
468+
if (
469+
anchor.href &&
470+
hasValidLinkText(anchor)
471+
) {
472+
const entityConfig = {};
473+
474+
anchorAttr.forEach((attr) => {
475+
const anchorAttribute = anchor.getAttribute(attr);
476+
if (anchorAttribute) {
477+
entityConfig[attr] = anchorAttribute;
478+
}
479+
});
482480

483-
entityConfig.url = new URI(anchor.href).toString();
484-
// TODO: update this when we remove DraftEntity completely
485-
entityId = DraftEntity.__create(
486-
'LINK',
487-
'MUTABLE',
488-
entityConfig || {},
489-
);
490-
} else {
491-
entityId = undefined;
481+
entityConfig.url = new URI(anchor.href).toString();
482+
// TODO: update this when we remove DraftEntity completely
483+
entityId = DraftEntity.__create(
484+
'LINK',
485+
'MUTABLE',
486+
entityConfig || {},
487+
);
488+
}
492489
}
493490

494491
const {

0 commit comments

Comments
 (0)