UNPKG

923 BJavaScriptView Raw
1/**
2 * Copyright (c) Facebook, Inc. and its affiliates.
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 * @format
8 *
9 * @emails oncall+draft_js
10 */
11'use strict';
12/**
13 * Get offset key from a node or it's child nodes. Return the first offset key
14 * found on the DOM tree of given node.
15 */
16
17var isElement = require("./isElement");
18
19function getSelectionOffsetKeyForNode(node) {
20 if (isElement(node)) {
21 var castedNode = node;
22 var offsetKey = castedNode.getAttribute('data-offset-key');
23
24 if (offsetKey) {
25 return offsetKey;
26 }
27
28 for (var ii = 0; ii < castedNode.childNodes.length; ii++) {
29 var childOffsetKey = getSelectionOffsetKeyForNode(castedNode.childNodes[ii]);
30
31 if (childOffsetKey) {
32 return childOffsetKey;
33 }
34 }
35 }
36
37 return null;
38}
39
40module.exports = getSelectionOffsetKeyForNode;
\No newline at end of file