contenteditable_test.min.js (4038B)
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("ambiguous_diff_middle", function(cm) { 64 cm.setSelection(Pos(0, 2)) 65 findTextNode(cm, "baah").nodeValue = "baaah" 66 cm.display.input.updateFromDOM() 67 eqCharPos(cm.getCursor(), Pos(0, 3)) 68 }, {inputStyle: "contenteditable", value: "baah"}) 69 70 testCM("ambiguous_diff_start", function(cm) { 71 cm.setSelection(Pos(0, 1)) 72 findTextNode(cm, "baah").nodeValue = "baaah" 73 cm.display.input.updateFromDOM() 74 eqCharPos(cm.getCursor(), Pos(0, 2)) 75 }, {inputStyle: "contenteditable", value: "baah"}) 76 77 testCM("ambiguous_diff_end", function(cm) { 78 cm.setSelection(Pos(0, 3)) 79 findTextNode(cm, "baah").nodeValue = "baaah" 80 cm.display.input.updateFromDOM() 81 eqCharPos(cm.getCursor(), Pos(0, 4)) 82 }, {inputStyle: "contenteditable", value: "baah"}) 83 84 testCM("force_redraw", function(cm) { 85 findTextNode(cm, "foo").parentNode.appendChild(document.createElement("hr")).className = "inserted" 86 cm.display.input.updateFromDOM() 87 eq(byClassName(cm.getInputField(), "inserted").length, 0) 88 }, {inputStyle: "contenteditable", value: "foo"}) 89 90 testCM("type_on_empty_line", function(cm) { 91 cm.setSelection(Pos(1, 0)) 92 findTextNode(cm, "\u200b").nodeValue += "hello" 93 cm.display.input.updateFromDOM() 94 eq(cm.getValue(), "foo\nhello\nbar") 95 }, {inputStyle: "contenteditable", value: "foo\n\nbar"}) 96 97 testCM("type_after_empty_line", function(cm) { 98 cm.setSelection(Pos(2, 0)) 99 findTextNode(cm, "bar").nodeValue = "hellobar" 100 cm.display.input.updateFromDOM() 101 eq(cm.getValue(), "foo\n\nhellobar") 102 }, {inputStyle: "contenteditable", value: "foo\n\nbar"}) 103 104 testCM("type_before_empty_line", function(cm) { 105 cm.setSelection(Pos(0, 3)) 106 findTextNode(cm, "foo").nodeValue = "foohello" 107 cm.display.input.updateFromDOM() 108 eq(cm.getValue(), "foohello\n\nbar") 109 }, {inputStyle: "contenteditable", value: "foo\n\nbar"}) 110 })();