Skip to content

Commit

Permalink
fix for getBounds around image
Browse files Browse the repository at this point in the history
  • Loading branch information
jhchen committed Mar 20, 2015
1 parent 76253ce commit aa07620
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
7 changes: 5 additions & 2 deletions src/core/editor.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,11 @@ class Editor
throw new Error('Invalid index') unless leaf?
containerBounds = @root.parentNode.getBoundingClientRect()
side = 'left'
if leaf.length == 0
if leaf.length == 0 # BR case
bounds = leaf.node.parentNode.getBoundingClientRect()
else if dom.VOID_TAGS[leaf.node.tagName]
bounds = leaf.node.getBoundingClientRect()
side = 'right' if offset == 1
else
range = document.createRange()
if offset < leaf.length
Expand All @@ -98,7 +101,7 @@ class Editor
bounds = range.getBoundingClientRect()
return {
height: bounds.height
left: bounds[side] - containerBounds.left,
left: bounds[side] - containerBounds.left
top: bounds.top - containerBounds.top
}

Expand Down
5 changes: 1 addition & 4 deletions test/unit/core/editor.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -346,23 +346,20 @@ describe('Editor', ->

describe('with image', ->
beforeEach( ->
@editor.root.innerHTML = '<div><img src="data:image/gif;base64,R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkAABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKUE1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7"/></div>'

@editor.root.innerHTML = '<div><img src="data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAACf0lEQVR42r2XS2gTURSG04K2VReilorEECVKiJk8EYuurIgPEFddKW4El1J3FbRUEOzKKuhKdy4Uql0H0UVxoYIKkoWCrxaKz1qKTayNYv0O3IEhzNzecSYz8HNnJpPz3XPm3HPuxGIRHNlstqdQKBwul8tDpVLpDprg/BV63hJgPB7vAngU0HX0BtCSh76FCs7n89sBjqJZDfS343whFHCxWNyEsZvojwb8jok9YKw77tUDwzF6CtW8wPw2zwQvMN51+f3jf4MzmcwaDIxpPBb4S8Zd6JHHM9UgIa/q4OgqObFDQq+Z4G3fcLJ77TLwBSZ4gueSACaXmeRZv2FfidGHGo9+MO7N5XJbDOBLRKjoN+Eu69Y0Xu80haO3mGzzAz+I/np4Pk3YMwLnesoALv8ZMIYnk8lOTTLNCNyyrK2mcPQerTKeAA8PenhRQ70+4T95Vbv9rvcZF0MNPD/EmNDBmeB3qYDSF7geAb7fb+KdcTMM/CTjBtXVnMAv6BY6ThfcHLjUYvS1i1ejKjJPm+7PomP8rT2UJiPvygVekXbL+X3Ne37BcwfCaDRXmuCT6XR6vWwqDJdaRVZQkAl8cPZxIrKHe9cM4Z9RX5DwF5qMnlcygY+TpN1Bwz/sMPpEst6rEjqTUBpRKAmIscfK6C/G07LuNfCG5AsrY10ocGr6ahsoPZtxzsPjRcYbUglD3VwSxn12b0efXMBfVWdMtGRbLXs4j7o/Ltttrle07CNCdT57xyNldkSWUyqV6ojiI6YN2D17wyi5EIvyIPTnFHyOUG+LFA60X9a50pGo4ZZ8QCjvL0Ud9m675kvzCK2V+qh4F9Ez+Xqhkm2MRXz8AzAAXszjgRshAAAAAElFTkSuQmCC"/></div>'
reference.image =
height: @editor.root.firstChild.firstChild.offsetHeight
width: @editor.root.firstChild.firstChild.offsetWidth
)

it('directly before image', ->
bounds = @editor.getBounds(0)

expect(bounds.height).toBeApproximately(reference.image.height, 1)
expect(bounds.left).toBeApproximately(0, 1)
)

it('directly after image', ->
bounds = @editor.getBounds(1)

expect(bounds.height).toBeApproximately(reference.image.height, 1)
expect(bounds.left).toBeApproximately(reference.image.width, 1)
)
Expand Down

0 comments on commit aa07620

Please sign in to comment.