|
| 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 | +})(); |
0 commit comments