1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-18 21:21:21 +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++) { for (const rule of this.rules) {
const rule = this.rules[i]
if (!rule.deserialize) continue if (!rule.deserialize) continue
const ret = rule.deserialize(element, next) const ret = rule.deserialize(element, next)
const type = typeOf(ret) const type = typeOf(ret)
@@ -354,8 +353,7 @@ class Html {
const children = node.nodes.map(this.serializeNode) const children = node.nodes.map(this.serializeNode)
for (let i = 0; i < this.rules.length; i++) { for (const rule of this.rules) {
const rule = this.rules[i]
if (!rule.serialize) continue if (!rule.serialize) continue
const ret = rule.serialize(node, children) const ret = rule.serialize(node, children)
if (ret) return addKey(ret) if (ret) return addKey(ret)
@@ -376,8 +374,7 @@ class Html {
const text = this.serializeString(string) const text = this.serializeString(string)
return leaf.marks.reduce((children, mark) => { return leaf.marks.reduce((children, mark) => {
for (let i = 0; i < this.rules.length; i++) { for (const rule of this.rules) {
const rule = this.rules[i]
if (!rule.serialize) continue if (!rule.serialize) continue
const ret = rule.serialize(mark, children) const ret = rule.serialize(mark, children)
if (ret) return addKey(ret) if (ret) return addKey(ret)
@@ -395,8 +392,7 @@ class Html {
*/ */
serializeString = (string) => { serializeString = (string) => {
for (let i = 0; i < this.rules.length; i++) { for (const rule of this.rules) {
const rule = this.rules[i]
if (!rule.serialize) continue if (!rule.serialize) continue
const ret = rule.serialize(string, string.text) const ret = rule.serialize(string, string.text)
if (ret) return ret if (ret) return ret

View File

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

View File

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

View File

@@ -59,16 +59,14 @@ let OS
if (browser) { if (browser) {
const { userAgent } = window.navigator const { userAgent } = window.navigator
for (let i = 0; i < BROWSER_RULES.length; i++) { for (const [ name, regexp ] of BROWSER_RULES) {
const [ name, regexp ] = BROWSER_RULES[i]
if (regexp.test(userAgent)) { if (regexp.test(userAgent)) {
BROWSER = name BROWSER = name
break break
} }
} }
for (let i = 0; i < OS_RULES.length; i++) { for (const [ name, regexp ] of OS_RULES) {
const [ name, regexp ] = OS_RULES[i]
if (regexp.test(userAgent)) { if (regexp.test(userAgent)) {
OS = name OS = name
break break
@@ -78,8 +76,7 @@ if (browser) {
const testEl = window.document.createElement('div') const testEl = window.document.createElement('div')
testEl.contentEditable = true testEl.contentEditable = true
for (let i = 0; i < EVENT_RULES.length; i++) { for (const [ name, testFn ] of EVENT_RULES) {
const [ name, testFn ] = EVENT_RULES[i]
EVENTS[name] = testFn(testEl) 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`. * Find a Slate node from a DOM `element`.
* *
@@ -9,7 +7,7 @@ import findClosestNode from './find-closest-node'
*/ */
function findNode(element, state) { function findNode(element, state) {
const closest = findClosestNode(element, '[data-key]') const closest = element.closest('[data-key]')
if (!closest) return null if (!closest) return null
const key = closest.getAttribute('data-key') const key = closest.getAttribute('data-key')

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,6 +1,4 @@
import Map from 'es6-map'
/** /**
* Is in development? * Is in development?
* *
@@ -67,8 +65,7 @@ const UNSET = undefined
function memoize(object, properties, options = {}) { function memoize(object, properties, options = {}) {
const { takesArguments = true } = options const { takesArguments = true } = options
for (let i = 0; i < properties.length; i++) { for (const property of properties) {
const property = properties[i]
const original = object[property] const original = object[property]
if (!original) { if (!original) {
@@ -83,12 +80,12 @@ function memoize(object, properties, options = {}) {
// If the cache key is different, previous caches must be cleared. // If the cache key is different, previous caches must be cleared.
if (CACHE_KEY !== this.__cache_key) { if (CACHE_KEY !== this.__cache_key) {
this.__cache_key = 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) { if (!this.__cache) {
this.__cache = new Map() this.__cache = new Map() // eslint-disable-line no-undef,no-restricted-globals
} }
let cachedValue let cachedValue
@@ -133,8 +130,7 @@ function memoize(object, properties, options = {}) {
*/ */
function getIn(map, keys) { function getIn(map, keys) {
for (let i = 0; i < keys.length; i++) { for (const key of keys) {
const key = keys[i]
map = map.get(key) map = map.get(key)
if (map === UNSET) return UNSET if (map === UNSET) return UNSET
} }
@@ -155,13 +151,12 @@ function setIn(map, keys, value) {
let parent = map let parent = map
let child let child
for (let i = 0; i < keys.length; i++) { for (const key of keys) {
const key = keys[i]
child = parent.get(key) child = parent.get(key)
// If the path was not created yet... // If the path was not created yet...
if (child === UNSET) { if (child === UNSET) {
child = new Map() child = new Map() // eslint-disable-line no-undef,no-restricted-globals
parent.set(key, child) 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" es5-ext "^0.10.14"
es6-symbol "^3.1" es6-symbol "^3.1"
es6-map@^0.1.3, es6-map@^0.1.4: es6-map@^0.1.3:
version "0.1.5" version "0.1.5"
resolved "https://registry.yarnpkg.com/es6-map/-/es6-map-0.1.5.tgz#9136e0503dcc06a301690f0bb14ff4e364e949f0" resolved "https://registry.yarnpkg.com/es6-map/-/es6-map-0.1.5.tgz#9136e0503dcc06a301690f0bb14ff4e364e949f0"
dependencies: dependencies: