getLeaf() should ignore non-leaf blots #3489
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Previously,
Quill#getLeaf()
can return non-leaf blots, which is unexpected. For example, it breaksSelection#getBounds()
as it callsleaf.position(offset, true)
, which is only provided by leaf blots.The reason
Quill#getLeaf()
may return a non-leaf blot is, inParentBlot#path()
, ifthis.children.find(index, inclusive)
return[null, 0]
(aka the parent blot is empty), the last element of the path would be the parent blot itself, causingScroll#leaf()
gets the parent blot.The reason that a parent blot can be empty is in
Container#optimize()
, we don't call container's children'soptimize()
function, so the children who are parent blots don't have a chance to create a default child. I haven't reproduced this in practice so not 100% sure, but programmatically a reproducible example can be found in the added test case in this PR.This PR ensures
Scroll#leaf()
always returns a leaf ornull
. In fact, this won't happen if we getContainer#optimize()
fixed, so it may be debatable whether we should do the check. However, IMO people may overrideContainer#optmize()
and forget optimizing children even we fix it on our side, so to makeScroll#leaf()
complete, a check would be helpful.