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
add checks for other elements
  • Loading branch information
haikyuu committed Sep 20, 2018
commit 4dedfb3d4214ba2423b9f608a29e24d1b9dbdc95
2 changes: 1 addition & 1 deletion src/component/handlers/drag/DraftEditorDragHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function getSelectionForEvent(
/* $FlowFixMe(>=0.68.0 site=www,mobile) This comment suppresses an error
* found when Flow v0.68 was deployed. To see the error delete this comment
* and run Flow. */
const {ownerDocument} = event.target;
const {ownerDocument} = event.currentTarget;

if (typeof ownerDocument.caretRangeFromPoint === 'function') {
const dropRange = ownerDocument.caretRangeFromPoint(event.x, event.y);
Expand Down
4 changes: 2 additions & 2 deletions src/component/handlers/edit/editOnCut.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import type DraftEditor from 'DraftEditor.react';
const DraftModifier = require('DraftModifier');
const EditorState = require('EditorState');
const Style = require('Style');

const isNode = require('isNode');
const getFragmentFromSelection = require('getFragmentFromSelection');
const getScrollPosition = require('getScrollPosition');

Expand All @@ -45,7 +45,7 @@ function editOnCut(editor: DraftEditor, e: SyntheticClipboardEvent<>): void {

// Track the current scroll position so that it can be forced back in place
// after the editor regains control of the DOM.
if (element instanceof Node) {
if (isNode(element)) {
scrollPosition = getScrollPosition(Style.getScrollParent(element));
}

Expand Down
8 changes: 8 additions & 0 deletions src/component/utils/isHTMLAnchorElement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function isHTMLAnchorElement(node) {
if (!node || !node.ownerDocument || !node.ownerDocument.defaultView) {
return false;
}
return node instanceof node.ownerDocument.defaultView.HTMLAnchorElement;
}

module.exports = isHTMLAnchorElement;
8 changes: 8 additions & 0 deletions src/component/utils/isHTMLImageElement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function isHTMLImageElement(node) {
if (!node || !node.ownerDocument || !node.ownerDocument.defaultView) {
return false;
}
return node instanceof node.ownerDocument.defaultView.HTMLImageElement;
}

module.exports = isHTMLImageElement;
8 changes: 8 additions & 0 deletions src/component/utils/isNode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function isNode(node) {
if (!node || !node.ownerDocument || !node.ownerDocument.defaultView) {
return false;
}
return node instanceof node.ownerDocument.defaultView.Node;
}

module.exports = isNode;
15 changes: 5 additions & 10 deletions src/model/encoding/convertFromHTMLToContentBlocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ const cx = require('cx');
const generateRandomKey = require('generateRandomKey');
const getSafeBodyFromHTML = require('getSafeBodyFromHTML');
const isHTMLElement = require('isHTMLElement');
const isHTMLAnchorElement = require('isHTMLAnchorElement');
const isHTMLImageElement = require('isHTMLImageElement');
const gkx = require('gkx');
const invariant = require('invariant');
const sanitizeDraftText = require('sanitizeDraftText');
Expand Down Expand Up @@ -284,10 +286,7 @@ const containsSemanticBlockMarkup = (
};

const hasValidLinkText = (link: Node): boolean => {
invariant(
link instanceof HTMLAnchorElement,
'Link must be an HTMLAnchorElement.',
);
invariant(isHTMLAnchorElement(link), 'Link must be an HTMLAnchorElement.');
const protocol = link.protocol;
return (
protocol === 'http:' || protocol === 'https:' || protocol === 'mailto:'
Expand Down Expand Up @@ -432,7 +431,7 @@ const genFragment = (
// IMG tags
if (
nodeName === 'img' &&
node instanceof HTMLImageElement &&
isHTMLImageElement(node) &&
node.attributes.getNamedItem('src') &&
node.attributes.getNamedItem('src').value
) {
Expand Down Expand Up @@ -510,11 +509,7 @@ const genFragment = (
let entityId: ?string = null;

while (child) {
if (
child instanceof HTMLAnchorElement &&
child.href &&
hasValidLinkText(child)
) {
if (isHTMLAnchorElement(child) && child.href && hasValidLinkText(child)) {
const anchor: HTMLAnchorElement = child;
const entityConfig = {};

Expand Down
11 changes: 6 additions & 5 deletions src/model/encoding/convertFromHTMLToContentBlocks2.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ const DefaultDraftBlockRenderMap = require('DefaultDraftBlockRenderMap');
const DraftEntity = require('DraftEntity');
const {List, Map, OrderedSet} = require('immutable');
const URI = require('URI');

const isHTMLAnchorElement = require('isHTMLAnchorElement');
const isHTMLImageElement = require('isHTMLImageElement');
const cx = require('cx');
const generateRandomKey = require('generateRandomKey');
const getSafeBodyFromHTML = require('getSafeBodyFromHTML');
Expand Down Expand Up @@ -136,7 +137,7 @@ const getListItemDepth = (node: HTMLElement, depth: number = 0): number => {
*/
const isValidAnchor = (node: Node) => {
return !!(
node instanceof HTMLAnchorElement &&
isHTMLAnchorElement(node) &&
node.href &&
(node.protocol === 'http:' ||
node.protocol === 'https:' ||
Expand All @@ -150,7 +151,7 @@ const isValidAnchor = (node: Node) => {
*/
const isValidImage = (node: Node): boolean => {
return !!(
node instanceof HTMLImageElement &&
isHTMLImageElement(node) &&
node.attributes.getNamedItem('src') &&
node.attributes.getNamedItem('src').value
);
Expand Down Expand Up @@ -515,7 +516,7 @@ class ContentBlocksBuilder {
* Add the content of an HTML img node to the internal state
*/
_addImgNode(node: Node) {
if (!(node instanceof HTMLImageElement)) {
if (!isHTMLImageElement(node)) {
return;
}
const image: HTMLImageElement = node;
Expand Down Expand Up @@ -557,7 +558,7 @@ class ContentBlocksBuilder {
_addAnchorNode(node: Node, blockConfigs: Array<ContentBlockConfig>) {
// The check has already been made by isValidAnchor but
// we have to do it again to keep flow happy.
if (!(node instanceof HTMLAnchorElement)) {
if (!isHTMLAnchorElement(node)) {
return;
}
const anchor: HTMLAnchorElement = node;
Expand Down