Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix setting selection after nested block #312

Merged
merged 1 commit into from Sep 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/trix/models/location_mapper.coffee
Expand Up @@ -63,11 +63,12 @@ class Trix.LocationMapper
else
container = node.parentNode

unless nodeIsBlockContainer(container)
while node is container.lastChild
node = container
container = container.parentNode
break if nodeIsBlockContainer(container)
unless nodeIsBlockStart(node.previousSibling)
unless nodeIsBlockContainer(container)
while node is container.lastChild
node = container
container = container.parentNode
break if nodeIsBlockContainer(container)

offset = findChildIndexOfNode(node)
offset++ unless location.offset is 0
Expand Down
42 changes: 36 additions & 6 deletions test/src/unit/location_mapper_test.coffee
Expand Up @@ -3,7 +3,7 @@
testGroup "Trix.LocationMapper", ->
test "findLocationFromContainerAndOffset", ->
setDocument [
# <trix-document>
# <trix-editor>
# 0 <div>
# 0 <!--block-->
# 1 <strong>
Expand All @@ -26,7 +26,7 @@ testGroup "Trix.LocationMapper", ->
# </span>
# 5 e
# </blockquote>
# </trix-document>
# </trix-editor>
{"text":[
{"type":"string","attributes":{"bold":true},"string":"a\n"},
{"type":"string","attributes":{"blockBreak":true},"string":"\n"}
Expand Down Expand Up @@ -88,14 +88,14 @@ testGroup "Trix.LocationMapper", ->

test "findContainerAndOffsetFromLocation: (0/0)", ->
setDocument [
# <trix-document>
# <trix-editor>
# 0 <ul>
# 0 <li>
# 0 <!--block-->
# 1 <br>
# </li>
# </ul>
# </trix-document>
# </trix-editor>
{"text":[
{"type":"string","attributes":{"blockBreak":true},"string":"\n"}
],"attributes":["bulletList","bullet"]},
Expand All @@ -109,15 +109,15 @@ testGroup "Trix.LocationMapper", ->

test "findContainerAndOffsetFromLocation after newline in formatted text", ->
setDocument [
# <trix-document>
# <trix-editor>
# 0 <div>
# 0 <!--block-->
# 0 <strong>
# 0 a
# 1 <br>
# </strong>
# </div>
# </trix-document>
# </trix-editor>
{"text":[
{"type":"string","attributes":{"bold":true},"string":"a\n"}
{"type":"string","attributes":{"blockBreak":true},"string":"\n"}
Expand All @@ -130,6 +130,36 @@ testGroup "Trix.LocationMapper", ->

assert.deepEqual mapper.findContainerAndOffsetFromLocation(location), [container, offset]

test "findContainerAndOffsetFromLocation after nested block", ->
setDocument [
# <trix-editor>
# <blockquote>
# <ul>
# <li>
# <!--block-->
# a
# </li>
# </ul>
# <!--block-->
# <br>
# </blockquote>
# </trix-editor>
{
"text":[{"type":"string","attributes":{},"string":"a"},{"type":"string","attributes":{"blockBreak":true},"string":"\n"}],
"attributes":["quote","bulletList","bullet"]
},
{
"text":[{"type":"string","attributes":{"blockBreak":true},"string":"\n"}],
"attributes":["quote"]
}
]

location = index: 1, offset: 0
container = findContainer([0])
offset = 2

assert.deepEqual mapper.findContainerAndOffsetFromLocation(location), [container, offset]

# ---
document = null
element = null
Expand Down