splitText-normalize.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /** Test for Bug 191864 **/
  2. var tests_done = 0;
  3. var tests = [
  4. [ {}, [0,4], "012345678" ],
  5. [ {}, [0,0], "012345678" ],
  6. [ {}, [0,9], "012345678" ],
  7. [ {startOffset:4}, [0,4], "012345678" ],
  8. [ {startOffset:5}, [0,4], "012345678" ],
  9. [ {startOffset:5,endOffset:6}, [0,4], "012345678" ],
  10. [ {endOffset:5}, [0,4], "012345678" ],
  11. [ {endOffset:4}, [0,4], "012345678" ],
  12. [ {endOffset:3}, [0,4], "012345678" ],
  13. [ {startOffset:1,endOffset:3}, [0,4], "012345678" ],
  14. [ {startOffset:7,endOffset:7}, [0,4], "012345678" ],
  15. [ {startOffset:4,endOffset:4}, [0,4], "012345678" ],
  16. [ {endNode:1}, [0,4], "012345678", "" ],
  17. [ {endNode:1}, [0,4], "01234567", "8" ],
  18. [ {endNode:1}, [1,4], "0", "12345678" ],
  19. [ {startOffset:1,endNode:1}, [0,0], "0", "12345678" ],
  20. [ {endNode:2}, [1,4], "0", "12345", "678" ],
  21. ]
  22. function runtest(f,t,nosplit) {
  23. // create content
  24. let doc = f.contentDocument;
  25. for (let i = 2; i < t.length; ++i) {
  26. c = doc.createTextNode(t[i]);
  27. doc.body.appendChild(c);
  28. }
  29. // setup selection
  30. let sel = t[0]
  31. let startNode = sel.startNode === undefined ? doc.body.firstChild : doc.body.childNodes[sel.startNode];
  32. let startOffset = sel.startOffset === undefined ? 0 : sel.startOffset;
  33. let endNode = sel.endNode === undefined ? startNode : doc.body.childNodes[sel.endNode];
  34. let endOffset = sel.endOffset === undefined ? endNode.length : sel.endOffset;
  35. let selection = f.contentWindow.getSelection();
  36. let r = doc.createRange();
  37. r.setStart(startNode, startOffset);
  38. r.setEnd(endNode, endOffset);
  39. selection.addRange(r);
  40. // splitText
  41. let split = t[1]
  42. if (!nosplit)
  43. doc.body.childNodes[split[0]].splitText(split[1])
  44. }
  45. function test_ref(f,t,nosplit) {
  46. runtest(f,t,true);
  47. }
  48. function test_split(f,t) {
  49. runtest(f,t);
  50. }
  51. function test_split_insert(f,t) {
  52. runtest(f,t);
  53. let doc = f.contentDocument;
  54. doc.body.firstChild;
  55. let split = t[1]
  56. let text1 = doc.body.childNodes[split[0]]
  57. let text2 = doc.body.childNodes[split[0]+1]
  58. if (text2.textContent.length==0) return;
  59. let c = doc.createTextNode(text2.textContent[0]);
  60. doc.body.insertBefore(c,text2);
  61. let r = doc.createRange();
  62. r.setStart(text2, 0);
  63. r.setEnd(text2, 1);
  64. r.deleteContents();
  65. }
  66. function test_split_merge(f,t) {
  67. runtest(f,t);
  68. f.contentDocument.body.normalize();
  69. }
  70. function test_merge(f,t) {
  71. runtest(f,t,true);
  72. f.contentDocument.body.normalize();
  73. }
  74. function createFrame(run,t) {
  75. let f = document.createElement('iframe');
  76. f.setAttribute('height','22');
  77. f.setAttribute('frameborder','0');
  78. f.setAttribute('width','200');
  79. f.src = 'data:text/html,<body style="margin:0;padding:0">';
  80. f.onload = function () { try { run(f, t); } finally { ++tests_done; } }
  81. return f;
  82. }