-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #102 from inokawa/range-at-index
Fix deprecated warning caused by range-at-index
- Loading branch information
Showing
7 changed files
with
67 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
declare function RangeAtIndex( | ||
el: HTMLElement, | ||
index: number, | ||
offset: number, | ||
range?: Range | ||
): Range; | ||
|
||
export default RangeAtIndex; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* range-at-index | ||
* https://github.com/webmodules/range-at-index/ | ||
* Nathan Rajlich | ||
* | ||
* Forked from version 1.0.4; includes the following modifications: | ||
* 1) Change module.exports to export default. | ||
**/ | ||
|
||
/** | ||
* Returns a Range instance selecting text within HTML Element `el`, | ||
* at the given `start` and `end` offsets. | ||
* | ||
* @param {HTMLElement} el - DOM element to select text within | ||
* @public | ||
*/ | ||
|
||
function RangeAtIndex(el, index, offset, range) { | ||
var doc = el.ownerDocument; | ||
if (!range) range = doc.createRange(); | ||
|
||
let iterator = doc.createNodeIterator(el, NodeFilter.SHOW_TEXT, null, false); | ||
|
||
let start = {}; | ||
let end = {}; | ||
let node, val, len; | ||
|
||
while ((node = iterator.nextNode())) { | ||
val = node.nodeValue; | ||
len = val.length; | ||
|
||
if (!start.node && len > index) { | ||
start.node = node; | ||
start.offset = index; | ||
} | ||
|
||
if (!end.node && len >= offset) { | ||
end.node = node; | ||
end.offset = offset; | ||
} | ||
|
||
index -= len; | ||
offset -= len; | ||
} | ||
|
||
// update the range with the start and end offsets | ||
if (start.node) range.setStart(start.node, start.offset); | ||
if (end.node) range.setEnd(end.node, end.offset); | ||
|
||
return range; | ||
} | ||
|
||
export default RangeAtIndex; |