blockquote_test.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // Copyright 2009 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.editor.plugins.BlockquoteTest');
  15. goog.setTestOnly('goog.editor.plugins.BlockquoteTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.dom.Range');
  18. goog.require('goog.dom.TagName');
  19. goog.require('goog.editor.BrowserFeature');
  20. goog.require('goog.editor.plugins.Blockquote');
  21. goog.require('goog.testing.editor.FieldMock');
  22. goog.require('goog.testing.editor.TestHelper');
  23. goog.require('goog.testing.jsunit');
  24. var SPLIT = '<span id="split-point"></span>';
  25. var root, helper, field, plugin;
  26. function setUp() {
  27. root = goog.dom.getElement('root');
  28. helper = new goog.testing.editor.TestHelper(root);
  29. field = new goog.testing.editor.FieldMock();
  30. helper.setUpEditableElement();
  31. }
  32. function tearDown() {
  33. field.$verify();
  34. helper.tearDownEditableElement();
  35. }
  36. function createPlugin(requireClassname, opt_paragraphMode) {
  37. field.queryCommandValue('+defaultTag')
  38. .$anyTimes()
  39. .$returns(opt_paragraphMode ? goog.dom.TagName.P : undefined);
  40. plugin = new goog.editor.plugins.Blockquote(requireClassname);
  41. plugin.registerFieldObject(field);
  42. plugin.enable(field);
  43. }
  44. function execCommand() {
  45. field.$replay();
  46. // With splitPoint we try to mimic the behavior of EnterHandler's
  47. // deleteCursorSelection_.
  48. var splitPoint = goog.dom.getElement('split-point');
  49. var position = goog.editor.BrowserFeature.HAS_W3C_RANGES ?
  50. {node: splitPoint.nextSibling, offset: 0} :
  51. splitPoint;
  52. if (goog.editor.BrowserFeature.HAS_W3C_RANGES) {
  53. goog.dom.removeNode(splitPoint);
  54. goog.dom.Range.createCaret(position.node, 0).select();
  55. } else {
  56. goog.dom.Range.createCaret(position, 0).select();
  57. }
  58. var result = plugin.execCommand(
  59. goog.editor.plugins.Blockquote.SPLIT_COMMAND, position);
  60. if (!goog.editor.BrowserFeature.HAS_W3C_RANGES) {
  61. goog.dom.removeNode(splitPoint);
  62. }
  63. return result;
  64. }
  65. function testSplitBlockquoteDoesNothingWhenNotInBlockquote() {
  66. root.innerHTML = '<div>Test' + SPLIT + 'ing</div>';
  67. createPlugin(false);
  68. assertFalse(execCommand());
  69. helper.assertHtmlMatches('<div>Testing</div>');
  70. }
  71. function testSplitBlockquoteDoesNothingWhenNotInBlockquoteWithClass() {
  72. root.innerHTML = '<blockquote>Test' + SPLIT + 'ing</blockquote>';
  73. createPlugin(true);
  74. assertFalse(execCommand());
  75. helper.assertHtmlMatches('<blockquote>Testing</blockquote>');
  76. }
  77. function testSplitBlockquoteInBlockquoteWithoutClass() {
  78. root.innerHTML = '<blockquote>Test' + SPLIT + 'ing</blockquote>';
  79. createPlugin(false);
  80. assertTrue(execCommand());
  81. helper.assertHtmlMatches(
  82. '<blockquote>Test</blockquote>' +
  83. '<div>' + (goog.editor.BrowserFeature.HAS_W3C_RANGES ? '&nbsp;' : '') +
  84. '</div>' +
  85. '<blockquote>ing</blockquote>');
  86. }
  87. function testSplitBlockquoteInBlockquoteWithoutClassInParagraphMode() {
  88. root.innerHTML = '<blockquote>Test' + SPLIT + 'ing</blockquote>';
  89. createPlugin(false, true);
  90. assertTrue(execCommand());
  91. helper.assertHtmlMatches(
  92. '<blockquote>Test</blockquote>' +
  93. '<p>' + (goog.editor.BrowserFeature.HAS_W3C_RANGES ? '&nbsp;' : '') +
  94. '</p>' +
  95. '<blockquote>ing</blockquote>');
  96. }
  97. function testSplitBlockquoteInBlockquoteWithClass() {
  98. root.innerHTML =
  99. '<blockquote class="tr_bq">Test' + SPLIT + 'ing</blockquote>';
  100. createPlugin(true);
  101. assertTrue(execCommand());
  102. helper.assertHtmlMatches(
  103. '<blockquote class="tr_bq">Test</blockquote>' +
  104. '<div>' + (goog.editor.BrowserFeature.HAS_W3C_RANGES ? '&nbsp;' : '') +
  105. '</div>' +
  106. '<blockquote class="tr_bq">ing</blockquote>');
  107. }
  108. function testSplitBlockquoteInBlockquoteWithClassInParagraphMode() {
  109. root.innerHTML =
  110. '<blockquote class="tr_bq">Test' + SPLIT + 'ing</blockquote>';
  111. createPlugin(true, true);
  112. assertTrue(execCommand());
  113. helper.assertHtmlMatches(
  114. '<blockquote class="tr_bq">Test</blockquote>' +
  115. '<p>' + (goog.editor.BrowserFeature.HAS_W3C_RANGES ? '&nbsp;' : '') +
  116. '</p>' +
  117. '<blockquote class="tr_bq">ing</blockquote>');
  118. }
  119. function testIsSplittableBlockquoteWhenRequiresClassNameToSplit() {
  120. createPlugin(true);
  121. var blockquoteWithClassName =
  122. goog.dom.createDom(goog.dom.TagName.BLOCKQUOTE, 'tr_bq');
  123. assertTrue(
  124. 'blockquote should be detected as splittable',
  125. plugin.isSplittableBlockquote(blockquoteWithClassName));
  126. var blockquoteWithoutClassName =
  127. goog.dom.createDom(goog.dom.TagName.BLOCKQUOTE, 'foo');
  128. assertFalse(
  129. 'blockquote should not be detected as splittable',
  130. plugin.isSplittableBlockquote(blockquoteWithoutClassName));
  131. var nonBlockquote = goog.dom.createDom(goog.dom.TagName.SPAN, 'tr_bq');
  132. assertFalse(
  133. 'element should not be detected as splittable',
  134. plugin.isSplittableBlockquote(nonBlockquote));
  135. }
  136. function testIsSplittableBlockquoteWhenNotRequiresClassNameToSplit() {
  137. createPlugin(false);
  138. var blockquoteWithClassName =
  139. goog.dom.createDom(goog.dom.TagName.BLOCKQUOTE, 'tr_bq');
  140. assertTrue(
  141. 'blockquote should be detected as splittable',
  142. plugin.isSplittableBlockquote(blockquoteWithClassName));
  143. var blockquoteWithoutClassName =
  144. goog.dom.createDom(goog.dom.TagName.BLOCKQUOTE, 'foo');
  145. assertTrue(
  146. 'blockquote should be detected as splittable',
  147. plugin.isSplittableBlockquote(blockquoteWithoutClassName));
  148. var nonBlockquote = goog.dom.createDom(goog.dom.TagName.SPAN, 'tr_bq');
  149. assertFalse(
  150. 'element should not be detected as splittable',
  151. plugin.isSplittableBlockquote(nonBlockquote));
  152. }
  153. function testIsSetupBlockquote() {
  154. createPlugin(false);
  155. var blockquoteWithClassName =
  156. goog.dom.createDom(goog.dom.TagName.BLOCKQUOTE, 'tr_bq');
  157. assertTrue(
  158. 'blockquote should be detected as setup',
  159. plugin.isSetupBlockquote(blockquoteWithClassName));
  160. var blockquoteWithoutClassName =
  161. goog.dom.createDom(goog.dom.TagName.BLOCKQUOTE, 'foo');
  162. assertFalse(
  163. 'blockquote should not be detected as setup',
  164. plugin.isSetupBlockquote(blockquoteWithoutClassName));
  165. var nonBlockquote = goog.dom.createDom(goog.dom.TagName.SPAN, 'tr_bq');
  166. assertFalse(
  167. 'element should not be detected as setup',
  168. plugin.isSetupBlockquote(nonBlockquote));
  169. }
  170. function testIsUnsetupBlockquote() {
  171. createPlugin(false);
  172. var blockquoteWithClassName =
  173. goog.dom.createDom(goog.dom.TagName.BLOCKQUOTE, 'tr_bq');
  174. assertFalse(
  175. 'blockquote should not be detected as unsetup',
  176. plugin.isUnsetupBlockquote(blockquoteWithClassName));
  177. var blockquoteWithoutClassName =
  178. goog.dom.createDom(goog.dom.TagName.BLOCKQUOTE, 'foo');
  179. assertTrue(
  180. 'blockquote should be detected as unsetup',
  181. plugin.isUnsetupBlockquote(blockquoteWithoutClassName));
  182. var nonBlockquote = goog.dom.createDom(goog.dom.TagName.SPAN, 'tr_bq');
  183. assertFalse(
  184. 'element should not be detected as unsetup',
  185. plugin.isUnsetupBlockquote(nonBlockquote));
  186. }