Path: blob/trunk/third_party/closure/goog/editor/style.js
2868 views
// Copyright 2009 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Utilties for working with the styles of DOM nodes, and16* related to rich text editing.17*18* Many of these are not general enough to go into goog.style, and use19* constructs (like "isContainer") that only really make sense inside20* of an HTML editor.21*22* The API has been optimized for iterating over large, irregular DOM23* structures (with lots of text nodes), and so the API tends to be a bit24* more permissive than the goog.style API should be. For example,25* goog.style.getComputedStyle will throw an exception if you give it a26* text node.27*28* @author [email protected] (Nick Santos)29*/3031goog.provide('goog.editor.style');3233goog.require('goog.array');34goog.require('goog.asserts');35goog.require('goog.dom');36goog.require('goog.dom.NodeType');37goog.require('goog.dom.TagName');38goog.require('goog.editor.BrowserFeature');39goog.require('goog.events.EventHandler');40goog.require('goog.events.EventType');41goog.require('goog.object');42goog.require('goog.style');43goog.require('goog.userAgent');444546/**47* Gets the computed or cascaded style.48*49* This is different than goog.style.getStyle_ because it returns null50* for text nodes (instead of throwing an exception), and never reads51* inline style. These two functions may need to be reconciled.52*53* @param {!Node} node Node to get style of.54* @param {string} stylePropertyName Property to get (must be camelCase,55* not css-style).56* @return {?string} Style value, or null if this is not an element node.57* @private58*/59goog.editor.style.getComputedOrCascadedStyle_ = function(60node, stylePropertyName) {61if (node.nodeType != goog.dom.NodeType.ELEMENT) {62// Only element nodes have style.63return null;64}65return goog.userAgent.IE ?66goog.style.getCascadedStyle(67/** @type {!Element} */ (node), stylePropertyName) :68goog.style.getComputedStyle(69/** @type {!Element} */ (node), stylePropertyName);70};717273/**74* Checks whether the given element inherits display: block.75* @param {!Node} node The Node to check.76* @return {boolean} Whether the element inherits CSS display: block.77*/78goog.editor.style.isDisplayBlock = function(node) {79return goog.editor.style.getComputedOrCascadedStyle_(node, 'display') ==80'block';81};828384/**85* Returns true if the element is a container of other non-inline HTML86* Note that span, strong and em tags, being inline can only contain87* other inline elements and are thus, not containers. Containers are elements88* that should not be broken up when wrapping selections with a node of an89* inline block styling.90* @param {Node} element The element to check.91* @return {boolean} Whether the element is a container.92*/93goog.editor.style.isContainer = function(element) {94var nodeName = element && element.nodeName;95return !!(96element &&97(goog.editor.style.isDisplayBlock(element) ||98nodeName == goog.dom.TagName.TD || nodeName == goog.dom.TagName.TABLE ||99nodeName == goog.dom.TagName.LI));100};101102103/**104* Return the first ancestor of this node that is a container, inclusive.105* @see isContainer106* @param {Node} node Node to find the container of.107* @return {Element} The element which contains node.108*/109goog.editor.style.getContainer = function(node) {110// We assume that every node must have a container.111return /** @type {Element} */ (112goog.dom.getAncestor(node, goog.editor.style.isContainer, true));113};114115116/**117* Set of input types that should be kept selectable even when their ancestors118* are made unselectable.119* @type {Object}120* @private121*/122goog.editor.style.SELECTABLE_INPUT_TYPES_ =123goog.object.createSet('text', 'file', 'url');124125126/**127* Prevent the default action on mousedown events.128* @param {goog.events.Event} e The mouse down event.129* @private130*/131goog.editor.style.cancelMouseDownHelper_ = function(e) {132var targetTagName = e.target.tagName;133if (targetTagName != goog.dom.TagName.TEXTAREA &&134targetTagName != goog.dom.TagName.INPUT) {135e.preventDefault();136}137};138139140/**141* Makes the given element unselectable, as well as all of its children, except142* for text areas, text, file and url inputs.143* @param {Element} element The element to make unselectable.144* @param {goog.events.EventHandler} eventHandler An EventHandler to register145* the event with. Assumes when the node is destroyed, the eventHandler's146* listeners are destroyed as well.147*/148goog.editor.style.makeUnselectable = function(element, eventHandler) {149if (goog.editor.BrowserFeature.HAS_UNSELECTABLE_STYLE) {150// The mousing down on a node should not blur the focused node.151// This is consistent with how IE works.152// TODO: Consider using just the mousedown handler and not the css property.153eventHandler.listen(154element, goog.events.EventType.MOUSEDOWN,155goog.editor.style.cancelMouseDownHelper_, true);156}157158goog.style.setUnselectable(element, true);159160// Make inputs and text areas selectable.161var inputs = goog.dom.getElementsByTagName(162goog.dom.TagName.INPUT, goog.asserts.assert(element));163for (var i = 0, len = inputs.length; i < len; i++) {164var input = inputs[i];165if (input.type in goog.editor.style.SELECTABLE_INPUT_TYPES_) {166goog.editor.style.makeSelectable(input);167}168}169goog.array.forEach(170goog.dom.getElementsByTagName(171goog.dom.TagName.TEXTAREA, goog.asserts.assert(element)),172goog.editor.style.makeSelectable);173};174175176/**177* Make the given element selectable.178*179* For IE this simply turns off the "unselectable" property.180*181* Under FF no descendent of an unselectable node can be selectable:182*183* https://bugzilla.mozilla.org/show_bug.cgi?id=203291184*185* So we make each ancestor of node selectable, while trying to preserve the186* unselectability of other nodes along that path187*188* This may cause certain text nodes which should be unselectable, to become189* selectable. For example:190*191* <div id=div1 style="-moz-user-select: none">192* Text1193* <span id=span1>Text2</span>194* </div>195*196* If we call makeSelectable on span1, then it will cause "Text1" to become197* selectable, since it had to make div1 selectable in order for span1 to be198* selectable.199*200* If "Text1" were enclosed within a `<p>` or `<span>`, then this problem would201* not arise. Text nodes do not have styles, so its style can't be set to202* unselectable.203*204* @param {!Element} element The element to make selectable.205*/206goog.editor.style.makeSelectable = function(element) {207goog.style.setUnselectable(element, false);208if (goog.editor.BrowserFeature.HAS_UNSELECTABLE_STYLE) {209// Go up ancestor chain, searching for nodes that are unselectable.210// If such a node exists, mark it as selectable but mark its other children211// as unselectable so the minimum set of nodes is changed.212var child = element;213var current = /** @type {Element} */ (element.parentNode);214while (current && current.tagName != goog.dom.TagName.HTML) {215if (goog.style.isUnselectable(current)) {216goog.style.setUnselectable(current, false, true);217218for (var i = 0, len = current.childNodes.length; i < len; i++) {219var node = current.childNodes[i];220if (node != child && node.nodeType == goog.dom.NodeType.ELEMENT) {221goog.style.setUnselectable(222/** @type {!Element} */ (current.childNodes[i]), true);223}224}225}226227child = current;228current = /** @type {Element} */ (current.parentNode);229}230}231};232233234