directionality.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. module( "plugins.directionality" );
  2. //1 notblockelement&&collapsed=false
  3. test( '非块元素,不闭合', function() {
  4. var editor = te.obj[0], d = editor.document, range = te.obj[1], db = editor.body;
  5. editor.setContent('xxxx<b><i>gggsiekes</i></b>');
  6. setTimeout(function () {
  7. range.selectNode(d.getElementsByTagName("strong")[0]).collapse(true).select();
  8. equal(editor.queryCommandValue('directionality'), "ltr", "ltr queryCommandValue");
  9. editor.execCommand('directionality', "rtl");
  10. equal(ua.getChildHTML(db), "<p dir=\"rtl\">xxxx<strong><em>gggsiekes</em></strong></p>", "directionrtl");
  11. equal(editor.queryCommandValue('directionality'), "rtl", "directionrtl queryCommandValue");
  12. editor.execCommand('directionality', "ltr");
  13. equal(ua.getChildHTML(db), "<p dir=\"ltr\">xxxx<strong><em>gggsiekes</em></strong></p>", "directionltr");
  14. equal(editor.queryCommandValue('directionality'), "ltr", "directionltr queryCommandValue");
  15. start();
  16. }, 20);
  17. stop();
  18. } );
  19. //2 blockelement&&collapsed=false
  20. test( '块元素,不闭合', function() {
  21. var editor = te.obj[0],d = editor.document,range = te.obj[1],db = editor.body;
  22. editor.setContent('<h1>gggsiekes</h1>');
  23. setTimeout(function () {
  24. range.selectNode(d.getElementsByTagName("h1")[0]).select();
  25. editor.execCommand('directionality', "ltr");
  26. equal(ua.getChildHTML(db), "<h1 dir=\"ltr\">gggsiekes</h1>", "directionltr");
  27. equal(editor.queryCommandValue('directionality'), "ltr", "directionltr queryCommandValue");
  28. editor.execCommand('directionality', "rtl");
  29. equal(ua.getChildHTML(db), "<h1 dir=\"rtl\">gggsiekes</h1>", "directionrtl");
  30. equal(editor.queryCommandValue('directionality'), "rtl", "directionrtl queryCommandValue");
  31. start();
  32. }, 20);
  33. stop();
  34. } );
  35. test( '非块元素,闭合', function() {
  36. var editor = te.obj[0], d = editor.document, range = te.obj[1], db = editor.body;
  37. editor.setContent('<strong><em>gggsiekes</em></strong>');
  38. setTimeout(function () {
  39. range.selectNode(d.getElementsByTagName("strong")[0].firstChild).collapse(true).select();
  40. editor.execCommand('directionality', "rtl");
  41. equal(ua.getChildHTML(db), "<p dir=\"rtl\"><strong><em>gggsiekes</em></strong></p>", "directionrtl");
  42. equal(editor.queryCommandValue('directionality'), "rtl", "directionrtl queryCommandValue");
  43. editor.execCommand('directionality', "ltr");
  44. equal(ua.getChildHTML(db), "<p dir=\"ltr\"><strong><em>gggsiekes</em></strong></p>", "directionltr");
  45. equal(editor.queryCommandValue('directionality'), "ltr", "directionltr queryCommandValue");
  46. editor.setContent('<strong><em>gggsiekes</em></strong>');
  47. range.selectNode(d.getElementsByTagName("strong")[0]).collapse(true).select();
  48. editor.execCommand('directionality', "rtl");
  49. equal(ua.getChildHTML(db), "<p dir=\"rtl\"><strong><em>gggsiekes</em></strong></p>", "directionrtl");
  50. equal(editor.queryCommandValue('directionality'), "rtl", "查询文字输入方向为从右向左");
  51. editor.execCommand('directionality', "ltr");
  52. equal(ua.getChildHTML(db), "<p dir=\"ltr\"><strong><em>gggsiekes</em></strong></p>", "从左向右");
  53. equal(editor.queryCommandValue('directionality'), "ltr", "查询文字输入方向为从左向右");
  54. start();
  55. }, 20);
  56. stop();
  57. } );
  58. //4 range between blockelement and notblockelement
  59. test( '选区包含块和非块元素', function() {
  60. var editor = te.obj[0],d = editor.document,range = te.obj[1],db = editor.body;
  61. editor.setContent('<strong><em>gggsiekes</em></strong><p>xx</p>');
  62. setTimeout(function () {
  63. range.setStart(d.getElementsByTagName("strong")[0].firstChild, 0).setEnd(d.getElementsByTagName("p")[1].firstChild, 2).select();
  64. editor.execCommand('directionality', "rtl");
  65. equal(ua.getChildHTML(db), "<p dir=\"rtl\"><strong><em>gggsiekes</em></strong></p><p dir=\"rtl\">xx</p>", "directionrtl");
  66. equal(editor.queryCommandValue('directionality'), "rtl", "directionrtl queryCommandValue");
  67. editor.execCommand('directionality', "ltr");
  68. equal(ua.getChildHTML(db), "<p dir=\"ltr\"><strong><em>gggsiekes</em></strong></p><p dir=\"ltr\">xx</p>", "directionltr");
  69. equal(editor.queryCommandValue('directionality'), "ltr", "directionltr queryCommandValue");
  70. start();
  71. }, 20);
  72. stop();
  73. } );
  74. //5 betweenblockelement
  75. test( '选区在两个块元素之间', function() {
  76. var editor = te.obj[0], d = editor.document, range = te.obj[1], db = editor.body;
  77. editor.setContent('<p dir="rtl">sss</p><p>xx</p>');
  78. setTimeout(function () {
  79. range.setStart(d.getElementsByTagName("p")[0].firstChild, 0).setEnd(d.getElementsByTagName("p")[1].firstChild, 2).select();
  80. editor.execCommand('directionality', "rtl");
  81. equal(ua.getChildHTML(db), "<p dir=\"rtl\">sss</p><p dir=\"rtl\">xx</p>", "directionrtl");
  82. equal(editor.queryCommandValue('directionality'), "rtl", "directionrtl queryCommandValue");
  83. editor.execCommand('directionality', "ltr");
  84. equal(ua.getChildHTML(db), "<p dir=\"ltr\">sss</p><p dir=\"ltr\">xx</p>", "directionltr");
  85. equal(editor.queryCommandValue('directionality'), "ltr", "directionltr queryCommandValue");
  86. start();
  87. }, 20);
  88. stop();
  89. } );
  90. //6 br
  91. test( 'betweenblockelement', function() {
  92. var editor = te.obj[0],d = editor.document,range = te.obj[1],db = editor.body;
  93. editor.setContent( '<p>xx</p>br' );
  94. setTimeout(function () {
  95. range.setStart(d.getElementsByTagName("p")[0].firstChild, 0).setEnd(db.lastChild, 1).select();
  96. editor.execCommand('directionality', "rtl");
  97. equal(ua.getChildHTML(db), "<p dir=\"rtl\">xx</p><p dir=\"rtl\">br</p>", "directionrtl");
  98. equal(editor.queryCommandValue('directionality'), "rtl", "directionrtl queryCommandValue");
  99. editor.execCommand('directionality', "ltr");
  100. equal(ua.getChildHTML(db), "<p dir=\"ltr\">xx</p><p dir=\"ltr\">br</p>", "directionltr");
  101. equal(editor.queryCommandValue('directionality'), "ltr", "directionltr queryCommandValue");
  102. start();
  103. }, 20);
  104. stop();
  105. } );
  106. //7 &nbsp;
  107. test( '空格&nbsp;', function() {
  108. var editor = te.obj[0], d = editor.document, range = te.obj[1], db = editor.body;
  109. editor.setContent('<p>xx</p>&nbsp;');
  110. setTimeout(function () {
  111. range.setStart(d.getElementsByTagName("p")[0].firstChild, 0).setEnd(db.lastChild, 1).select();
  112. editor.execCommand('directionality', "rtl");
  113. equal(ua.getChildHTML(db), "<p dir=\"rtl\">xx</p><p dir=\"rtl\">&nbsp;</p>", "directionrtl");
  114. equal(editor.queryCommandValue('directionality'), "rtl", "directionrtl queryCommandValue");
  115. editor.execCommand('directionality', "ltr");
  116. equal(ua.getChildHTML(db), "<p dir=\"ltr\">xx</p><p dir=\"ltr\">&nbsp;</p>", "directionltr");
  117. equal(editor.queryCommandValue('directionality'), "ltr", "directionltr queryCommandValue");
  118. start();
  119. }, 20);
  120. stop();
  121. } );
  122. test('body',function(){
  123. var editor=te.obj[0];
  124. var range=te.obj[1];
  125. editor.setContent('<p>xx</p>');
  126. range.setStart(editor.body,0).collapse(1).select();
  127. editor.execCommand('directionality', 'rtl');
  128. if(ua.browser.ie){
  129. equal( ua.getChildHTML( editor.body ), "<p dir=\"rtl\">xx</p>", "directionrtl" );
  130. }
  131. else{
  132. equal( ua.getChildHTML( editor.body ), "<p dir=\"rtl\"></p><p>xx</p>", "directionrtl" );
  133. }
  134. })