1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-17 20:51:20 +02:00

remove some polyfills (#1288)

* remove some polyfills

* use for-of for iteration when possible
This commit is contained in:
Ian Storm Taylor
2017-10-25 17:55:18 -07:00
committed by GitHub
parent 509d3d50fc
commit ed593c732b
12 changed files with 28 additions and 89 deletions

View File

@@ -256,8 +256,7 @@ class Html {
}
}
for (let i = 0; i < this.rules.length; i++) {
const rule = this.rules[i]
for (const rule of this.rules) {
if (!rule.deserialize) continue
const ret = rule.deserialize(element, next)
const type = typeOf(ret)
@@ -354,8 +353,7 @@ class Html {
const children = node.nodes.map(this.serializeNode)
for (let i = 0; i < this.rules.length; i++) {
const rule = this.rules[i]
for (const rule of this.rules) {
if (!rule.serialize) continue
const ret = rule.serialize(node, children)
if (ret) return addKey(ret)
@@ -376,8 +374,7 @@ class Html {
const text = this.serializeString(string)
return leaf.marks.reduce((children, mark) => {
for (let i = 0; i < this.rules.length; i++) {
const rule = this.rules[i]
for (const rule of this.rules) {
if (!rule.serialize) continue
const ret = rule.serialize(mark, children)
if (ret) return addKey(ret)
@@ -395,8 +392,7 @@ class Html {
*/
serializeString = (string) => {
for (let i = 0; i < this.rules.length; i++) {
const rule = this.rules[i]
for (const rule of this.rules) {
if (!rule.serialize) continue
const ret = rule.serialize(string, string.text)
if (ret) return ret

View File

@@ -8,7 +8,6 @@ import logger from 'slate-dev-logger'
import EVENT_HANDLERS from '../constants/event-handlers'
import Node from './node'
import findClosestNode from '../utils/find-closest-node'
import findDOMRange from '../utils/find-dom-range'
import findRange from '../utils/find-range'
import scrollToSelection from '../utils/scroll-to-selection'
@@ -210,7 +209,7 @@ class Content extends React.Component {
const el = target.nodeType === 3 ? target.parentNode : target
return (
(el.isContentEditable) &&
(el === element || findClosestNode(el, '[data-slate-editor]') === element)
(el === element || el.closest('[data-slate-editor]') === element)
)
}
@@ -269,7 +268,7 @@ class Content extends React.Component {
handler == 'onDragStart'
) {
const { target } = event
const targetEditorNode = findClosestNode(target, '[data-slate-editor]')
const targetEditorNode = target.closest('[data-slate-editor]')
if (targetEditorNode !== this.element) return
}

View File

@@ -140,8 +140,7 @@ class Editor extends React.Component {
let isNew = false
// Check to see if any plugin-related properœties have changed.
for (let i = 0; i < PLUGINS_PROPS.length; i++) {
const prop = PLUGINS_PROPS[i]
for (const prop of PLUGINS_PROPS) {
if (props[prop] == this.props[prop]) continue
isNew = true
break
@@ -377,9 +376,8 @@ function resolvePlugins(props) {
* Mix in the property types for the event handlers.
*/
for (let i = 0; i < EVENT_HANDLERS.length; i++) {
const property = EVENT_HANDLERS[i]
Editor.propTypes[property] = Types.func
for (const prop of EVENT_HANDLERS) {
Editor.propTypes[prop] = Types.func
}
/**

View File

@@ -59,16 +59,14 @@ let OS
if (browser) {
const { userAgent } = window.navigator
for (let i = 0; i < BROWSER_RULES.length; i++) {
const [ name, regexp ] = BROWSER_RULES[i]
for (const [ name, regexp ] of BROWSER_RULES) {
if (regexp.test(userAgent)) {
BROWSER = name
break
}
}
for (let i = 0; i < OS_RULES.length; i++) {
const [ name, regexp ] = OS_RULES[i]
for (const [ name, regexp ] of OS_RULES) {
if (regexp.test(userAgent)) {
OS = name
break
@@ -78,8 +76,7 @@ if (browser) {
const testEl = window.document.createElement('div')
testEl.contentEditable = true
for (let i = 0; i < EVENT_RULES.length; i++) {
const [ name, testFn ] = EVENT_RULES[i]
for (const [ name, testFn ] of EVENT_RULES) {
EVENTS[name] = testFn(testEl)
}
}

View File

@@ -1,37 +0,0 @@
/**
* Find the closest ancestor of a DOM `element` that matches a given selector.
*
* COMPAT: In IE11, the `Node.closest` method doesn't exist natively, so we
* have to polyfill it. (2017/09/06)
*
* https://developer.mozilla.org/en-US/docs/Web/API/Element/closest#Polyfill
*
* @param {Element} node
* @param {String} selector
* @return {Element}
*/
function findClosestNode(node, selector) {
if (typeof node.closest === 'function') return node.closest(selector)
const matches = (node.document || node.ownerDocument).querySelectorAll(selector)
let parentNode = node
let i
do {
i = matches.length
while (--i >= 0 && matches.item(i) !== parentNode);
}
while ((i < 0) && (parentNode = parentNode.parentElement))
return parentNode
}
/**
* Export.
*
* @type {Function}
*/
export default findClosestNode

View File

@@ -1,6 +1,4 @@
import findClosestNode from './find-closest-node'
/**
* Find a Slate node from a DOM `element`.
*
@@ -9,7 +7,7 @@ import findClosestNode from './find-closest-node'
*/
function findNode(element, state) {
const closest = findClosestNode(element, '[data-key]')
const closest = element.closest('[data-key]')
if (!closest) return null
const key = closest.getAttribute('data-key')

View File

@@ -2,7 +2,6 @@
import getWindow from 'get-window'
import OffsetKey from './offset-key'
import findClosestNode from './find-closest-node'
/**
* Constants.
@@ -32,7 +31,7 @@ function findPoint(nativeNode, nativeOffset, state) {
const window = getWindow(nativeNode)
const { parentNode } = nearestNode
let rangeNode = findClosestNode(parentNode, RANGE_SELECTOR)
let rangeNode = parentNode.closest(RANGE_SELECTOR)
let offset
let node
@@ -40,7 +39,7 @@ function findPoint(nativeNode, nativeOffset, state) {
// determine what the offset relative to the text node is.
if (rangeNode) {
const range = window.document.createRange()
const textNode = findClosestNode(rangeNode, TEXT_SELECTOR)
const textNode = rangeNode.closest(TEXT_SELECTOR)
range.setStart(textNode, 0)
range.setEnd(nearestNode, nearestOffset)
node = textNode
@@ -50,7 +49,7 @@ function findPoint(nativeNode, nativeOffset, state) {
// For void nodes, the element with the offset key will be a cousin, not an
// ancestor, so find it by going down from the nearest void parent.
else {
const voidNode = findClosestNode(parentNode, VOID_SELECTOR)
const voidNode = parentNode.closest(VOID_SELECTOR)
if (!voidNode) return null
rangeNode = voidNode.querySelector(RANGE_SELECTOR)
node = rangeNode

View File

@@ -8,7 +8,6 @@
"dependencies": {
"debug": "^2.3.2",
"direction": "^0.1.5",
"es6-map": "^0.1.4",
"esrever": "^0.2.0",
"is-empty": "^1.0.0",
"is-plain-object": "^2.0.4",

View File

@@ -649,8 +649,7 @@ class Node {
getDescendantAtPath(path) {
let descendant = this
for (let i = 0; i < path.length; i++) {
const index = path[i]
for (const index of path) {
if (!descendant) return
if (!descendant.nodes) return
descendant = descendant.nodes.get(index)

View File

@@ -76,8 +76,7 @@ class Stack extends Record(DEFAULTS) {
find(property, ...args) {
const plugins = this.getPluginsWith(property)
for (let i = 0; i < plugins.length; i++) {
const plugin = plugins[i]
for (const plugin of plugins) {
const ret = plugin[property](...args)
if (ret != null) return ret
}
@@ -95,8 +94,7 @@ class Stack extends Record(DEFAULTS) {
const plugins = this.getPluginsWith(property)
const array = []
for (let i = 0; i < plugins.length; i++) {
const plugin = plugins[i]
for (const plugin of plugins) {
const value = plugin[property](...args)
if (value != null) array.push(value)
}
@@ -114,8 +112,7 @@ class Stack extends Record(DEFAULTS) {
run(property, ...args) {
const plugins = this.getPluginsWith(property)
for (let i = 0; i < plugins.length; i++) {
const plugin = plugins[i]
for (const plugin of plugins) {
const ret = plugin[property](...args)
if (ret != null) return
}
@@ -133,8 +130,7 @@ class Stack extends Record(DEFAULTS) {
const plugins = this.getPluginsWith(property).reverse()
let { children = null } = props
for (let i = 0; i < plugins.length; i++) {
const plugin = plugins[i]
for (const plugin of plugins) {
const value = plugin[property](props, ...args)
if (value == null) continue
props.children = children = value

View File

@@ -1,6 +1,4 @@
import Map from 'es6-map'
/**
* Is in development?
*
@@ -67,8 +65,7 @@ const UNSET = undefined
function memoize(object, properties, options = {}) {
const { takesArguments = true } = options
for (let i = 0; i < properties.length; i++) {
const property = properties[i]
for (const property of properties) {
const original = object[property]
if (!original) {
@@ -83,12 +80,12 @@ function memoize(object, properties, options = {}) {
// If the cache key is different, previous caches must be cleared.
if (CACHE_KEY !== this.__cache_key) {
this.__cache_key = CACHE_KEY
this.__cache = new Map()
this.__cache = new Map() // eslint-disable-line no-undef,no-restricted-globals
}
}
if (!this.__cache) {
this.__cache = new Map()
this.__cache = new Map() // eslint-disable-line no-undef,no-restricted-globals
}
let cachedValue
@@ -133,8 +130,7 @@ function memoize(object, properties, options = {}) {
*/
function getIn(map, keys) {
for (let i = 0; i < keys.length; i++) {
const key = keys[i]
for (const key of keys) {
map = map.get(key)
if (map === UNSET) return UNSET
}
@@ -155,13 +151,12 @@ function setIn(map, keys, value) {
let parent = map
let child
for (let i = 0; i < keys.length; i++) {
const key = keys[i]
for (const key of keys) {
child = parent.get(key)
// If the path was not created yet...
if (child === UNSET) {
child = new Map()
child = new Map() // eslint-disable-line no-undef,no-restricted-globals
parent.set(key, child)
}

View File

@@ -2198,7 +2198,7 @@ es6-iterator@2, es6-iterator@^2.0.1, es6-iterator@~2.0.1:
es5-ext "^0.10.14"
es6-symbol "^3.1"
es6-map@^0.1.3, es6-map@^0.1.4:
es6-map@^0.1.3:
version "0.1.5"
resolved "https://registry.yarnpkg.com/es6-map/-/es6-map-0.1.5.tgz#9136e0503dcc06a301690f0bb14ff4e364e949f0"
dependencies: