You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
3.9 KiB

7 months ago
  1. (function() {
  2. "use strict";
  3. namespace = "contenteditable_";
  4. var Pos = CodeMirror.Pos
  5. function findTextNode(dom, text) {
  6. if (dom instanceof CodeMirror) dom = dom.getInputField()
  7. if (dom.nodeType == 1) {
  8. for (var ch = dom.firstChild; ch; ch = ch.nextSibling) {
  9. var found = findTextNode(ch, text)
  10. if (found) return found
  11. }
  12. } else if (dom.nodeType == 3 && dom.nodeValue == text) {
  13. return dom
  14. }
  15. }
  16. function lineElt(node) {
  17. for (;;) {
  18. var parent = node.parentNode
  19. if (/CodeMirror-code/.test(parent.className)) return node
  20. node = parent
  21. }
  22. }
  23. testCM("insert_text", function(cm) {
  24. findTextNode(cm, "foobar").nodeValue = "foo bar"
  25. cm.display.input.updateFromDOM()
  26. eq(cm.getValue(), "foo bar")
  27. }, {inputStyle: "contenteditable", value: "foobar"})
  28. testCM("split_line", function(cm) {
  29. cm.setSelection(Pos(2, 3))
  30. var node = findTextNode(cm, "foobar")
  31. node.nodeValue = "foo"
  32. var lineNode = lineElt(node)
  33. lineNode.parentNode.insertBefore(document.createElement("pre"), lineNode.nextSibling).textContent = "bar"
  34. cm.display.input.updateFromDOM()
  35. eq(cm.getValue(), "one\ntwo\nfoo\nbar\nthree\nfour\n")
  36. }, {inputStyle: "contenteditable", value: "one\ntwo\nfoobar\nthree\nfour\n"})
  37. testCM("join_line", function(cm) {
  38. cm.setSelection(Pos(2, 3))
  39. var node = findTextNode(cm, "foo")
  40. node.nodeValue = "foobar"
  41. var lineNode = lineElt(node)
  42. lineNode.parentNode.removeChild(lineNode.nextSibling)
  43. cm.display.input.updateFromDOM()
  44. eq(cm.getValue(), "one\ntwo\nfoobar\nthree\nfour\n")
  45. }, {inputStyle: "contenteditable", value: "one\ntwo\nfoo\nbar\nthree\nfour\n"})
  46. testCM("delete_multiple", function(cm) {
  47. cm.setSelection(Pos(1, 3), Pos(4, 0))
  48. var text = findTextNode(cm, "two"), startLine = lineElt(text)
  49. for (var i = 0; i < 3; i++)
  50. startLine.parentNode.removeChild(startLine.nextSibling)
  51. text.nodeValue = "twothree"
  52. cm.display.input.updateFromDOM()
  53. eq(cm.getValue(), "one\ntwothree\nfour\n")
  54. }, {inputStyle: "contenteditable", value: "one\ntwo\nfoo\nbar\nthree\nfour\n"})
  55. testCM("ambiguous_diff_middle", function(cm) {
  56. cm.setSelection(Pos(0, 2))
  57. findTextNode(cm, "baah").nodeValue = "baaah"
  58. cm.display.input.updateFromDOM()
  59. eqCharPos(cm.getCursor(), Pos(0, 3))
  60. }, {inputStyle: "contenteditable", value: "baah"})
  61. testCM("ambiguous_diff_start", function(cm) {
  62. cm.setSelection(Pos(0, 1))
  63. findTextNode(cm, "baah").nodeValue = "baaah"
  64. cm.display.input.updateFromDOM()
  65. eqCharPos(cm.getCursor(), Pos(0, 2))
  66. }, {inputStyle: "contenteditable", value: "baah"})
  67. testCM("ambiguous_diff_end", function(cm) {
  68. cm.setSelection(Pos(0, 3))
  69. findTextNode(cm, "baah").nodeValue = "baaah"
  70. cm.display.input.updateFromDOM()
  71. eqCharPos(cm.getCursor(), Pos(0, 4))
  72. }, {inputStyle: "contenteditable", value: "baah"})
  73. testCM("force_redraw", function(cm) {
  74. findTextNode(cm, "foo").parentNode.appendChild(document.createElement("hr")).className = "inserted"
  75. cm.display.input.updateFromDOM()
  76. eq(byClassName(cm.getInputField(), "inserted").length, 0)
  77. }, {inputStyle: "contenteditable", value: "foo"})
  78. testCM("type_on_empty_line", function(cm) {
  79. cm.setSelection(Pos(1, 0))
  80. findTextNode(cm, "\u200b").nodeValue += "hello"
  81. cm.display.input.updateFromDOM()
  82. eq(cm.getValue(), "foo\nhello\nbar")
  83. }, {inputStyle: "contenteditable", value: "foo\n\nbar"})
  84. testCM("type_after_empty_line", function(cm) {
  85. cm.setSelection(Pos(2, 0))
  86. findTextNode(cm, "bar").nodeValue = "hellobar"
  87. cm.display.input.updateFromDOM()
  88. eq(cm.getValue(), "foo\n\nhellobar")
  89. }, {inputStyle: "contenteditable", value: "foo\n\nbar"})
  90. testCM("type_before_empty_line", function(cm) {
  91. cm.setSelection(Pos(0, 3))
  92. findTextNode(cm, "foo").nodeValue = "foohello"
  93. cm.display.input.updateFromDOM()
  94. eq(cm.getValue(), "foohello\n\nbar")
  95. }, {inputStyle: "contenteditable", value: "foo\n\nbar"})
  96. })();