Skip to content

Commit 3842167

Browse files
committed
Add first contenteditable tests
1 parent bad02ce commit 3842167

File tree

3 files changed

+77
-6
lines changed

3 files changed

+77
-6
lines changed

src/input/ContentEditableInput.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ export default class ContentEditableInput {
3434
on(div, "paste", e => {
3535
if (signalDOMEvent(cm, e) || handlePaste(e, cm)) return
3636
// IE doesn't fire input events, so we schedule a read for the pasted content in this way
37-
if (ie_version <= 11) setTimeout(operation(cm, () => {
38-
if (!input.pollContent()) regChange(cm)
39-
}), 20)
37+
if (ie_version <= 11) setTimeout(operation(cm, () => this.updateFromDOM()), 20)
4038
})
4139

4240
on(div, "compositionstart", e => {
@@ -303,7 +301,7 @@ export default class ContentEditableInput {
303301
if (!this.composing) return
304302
clearTimeout(this.readDOMTimeout)
305303
this.composing = null
306-
if (!this.pollContent()) regChange(this.cm)
304+
this.updateFromDOM()
307305
this.div.blur()
308306
this.div.focus()
309307
}
@@ -315,11 +313,15 @@ export default class ContentEditableInput {
315313
if (this.composing.done) this.composing = null
316314
else return
317315
}
318-
if (this.cm.isReadOnly() || !this.pollContent())
319-
runInOp(this.cm, () => regChange(this.cm))
316+
this.updateFromDOM()
320317
}, 80)
321318
}
322319

320+
updateFromDOM() {
321+
if (this.cm.isReadOnly() || !this.pollContent())
322+
runInOp(this.cm, () => regChange(this.cm))
323+
}
324+
323325
setUneditable(node) {
324326
node.contentEditable = "false"
325327
}

test/contenteditable_test.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
(function() {
2+
"use strict";
3+
4+
namespace = "contenteditable_";
5+
var Pos = CodeMirror.Pos
6+
7+
function findTextNode(dom, text) {
8+
if (dom instanceof CodeMirror) dom = dom.getInputField()
9+
if (dom.nodeType == 1) {
10+
for (var ch = dom.firstChild; ch; ch = ch.nextSibling) {
11+
var found = findTextNode(ch, text)
12+
if (found) return found
13+
}
14+
} else if (dom.nodeType == 3 && dom.nodeValue == text) {
15+
return dom
16+
}
17+
}
18+
19+
function lineElt(node) {
20+
for (;;) {
21+
var parent = node.parentNode
22+
if (/CodeMirror-code/.test(parent.className)) return node
23+
node = parent
24+
}
25+
}
26+
27+
testCM("insert_text", function(cm) {
28+
findTextNode(cm, "foobar").nodeValue = "foo bar"
29+
cm.display.input.updateFromDOM()
30+
eq(cm.getValue(), "foo bar")
31+
}, {inputStyle: "contenteditable", value: "foobar"})
32+
33+
testCM("split_line", function(cm) {
34+
cm.setSelection(Pos(2, 3))
35+
var node = findTextNode(cm, "foobar")
36+
node.nodeValue = "foo"
37+
var lineNode = lineElt(node)
38+
lineNode.parentNode.insertBefore(document.createElement("pre"), lineNode.nextSibling).textContent = "bar"
39+
cm.display.input.updateFromDOM()
40+
eq(cm.getValue(), "one\ntwo\nfoo\nbar\nthree\nfour\n")
41+
}, {inputStyle: "contenteditable", value: "one\ntwo\nfoobar\nthree\nfour\n"})
42+
43+
testCM("join_line", function(cm) {
44+
cm.setSelection(Pos(2, 3))
45+
var node = findTextNode(cm, "foo")
46+
node.nodeValue = "foobar"
47+
var lineNode = lineElt(node)
48+
lineNode.parentNode.removeChild(lineNode.nextSibling)
49+
cm.display.input.updateFromDOM()
50+
eq(cm.getValue(), "one\ntwo\nfoobar\nthree\nfour\n")
51+
}, {inputStyle: "contenteditable", value: "one\ntwo\nfoo\nbar\nthree\nfour\n"})
52+
53+
testCM("delete_multiple", function(cm) {
54+
cm.setSelection(Pos(1, 3), Pos(4, 0))
55+
var text = findTextNode(cm, "two"), startLine = lineElt(text)
56+
for (var i = 0; i < 3; i++)
57+
startLine.parentNode.removeChild(startLine.nextSibling)
58+
text.nodeValue = "twothree"
59+
cm.display.input.updateFromDOM()
60+
eq(cm.getValue(), "one\ntwothree\nfour\n")
61+
}, {inputStyle: "contenteditable", value: "one\ntwo\nfoo\nbar\nthree\nfour\n"})
62+
63+
testCM("force_redraw", function(cm) {
64+
findTextNode(cm, "foo").parentNode.appendChild(document.createElement("hr")).className = "inserted"
65+
cm.display.input.updateFromDOM()
66+
eq(byClassName(cm.getInputField(), "inserted").length, 0)
67+
}, {inputStyle: "contenteditable", value: "foo"})
68+
})();

test/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ <h2>Test Suite</h2>
103103
<script src="test.js"></script>
104104
<script src="doc_test.js"></script>
105105
<script src="multi_test.js"></script>
106+
<script src="contenteditable_test.js"></script>
106107
<script src="scroll_test.js"></script>
107108
<script src="comment_test.js"></script>
108109
<script src="search_test.js"></script>

0 commit comments

Comments
 (0)