1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-22 06:53:25 +02:00

Use strict equality except for null/undefined for improved performance and behavior (#2514)

* Convert Non-strict to strict equality checking
Convert non-strict equality checking, using `==`, to the strict version, using `===`.

* Convert Non-strict to strict equality checking
Convert non-strict equality checking, using `==`, to the strict version, using `===`.

* Convert Non-strict to strict equality checking
Convert non-strict equality checking, using `==`, to the strict version, using `===`.

* Convert Non-strict to strict equality checking
Convert non-strict equality checking, using `==`, to the strict version, using `===`.

* Convert Non-strict to strict equality checking
Convert non-strict equality checking, using `==`, to the strict version, using `===`.

* Convert Non-strict to strict equality checking
Convert non-strict equality checking, using `==`, to the strict version, using `===`.

* Convert Non-strict to strict equality checking
Convert non-strict equality checking, using `==`, to the strict version, using `===`.

* Convert Non-strict to strict equality checking
Convert non-strict equality checking, using `==`, to the strict version, using `===`.

* Convert Non-strict to strict equality checking
Convert non-strict equality checking, using `==`, to the strict version, using `===`.

* Convert Non-strict to strict equality checking
Convert non-strict equality checking, using `==`, to the strict version, using `===`.

* Convert Non-strict to strict equality checking
Convert non-strict equality checking, using `==`, to the strict version, using `===`.

* Convert Non-strict to strict equality checking
Convert non-strict equality checking, using `==`, to the strict version, using `===`.

* Convert Non-strict to strict equality checking
Convert non-strict equality checking, using `==`, to the strict version, using `===`.

* Convert Non-strict to strict equality checking
Convert non-strict equality checking, using `==`, to the strict version, using `===`.

* Convert Non-strict to strict equality checking
Convert non-strict equality checking, using `==`, to the strict version, using `===`.

* Convert Non-strict to strict equality checking
Convert non-strict equality checking, using `==`, to the strict version, using `===`.

* Convert Non-strict to strict equality checking
Convert non-strict equality checking, using `==`, to the strict version, using `===`.

* Convert Non-strict to strict equality checking
Convert non-strict equality checking, using `==`, to the strict version, using `===`.

* Convert Non-strict to strict equality checking
Convert non-strict equality checking, using `==`, to the strict version, using `===`.

* Convert Non-strict to strict equality checking
Convert non-strict equality checking, using `==`, to the strict version, using `===`.

* Convert Non-strict to strict equality checking
Convert non-strict equality checking, using `==`, to the strict version, using `===`.

* Convert Non-strict to strict equality checking
Convert non-strict equality checking, using `==`, to the strict version, using `===`.

* Convert Non-strict to strict equality checking
Convert non-strict equality checking, using `==`, to the strict version, using `===`.

* Convert Non-strict to strict equality checking
Convert non-strict equality checking, using `==`, to the strict version, using `===`.

* Convert Non-strict to strict equality checking
Convert non-strict equality checking, using `==`, to the strict version, using `===`.

* Convert Non-strict to strict equality checking
Convert non-strict equality checking, using `==`, to the strict version, using `===`.

* Convert Non-strict to strict equality checking
Convert non-strict equality checking, using `==`, to the strict version, using `===`.

* Convert Non-strict to strict equality checking
Convert non-strict equality checking, using `==`, to the strict version, using `===`.

* Convert Non-strict to strict equality checking
Convert non-strict equality checking, using `==`, to the strict version, using `===`.

* Convert Non-strict to strict equality checking
Convert non-strict equality checking, using `==`, to the strict version, using `===`.

* Convert Non-strict to strict equality checking
Convert non-strict equality checking, using `==`, to the strict version, using `===`.

* Convert Non-strict to strict equality checking
Convert non-strict equality checking, using `==`, to the strict version, using `===`.

* Convert Non-strict to strict equality checking
Convert non-strict equality checking, using `==`, to the strict version, using `===`.

* Convert Non-strict to strict equality checking
Convert non-strict equality checking, using `==`, to the strict version, using `===`.

* Convert Non-strict to strict equality checking
Convert non-strict equality checking, using `==`, to the strict version, using `===`.

* Convert Non-strict to strict equality checking
Convert non-strict equality checking, using `==`, to the strict version, using `===`.

* Convert Non-strict to strict equality checking
Convert non-strict equality checking, using `==`, to the strict version, using `===`.

* Convert Non-strict to strict equality checking
Convert non-strict equality checking, using `==`, to the strict version, using `===`.

* Convert Non-strict to strict equality checking
Convert non-strict equality checking, using `==`, to the strict version, using `===`.

* Convert Non-strict to strict equality checking
Convert non-strict equality checking, using `==`, to the strict version, using `===`.

* undo strict null checking

* remove added semicolons

* Prettier Formatting Changes

* Update .eslintrc

add smart strict equality rule

* Convert Non-strict to strict equality checking
Convert non-strict equality checking, using `==`, to the strict version, using `===`.

* Convert Non-strict to strict equality checking
Convert non-strict equality checking, using `==`, to the strict version, using `===`.

* Convert Non-strict to strict equality checking
Convert non-strict equality checking, using `==`, to the strict version, using `===`.

* Convert Non-strict to strict equality checking
Convert non-strict equality checking, using `==`, to the strict version, using `===`.

* Convert files to use strict equality

* fix prettier eslint errors

* fix remaining prettier complaints

* use strict null equality in serializer
This commit is contained in:
Dominic Amato
2019-03-06 15:48:41 -06:00
committed by Ian Storm Taylor
parent bd7ee85c9b
commit 3dce916074
96 changed files with 341 additions and 338 deletions

View File

@@ -202,14 +202,14 @@ class Content extends React.Component {
// to check both orientations here. (2017/10/31)
if (current) {
if (
(startContainer == current.startContainer &&
startOffset == current.startOffset &&
endContainer == current.endContainer &&
endOffset == current.endOffset) ||
(startContainer == current.endContainer &&
startOffset == current.endOffset &&
endContainer == current.startContainer &&
endOffset == current.startOffset)
(startContainer === current.startContainer &&
startOffset === current.startOffset &&
endContainer === current.endContainer &&
endOffset === current.endOffset) ||
(startContainer === current.endContainer &&
startOffset === current.endOffset &&
endContainer === current.startContainer &&
endOffset === current.startOffset)
) {
return
}
@@ -330,7 +330,7 @@ class Content extends React.Component {
// programmatically while updating selection.
if (
this.tmp.isUpdatingSelection &&
(handler == 'onSelect' || handler == 'onBlur' || handler == 'onFocus')
(handler === 'onSelect' || handler === 'onBlur' || handler === 'onFocus')
) {
return
}
@@ -340,7 +340,7 @@ class Content extends React.Component {
// cases we don't need to trigger any changes, since our internal model is
// already up to date, but we do want to update the native selection again
// to make sure it is in sync. (2017/10/16)
if (handler == 'onSelect') {
if (handler === 'onSelect') {
const { editor } = this.props
const { value } = editor
const { selection } = value
@@ -356,13 +356,13 @@ class Content extends React.Component {
// Don't handle drag and drop events coming from embedded editors.
if (
handler == 'onDragEnd' ||
handler == 'onDragEnter' ||
handler == 'onDragExit' ||
handler == 'onDragLeave' ||
handler == 'onDragOver' ||
handler == 'onDragStart' ||
handler == 'onDrop'
handler === 'onDragEnd' ||
handler === 'onDragEnter' ||
handler === 'onDragExit' ||
handler === 'onDragLeave' ||
handler === 'onDragOver' ||
handler === 'onDragStart' ||
handler === 'onDrop'
) {
const closest = event.target.closest('[data-slate-editor]')
@@ -374,18 +374,18 @@ class Content extends React.Component {
// Some events require being in editable in the editor, so if the event
// target isn't, ignore them.
if (
handler == 'onBeforeInput' ||
handler == 'onBlur' ||
handler == 'onCompositionEnd' ||
handler == 'onCompositionStart' ||
handler == 'onCopy' ||
handler == 'onCut' ||
handler == 'onFocus' ||
handler == 'onInput' ||
handler == 'onKeyDown' ||
handler == 'onKeyUp' ||
handler == 'onPaste' ||
handler == 'onSelect'
handler === 'onBeforeInput' ||
handler === 'onBlur' ||
handler === 'onCompositionEnd' ||
handler === 'onCompositionStart' ||
handler === 'onCopy' ||
handler === 'onCut' ||
handler === 'onFocus' ||
handler === 'onInput' ||
handler === 'onKeyDown' ||
handler === 'onKeyUp' ||
handler === 'onPaste' ||
handler === 'onSelect'
) {
if (!this.isInEditor(event.target)) {
return

View File

@@ -59,10 +59,10 @@ class Leaf extends React.Component {
shouldComponentUpdate(props) {
// If any of the regular properties have changed, re-render.
if (
props.index != this.props.index ||
props.marks != this.props.marks ||
props.text != this.props.text ||
props.parent != this.props.parent
props.index !== this.props.index ||
props.marks !== this.props.marks ||
props.text !== this.props.text ||
props.parent !== this.props.parent
) {
return true
}

View File

@@ -89,13 +89,13 @@ class Node extends React.Component {
// If the `readOnly` status has changed, re-render in case there is any
// user-land logic that depends on it, like nested editable contents.
if (n.readOnly != p.readOnly) return true
if (n.readOnly !== p.readOnly) return true
// If the node has changed, update. PERF: There are cases where it will have
// changed, but it's properties will be exactly the same (eg. copy-paste)
// which this won't catch. But that's rare and not a drag on performance, so
// for simplicity we just let them through.
if (n.node != p.node) return true
if (n.node !== p.node) return true
// If the selection value of the node or of some of its children has changed,
// re-render in case there is any user-land logic depends on it to render.
@@ -152,7 +152,7 @@ class Node extends React.Component {
// for text direction.
if (node.isLeafBlock()) {
const direction = node.getTextDirection()
if (direction == 'rtl') attributes.dir = 'rtl'
if (direction === 'rtl') attributes.dir = 'rtl'
}
const props = {
@@ -189,11 +189,11 @@ class Node extends React.Component {
renderNode = (child, isSelected, decorations) => {
const { block, editor, node, readOnly, isFocused } = this.props
const Component = child.object == 'text' ? Text : Node
const Component = child.object === 'text' ? Text : Node
return (
<Component
block={node.object == 'block' ? node : block}
block={node.object === 'block' ? node : block}
decorations={decorations}
editor={editor}
isSelected={isSelected}

View File

@@ -77,14 +77,14 @@ class Text extends React.Component {
// changed, but it's properties will be exactly the same (eg. copy-paste)
// which this won't catch. But that's rare and not a drag on performance, so
// for simplicity we just let them through.
if (n.node != p.node) return true
if (n.node !== p.node) return true
// If the node parent is a block node, and it was the last child of the
// block, re-render to cleanup extra `\n`.
if (n.parent.object == 'block') {
if (n.parent.object === 'block') {
const pLast = p.parent.nodes.last()
const nLast = n.parent.nodes.last()
if (p.node == pLast && n.node != nLast) return true
if (p.node === pLast && n.node !== nLast) return true
}
// Re-render if the current decorations have changed.

View File

@@ -58,7 +58,7 @@ class Void extends React.Component {
render() {
const { props } = this
const { children, node, readOnly } = props
const Tag = node.object == 'block' ? 'div' : 'span'
const Tag = node.object === 'block' ? 'div' : 'span'
const style = {
height: '0',
color: 'transparent',
@@ -82,7 +82,7 @@ class Void extends React.Component {
<Tag
data-slate-void
data-key={node.key}
contentEditable={readOnly || node.object == 'block' ? null : false}
contentEditable={readOnly || node.object === 'block' ? null : false}
>
{readOnly ? null : spacer}
{content}
@@ -106,7 +106,7 @@ class Void extends React.Component {
const child = node.getFirstText()
return (
<Text
block={node.object == 'block' ? node : block}
block={node.object === 'block' ? node : block}
decorations={decorations}
editor={editor}
key={child.key}

View File

@@ -133,7 +133,7 @@ function AfterPlugin(options = {}) {
// If the text was successfully inserted, and the selection had marks
// on it, unset the selection's marks.
if (selection.marks && value.document != editor.value.document) {
if (selection.marks && value.document !== editor.value.document) {
editor.select({ marks: null })
}
@@ -313,11 +313,11 @@ function AfterPlugin(options = {}) {
// needs to account for the selection's content being deleted.
if (
isDraggingInternally &&
selection.end.key == target.end.key &&
selection.end.key === target.end.key &&
selection.end.offset < target.end.offset
) {
target = target.moveForward(
selection.start.key == selection.end.key
selection.start.key === selection.end.key
? 0 - selection.end.offset + selection.start.offset
: 0 - selection.end.offset
)
@@ -329,7 +329,7 @@ function AfterPlugin(options = {}) {
editor.select(target)
if (type == 'text' || type == 'html') {
if (type === 'text' || type === 'html') {
const { anchor } = target
let hasVoidParent = document.hasVoidParent(anchor.key, editor)
@@ -353,7 +353,7 @@ function AfterPlugin(options = {}) {
}
}
if (type == 'fragment') {
if (type === 'fragment') {
editor.insertFragment(fragment)
}
@@ -539,7 +539,7 @@ function AfterPlugin(options = {}) {
const isPreviousInVoid =
previousText && document.hasVoidParent(previousText.key, editor)
if (hasVoidParent || isPreviousInVoid || startText.text == '') {
if (hasVoidParent || isPreviousInVoid || startText.text === '') {
event.preventDefault()
return editor.moveFocusBackward()
}
@@ -550,7 +550,7 @@ function AfterPlugin(options = {}) {
const isNextInVoid =
nextText && document.hasVoidParent(nextText.key, editor)
if (hasVoidParent || isNextInVoid || startText.text == '') {
if (hasVoidParent || isNextInVoid || startText.text === '') {
event.preventDefault()
return editor.moveFocusForward()
}
@@ -602,11 +602,11 @@ function AfterPlugin(options = {}) {
const transfer = getEventTransfer(event)
const { type, fragment, text } = transfer
if (type == 'fragment') {
if (type === 'fragment') {
editor.insertFragment(fragment)
}
if (type == 'text' || type == 'html') {
if (type === 'text' || type === 'html') {
if (!text) return next()
const { document, selection, startBlock } = value
if (editor.isVoid(startBlock)) return next()

View File

@@ -346,7 +346,7 @@ function BeforePlugin() {
// COMPAT: If the editor has nested editable elements, the focus can go to
// those elements. In Firefox, this must be prevented because it results in
// issues with keyboard navigation. (2017/03/30)
if (IS_FIREFOX && event.target != el) {
if (IS_FIREFOX && event.target !== el) {
el.focus()
return
}

View File

@@ -84,9 +84,9 @@ function ReactPlugin(options = {}) {
function renderNode(props, editor, next) {
const { attributes, children, node } = props
const { object } = node
if (object != 'block' && object != 'inline') return null
if (object !== 'block' && object !== 'inline') return null
const Tag = object == 'block' ? 'div' : 'span'
const Tag = object === 'block' ? 'div' : 'span'
const style = { position: 'relative' }
return (
<Tag {...attributes} style={style}>

View File

@@ -77,7 +77,7 @@ function cloneFragment(event, editor, callback = () => undefined) {
// Set a `data-slate-fragment` attribute on a non-empty node, so it shows up
// in the HTML, and can be used for intra-Slate pasting. If it's a text
// node, wrap it in a `<span>` so we have something to set an attribute on.
if (attach.nodeType == 3) {
if (attach.nodeType === 3) {
const span = window.document.createElement('span')
// COMPAT: In Chrome and Safari, if we don't add the `white-space` style

View File

@@ -68,7 +68,7 @@ function findPoint(nativeNode, nativeOffset, editor) {
// ASCII characters will be prepended to the zero-width space, so subtract 1
// from the offset to account for the zero-width space character.
if (
offset == node.textContent.length &&
offset === node.textContent.length &&
parentNode.hasAttribute(ZERO_WIDTH_ATTRIBUTE)
) {
offset--
@@ -102,15 +102,15 @@ function findPoint(nativeNode, nativeOffset, editor) {
function normalizeNodeAndOffset(node, offset) {
// If it's an element node, its offset refers to the index of its children
// including comment nodes, so try to find the right text child node.
if (node.nodeType == 1 && node.childNodes.length) {
const isLast = offset == node.childNodes.length
if (node.nodeType === 1 && node.childNodes.length) {
const isLast = offset === node.childNodes.length
const direction = isLast ? 'backward' : 'forward'
const index = isLast ? offset - 1 : offset
node = getEditableChild(node, index, direction)
// If the node has children, traverse until we have a leaf node. Leaf nodes
// can be either text nodes, or other void DOM nodes.
while (node.nodeType == 1 && node.childNodes.length) {
while (node.nodeType === 1 && node.childNodes.length) {
const i = isLast ? node.childNodes.length - 1 : 0
node = getEditableChild(node, i, direction)
}
@@ -143,9 +143,9 @@ function getEditableChild(parent, index, direction) {
// While the child is a comment node, or an element node with no children,
// keep iterating to find a sibling non-void, non-comment node.
while (
child.nodeType == 8 ||
(child.nodeType == 1 && child.childNodes.length == 0) ||
(child.nodeType == 1 && child.getAttribute('contenteditable') == 'false')
child.nodeType === 8 ||
(child.nodeType === 1 && child.childNodes.length === 0) ||
(child.nodeType === 1 && child.getAttribute('contenteditable') === 'false')
) {
if (triedForward && triedBackward) break
@@ -164,8 +164,8 @@ function getEditableChild(parent, index, direction) {
}
child = childNodes[i]
if (direction == 'forward') i++
if (direction == 'backward') i--
if (direction === 'forward') i++
if (direction === 'backward') i--
}
return child || null

View File

@@ -37,7 +37,7 @@ function getEventRange(event, editor) {
if (editor.query('isVoid', node)) {
const rect = target.getBoundingClientRect()
const isPrevious =
node.object == 'inline'
node.object === 'inline'
? x - rect.left < rect.left + rect.width - x
: y - rect.top < rect.top + rect.height - y

View File

@@ -69,7 +69,7 @@ function getEventTransfer(event) {
// Get and normalize files if they exist.
if (transfer.items && transfer.items.length) {
files = Array.from(transfer.items)
.map(item => (item.kind == 'file' ? item.getAsFile() : null))
.map(item => (item.kind === 'file' ? item.getAsFile() : null))
.filter(exists => exists)
} else if (transfer.files && transfer.files.length) {
files = Array.from(transfer.files)
@@ -97,7 +97,7 @@ function getEventTransfer(event) {
function getEmbeddedTypes(text) {
const prefix = 'SLATE-DATA-EMBED::'
if (text.substring(0, prefix.length) != prefix) {
if (text.substring(0, prefix.length) !== prefix) {
return { TEXT: text }
}
@@ -147,7 +147,7 @@ function getType(transfer, type) {
if (!transfer.types || !transfer.types.length) {
// COMPAT: In IE 11, there is no `types` field but `getData('Text')`
// is supported`. (2017/06/23)
return type == TEXT ? transfer.getData('Text') || null : null
return type === TEXT ? transfer.getData('Text') || null : null
}
// COMPAT: In Edge, transfer.types doesn't respond to `indexOf`. (2017/10/25)

View File

@@ -65,8 +65,8 @@ function scrollToSelection(selection) {
const window = getWindow(selection.anchorNode)
const scroller = findScrollContainer(selection.anchorNode, window)
const isWindow =
scroller == window.document.body ||
scroller == window.document.documentElement
scroller === window.document.body ||
scroller === window.document.documentElement
const backward = isBackward(selection)
const range = selection.getRangeAt(0).cloneRange()
@@ -79,8 +79,8 @@ function scrollToSelection(selection) {
// https://bugs.webkit.org/show_bug.cgi?id=138949
// https://bugs.chromium.org/p/chromium/issues/detail?id=435438
if (IS_SAFARI) {
if (range.collapsed && cursorRect.top == 0 && cursorRect.height == 0) {
if (range.startOffset == 0) {
if (range.collapsed && cursorRect.top === 0 && cursorRect.height === 0) {
if (range.startOffset === 0) {
range.setEnd(range.endContainer, 1)
} else {
range.setStart(range.startContainer, range.startOffset - 1)
@@ -88,7 +88,7 @@ function scrollToSelection(selection) {
cursorRect = range.getBoundingClientRect()
if (cursorRect.top == 0 && cursorRect.height == 0) {
if (cursorRect.top === 0 && cursorRect.height === 0) {
if (range.getClientRects().length) {
cursorRect = range.getClientRects()[0]
}

View File

@@ -35,10 +35,10 @@ export default function setSelectionFromDOM(window, editor, domSelection) {
if (
anchorBlock &&
!editor.isVoid(anchorBlock) &&
anchor.offset == 0 &&
anchor.offset === 0 &&
focusBlock &&
editor.isVoid(focusBlock) &&
focus.offset != 0
focus.offset !== 0
) {
range = range.setFocus(focus.setOffset(0))
}
@@ -49,7 +49,7 @@ export default function setSelectionFromDOM(window, editor, domSelection) {
if (
anchorInline &&
!editor.isVoid(anchorInline) &&
anchor.offset == anchorText.text.length
anchor.offset === anchorText.text.length
) {
const block = document.getClosestBlock(anchor.key)
const nextText = block.getNextText(anchor.key)
@@ -59,7 +59,7 @@ export default function setSelectionFromDOM(window, editor, domSelection) {
if (
focusInline &&
!editor.isVoid(focusInline) &&
focus.offset == focusText.text.length
focus.offset === focusText.text.length
) {
const block = document.getClosestBlock(focus.key)
const nextText = block.getNextText(focus.key)

View File

@@ -25,19 +25,19 @@ export default function setTextFromDomNode(window, editor, domNode) {
// Get the text information.
const { text } = leaf
let { textContent } = domNode
const isLastText = node == lastText
const isLastLeaf = leaf == lastLeaf
const isLastText = node === lastText
const isLastLeaf = leaf === lastLeaf
const lastChar = textContent.charAt(textContent.length - 1)
// COMPAT: If this is the last leaf, and the DOM text ends in a new line,
// we will have added another new line in <Leaf>'s render method to account
// for browsers collapsing a single trailing new lines, so remove it.
if (isLastText && isLastLeaf && lastChar == '\n') {
if (isLastText && isLastLeaf && lastChar === '\n') {
textContent = textContent.slice(0, -1)
}
// If the text is no different, abort.
if (textContent == text) return
if (textContent === text) return
// Determine what the selection should be after changing the text.
// const delta = textContent.length - text.length