style.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. /**
  15. * @fileoverview Utilties for working with the styles of DOM nodes, and
  16. * related to rich text editing.
  17. *
  18. * Many of these are not general enough to go into goog.style, and use
  19. * constructs (like "isContainer") that only really make sense inside
  20. * of an HTML editor.
  21. *
  22. * The API has been optimized for iterating over large, irregular DOM
  23. * structures (with lots of text nodes), and so the API tends to be a bit
  24. * more permissive than the goog.style API should be. For example,
  25. * goog.style.getComputedStyle will throw an exception if you give it a
  26. * text node.
  27. *
  28. * @author nicksantos@google.com (Nick Santos)
  29. */
  30. goog.provide('goog.editor.style');
  31. goog.require('goog.array');
  32. goog.require('goog.asserts');
  33. goog.require('goog.dom');
  34. goog.require('goog.dom.NodeType');
  35. goog.require('goog.dom.TagName');
  36. goog.require('goog.editor.BrowserFeature');
  37. goog.require('goog.events.EventHandler');
  38. goog.require('goog.events.EventType');
  39. goog.require('goog.object');
  40. goog.require('goog.style');
  41. goog.require('goog.userAgent');
  42. /**
  43. * Gets the computed or cascaded style.
  44. *
  45. * This is different than goog.style.getStyle_ because it returns null
  46. * for text nodes (instead of throwing an exception), and never reads
  47. * inline style. These two functions may need to be reconciled.
  48. *
  49. * @param {!Node} node Node to get style of.
  50. * @param {string} stylePropertyName Property to get (must be camelCase,
  51. * not css-style).
  52. * @return {?string} Style value, or null if this is not an element node.
  53. * @private
  54. */
  55. goog.editor.style.getComputedOrCascadedStyle_ = function(
  56. node, stylePropertyName) {
  57. if (node.nodeType != goog.dom.NodeType.ELEMENT) {
  58. // Only element nodes have style.
  59. return null;
  60. }
  61. return goog.userAgent.IE ?
  62. goog.style.getCascadedStyle(
  63. /** @type {!Element} */ (node), stylePropertyName) :
  64. goog.style.getComputedStyle(
  65. /** @type {!Element} */ (node), stylePropertyName);
  66. };
  67. /**
  68. * Checks whether the given element inherits display: block.
  69. * @param {!Node} node The Node to check.
  70. * @return {boolean} Whether the element inherits CSS display: block.
  71. */
  72. goog.editor.style.isDisplayBlock = function(node) {
  73. return goog.editor.style.getComputedOrCascadedStyle_(node, 'display') ==
  74. 'block';
  75. };
  76. /**
  77. * Returns true if the element is a container of other non-inline HTML
  78. * Note that span, strong and em tags, being inline can only contain
  79. * other inline elements and are thus, not containers. Containers are elements
  80. * that should not be broken up when wrapping selections with a node of an
  81. * inline block styling.
  82. * @param {Node} element The element to check.
  83. * @return {boolean} Whether the element is a container.
  84. */
  85. goog.editor.style.isContainer = function(element) {
  86. var nodeName = element && element.nodeName;
  87. return !!(
  88. element &&
  89. (goog.editor.style.isDisplayBlock(element) ||
  90. nodeName == goog.dom.TagName.TD || nodeName == goog.dom.TagName.TABLE ||
  91. nodeName == goog.dom.TagName.LI));
  92. };
  93. /**
  94. * Return the first ancestor of this node that is a container, inclusive.
  95. * @see isContainer
  96. * @param {Node} node Node to find the container of.
  97. * @return {Element} The element which contains node.
  98. */
  99. goog.editor.style.getContainer = function(node) {
  100. // We assume that every node must have a container.
  101. return /** @type {Element} */ (
  102. goog.dom.getAncestor(node, goog.editor.style.isContainer, true));
  103. };
  104. /**
  105. * Set of input types that should be kept selectable even when their ancestors
  106. * are made unselectable.
  107. * @type {Object}
  108. * @private
  109. */
  110. goog.editor.style.SELECTABLE_INPUT_TYPES_ =
  111. goog.object.createSet('text', 'file', 'url');
  112. /**
  113. * Prevent the default action on mousedown events.
  114. * @param {goog.events.Event} e The mouse down event.
  115. * @private
  116. */
  117. goog.editor.style.cancelMouseDownHelper_ = function(e) {
  118. var targetTagName = e.target.tagName;
  119. if (targetTagName != goog.dom.TagName.TEXTAREA &&
  120. targetTagName != goog.dom.TagName.INPUT) {
  121. e.preventDefault();
  122. }
  123. };
  124. /**
  125. * Makes the given element unselectable, as well as all of its children, except
  126. * for text areas, text, file and url inputs.
  127. * @param {Element} element The element to make unselectable.
  128. * @param {goog.events.EventHandler} eventHandler An EventHandler to register
  129. * the event with. Assumes when the node is destroyed, the eventHandler's
  130. * listeners are destroyed as well.
  131. */
  132. goog.editor.style.makeUnselectable = function(element, eventHandler) {
  133. if (goog.editor.BrowserFeature.HAS_UNSELECTABLE_STYLE) {
  134. // The mousing down on a node should not blur the focused node.
  135. // This is consistent with how IE works.
  136. // TODO: Consider using just the mousedown handler and not the css property.
  137. eventHandler.listen(
  138. element, goog.events.EventType.MOUSEDOWN,
  139. goog.editor.style.cancelMouseDownHelper_, true);
  140. }
  141. goog.style.setUnselectable(element, true);
  142. // Make inputs and text areas selectable.
  143. var inputs = goog.dom.getElementsByTagName(
  144. goog.dom.TagName.INPUT, goog.asserts.assert(element));
  145. for (var i = 0, len = inputs.length; i < len; i++) {
  146. var input = inputs[i];
  147. if (input.type in goog.editor.style.SELECTABLE_INPUT_TYPES_) {
  148. goog.editor.style.makeSelectable(input);
  149. }
  150. }
  151. goog.array.forEach(
  152. goog.dom.getElementsByTagName(
  153. goog.dom.TagName.TEXTAREA, goog.asserts.assert(element)),
  154. goog.editor.style.makeSelectable);
  155. };
  156. /**
  157. * Make the given element selectable.
  158. *
  159. * For IE this simply turns off the "unselectable" property.
  160. *
  161. * Under FF no descendent of an unselectable node can be selectable:
  162. *
  163. * https://bugzilla.mozilla.org/show_bug.cgi?id=203291
  164. *
  165. * So we make each ancestor of node selectable, while trying to preserve the
  166. * unselectability of other nodes along that path
  167. *
  168. * This may cause certain text nodes which should be unselectable, to become
  169. * selectable. For example:
  170. *
  171. * <div id=div1 style="-moz-user-select: none">
  172. * Text1
  173. * <span id=span1>Text2</span>
  174. * </div>
  175. *
  176. * If we call makeSelectable on span1, then it will cause "Text1" to become
  177. * selectable, since it had to make div1 selectable in order for span1 to be
  178. * selectable.
  179. *
  180. * If "Text1" were enclosed within a `<p>` or `<span>`, then this problem would
  181. * not arise. Text nodes do not have styles, so its style can't be set to
  182. * unselectable.
  183. *
  184. * @param {!Element} element The element to make selectable.
  185. */
  186. goog.editor.style.makeSelectable = function(element) {
  187. goog.style.setUnselectable(element, false);
  188. if (goog.editor.BrowserFeature.HAS_UNSELECTABLE_STYLE) {
  189. // Go up ancestor chain, searching for nodes that are unselectable.
  190. // If such a node exists, mark it as selectable but mark its other children
  191. // as unselectable so the minimum set of nodes is changed.
  192. var child = element;
  193. var current = /** @type {Element} */ (element.parentNode);
  194. while (current && current.tagName != goog.dom.TagName.HTML) {
  195. if (goog.style.isUnselectable(current)) {
  196. goog.style.setUnselectable(current, false, true);
  197. for (var i = 0, len = current.childNodes.length; i < len; i++) {
  198. var node = current.childNodes[i];
  199. if (node != child && node.nodeType == goog.dom.NodeType.ELEMENT) {
  200. goog.style.setUnselectable(
  201. /** @type {!Element} */ (current.childNodes[i]), true);
  202. }
  203. }
  204. }
  205. child = current;
  206. current = /** @type {Element} */ (current.parentNode);
  207. }
  208. }
  209. };