1 | /**
|
2 | * Copyright (c) 2013-present, Facebook, Inc.
|
3 | *
|
4 | * This source code is licensed under the MIT license found in the
|
5 | * LICENSE file in the root directory of this source tree.
|
6 | *
|
7 | */
|
8 |
|
9 | ;
|
10 |
|
11 | /**
|
12 | * Given any node return the first leaf node without children.
|
13 | *
|
14 | * @param {DOMElement|DOMTextNode} node
|
15 | * @return {DOMElement|DOMTextNode}
|
16 | */
|
17 |
|
18 | function getLeafNode(node) {
|
19 | while (node && node.firstChild) {
|
20 | node = node.firstChild;
|
21 | }
|
22 | return node;
|
23 | }
|
24 |
|
25 | /**
|
26 | * Get the next sibling within a container. This will walk up the
|
27 | * DOM if a node's siblings have been exhausted.
|
28 | *
|
29 | * @param {DOMElement|DOMTextNode} node
|
30 | * @return {?DOMElement|DOMTextNode}
|
31 | */
|
32 | function getSiblingNode(node) {
|
33 | while (node) {
|
34 | if (node.nextSibling) {
|
35 | return node.nextSibling;
|
36 | }
|
37 | node = node.parentNode;
|
38 | }
|
39 | }
|
40 |
|
41 | /**
|
42 | * Get object describing the nodes which contain characters at offset.
|
43 | *
|
44 | * @param {DOMElement|DOMTextNode} root
|
45 | * @param {number} offset
|
46 | * @return {?object}
|
47 | */
|
48 | function getNodeForCharacterOffset(root, offset) {
|
49 | var node = getLeafNode(root);
|
50 | var nodeStart = 0;
|
51 | var nodeEnd = 0;
|
52 |
|
53 | while (node) {
|
54 | if (node.nodeType === 3) {
|
55 | nodeEnd = nodeStart + node.textContent.length;
|
56 |
|
57 | if (nodeStart <= offset && nodeEnd >= offset) {
|
58 | return {
|
59 | node: node,
|
60 | offset: offset - nodeStart
|
61 | };
|
62 | }
|
63 |
|
64 | nodeStart = nodeEnd;
|
65 | }
|
66 |
|
67 | node = getLeafNode(getSiblingNode(node));
|
68 | }
|
69 | }
|
70 |
|
71 | module.exports = getNodeForCharacterOffset; |
\ | No newline at end of file |