Skip to content
This repository was archived by the owner on Feb 6, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix verifying that nodes are instances of Element/HTMLElement
  • Loading branch information
haikyuu committed Sep 20, 2018
commit 1fa09732425c5315ab386b14f6e8f6e46a82a3ee
5 changes: 3 additions & 2 deletions src/component/base/DraftEditor.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const DraftEditorEditHandler = require('DraftEditorEditHandler');
const DraftEditorPlaceholder = require('DraftEditorPlaceholder.react');
const DraftEffects = require('DraftEffects');
const EditorState = require('EditorState');
const isHTMLElement = require('isHTMLElement');
const React = require('React');
const ReactDOM = require('ReactDOM');
const Scroll = require('Scroll');
Expand Down Expand Up @@ -486,7 +487,7 @@ class DraftEditor extends React.Component<DraftEditorProps, State> {
const {x, y} = scrollPosition || getScrollPosition(scrollParent);

invariant(
editorNode instanceof HTMLElement,
isHTMLElement(editorNode),
'editorNode is not an HTMLElement',
);

Expand All @@ -513,7 +514,7 @@ class DraftEditor extends React.Component<DraftEditorProps, State> {
blur = (): void => {
const editorNode = ReactDOM.findDOMNode(this.editor);
invariant(
editorNode instanceof HTMLElement,
isHTMLElement(editorNode),
'editorNode is not an HTMLElement',
);
editorNode.blur();
Expand Down
6 changes: 2 additions & 4 deletions src/component/contents/DraftEditorBlock.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const cx = require('cx');
const getElementPosition = require('getElementPosition');
const getScrollPosition = require('getScrollPosition');
const getViewportDimensions = require('getViewportDimensions');
const isHTMLElement = require('isHTMLElement');
const invariant = require('invariant');
const nullthrows = require('nullthrows');

Expand Down Expand Up @@ -119,10 +120,7 @@ class DraftEditorBlock extends React.Component<Props> {
);
}
} else {
invariant(
blockNode instanceof HTMLElement,
'blockNode is not an HTMLElement',
);
invariant(isHTMLElement(blockNode), 'blockNode is not an HTMLElement');
const blockBottom = blockNode.offsetHeight + blockNode.offsetTop;
const scrollBottom = scrollParent.offsetHeight + scrollPosition.y;
scrollDelta = blockBottom - scrollBottom;
Expand Down
4 changes: 3 additions & 1 deletion src/component/contents/DraftEditorTextNode.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const React = require('React');
const ReactDOM = require('ReactDOM');
const UserAgent = require('UserAgent');

const isElement = require('isElement');
const invariant = require('invariant');

// In IE, spans with <br> tags render as two newlines. By rendering a span
Expand Down Expand Up @@ -81,7 +82,8 @@ class DraftEditorTextNode extends React.Component<Props> {
shouldComponentUpdate(nextProps: Props): boolean {
const node = ReactDOM.findDOMNode(this);
const shouldBeNewline = nextProps.children === '';
invariant(node instanceof Element, 'node is not an Element');

invariant(isElement(node), 'node is not an Element');
if (shouldBeNewline) {
return !isNewline(node);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const Style = require('Style');

const getElementPosition = require('getElementPosition');
const getScrollPosition = require('getScrollPosition');
const isHTMLElement = require('isHTMLElement');
const getViewportDimensions = require('getViewportDimensions');
const Immutable = require('immutable');
const invariant = require('invariant');
Expand Down Expand Up @@ -253,10 +254,7 @@ class DraftEditorBlockNode extends React.Component<Props> {
);
}
} else {
invariant(
blockNode instanceof HTMLElement,
'blockNode is not an HTMLElement',
);
invariant(isHTMLElement(blockNode), 'blockNode is not an HTMLElement');
const blockBottom = blockNode.offsetHeight + blockNode.offsetTop;
const scrollBottom = scrollParent.offsetHeight + scrollPosition.y;
scrollDelta = blockBottom - scrollBottom;
Expand Down
6 changes: 2 additions & 4 deletions src/component/handlers/edit/editOnSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const ReactDOM = require('ReactDOM');

const getDraftEditorSelection = require('getDraftEditorSelection');
const invariant = require('invariant');
const isHTMLElement = require('isHTMLElement');

function editOnSelect(editor: DraftEditor): void {
if (
Expand All @@ -43,11 +44,8 @@ function editOnSelect(editor: DraftEditor): void {
let editorState = editor.props.editorState;
const editorNode = ReactDOM.findDOMNode(editor.editorContainer);
invariant(editorNode, 'Missing editorNode');
const windowHTMLElement = editor.props.iframeWindow
? editor.props.iframeWindow.HTMLElement
: HTMLElement;
invariant(
editorNode.firstChild instanceof windowHTMLElement,
isHTMLElement(editorNode.firstChild),
'editorNode.firstChild is not an HTMLElement',
);
const documentSelection = getDraftEditorSelection(
Expand Down
7 changes: 4 additions & 3 deletions src/component/selection/getDraftEditorSelectionWithNodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type EditorState from 'EditorState';
const findAncestorOffsetKey = require('findAncestorOffsetKey');
const getSelectionOffsetKeyForNode = require('getSelectionOffsetKeyForNode');
const getUpdatedSelectionState = require('getUpdatedSelectionState');
const isElement = require('isElement');
const invariant = require('invariant');
const nullthrows = require('nullthrows');

Expand Down Expand Up @@ -125,7 +126,7 @@ function getFirstLeaf(node: any): Node {
while (
node.firstChild &&
// data-blocks has no offset
((node.firstChild instanceof Element &&
((isElement(node.firstChild) &&
node.firstChild.getAttribute('data-blocks') === 'true') ||
getSelectionOffsetKeyForNode(node.firstChild))
) {
Expand All @@ -141,7 +142,7 @@ function getLastLeaf(node: any): Node {
while (
node.lastChild &&
// data-blocks has no offset
((node.lastChild instanceof Element &&
((isElement(node.lastChild) &&
node.lastChild.getAttribute('data-blocks') === 'true') ||
getSelectionOffsetKeyForNode(node.lastChild))
) {
Expand Down Expand Up @@ -169,7 +170,7 @@ function getPointForNonTextNode(
if (editorRoot === node) {
node = node.firstChild;
invariant(
node instanceof Element && node.getAttribute('data-contents') === 'true',
isElement(node) && node.getAttribute('data-contents') === 'true',
'Invalid DraftEditorContents structure.',
);
if (childOffset > 0) {
Expand Down
4 changes: 3 additions & 1 deletion src/component/selection/getSelectionOffsetKeyForNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
* Get offset key from a node or it's child nodes. Return the first offset key
* found on the DOM tree of given node.
*/
const isElement = require('isElement');

function getSelectionOffsetKeyForNode(node: Node): ?string {
if (node instanceof Element) {
if (isElement(node)) {
const offsetKey = node.getAttribute('data-offset-key');
if (offsetKey) {
return offsetKey;
Expand Down
8 changes: 3 additions & 5 deletions src/component/selection/setDraftEditorSelection.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const DraftJsDebugLogging = require('DraftJsDebugLogging');

const containsNode = require('containsNode');
const getActiveElement = require('getActiveElement');
const isElement = require('isElement');
const invariant = require('invariant');

function getAnonymizedDOM(
Expand All @@ -36,7 +37,7 @@ function getAnonymizedDOM(
}

invariant(
anonymized instanceof Element,
isElement(anonymized),
'Node must be an Element if it is not a text node.',
);
return anonymized.outerHTML;
Expand Down Expand Up @@ -77,10 +78,7 @@ function getAnonymizedEditorDOM(
// grabbing the DOM content of the Draft editor
let currentNode = node;
while (currentNode) {
if (
currentNode instanceof Element &&
currentNode.hasAttribute('contenteditable')
) {
if (isElement(currentNode) && currentNode.hasAttribute('contenteditable')) {
// found the Draft editor container
return getAnonymizedDOM(currentNode, getNodeLabels);
} else {
Expand Down
8 changes: 8 additions & 0 deletions src/component/utils/isElement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function isElement(node) {
if (!node || !node.ownerDocument || !node.ownerDocument.defaultView) {
return false;
}
return node instanceof node.ownerDocument.defaultView.Element;
}

module.exports = isElement;
9 changes: 9 additions & 0 deletions src/component/utils/isHTMLElement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function isHTMLElement(node) {
// the third case happens when pasting content
if (!node || !node.ownerDocument || !node.ownerDocument.defaultView) {
return false;
}
return node instanceof node.ownerDocument.defaultView.HTMLElement;
}

module.exports = isHTMLElement;
5 changes: 3 additions & 2 deletions src/model/encoding/convertFromHTMLToContentBlocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const URI = require('URI');
const cx = require('cx');
const generateRandomKey = require('generateRandomKey');
const getSafeBodyFromHTML = require('getSafeBodyFromHTML');
const isHTMLElement = require('isHTMLElement');
const gkx = require('gkx');
const invariant = require('invariant');
const sanitizeDraftText = require('sanitizeDraftText');
Expand Down Expand Up @@ -198,7 +199,7 @@ const processInlineTag = (
const styleToCheck = inlineTags[tag];
if (styleToCheck) {
currentStyle = currentStyle.add(styleToCheck).toOrderedSet();
} else if (node instanceof HTMLElement) {
} else if (isHTMLElement(node)) {
const htmlElement = node;
currentStyle = currentStyle
.withMutations(style => {
Expand Down Expand Up @@ -475,7 +476,7 @@ const genFragment = (
if (
!experimentalTreeDataSupport &&
nodeName === 'li' &&
node instanceof HTMLElement
isHTMLElement(node)
) {
depth = getListItemDepth(node, depth);
}
Expand Down
5 changes: 3 additions & 2 deletions src/model/encoding/convertFromHTMLToContentBlocks2.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import type {EntityMap} from 'EntityMap';
const CharacterMetadata = require('CharacterMetadata');
const ContentBlock = require('ContentBlock');
const ContentBlockNode = require('ContentBlockNode');
const isHTMLElement = require('isHTMLElement');
const DefaultDraftBlockRenderMap = require('DefaultDraftBlockRenderMap');
const DraftEntity = require('DraftEntity');
const {List, Map, OrderedSet} = require('immutable');
Expand Down Expand Up @@ -392,7 +393,7 @@ class ContentBlocksBuilder {

if (
!experimentalTreeDataSupport &&
node instanceof HTMLElement &&
isHTMLElement(node) &&
(blockType === 'unordered-list-item' ||
blockType === 'ordered-list-item')
) {
Expand Down Expand Up @@ -586,7 +587,7 @@ class ContentBlocksBuilder {
* styles (font-weight, font-style and text-decoration).
*/
_updateStyleFromNodeAttributes(node: Node) {
if (!(node instanceof HTMLElement)) {
if (!isHTMLElement(node)) {
return;
}

Expand Down