-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy patheditor_unittest.js
129 lines (119 loc) · 5.81 KB
/
editor_unittest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict';
//
// getSelectionFromDom
//
testCaseWithSample('editor.getSelectionFromDom.1',
// editing/style/apply-style-atomic.html
'<div contenteditable>|1<progress><a style>2</a></progress></div>',
function(sample) {
var editor = editing.Editor.getOrCreate(sample.document);
var range = sample.document.createRange();
range.selectNodeContents(sample.document.querySelector('div'));
sample.document.getSelection().removeAllRanges();
sample.document.getSelection().addRange(range);
// |range| start is DIV,0 and end is DIV,2
// selection.anchor is "1",0 and focus is DIV,2
var selection = editor.getSelectionFromDom();
expectEq('1', function() { return selection.anchorNode.nodeValue; });
expectEq(0, function() { return selection.anchorOffset; });
expectEq('DIV', function() { return selection.focusNode.nodeName; });
expectEq(2, function() { return selection.focusOffset; });
expectEq(editing.SelectionDirection.ANCHOR_IS_START,
function() { return selection.direction; });
});
//
// setDomSelection
//
testCaseWithSample('editor.setDomSelection.caret.1',
'<p contenteditable>|0123</p>',
function(sample, selection) {
var editor = editing.Editor.getOrCreate(sample.document);
editor.setDomSelection(selection);
var domSelection = editor.document.getSelection();
expectEq('0123', function() { return domSelection.anchorNode.nodeValue; });
expectEq(0, function() { return domSelection.anchorOffset; });
expectEq('0123', function() { return domSelection.focusNode.nodeValue; });
expectEq(0, function() { return domSelection.focusOffset; });
});
testCaseWithSample('editor.setDomSelection.caret.2',
'<p contenteditable>0|123</p>',
function(sample, selection) {
var editor = editing.Editor.getOrCreate(sample.document);
editor.setDomSelection(selection);
var domSelection = editor.document.getSelection();
expectEq('0123', function() { return domSelection.anchorNode.nodeValue; });
expectEq(1, function() { return domSelection.anchorOffset; });
expectEq('0123', function() { return domSelection.focusNode.nodeValue; });
expectEq(1, function() { return domSelection.focusOffset; });
});
testCaseWithSample('editor.setDomSelection.caret.3',
'<p contenteditable>0123|</p>',
function(sample, selection) {
var editor = editing.Editor.getOrCreate(sample.document);
editor.setDomSelection(selection);
var domSelection = editor.document.getSelection();
expectEq('0123', function() { return domSelection.anchorNode.nodeValue; });
expectEq(4, function() { return domSelection.anchorOffset; });
expectEq('0123', function() { return domSelection.focusNode.nodeValue; });
expectEq(4, function() { return domSelection.focusOffset; });
});
testCaseWithSample('editor.setDomSelection.caret.4',
'<p contenteditable>| 0123</p>',
function(sample, selection) {
var editor = editing.Editor.getOrCreate(sample.document);
editor.setDomSelection(selection);
var domSelection = editor.document.getSelection();
expectEq(' 0123', function() { return domSelection.anchorNode.nodeValue; });
expectEq(1, function() { return domSelection.anchorOffset; });
expectEq(' 0123', function() { return domSelection.focusNode.nodeValue; });
expectEq(1, function() { return domSelection.focusOffset; });
});
testCaseWithSample('editor.setDomSelection.caret.5',
'<p contenteditable>0123 |</p>',
function(sample, selection) {
var editor = editing.Editor.getOrCreate(sample.document);
editor.setDomSelection(selection);
var domSelection = editor.document.getSelection();
expectEq('0123 ', function() { return domSelection.anchorNode.nodeValue; });
expectEq(4, function() { return domSelection.anchorOffset; });
expectEq('0123 ', function() { return domSelection.focusNode.nodeValue; });
expectEq(4, function() { return domSelection.focusOffset; });
});
testCaseWithSample('editor.setDomSelection.range.1',
'<p contenteditable>^0123|</p>',
function(sample, selection) {
var editor = editing.Editor.getOrCreate(sample.document);
editor.setDomSelection(selection);
var domSelection = editor.document.getSelection();
expectEq('0123', function() { return domSelection.anchorNode.nodeValue; });
expectEq(0, function() { return domSelection.anchorOffset; });
expectEq('0123', function() { return domSelection.focusNode.nodeValue; });
expectEq(4, function() { return domSelection.focusOffset; });
});
testCaseWithSample('editor.setDomSelection.range.2',
'<p contenteditable>0^12|3</p>',
function(sample, selection) {
var editor = editing.Editor.getOrCreate(sample.document);
editor.setDomSelection(selection);
var domSelection = editor.document.getSelection();
expectEq('0123', function() { return domSelection.anchorNode.nodeValue; });
expectEq(1, function() { return domSelection.anchorOffset; });
expectEq('0123', function() { return domSelection.focusNode.nodeValue; });
expectEq(3, function() { return domSelection.focusOffset; });
});
// Chrome does canonicalization to move boundary points to skip leading and
// trailing spaces.
testCaseWithSample('editor.setDomSelection.range.3',
'<p contenteditable>^ 0123 |</p>',
function(sample, selection) {
var editor = editing.Editor.getOrCreate(sample.document);
editor.setDomSelection(selection);
var domSelection = editor.document.getSelection();
expectEq(' 0123 ', function() { return domSelection.anchorNode.nodeValue; });
expectEq(1, function() { return domSelection.anchorOffset; });
expectEq(' 0123 ', function() { return domSelection.focusNode.nodeValue; });
expectEq(5, function() { return domSelection.focusOffset; });
});