style_test.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Copyright 2007 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.styleTest');
  15. goog.setTestOnly('goog.editor.styleTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.dom.TagName');
  18. goog.require('goog.editor.BrowserFeature');
  19. goog.require('goog.editor.style');
  20. goog.require('goog.events.EventHandler');
  21. goog.require('goog.events.EventType');
  22. goog.require('goog.style');
  23. goog.require('goog.testing.LooseMock');
  24. goog.require('goog.testing.jsunit');
  25. goog.require('goog.testing.mockmatchers');
  26. var parentNode = null;
  27. var childNode1 = null;
  28. var childNode2 = null;
  29. var childNode3 = null;
  30. var gChildWsNode1 = null;
  31. var gChildTextNode1 = null;
  32. var gChildNbspNode1 = null;
  33. var gChildMixedNode1 = null;
  34. var gChildWsNode2a = null;
  35. var gChildWsNode2b = null;
  36. var gChildTextNode3a = null;
  37. var gChildWsNode3 = null;
  38. var gChildTextNode3b = null;
  39. var $dom = goog.dom.createDom;
  40. var $text = goog.dom.createTextNode;
  41. function setUpGetNodeFunctions() {
  42. parentNode = $dom(
  43. goog.dom.TagName.P, {id: 'parentNode'},
  44. childNode1 = $dom(
  45. goog.dom.TagName.DIV, null, gChildWsNode1 = $text(' \t\r\n'),
  46. gChildTextNode1 = $text('Child node'),
  47. gChildNbspNode1 = $text('\u00a0'),
  48. gChildMixedNode1 = $text('Text\n plus\u00a0')),
  49. childNode2 = $dom(
  50. goog.dom.TagName.DIV, null, gChildWsNode2a = $text(''),
  51. gChildWsNode2b = $text(' ')),
  52. childNode3 = $dom(
  53. goog.dom.TagName.DIV, null,
  54. gChildTextNode3a = $text('I am a grand child'),
  55. gChildWsNode3 = $text(' \t \r \n'),
  56. gChildTextNode3b = $text('I am also a grand child')));
  57. document.body.appendChild(parentNode);
  58. }
  59. function tearDownGetNodeFunctions() {
  60. document.body.removeChild(parentNode);
  61. parentNode = null;
  62. childNode1 = null;
  63. childNode2 = null;
  64. childNode3 = null;
  65. gChildWsNode1 = null;
  66. gChildTextNode1 = null;
  67. gChildNbspNode1 = null;
  68. gChildMixedNode1 = null;
  69. gChildWsNode2a = null;
  70. gChildWsNode2b = null;
  71. gChildTextNode3a = null;
  72. gChildWsNode3 = null;
  73. gChildTextNode3b = null;
  74. }
  75. /**
  76. * Test isBlockLevel with a node that is block style and a node that is not
  77. */
  78. function testIsDisplayBlock() {
  79. assertTrue(
  80. 'Body is block style', goog.editor.style.isDisplayBlock(document.body));
  81. var tableNode = $dom(goog.dom.TagName.TABLE);
  82. assertFalse(
  83. 'Table is not block style', goog.editor.style.isDisplayBlock(tableNode));
  84. }
  85. /**
  86. * Test that isContainer returns true when the node is of non-inline HTML and
  87. * false when it is not
  88. */
  89. function testIsContainer() {
  90. var tableNode = $dom(goog.dom.TagName.TABLE);
  91. var liNode = $dom(goog.dom.TagName.LI);
  92. var textNode = $text('I am text');
  93. document.body.appendChild(textNode);
  94. assertTrue('Table is a container', goog.editor.style.isContainer(tableNode));
  95. assertTrue(
  96. 'Body is a container', goog.editor.style.isContainer(document.body));
  97. assertTrue('List item is a container', goog.editor.style.isContainer(liNode));
  98. assertFalse(
  99. 'Text node is not a container', goog.editor.style.isContainer(textNode));
  100. }
  101. /**
  102. * Test that getContainer properly returns the node itself if it is a
  103. * container, an ancestor node if it is a container, and null otherwise
  104. */
  105. function testGetContainer() {
  106. setUpGetNodeFunctions();
  107. assertEquals(
  108. 'Should return self', childNode1,
  109. goog.editor.style.getContainer(childNode1));
  110. assertEquals(
  111. 'Should return parent', childNode1,
  112. goog.editor.style.getContainer(gChildWsNode1));
  113. assertNull(
  114. 'Document has no ancestors', goog.editor.style.getContainer(document));
  115. tearDownGetNodeFunctions();
  116. }
  117. function testMakeUnselectable() {
  118. var div = goog.dom.createElement(goog.dom.TagName.DIV);
  119. div.innerHTML = '<div>No input</div>' +
  120. '<p><input type="checkbox">Checkbox</p>' +
  121. '<span><input type="text"></span>';
  122. document.body.appendChild(div);
  123. var eventHandler = new goog.testing.LooseMock(goog.events.EventHandler);
  124. if (goog.editor.BrowserFeature.HAS_UNSELECTABLE_STYLE) {
  125. eventHandler.listen(
  126. div, goog.events.EventType.MOUSEDOWN,
  127. goog.testing.mockmatchers.isFunction, true);
  128. }
  129. eventHandler.$replay();
  130. var childDiv = div.firstChild;
  131. var p = div.childNodes[1];
  132. var span = div.lastChild;
  133. var checkbox = p.firstChild;
  134. var text = span.firstChild;
  135. goog.editor.style.makeUnselectable(div, eventHandler);
  136. assertEquals(
  137. 'For browsers with non-overridable selectability, the root should be ' +
  138. 'selectable. Otherwise it should be unselectable.',
  139. !goog.editor.BrowserFeature.HAS_UNSELECTABLE_STYLE,
  140. goog.style.isUnselectable(div));
  141. assertTrue(goog.style.isUnselectable(childDiv));
  142. assertTrue(goog.style.isUnselectable(p));
  143. assertTrue(goog.style.isUnselectable(checkbox));
  144. assertEquals(
  145. 'For browsers with non-overridable selectability, the span will be ' +
  146. 'selectable. Otherwise it will be unselectable. ',
  147. !goog.editor.BrowserFeature.HAS_UNSELECTABLE_STYLE,
  148. goog.style.isUnselectable(span));
  149. assertFalse(goog.style.isUnselectable(text));
  150. eventHandler.$verify();
  151. }