Skip to content

Commit

Permalink
Adding check to see if rangy is being used as an interface, and using…
Browse files Browse the repository at this point in the history
… that if possible.

per openannotation#4
  • Loading branch information
Bill Hunt committed Sep 10, 2014
1 parent b1827dd commit 7e3dd65
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/range.coffee
Expand Up @@ -278,7 +278,10 @@ class Range.NormalizedRange
#
# Returns a Range object.
toRange: ->
range = document.createRange()
# If we're using the rangy library for older browsers, use that.
# Otherwise just use document.
doc = rangy ? document
range = doc.createRange()
range.setStartBefore(@start)
range.setEndAfter(@end)
range
Expand Down
5 changes: 4 additions & 1 deletion src/util.coffee
Expand Up @@ -87,7 +87,10 @@ Util.contains = (parent, child) ->
# value of the selection. What this returns is very close to what the user
# actually sees.
Util.readRangeViaSelection = (range) ->
sel = Util.getGlobal().getSelection() # Get the browser selection object
# If we're using the rangy library for older browsers, use that.
# Otherwise get window from the global space.
win = rangy ? Util.getGlobal()
sel = win.getSelection() # Get the browser selection object
sel.removeAllRanges() # clear the selection
sel.addRange range.toRange() # Select the range
sel.toString() # Read out the selection
Expand Down
8 changes: 5 additions & 3 deletions test/spec/range_spec.coffee
Expand Up @@ -214,13 +214,15 @@ describe 'Range', ->
setStartBefore: sinon.spy()
setEndAfter: sinon.spy()

sinon.stub(document, 'createRange').returns(mockRange)
doc = rangy ? document

sinon.stub(doc, 'createRange').returns(mockRange)
r.toRange()

assert(document.createRange.calledOnce)
assert(doc.createRange.calledOnce)
assert(mockRange.setStartBefore.calledOnce)
assert.isTrue(mockRange.setStartBefore.calledWith(r.start))
assert(mockRange.setEndAfter.calledOnce)
assert.isTrue(mockRange.setEndAfter.calledWith(r.end))

document.createRange.restore()
doc.createRange.restore()

0 comments on commit 7e3dd65

Please sign in to comment.