Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81165 views
1
/**
2
* Copyright 2013-2014, Facebook, Inc.
3
* All rights reserved.
4
*
5
* This source code is licensed under the BSD-style license found in the
6
* LICENSE file in the root directory of this source tree. An additional grant
7
* of patent rights can be found in the PATENTS file in the same directory.
8
*
9
* @emails react-core
10
*/
11
12
/*jslint evil: true */
13
14
"use strict";
15
16
var getTestDocument = require('getTestDocument');
17
18
var getNodeForCharacterOffset = require('getNodeForCharacterOffset');
19
20
// Create node from HTML string
21
function createNode(html) {
22
var node = (getTestDocument() || document).createElement('div');
23
node.innerHTML = html;
24
return node;
25
}
26
27
// Check getNodeForCharacterOffset return value matches expected result.
28
function expectNodeOffset(result, textContent, nodeOffset) {
29
expect(result.node.textContent).toBe(textContent);
30
expect(result.offset).toBe(nodeOffset);
31
}
32
33
describe('getNodeForCharacterOffset', function() {
34
it('should handle siblings', function() {
35
var node = createNode('<i>123</i><i>456</i><i>789</i>');
36
37
expectNodeOffset(getNodeForCharacterOffset(node, 0), '123', 0);
38
expectNodeOffset(getNodeForCharacterOffset(node, 4), '456', 1);
39
});
40
41
it('should handle trailing chars', function() {
42
var node = createNode('<i>123</i><i>456</i><i>789</i>');
43
44
expectNodeOffset(getNodeForCharacterOffset(node, 3), '123', 3);
45
expectNodeOffset(getNodeForCharacterOffset(node, 9), '789', 3);
46
});
47
48
it('should handle trees', function() {
49
var node = createNode(
50
'<i>' +
51
'<i>1</i>' +
52
'<i>' +
53
'<i>' +
54
'<i>2</i>' +
55
'<i></i>' +
56
'</i>' +
57
'</i>' +
58
'<i>' +
59
'3' +
60
'<i>45</i>' +
61
'</i>' +
62
'</i>'
63
);
64
65
expectNodeOffset(getNodeForCharacterOffset(node, 3), '3', 1);
66
expectNodeOffset(getNodeForCharacterOffset(node, 5), '45', 2);
67
expect(getNodeForCharacterOffset(node, 10)).toBeUndefined();
68
});
69
70
it('should handle non-existent offset', function() {
71
var node = createNode('<i>123</i>');
72
73
expect(getNodeForCharacterOffset(node, -1)).toBeUndefined();
74
expect(getNodeForCharacterOffset(node, 4)).toBeUndefined();
75
});
76
});
77
78