Prevent scrollLeft/scrollTop from going out of bounds

This commit is contained in:
David Graham & Nathan Sobo
2014-04-09 15:04:41 -06:00
committed by Nathan Sobo
parent 7fc2e0b540
commit d0a917ed14
2 changed files with 40 additions and 2 deletions

View File

@@ -955,3 +955,41 @@ describe "DisplayBuffer", ->
{start, end} = marker.getPixelRange()
expect(start.top).toBe 5 * 20
expect(start.left).toBe (4 * 10) + (6 * 11)
describe "::setScrollTop", ->
beforeEach ->
displayBuffer.setLineHeight(10)
it "disallows negative values", ->
displayBuffer.setHeight(displayBuffer.getScrollHeight() + 100)
expect(displayBuffer.setScrollTop(-10)).toBe 0
expect(displayBuffer.getScrollTop()).toBe 0
it "disallows values that would make ::getScrollBottom() exceed ::getScrollHeight()", ->
displayBuffer.setHeight(50)
maxScrollTop = displayBuffer.getScrollHeight() - displayBuffer.getHeight()
expect(displayBuffer.setScrollTop(maxScrollTop)).toBe maxScrollTop
expect(displayBuffer.getScrollTop()).toBe maxScrollTop
expect(displayBuffer.setScrollTop(maxScrollTop + 50)).toBe maxScrollTop
expect(displayBuffer.getScrollTop()).toBe maxScrollTop
describe "::setScrollLeft", ->
beforeEach ->
displayBuffer.setDefaultCharWidth(10)
it "disallows negative values", ->
displayBuffer.setWidth(displayBuffer.getScrollWidth() + 100)
expect(displayBuffer.setScrollLeft(-10)).toBe 0
expect(displayBuffer.getScrollLeft()).toBe 0
it "disallows values that would make ::getScrollRight() exceed ::getScrollWidth()", ->
displayBuffer.setWidth(50)
maxScrollLeft = displayBuffer.getScrollWidth() - displayBuffer.getWidth()
expect(displayBuffer.setScrollLeft(maxScrollLeft)).toBe maxScrollLeft
expect(displayBuffer.getScrollLeft()).toBe maxScrollLeft
expect(displayBuffer.setScrollLeft(maxScrollLeft + 50)).toBe maxScrollLeft
expect(displayBuffer.getScrollLeft()).toBe maxScrollLeft

View File

@@ -113,7 +113,7 @@ class DisplayBuffer extends Model
getScrollTop: -> @scrollTop
setScrollTop: (scrollTop) ->
@scrollTop = Math.min(@getScrollHeight() - @getHeight(), Math.max(0, scrollTop))
@scrollTop = Math.max(0, Math.min(@getScrollHeight() - @getHeight(), scrollTop))
getScrollBottom: -> @scrollTop + @height
setScrollBottom: (scrollBottom) ->
@@ -122,7 +122,7 @@ class DisplayBuffer extends Model
getScrollLeft: -> @scrollLeft
setScrollLeft: (scrollLeft) ->
@scrollLeft = Math.min(@getScrollWidth() - @getWidth(), Math.max(0, scrollLeft))
@scrollLeft = Math.max(0, Math.min(@getScrollWidth() - @getWidth(), scrollLeft))
getScrollRight: -> @scrollLeft + @width
setScrollRight: (scrollRight) ->