1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-29 18:09:49 +02:00

fix get*AtRange methods for unset ranges, fixes #1002 (#1068)

This commit is contained in:
Ian Storm Taylor
2017-09-06 18:58:47 -07:00
committed by GitHub
parent ca514eee66
commit 0db3ac7222

View File

@@ -360,6 +360,8 @@ class Node {
getBlocksAtRangeAsArray(range) {
range = range.normalize(this)
if (range.isUnset) return []
const { startKey, endKey } = range
const startBlock = this.getClosestBlock(startKey)
@@ -451,6 +453,9 @@ class Node {
*/
getCharactersAtRangeAsArray(range) {
range = range.normalize(this)
if (range.isUnset) return []
return this
.getTextsAtRange(range)
.reduce((arr, text) => {
@@ -689,10 +694,13 @@ class Node {
* Get a fragment of the node at a `range`.
*
* @param {Selection} range
* @return {List<Node>}
* @return {Document}
*/
getFragmentAtRange(range) {
range = range.normalize(this)
if (range.isUnset) return Document.create()
let node = this
// Make sure the children exist.
@@ -875,6 +883,9 @@ class Node {
*/
getInlinesAtRangeAsArray(range) {
range = range.normalize(this)
if (range.isUnset) return []
return this
.getTextsAtRangeAsArray(range)
.map(text => this.getClosestInline(text.key))
@@ -1026,6 +1037,8 @@ class Node {
getMarksAtRangeAsArray(range) {
range = range.normalize(this)
if (range.isUnset) return []
const { startKey, startOffset } = range
// If the range is collapsed at the start of the node, check the previous.
@@ -1054,6 +1067,8 @@ class Node {
getActiveMarksAtRangeAsArray(range) {
range = range.normalize(this)
if (range.isUnset) return []
const { startKey, startOffset } = range
// If the range is collapsed at the start of the node, check the previous.
@@ -1234,6 +1249,10 @@ class Node {
getOffsetAtRange(range) {
range = range.normalize(this)
if (range.isUnset) {
throw new Error('The range cannot be unset to calculcate its offset.')
}
if (range.isExpanded) {
throw new Error('The range must be collapsed to calculcate its offset.')
}
@@ -1443,6 +1462,8 @@ class Node {
getTextsAtRangeAsArray(range) {
range = range.normalize(this)
if (range.isUnset) return []
const { startKey, endKey } = range
const startText = this.getDescendant(startKey)