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