| 1 |
// Copyright 2006 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 |
/** |
| 16 |
* @fileoverview Utilities for element styles. |
| 17 |
* |
| 18 |
* @author [email protected] (Erik Arvidsson) |
| 19 |
* @author [email protected] (Emil A Eklund) |
| 20 |
* @see ../demos/inline_block_quirks.html |
| 21 |
* @see ../demos/inline_block_standards.html |
| 22 |
* @see ../demos/style_viewport.html |
| 23 |
*/ |
| 24 |
|
| 25 |
goog.provide('goog.style'); |
| 26 |
|
| 27 |
|
| 28 |
goog.require('goog.array'); |
| 29 |
goog.require('goog.asserts'); |
| 30 |
goog.require('goog.dom'); |
| 31 |
goog.require('goog.dom.NodeType'); |
| 32 |
goog.require('goog.dom.TagName'); |
| 33 |
goog.require('goog.dom.vendor'); |
| 34 |
goog.require('goog.html.SafeStyleSheet'); |
| 35 |
goog.require('goog.math.Box'); |
| 36 |
goog.require('goog.math.Coordinate'); |
| 37 |
goog.require('goog.math.Rect'); |
| 38 |
goog.require('goog.math.Size'); |
| 39 |
goog.require('goog.object'); |
| 40 |
goog.require('goog.reflect'); |
| 41 |
goog.require('goog.string'); |
| 42 |
goog.require('goog.userAgent'); |
| 43 |
|
| 44 |
goog.forwardDeclare('goog.events.Event'); |
| 45 |
|
| 46 |
|
| 47 |
/** |
| 48 |
* Sets a style value on an element. |
| 49 |
* |
| 50 |
* This function is not indended to patch issues in the browser's style |
| 51 |
* handling, but to allow easy programmatic access to setting dash-separated |
| 52 |
* style properties. An example is setting a batch of properties from a data |
| 53 |
* object without overwriting old styles. When possible, use native APIs: |
| 54 |
* elem.style.propertyKey = 'value' or (if obliterating old styles is fine) |
| 55 |
* elem.style.cssText = 'property1: value1; property2: value2'. |
| 56 |
* |
| 57 |
* @param {Element} element The element to change. |
| 58 |
* @param {string|Object} style If a string, a style name. If an object, a hash |
| 59 |
* of style names to style values. |
| 60 |
* @param {string|number|boolean=} opt_value If style was a string, then this |
| 61 |
* should be the value. |
| 62 |
*/ |
| 63 |
goog.style.setStyle = function(element, style, opt_value) { |
| 64 |
if (goog.isString(style)) { |
| 65 |
goog.style.setStyle_(element, opt_value, style); |
| 66 |
} else { |
| 67 |
for (var key in style) { |
| 68 |
goog.style.setStyle_(element, style[key], key); |
| 69 |
} |
| 70 |
} |
| 71 |
}; |
| 72 |
|
| 73 |
|
| 74 |
/** |
| 75 |
* Sets a style value on an element, with parameters swapped to work with |
| 76 |
* {@code goog.object.forEach()}. Prepends a vendor-specific prefix when |
| 77 |
* necessary. |
| 78 |
* @param {Element} element The element to change. |
| 79 |
* @param {string|number|boolean|undefined} value Style value. |
| 80 |
* @param {string} style Style name. |
| 81 |
* @private |
| 82 |
*/ |
| 83 |
goog.style.setStyle_ = function(element, value, style) { |
| 84 |
var propertyName = goog.style.getVendorJsStyleName_(element, style); |
| 85 |
|
| 86 |
if (propertyName) { |
| 87 |
// TODO(johnlenz): coerce to string? |
| 88 |
element.style[propertyName] = /** @type {?} */ (value); |
| 89 |
} |
| 90 |
}; |
| 91 |
|
| 92 |
|
| 93 |
/** |
| 94 |
* Style name cache that stores previous property name lookups. |
| 95 |
* |
| 96 |
* This is used by setStyle to speed up property lookups, entries look like: |
| 97 |
* { StyleName: ActualPropertyName } |
| 98 |
* |
| 99 |
* @private {!Object<string, string>} |
| 100 |
*/ |
| 101 |
goog.style.styleNameCache_ = {}; |
| 102 |
|
| 103 |
|
| 104 |
/** |
| 105 |
* Returns the style property name in camel-case. If it does not exist and a |
| 106 |
* vendor-specific version of the property does exist, then return the vendor- |
| 107 |
* specific property name instead. |
| 108 |
* @param {Element} element The element to change. |
| 109 |
* @param {string} style Style name. |
| 110 |
* @return {string} Vendor-specific style. |
| 111 |
* @private |
| 112 |
*/ |
| 113 |
goog.style.getVendorJsStyleName_ = function(element, style) { |
| 114 |
var propertyName = goog.style.styleNameCache_[style]; |
| 115 |
if (!propertyName) { |
| 116 |
var camelStyle = goog.string.toCamelCase(style); |
| 117 |
propertyName = camelStyle; |
| 118 |
|
| 119 |
if (element.style[camelStyle] === undefined) { |
| 120 |
var prefixedStyle = goog.dom.vendor.getVendorJsPrefix() + |
| 121 |
goog.string.toTitleCase(camelStyle); |
| 122 |
|
| 123 |
if (element.style[prefixedStyle] !== undefined) { |
| 124 |
propertyName = prefixedStyle; |
| 125 |
} |
| 126 |
} |
| 127 |
goog.style.styleNameCache_[style] = propertyName; |
| 128 |
} |
| 129 |
|
| 130 |
return propertyName; |
| 131 |
}; |
| 132 |
|
| 133 |
|
| 134 |
/** |
| 135 |
* Returns the style property name in CSS notation. If it does not exist and a |
| 136 |
* vendor-specific version of the property does exist, then return the vendor- |
| 137 |
* specific property name instead. |
| 138 |
* @param {Element} element The element to change. |
| 139 |
* @param {string} style Style name. |
| 140 |
* @return {string} Vendor-specific style. |
| 141 |
* @private |
| 142 |
*/ |
| 143 |
goog.style.getVendorStyleName_ = function(element, style) { |
| 144 |
var camelStyle = goog.string.toCamelCase(style); |
| 145 |
|
| 146 |
if (element.style[camelStyle] === undefined) { |
| 147 |
var prefixedStyle = goog.dom.vendor.getVendorJsPrefix() + |
| 148 |
goog.string.toTitleCase(camelStyle); |
| 149 |
|
| 150 |
if (element.style[prefixedStyle] !== undefined) { |
| 151 |
return goog.dom.vendor.getVendorPrefix() + '-' + style; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
return style; |
| 156 |
}; |
| 157 |
|
| 158 |
|
| 159 |
/** |
| 160 |
* Retrieves an explicitly-set style value of a node. This returns '' if there |
| 161 |
* isn't a style attribute on the element or if this style property has not been |
| 162 |
* explicitly set in script. |
| 163 |
* |
| 164 |
* @param {Element} element Element to get style of. |
| 165 |
* @param {string} property Property to get, css-style (if you have a camel-case |
| 166 |
* property, use element.style[style]). |
| 167 |
* @return {string} Style value. |
| 168 |
*/ |
| 169 |
goog.style.getStyle = function(element, property) { |
| 170 |
// element.style is '' for well-known properties which are unset. |
| 171 |
// For for browser specific styles as 'filter' is undefined |
| 172 |
// so we need to return '' explicitly to make it consistent across |
| 173 |
// browsers. |
| 174 |
var styleValue = element.style[goog.string.toCamelCase(property)]; |
| 175 |
|
| 176 |
// Using typeof here because of a bug in Safari 5.1, where this value |
| 177 |
// was undefined, but === undefined returned false. |
| 178 |
if (typeof(styleValue) !== 'undefined') { |
| 179 |
return styleValue; |
| 180 |
} |
| 181 |
|
| 182 |
return element.style[goog.style.getVendorJsStyleName_(element, property)] || |
| 183 |
''; |
| 184 |
}; |
| 185 |
|
| 186 |
|
| 187 |
/** |
| 188 |
* Retrieves a computed style value of a node. It returns empty string if the |
| 189 |
* value cannot be computed (which will be the case in Internet Explorer) or |
| 190 |
* "none" if the property requested is an SVG one and it has not been |
| 191 |
* explicitly set (firefox and webkit). |
| 192 |
* |
| 193 |
* @param {Element} element Element to get style of. |
| 194 |
* @param {string} property Property to get (camel-case). |
| 195 |
* @return {string} Style value. |
| 196 |
*/ |
| 197 |
goog.style.getComputedStyle = function(element, property) { |
| 198 |
var doc = goog.dom.getOwnerDocument(element); |
| 199 |
if (doc.defaultView && doc.defaultView.getComputedStyle) { |
| 200 |
var styles = doc.defaultView.getComputedStyle(element, null); |
| 201 |
if (styles) { |
| 202 |
// element.style[..] is undefined for browser specific styles |
| 203 |
// as 'filter'. |
| 204 |
return styles[property] || styles.getPropertyValue(property) || ''; |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
return ''; |
| 209 |
}; |
| 210 |
|
| 211 |
|
| 212 |
/** |
| 213 |
* Gets the cascaded style value of a node, or null if the value cannot be |
| 214 |
* computed (only Internet Explorer can do this). |
| 215 |
* |
| 216 |
* @param {Element} element Element to get style of. |
| 217 |
* @param {string} style Property to get (camel-case). |
| 218 |
* @return {string} Style value. |
| 219 |
*/ |
| 220 |
goog.style.getCascadedStyle = function(element, style) { |
| 221 |
// TODO(nicksantos): This should be documented to return null. #fixTypes |
| 222 |
return /** @type {string} */ ( |
| 223 |
element.currentStyle ? element.currentStyle[style] : null); |
| 224 |
}; |
| 225 |
|
| 226 |
|
| 227 |
/** |
| 228 |
* Cross-browser pseudo get computed style. It returns the computed style where |
| 229 |
* available. If not available it tries the cascaded style value (IE |
| 230 |
* currentStyle) and in worst case the inline style value. It shouldn't be |
| 231 |
* called directly, see http://wiki/Main/ComputedStyleVsCascadedStyle for |
| 232 |
* discussion. |
| 233 |
* |
| 234 |
* @param {Element} element Element to get style of. |
| 235 |
* @param {string} style Property to get (must be camelCase, not css-style.). |
| 236 |
* @return {string} Style value. |
| 237 |
* @private |
| 238 |
*/ |
| 239 |
goog.style.getStyle_ = function(element, style) { |
| 240 |
return goog.style.getComputedStyle(element, style) || |
| 241 |
goog.style.getCascadedStyle(element, style) || |
| 242 |
(element.style && element.style[style]); |
| 243 |
}; |
| 244 |
|
| 245 |
|
| 246 |
/** |
| 247 |
* Retrieves the computed value of the box-sizing CSS attribute. |
| 248 |
* Browser support: http://caniuse.com/css3-boxsizing. |
| 249 |
* @param {!Element} element The element whose box-sizing to get. |
| 250 |
* @return {?string} 'content-box', 'border-box' or 'padding-box'. null if |
| 251 |
* box-sizing is not supported (IE7 and below). |
| 252 |
*/ |
| 253 |
goog.style.getComputedBoxSizing = function(element) { |
| 254 |
return goog.style.getStyle_(element, 'boxSizing') || |
| 255 |
goog.style.getStyle_(element, 'MozBoxSizing') || |
| 256 |
goog.style.getStyle_(element, 'WebkitBoxSizing') || null; |
| 257 |
}; |
| 258 |
|
| 259 |
|
| 260 |
/** |
| 261 |
* Retrieves the computed value of the position CSS attribute. |
| 262 |
* @param {Element} element The element to get the position of. |
| 263 |
* @return {string} Position value. |
| 264 |
*/ |
| 265 |
goog.style.getComputedPosition = function(element) { |
| 266 |
return goog.style.getStyle_(element, 'position'); |
| 267 |
}; |
| 268 |
|
| 269 |
|
| 270 |
/** |
| 271 |
* Retrieves the computed background color string for a given element. The |
| 272 |
* string returned is suitable for assigning to another element's |
| 273 |
* background-color, but is not guaranteed to be in any particular string |
| 274 |
* format. Accessing the color in a numeric form may not be possible in all |
| 275 |
* browsers or with all input. |
| 276 |
* |
| 277 |
* If the background color for the element is defined as a hexadecimal value, |
| 278 |
* the resulting string can be parsed by goog.color.parse in all supported |
| 279 |
* browsers. |
| 280 |
* |
| 281 |
* Whether named colors like "red" or "lightblue" get translated into a |
| 282 |
* format which can be parsed is browser dependent. Calling this function on |
| 283 |
* transparent elements will return "transparent" in most browsers or |
| 284 |
* "rgba(0, 0, 0, 0)" in WebKit. |
| 285 |
* @param {Element} element The element to get the background color of. |
| 286 |
* @return {string} The computed string value of the background color. |
| 287 |
*/ |
| 288 |
goog.style.getBackgroundColor = function(element) { |
| 289 |
return goog.style.getStyle_(element, 'backgroundColor'); |
| 290 |
}; |
| 291 |
|
| 292 |
|
| 293 |
/** |
| 294 |
* Retrieves the computed value of the overflow-x CSS attribute. |
| 295 |
* @param {Element} element The element to get the overflow-x of. |
| 296 |
* @return {string} The computed string value of the overflow-x attribute. |
| 297 |
*/ |
| 298 |
goog.style.getComputedOverflowX = function(element) { |
| 299 |
return goog.style.getStyle_(element, 'overflowX'); |
| 300 |
}; |
| 301 |
|
| 302 |
|
| 303 |
/** |
| 304 |
* Retrieves the computed value of the overflow-y CSS attribute. |
| 305 |
* @param {Element} element The element to get the overflow-y of. |
| 306 |
* @return {string} The computed string value of the overflow-y attribute. |
| 307 |
*/ |
| 308 |
goog.style.getComputedOverflowY = function(element) { |
| 309 |
return goog.style.getStyle_(element, 'overflowY'); |
| 310 |
}; |
| 311 |
|
| 312 |
|
| 313 |
/** |
| 314 |
* Retrieves the computed value of the z-index CSS attribute. |
| 315 |
* @param {Element} element The element to get the z-index of. |
| 316 |
* @return {string|number} The computed value of the z-index attribute. |
| 317 |
*/ |
| 318 |
goog.style.getComputedZIndex = function(element) { |
| 319 |
return goog.style.getStyle_(element, 'zIndex'); |
| 320 |
}; |
| 321 |
|
| 322 |
|
| 323 |
/** |
| 324 |
* Retrieves the computed value of the text-align CSS attribute. |
| 325 |
* @param {Element} element The element to get the text-align of. |
| 326 |
* @return {string} The computed string value of the text-align attribute. |
| 327 |
*/ |
| 328 |
goog.style.getComputedTextAlign = function(element) { |
| 329 |
return goog.style.getStyle_(element, 'textAlign'); |
| 330 |
}; |
| 331 |
|
| 332 |
|
| 333 |
/** |
| 334 |
* Retrieves the computed value of the cursor CSS attribute. |
| 335 |
* @param {Element} element The element to get the cursor of. |
| 336 |
* @return {string} The computed string value of the cursor attribute. |
| 337 |
*/ |
| 338 |
goog.style.getComputedCursor = function(element) { |
| 339 |
return goog.style.getStyle_(element, 'cursor'); |
| 340 |
}; |
| 341 |
|
| 342 |
|
| 343 |
/** |
| 344 |
* Retrieves the computed value of the CSS transform attribute. |
| 345 |
* @param {Element} element The element to get the transform of. |
| 346 |
* @return {string} The computed string representation of the transform matrix. |
| 347 |
*/ |
| 348 |
goog.style.getComputedTransform = function(element) { |
| 349 |
var property = goog.style.getVendorStyleName_(element, 'transform'); |
| 350 |
return goog.style.getStyle_(element, property) || |
| 351 |
goog.style.getStyle_(element, 'transform'); |
| 352 |
}; |
| 353 |
|
| 354 |
|
| 355 |
/** |
| 356 |
* Sets the top/left values of an element. If no unit is specified in the |
| 357 |
* argument then it will add px. The second argument is required if the first |
| 358 |
* argument is a string or number and is ignored if the first argument |
| 359 |
* is a coordinate. |
| 360 |
* @param {Element} el Element to move. |
| 361 |
* @param {string|number|goog.math.Coordinate} arg1 Left position or coordinate. |
| 362 |
* @param {string|number=} opt_arg2 Top position. |
| 363 |
*/ |
| 364 |
goog.style.setPosition = function(el, arg1, opt_arg2) { |
| 365 |
var x, y; |
| 366 |
|
| 367 |
if (arg1 instanceof goog.math.Coordinate) { |
| 368 |
x = arg1.x; |
| 369 |
y = arg1.y; |
| 370 |
} else { |
| 371 |
x = arg1; |
| 372 |
y = opt_arg2; |
| 373 |
} |
| 374 |
|
| 375 |
el.style.left = goog.style.getPixelStyleValue_( |
| 376 |
/** @type {number|string} */ (x), false); |
| 377 |
el.style.top = goog.style.getPixelStyleValue_( |
| 378 |
/** @type {number|string} */ (y), false); |
| 379 |
}; |
| 380 |
|
| 381 |
|
| 382 |
/** |
| 383 |
* Gets the offsetLeft and offsetTop properties of an element and returns them |
| 384 |
* in a Coordinate object |
| 385 |
* @param {Element} element Element. |
| 386 |
* @return {!goog.math.Coordinate} The position. |
| 387 |
*/ |
| 388 |
goog.style.getPosition = function(element) { |
| 389 |
return new goog.math.Coordinate( |
| 390 |
/** @type {!HTMLElement} */ (element).offsetLeft, |
| 391 |
/** @type {!HTMLElement} */ (element).offsetTop); |
| 392 |
}; |
| 393 |
|
| 394 |
|
| 395 |
/** |
| 396 |
* Returns the viewport element for a particular document |
| 397 |
* @param {Node=} opt_node DOM node (Document is OK) to get the viewport element |
| 398 |
* of. |
| 399 |
* @return {Element} document.documentElement or document.body. |
| 400 |
*/ |
| 401 |
goog.style.getClientViewportElement = function(opt_node) { |
| 402 |
var doc; |
| 403 |
if (opt_node) { |
| 404 |
doc = goog.dom.getOwnerDocument(opt_node); |
| 405 |
} else { |
| 406 |
doc = goog.dom.getDocument(); |
| 407 |
} |
| 408 |
|
| 409 |
// In old IE versions the document.body represented the viewport |
| 410 |
if (goog.userAgent.IE && !goog.userAgent.isDocumentModeOrHigher(9) && |
| 411 |
!goog.dom.getDomHelper(doc).isCss1CompatMode()) { |
| 412 |
return doc.body; |
| 413 |
} |
| 414 |
return doc.documentElement; |
| 415 |
}; |
| 416 |
|
| 417 |
|
| 418 |
/** |
| 419 |
* Calculates the viewport coordinates relative to the page/document |
| 420 |
* containing the node. The viewport may be the browser viewport for |
| 421 |
* non-iframe document, or the iframe container for iframe'd document. |
| 422 |
* @param {!Document} doc The document to use as the reference point. |
| 423 |
* @return {!goog.math.Coordinate} The page offset of the viewport. |
| 424 |
*/ |
| 425 |
goog.style.getViewportPageOffset = function(doc) { |
| 426 |
var body = doc.body; |
| 427 |
var documentElement = doc.documentElement; |
| 428 |
var scrollLeft = body.scrollLeft || documentElement.scrollLeft; |
| 429 |
var scrollTop = body.scrollTop || documentElement.scrollTop; |
| 430 |
return new goog.math.Coordinate(scrollLeft, scrollTop); |
| 431 |
}; |
| 432 |
|
| 433 |
|
| 434 |
/** |
| 435 |
* Gets the client rectangle of the DOM element. |
| 436 |
* |
| 437 |
* getBoundingClientRect is part of a new CSS object model draft (with a |
| 438 |
* long-time presence in IE), replacing the error-prone parent offset |
| 439 |
* computation and the now-deprecated Gecko getBoxObjectFor. |
| 440 |
* |
| 441 |
* This utility patches common browser bugs in getBoundingClientRect. It |
| 442 |
* will fail if getBoundingClientRect is unsupported. |
| 443 |
* |
| 444 |
* If the element is not in the DOM, the result is undefined, and an error may |
| 445 |
* be thrown depending on user agent. |
| 446 |
* |
| 447 |
* @param {!Element} el The element whose bounding rectangle is being queried. |
| 448 |
* @return {Object} A native bounding rectangle with numerical left, top, |
| 449 |
* right, and bottom. Reported by Firefox to be of object type ClientRect. |
| 450 |
* @private |
| 451 |
*/ |
| 452 |
goog.style.getBoundingClientRect_ = function(el) { |
| 453 |
var rect; |
| 454 |
try { |
| 455 |
rect = el.getBoundingClientRect(); |
| 456 |
} catch (e) { |
| 457 |
// In IE < 9, calling getBoundingClientRect on an orphan element raises an |
| 458 |
// "Unspecified Error". All other browsers return zeros. |
| 459 |
return {'left': 0, 'top': 0, 'right': 0, 'bottom': 0}; |
| 460 |
} |
| 461 |
|
| 462 |
// Patch the result in IE only, so that this function can be inlined if |
| 463 |
// compiled for non-IE. |
| 464 |
if (goog.userAgent.IE && el.ownerDocument.body) { |
| 465 |
// In IE, most of the time, 2 extra pixels are added to the top and left |
| 466 |
// due to the implicit 2-pixel inset border. In IE6/7 quirks mode and |
| 467 |
// IE6 standards mode, this border can be overridden by setting the |
| 468 |
// document element's border to zero -- thus, we cannot rely on the |
| 469 |
// offset always being 2 pixels. |
| 470 |
|
| 471 |
// In quirks mode, the offset can be determined by querying the body's |
| 472 |
// clientLeft/clientTop, but in standards mode, it is found by querying |
| 473 |
// the document element's clientLeft/clientTop. Since we already called |
| 474 |
// getBoundingClientRect we have already forced a reflow, so it is not |
| 475 |
// too expensive just to query them all. |
| 476 |
|
| 477 |
// See: http://msdn.microsoft.com/en-us/library/ms536433(VS.85).aspx |
| 478 |
var doc = el.ownerDocument; |
| 479 |
rect.left -= doc.documentElement.clientLeft + doc.body.clientLeft; |
| 480 |
rect.top -= doc.documentElement.clientTop + doc.body.clientTop; |
| 481 |
} |
| 482 |
return rect; |
| 483 |
}; |
| 484 |
|
| 485 |
|
| 486 |
/** |
| 487 |
* Returns the first parent that could affect the position of a given element. |
| 488 |
* @param {Element} element The element to get the offset parent for. |
| 489 |
* @return {Element} The first offset parent or null if one cannot be found. |
| 490 |
*/ |
| 491 |
goog.style.getOffsetParent = function(element) { |
| 492 |
// element.offsetParent does the right thing in IE7 and below. In other |
| 493 |
// browsers it only includes elements with position absolute, relative or |
| 494 |
// fixed, not elements with overflow set to auto or scroll. |
| 495 |
if (goog.userAgent.IE && !goog.userAgent.isDocumentModeOrHigher(8)) { |
| 496 |
goog.asserts.assert(element && 'offsetParent' in element); |
| 497 |
return element.offsetParent; |
| 498 |
} |
| 499 |
|
| 500 |
var doc = goog.dom.getOwnerDocument(element); |
| 501 |
var positionStyle = goog.style.getStyle_(element, 'position'); |
| 502 |
var skipStatic = positionStyle == 'fixed' || positionStyle == 'absolute'; |
| 503 |
for (var parent = element.parentNode; parent && parent != doc; |
| 504 |
parent = parent.parentNode) { |
| 505 |
// Skip shadowDOM roots. |
| 506 |
if (parent.nodeType == goog.dom.NodeType.DOCUMENT_FRAGMENT && parent.host) { |
| 507 |
// Cast because the assignment is not type safe, and without a cast we |
| 508 |
// start typing parent loosely and get bad disambiguation. |
| 509 |
parent = /** @type {!Element} */ (parent.host); |
| 510 |
} |
| 511 |
positionStyle = |
| 512 |
goog.style.getStyle_(/** @type {!Element} */ (parent), 'position'); |
| 513 |
skipStatic = skipStatic && positionStyle == 'static' && |
| 514 |
parent != doc.documentElement && parent != doc.body; |
| 515 |
if (!skipStatic && |
| 516 |
(parent.scrollWidth > parent.clientWidth || |
| 517 |
parent.scrollHeight > parent.clientHeight || |
| 518 |
positionStyle == 'fixed' || positionStyle == 'absolute' || |
| 519 |
positionStyle == 'relative')) { |
| 520 |
return /** @type {!Element} */ (parent); |
| 521 |
} |
| 522 |
} |
| 523 |
return null; |
| 524 |
}; |
| 525 |
|
| 526 |
|
| 527 |
/** |
| 528 |
* Calculates and returns the visible rectangle for a given element. Returns a |
| 529 |
* box describing the visible portion of the nearest scrollable offset ancestor. |
| 530 |
* Coordinates are given relative to the document. |
| 531 |
* |
| 532 |
* @param {Element} element Element to get the visible rect for. |
| 533 |
* @return {goog.math.Box} Bounding elementBox describing the visible rect or |
| 534 |
* null if scrollable ancestor isn't inside the visible viewport. |
| 535 |
*/ |
| 536 |
goog.style.getVisibleRectForElement = function(element) { |
| 537 |
var visibleRect = new goog.math.Box(0, Infinity, Infinity, 0); |
| 538 |
var dom = goog.dom.getDomHelper(element); |
| 539 |
var body = dom.getDocument().body; |
| 540 |
var documentElement = dom.getDocument().documentElement; |
| 541 |
var scrollEl = dom.getDocumentScrollElement(); |
| 542 |
|
| 543 |
// Determine the size of the visible rect by climbing the dom accounting for |
| 544 |
// all scrollable containers. |
| 545 |
for (var el = element; el = goog.style.getOffsetParent(el);) { |
| 546 |
// clientWidth is zero for inline block elements in IE. |
| 547 |
// on WEBKIT, body element can have clientHeight = 0 and scrollHeight > 0 |
| 548 |
if ((!goog.userAgent.IE || el.clientWidth != 0) && |
| 549 |
(!goog.userAgent.WEBKIT || el.clientHeight != 0 || el != body) && |
| 550 |
// body may have overflow set on it, yet we still get the entire |
| 551 |
// viewport. In some browsers, el.offsetParent may be |
| 552 |
// document.documentElement, so check for that too. |
| 553 |
(el != body && el != documentElement && |
| 554 |
goog.style.getStyle_(el, 'overflow') != 'visible')) { |
| 555 |
var pos = goog.style.getPageOffset(el); |
| 556 |
var client = goog.style.getClientLeftTop(el); |
| 557 |
pos.x += client.x; |
| 558 |
pos.y += client.y; |
| 559 |
|
| 560 |
visibleRect.top = Math.max(visibleRect.top, pos.y); |
| 561 |
visibleRect.right = Math.min(visibleRect.right, pos.x + el.clientWidth); |
| 562 |
visibleRect.bottom = |
| 563 |
Math.min(visibleRect.bottom, pos.y + el.clientHeight); |
| 564 |
visibleRect.left = Math.max(visibleRect.left, pos.x); |
| 565 |
} |
| 566 |
} |
| 567 |
|
| 568 |
// Clip by window's viewport. |
| 569 |
var scrollX = scrollEl.scrollLeft, scrollY = scrollEl.scrollTop; |
| 570 |
visibleRect.left = Math.max(visibleRect.left, scrollX); |
| 571 |
visibleRect.top = Math.max(visibleRect.top, scrollY); |
| 572 |
var winSize = dom.getViewportSize(); |
| 573 |
visibleRect.right = Math.min(visibleRect.right, scrollX + winSize.width); |
| 574 |
visibleRect.bottom = Math.min(visibleRect.bottom, scrollY + winSize.height); |
| 575 |
return visibleRect.top >= 0 && visibleRect.left >= 0 && |
| 576 |
visibleRect.bottom > visibleRect.top && |
| 577 |
visibleRect.right > visibleRect.left ? |
| 578 |
visibleRect : |
| 579 |
null; |
| 580 |
}; |
| 581 |
|
| 582 |
|
| 583 |
/** |
| 584 |
* Calculate the scroll position of {@code container} with the minimum amount so |
| 585 |
* that the content and the borders of the given {@code element} become visible. |
| 586 |
* If the element is bigger than the container, its top left corner will be |
| 587 |
* aligned as close to the container's top left corner as possible. |
| 588 |
* |
| 589 |
* @param {Element} element The element to make visible. |
| 590 |
* @param {Element=} opt_container The container to scroll. If not set, then the |
| 591 |
* document scroll element will be used. |
| 592 |
* @param {boolean=} opt_center Whether to center the element in the container. |
| 593 |
* Defaults to false. |
| 594 |
* @return {!goog.math.Coordinate} The new scroll position of the container, |
| 595 |
* in form of goog.math.Coordinate(scrollLeft, scrollTop). |
| 596 |
*/ |
| 597 |
goog.style.getContainerOffsetToScrollInto = function( |
| 598 |
element, opt_container, opt_center) { |
| 599 |
var container = opt_container || goog.dom.getDocumentScrollElement(); |
| 600 |
// Absolute position of the element's border's top left corner. |
| 601 |
var elementPos = goog.style.getPageOffset(element); |
| 602 |
// Absolute position of the container's border's top left corner. |
| 603 |
var containerPos = goog.style.getPageOffset(container); |
| 604 |
var containerBorder = goog.style.getBorderBox(container); |
| 605 |
if (container == goog.dom.getDocumentScrollElement()) { |
| 606 |
// The element position is calculated based on the page offset, and the |
| 607 |
// document scroll element holds the scroll position within the page. We can |
| 608 |
// use the scroll position to calculate the relative position from the |
| 609 |
// element. |
| 610 |
var relX = elementPos.x - container.scrollLeft; |
| 611 |
var relY = elementPos.y - container.scrollTop; |
| 612 |
if (goog.userAgent.IE && !goog.userAgent.isDocumentModeOrHigher(10)) { |
| 613 |
// In older versions of IE getPageOffset(element) does not include the |
| 614 |
// container border so it has to be added to accommodate. |
| 615 |
relX += containerBorder.left; |
| 616 |
relY += containerBorder.top; |
| 617 |
} |
| 618 |
} else { |
| 619 |
// Relative pos. of the element's border box to the container's content box. |
| 620 |
var relX = elementPos.x - containerPos.x - containerBorder.left; |
| 621 |
var relY = elementPos.y - containerPos.y - containerBorder.top; |
| 622 |
} |
| 623 |
// How much the element can move in the container, i.e. the difference between |
| 624 |
// the element's bottom-right-most and top-left-most position where it's |
| 625 |
// fully visible. |
| 626 |
var elementSize = goog.style.getSizeWithDisplay_(element); |
| 627 |
var spaceX = container.clientWidth - elementSize.width; |
| 628 |
var spaceY = container.clientHeight - elementSize.height; |
| 629 |
var scrollLeft = container.scrollLeft; |
| 630 |
var scrollTop = container.scrollTop; |
| 631 |
if (opt_center) { |
| 632 |
// All browsers round non-integer scroll positions down. |
| 633 |
scrollLeft += relX - spaceX / 2; |
| 634 |
scrollTop += relY - spaceY / 2; |
| 635 |
} else { |
| 636 |
// This formula was designed to give the correct scroll values in the |
| 637 |
// following cases: |
| 638 |
// - element is higher than container (spaceY < 0) => scroll down by relY |
| 639 |
// - element is not higher that container (spaceY >= 0): |
| 640 |
// - it is above container (relY < 0) => scroll up by abs(relY) |
| 641 |
// - it is below container (relY > spaceY) => scroll down by relY - spaceY |
| 642 |
// - it is in the container => don't scroll |
| 643 |
scrollLeft += Math.min(relX, Math.max(relX - spaceX, 0)); |
| 644 |
scrollTop += Math.min(relY, Math.max(relY - spaceY, 0)); |
| 645 |
} |
| 646 |
return new goog.math.Coordinate(scrollLeft, scrollTop); |
| 647 |
}; |
| 648 |
|
| 649 |
|
| 650 |
/** |
| 651 |
* Changes the scroll position of {@code container} with the minimum amount so |
| 652 |
* that the content and the borders of the given {@code element} become visible. |
| 653 |
* If the element is bigger than the container, its top left corner will be |
| 654 |
* aligned as close to the container's top left corner as possible. |
| 655 |
* |
| 656 |
* @param {Element} element The element to make visible. |
| 657 |
* @param {Element=} opt_container The container to scroll. If not set, then the |
| 658 |
* document scroll element will be used. |
| 659 |
* @param {boolean=} opt_center Whether to center the element in the container. |
| 660 |
* Defaults to false. |
| 661 |
*/ |
| 662 |
goog.style.scrollIntoContainerView = function( |
| 663 |
element, opt_container, opt_center) { |
| 664 |
var container = opt_container || goog.dom.getDocumentScrollElement(); |
| 665 |
var offset = |
| 666 |
goog.style.getContainerOffsetToScrollInto(element, container, opt_center); |
| 667 |
container.scrollLeft = offset.x; |
| 668 |
container.scrollTop = offset.y; |
| 669 |
}; |
| 670 |
|
| 671 |
|
| 672 |
/** |
| 673 |
* Returns clientLeft (width of the left border and, if the directionality is |
| 674 |
* right to left, the vertical scrollbar) and clientTop as a coordinate object. |
| 675 |
* |
| 676 |
* @param {Element} el Element to get clientLeft for. |
| 677 |
* @return {!goog.math.Coordinate} Client left and top. |
| 678 |
*/ |
| 679 |
goog.style.getClientLeftTop = function(el) { |
| 680 |
return new goog.math.Coordinate(el.clientLeft, el.clientTop); |
| 681 |
}; |
| 682 |
|
| 683 |
|
| 684 |
/** |
| 685 |
* Returns a Coordinate object relative to the top-left of the HTML document. |
| 686 |
* Implemented as a single function to save having to do two recursive loops in |
| 687 |
* opera and safari just to get both coordinates. If you just want one value do |
| 688 |
* use goog.style.getPageOffsetLeft() and goog.style.getPageOffsetTop(), but |
| 689 |
* note if you call both those methods the tree will be analysed twice. |
| 690 |
* |
| 691 |
* @param {Element} el Element to get the page offset for. |
| 692 |
* @return {!goog.math.Coordinate} The page offset. |
| 693 |
*/ |
| 694 |
goog.style.getPageOffset = function(el) { |
| 695 |
var doc = goog.dom.getOwnerDocument(el); |
| 696 |
// TODO(gboyer): Update the jsdoc in a way that doesn't break the universe. |
| 697 |
goog.asserts.assertObject(el, 'Parameter is required'); |
| 698 |
|
| 699 |
// NOTE(arv): If element is hidden (display none or disconnected or any the |
| 700 |
// ancestors are hidden) we get (0,0) by default but we still do the |
| 701 |
// accumulation of scroll position. |
| 702 |
|
| 703 |
// TODO(arv): Should we check if the node is disconnected and in that case |
| 704 |
// return (0,0)? |
| 705 |
|
| 706 |
var pos = new goog.math.Coordinate(0, 0); |
| 707 |
var viewportElement = goog.style.getClientViewportElement(doc); |
| 708 |
if (el == viewportElement) { |
| 709 |
// viewport is always at 0,0 as that defined the coordinate system for this |
| 710 |
// function - this avoids special case checks in the code below |
| 711 |
return pos; |
| 712 |
} |
| 713 |
|
| 714 |
var box = goog.style.getBoundingClientRect_(el); |
| 715 |
// Must add the scroll coordinates in to get the absolute page offset |
| 716 |
// of element since getBoundingClientRect returns relative coordinates to |
| 717 |
// the viewport. |
| 718 |
var scrollCoord = goog.dom.getDomHelper(doc).getDocumentScroll(); |
| 719 |
pos.x = box.left + scrollCoord.x; |
| 720 |
pos.y = box.top + scrollCoord.y; |
| 721 |
|
| 722 |
return pos; |
| 723 |
}; |
| 724 |
|
| 725 |
|
| 726 |
/** |
| 727 |
* Returns the left coordinate of an element relative to the HTML document |
| 728 |
* @param {Element} el Elements. |
| 729 |
* @return {number} The left coordinate. |
| 730 |
*/ |
| 731 |
goog.style.getPageOffsetLeft = function(el) { |
| 732 |
return goog.style.getPageOffset(el).x; |
| 733 |
}; |
| 734 |
|
| 735 |
|
| 736 |
/** |
| 737 |
* Returns the top coordinate of an element relative to the HTML document |
| 738 |
* @param {Element} el Elements. |
| 739 |
* @return {number} The top coordinate. |
| 740 |
*/ |
| 741 |
goog.style.getPageOffsetTop = function(el) { |
| 742 |
return goog.style.getPageOffset(el).y; |
| 743 |
}; |
| 744 |
|
| 745 |
|
| 746 |
/** |
| 747 |
* Returns a Coordinate object relative to the top-left of an HTML document |
| 748 |
* in an ancestor frame of this element. Used for measuring the position of |
| 749 |
* an element inside a frame relative to a containing frame. |
| 750 |
* |
| 751 |
* @param {Element} el Element to get the page offset for. |
| 752 |
* @param {Window} relativeWin The window to measure relative to. If relativeWin |
| 753 |
* is not in the ancestor frame chain of the element, we measure relative to |
| 754 |
* the top-most window. |
| 755 |
* @return {!goog.math.Coordinate} The page offset. |
| 756 |
*/ |
| 757 |
goog.style.getFramedPageOffset = function(el, relativeWin) { |
| 758 |
var position = new goog.math.Coordinate(0, 0); |
| 759 |
|
| 760 |
// Iterate up the ancestor frame chain, keeping track of the current window |
| 761 |
// and the current element in that window. |
| 762 |
var currentWin = goog.dom.getWindow(goog.dom.getOwnerDocument(el)); |
| 763 |
|
| 764 |
// MS Edge throws when accessing "parent" if el's containing iframe has been |
| 765 |
// deleted. |
| 766 |
if (!goog.reflect.canAccessProperty(currentWin, 'parent')) { |
| 767 |
return position; |
| 768 |
} |
| 769 |
|
| 770 |
var currentEl = el; |
| 771 |
do { |
| 772 |
// if we're at the top window, we want to get the page offset. |
| 773 |
// if we're at an inner frame, we only want to get the window position |
| 774 |
// so that we can determine the actual page offset in the context of |
| 775 |
// the outer window. |
| 776 |
var offset = currentWin == relativeWin ? |
| 777 |
goog.style.getPageOffset(currentEl) : |
| 778 |
goog.style.getClientPositionForElement_(goog.asserts.assert(currentEl)); |
| 779 |
|
| 780 |
position.x += offset.x; |
| 781 |
position.y += offset.y; |
| 782 |
} while (currentWin && currentWin != relativeWin && |
| 783 |
currentWin != currentWin.parent && |
| 784 |
(currentEl = currentWin.frameElement) && |
| 785 |
(currentWin = currentWin.parent)); |
| 786 |
|
| 787 |
return position; |
| 788 |
}; |
| 789 |
|
| 790 |
|
| 791 |
/** |
| 792 |
* Translates the specified rect relative to origBase page, for newBase page. |
| 793 |
* If origBase and newBase are the same, this function does nothing. |
| 794 |
* |
| 795 |
* @param {goog.math.Rect} rect The source rectangle relative to origBase page, |
| 796 |
* and it will have the translated result. |
| 797 |
* @param {goog.dom.DomHelper} origBase The DomHelper for the input rectangle. |
| 798 |
* @param {goog.dom.DomHelper} newBase The DomHelper for the resultant |
| 799 |
* coordinate. This must be a DOM for an ancestor frame of origBase |
| 800 |
* or the same as origBase. |
| 801 |
*/ |
| 802 |
goog.style.translateRectForAnotherFrame = function(rect, origBase, newBase) { |
| 803 |
if (origBase.getDocument() != newBase.getDocument()) { |
| 804 |
var body = origBase.getDocument().body; |
| 805 |
var pos = goog.style.getFramedPageOffset(body, newBase.getWindow()); |
| 806 |
|
| 807 |
// Adjust Body's margin. |
| 808 |
pos = goog.math.Coordinate.difference(pos, goog.style.getPageOffset(body)); |
| 809 |
|
| 810 |
if (goog.userAgent.IE && !goog.userAgent.isDocumentModeOrHigher(9) && |
| 811 |
!origBase.isCss1CompatMode()) { |
| 812 |
pos = goog.math.Coordinate.difference(pos, origBase.getDocumentScroll()); |
| 813 |
} |
| 814 |
|
| 815 |
rect.left += pos.x; |
| 816 |
rect.top += pos.y; |
| 817 |
} |
| 818 |
}; |
| 819 |
|
| 820 |
|
| 821 |
/** |
| 822 |
* Returns the position of an element relative to another element in the |
| 823 |
* document. A relative to B |
| 824 |
* @param {Element|Event|goog.events.Event} a Element or mouse event whose |
| 825 |
* position we're calculating. |
| 826 |
* @param {Element|Event|goog.events.Event} b Element or mouse event position |
| 827 |
* is relative to. |
| 828 |
* @return {!goog.math.Coordinate} The relative position. |
| 829 |
*/ |
| 830 |
goog.style.getRelativePosition = function(a, b) { |
| 831 |
var ap = goog.style.getClientPosition(a); |
| 832 |
var bp = goog.style.getClientPosition(b); |
| 833 |
return new goog.math.Coordinate(ap.x - bp.x, ap.y - bp.y); |
| 834 |
}; |
| 835 |
|
| 836 |
|
| 837 |
/** |
| 838 |
* Returns the position of the event or the element's border box relative to |
| 839 |
* the client viewport. |
| 840 |
* @param {!Element} el Element whose position to get. |
| 841 |
* @return {!goog.math.Coordinate} The position. |
| 842 |
* @private |
| 843 |
*/ |
| 844 |
goog.style.getClientPositionForElement_ = function(el) { |
| 845 |
var box = goog.style.getBoundingClientRect_(el); |
| 846 |
return new goog.math.Coordinate(box.left, box.top); |
| 847 |
}; |
| 848 |
|
| 849 |
|
| 850 |
/** |
| 851 |
* Returns the position of the event or the element's border box relative to |
| 852 |
* the client viewport. If an event is passed, and if this event is a "touch" |
| 853 |
* event, then the position of the first changedTouches will be returned. |
| 854 |
* @param {Element|Event|goog.events.Event} el Element or a mouse / touch event. |
| 855 |
* @return {!goog.math.Coordinate} The position. |
| 856 |
*/ |
| 857 |
goog.style.getClientPosition = function(el) { |
| 858 |
goog.asserts.assert(el); |
| 859 |
if (el.nodeType == goog.dom.NodeType.ELEMENT) { |
| 860 |
return goog.style.getClientPositionForElement_( |
| 861 |
/** @type {!Element} */ (el)); |
| 862 |
} else { |
| 863 |
var targetEvent = el.changedTouches ? el.changedTouches[0] : el; |
| 864 |
return new goog.math.Coordinate(targetEvent.clientX, targetEvent.clientY); |
| 865 |
} |
| 866 |
}; |
| 867 |
|
| 868 |
|
| 869 |
/** |
| 870 |
* Moves an element to the given coordinates relative to the client viewport. |
| 871 |
* @param {Element} el Absolutely positioned element to set page offset for. |
| 872 |
* It must be in the document. |
| 873 |
* @param {number|goog.math.Coordinate} x Left position of the element's margin |
| 874 |
* box or a coordinate object. |
| 875 |
* @param {number=} opt_y Top position of the element's margin box. |
| 876 |
*/ |
| 877 |
goog.style.setPageOffset = function(el, x, opt_y) { |
| 878 |
// Get current pageoffset |
| 879 |
var cur = goog.style.getPageOffset(el); |
| 880 |
|
| 881 |
if (x instanceof goog.math.Coordinate) { |
| 882 |
opt_y = x.y; |
| 883 |
x = x.x; |
| 884 |
} |
| 885 |
|
| 886 |
// NOTE(arv): We cannot allow strings for x and y. We could but that would |
| 887 |
// require us to manually transform between different units |
| 888 |
|
| 889 |
// Work out deltas |
| 890 |
var dx = goog.asserts.assertNumber(x) - cur.x; |
| 891 |
var dy = Number(opt_y) - cur.y; |
| 892 |
|
| 893 |
// Set position to current left/top + delta |
| 894 |
goog.style.setPosition( |
| 895 |
el, /** @type {!HTMLElement} */ (el).offsetLeft + dx, |
| 896 |
/** @type {!HTMLElement} */ (el).offsetTop + dy); |
| 897 |
}; |
| 898 |
|
| 899 |
|
| 900 |
/** |
| 901 |
* Sets the width/height values of an element. If an argument is numeric, |
| 902 |
* or a goog.math.Size is passed, it is assumed to be pixels and will add |
| 903 |
* 'px' after converting it to an integer in string form. (This just sets the |
| 904 |
* CSS width and height properties so it might set content-box or border-box |
| 905 |
* size depending on the box model the browser is using.) |
| 906 |
* |
| 907 |
* @param {Element} element Element to set the size of. |
| 908 |
* @param {string|number|goog.math.Size} w Width of the element, or a |
| 909 |
* size object. |
| 910 |
* @param {string|number=} opt_h Height of the element. Required if w is not a |
| 911 |
* size object. |
| 912 |
*/ |
| 913 |
goog.style.setSize = function(element, w, opt_h) { |
| 914 |
var h; |
| 915 |
if (w instanceof goog.math.Size) { |
| 916 |
h = w.height; |
| 917 |
w = w.width; |
| 918 |
} else { |
| 919 |
if (opt_h == undefined) { |
| 920 |
throw new Error('missing height argument'); |
| 921 |
} |
| 922 |
h = opt_h; |
| 923 |
} |
| 924 |
|
| 925 |
goog.style.setWidth(element, /** @type {string|number} */ (w)); |
| 926 |
goog.style.setHeight(element, h); |
| 927 |
}; |
| 928 |
|
| 929 |
|
| 930 |
/** |
| 931 |
* Helper function to create a string to be set into a pixel-value style |
| 932 |
* property of an element. Can round to the nearest integer value. |
| 933 |
* |
| 934 |
* @param {string|number} value The style value to be used. If a number, |
| 935 |
* 'px' will be appended, otherwise the value will be applied directly. |
| 936 |
* @param {boolean} round Whether to round the nearest integer (if property |
| 937 |
* is a number). |
| 938 |
* @return {string} The string value for the property. |
| 939 |
* @private |
| 940 |
*/ |
| 941 |
goog.style.getPixelStyleValue_ = function(value, round) { |
| 942 |
if (typeof value == 'number') { |
| 943 |
value = (round ? Math.round(value) : value) + 'px'; |
| 944 |
} |
| 945 |
|
| 946 |
return value; |
| 947 |
}; |
| 948 |
|
| 949 |
|
| 950 |
/** |
| 951 |
* Set the height of an element. Sets the element's style property. |
| 952 |
* @param {Element} element Element to set the height of. |
| 953 |
* @param {string|number} height The height value to set. If a number, 'px' |
| 954 |
* will be appended, otherwise the value will be applied directly. |
| 955 |
*/ |
| 956 |
goog.style.setHeight = function(element, height) { |
| 957 |
element.style.height = goog.style.getPixelStyleValue_(height, true); |
| 958 |
}; |
| 959 |
|
| 960 |
|
| 961 |
/** |
| 962 |
* Set the width of an element. Sets the element's style property. |
| 963 |
* @param {Element} element Element to set the width of. |
| 964 |
* @param {string|number} width The width value to set. If a number, 'px' |
| 965 |
* will be appended, otherwise the value will be applied directly. |
| 966 |
*/ |
| 967 |
goog.style.setWidth = function(element, width) { |
| 968 |
element.style.width = goog.style.getPixelStyleValue_(width, true); |
| 969 |
}; |
| 970 |
|
| 971 |
|
| 972 |
/** |
| 973 |
* Gets the height and width of an element, even if its display is none. |
| 974 |
* |
| 975 |
* Specifically, this returns the height and width of the border box, |
| 976 |
* irrespective of the box model in effect. |
| 977 |
* |
| 978 |
* Note that this function does not take CSS transforms into account. Please see |
| 979 |
* {@code goog.style.getTransformedSize}. |
| 980 |
* @param {Element} element Element to get size of. |
| 981 |
* @return {!goog.math.Size} Object with width/height properties. |
| 982 |
*/ |
| 983 |
goog.style.getSize = function(element) { |
| 984 |
return goog.style.evaluateWithTemporaryDisplay_( |
| 985 |
goog.style.getSizeWithDisplay_, /** @type {!Element} */ (element)); |
| 986 |
}; |
| 987 |
|
| 988 |
|
| 989 |
/** |
| 990 |
* Call {@code fn} on {@code element} such that {@code element}'s dimensions are |
| 991 |
* accurate when it's passed to {@code fn}. |
| 992 |
* @param {function(!Element): T} fn Function to call with {@code element} as |
| 993 |
* an argument after temporarily changing {@code element}'s display such |
| 994 |
* that its dimensions are accurate. |
| 995 |
* @param {!Element} element Element (which may have display none) to use as |
| 996 |
* argument to {@code fn}. |
| 997 |
* @return {T} Value returned by calling {@code fn} with {@code element}. |
| 998 |
* @template T |
| 999 |
* @private |
| 1000 |
*/ |
| 1001 |
goog.style.evaluateWithTemporaryDisplay_ = function(fn, element) { |
| 1002 |
if (goog.style.getStyle_(element, 'display') != 'none') { |
| 1003 |
return fn(element); |
| 1004 |
} |
| 1005 |
|
| 1006 |
var style = element.style; |
| 1007 |
var originalDisplay = style.display; |
| 1008 |
var originalVisibility = style.visibility; |
| 1009 |
var originalPosition = style.position; |
| 1010 |
|
| 1011 |
style.visibility = 'hidden'; |
| 1012 |
style.position = 'absolute'; |
| 1013 |
style.display = 'inline'; |
| 1014 |
|
| 1015 |
var retVal = fn(element); |
| 1016 |
|
| 1017 |
style.display = originalDisplay; |
| 1018 |
style.position = originalPosition; |
| 1019 |
style.visibility = originalVisibility; |
| 1020 |
|
| 1021 |
return retVal; |
| 1022 |
}; |
| 1023 |
|
| 1024 |
|
| 1025 |
/** |
| 1026 |
* Gets the height and width of an element when the display is not none. |
| 1027 |
* @param {Element} element Element to get size of. |
| 1028 |
* @return {!goog.math.Size} Object with width/height properties. |
| 1029 |
* @private |
| 1030 |
*/ |
| 1031 |
goog.style.getSizeWithDisplay_ = function(element) { |
| 1032 |
var offsetWidth = /** @type {!HTMLElement} */ (element).offsetWidth; |
| 1033 |
var offsetHeight = /** @type {!HTMLElement} */ (element).offsetHeight; |
| 1034 |
var webkitOffsetsZero = |
| 1035 |
goog.userAgent.WEBKIT && !offsetWidth && !offsetHeight; |
| 1036 |
if ((!goog.isDef(offsetWidth) || webkitOffsetsZero) && |
| 1037 |
element.getBoundingClientRect) { |
| 1038 |
// Fall back to calling getBoundingClientRect when offsetWidth or |
| 1039 |
// offsetHeight are not defined, or when they are zero in WebKit browsers. |
| 1040 |
// This makes sure that we return for the correct size for SVG elements, but |
| 1041 |
// will still return 0 on Webkit prior to 534.8, see |
| 1042 |
// http://trac.webkit.org/changeset/67252. |
| 1043 |
var clientRect = goog.style.getBoundingClientRect_(element); |
| 1044 |
return new goog.math.Size( |
| 1045 |
clientRect.right - clientRect.left, clientRect.bottom - clientRect.top); |
| 1046 |
} |
| 1047 |
return new goog.math.Size(offsetWidth, offsetHeight); |
| 1048 |
}; |
| 1049 |
|
| 1050 |
|
| 1051 |
/** |
| 1052 |
* Gets the height and width of an element, post transform, even if its display |
| 1053 |
* is none. |
| 1054 |
* |
| 1055 |
* This is like {@code goog.style.getSize}, except: |
| 1056 |
* <ol> |
| 1057 |
* <li>Takes webkitTransforms such as rotate and scale into account. |
| 1058 |
* <li>Will return null if {@code element} doesn't respond to |
| 1059 |
* {@code getBoundingClientRect}. |
| 1060 |
* <li>Currently doesn't make sense on non-WebKit browsers which don't support |
| 1061 |
* webkitTransforms. |
| 1062 |
* </ol> |
| 1063 |
* @param {!Element} element Element to get size of. |
| 1064 |
* @return {goog.math.Size} Object with width/height properties. |
| 1065 |
*/ |
| 1066 |
goog.style.getTransformedSize = function(element) { |
| 1067 |
if (!element.getBoundingClientRect) { |
| 1068 |
return null; |
| 1069 |
} |
| 1070 |
|
| 1071 |
var clientRect = goog.style.evaluateWithTemporaryDisplay_( |
| 1072 |
goog.style.getBoundingClientRect_, element); |
| 1073 |
return new goog.math.Size( |
| 1074 |
clientRect.right - clientRect.left, clientRect.bottom - clientRect.top); |
| 1075 |
}; |
| 1076 |
|
| 1077 |
|
| 1078 |
/** |
| 1079 |
* Returns a bounding rectangle for a given element in page space. |
| 1080 |
* @param {Element} element Element to get bounds of. Must not be display none. |
| 1081 |
* @return {!goog.math.Rect} Bounding rectangle for the element. |
| 1082 |
*/ |
| 1083 |
goog.style.getBounds = function(element) { |
| 1084 |
var o = goog.style.getPageOffset(element); |
| 1085 |
var s = goog.style.getSize(element); |
| 1086 |
return new goog.math.Rect(o.x, o.y, s.width, s.height); |
| 1087 |
}; |
| 1088 |
|
| 1089 |
|
| 1090 |
/** |
| 1091 |
* Converts a CSS selector in the form style-property to styleProperty. |
| 1092 |
* @param {*} selector CSS Selector. |
| 1093 |
* @return {string} Camel case selector. |
| 1094 |
* @deprecated Use goog.string.toCamelCase instead. |
| 1095 |
*/ |
| 1096 |
goog.style.toCamelCase = function(selector) { |
| 1097 |
return goog.string.toCamelCase(String(selector)); |
| 1098 |
}; |
| 1099 |
|
| 1100 |
|
| 1101 |
/** |
| 1102 |
* Converts a CSS selector in the form styleProperty to style-property. |
| 1103 |
* @param {string} selector Camel case selector. |
| 1104 |
* @return {string} Selector cased. |
| 1105 |
* @deprecated Use goog.string.toSelectorCase instead. |
| 1106 |
*/ |
| 1107 |
goog.style.toSelectorCase = function(selector) { |
| 1108 |
return goog.string.toSelectorCase(selector); |
| 1109 |
}; |
| 1110 |
|
| 1111 |
|
| 1112 |
/** |
| 1113 |
* Gets the opacity of a node (x-browser). This gets the inline style opacity |
| 1114 |
* of the node, and does not take into account the cascaded or the computed |
| 1115 |
* style for this node. |
| 1116 |
* @param {Element} el Element whose opacity has to be found. |
| 1117 |
* @return {number|string} Opacity between 0 and 1 or an empty string {@code ''} |
| 1118 |
* if the opacity is not set. |
| 1119 |
*/ |
| 1120 |
goog.style.getOpacity = function(el) { |
| 1121 |
goog.asserts.assert(el); |
| 1122 |
var style = el.style; |
| 1123 |
var result = ''; |
| 1124 |
if ('opacity' in style) { |
| 1125 |
result = style.opacity; |
| 1126 |
} else if ('MozOpacity' in style) { |
| 1127 |
result = style.MozOpacity; |
| 1128 |
} else if ('filter' in style) { |
| 1129 |
var match = style.filter.match(/alpha\(opacity=([\d.]+)\)/); |
| 1130 |
if (match) { |
| 1131 |
result = String(match[1] / 100); |
| 1132 |
} |
| 1133 |
} |
| 1134 |
return result == '' ? result : Number(result); |
| 1135 |
}; |
| 1136 |
|
| 1137 |
|
| 1138 |
/** |
| 1139 |
* Sets the opacity of a node (x-browser). |
| 1140 |
* @param {Element} el Elements whose opacity has to be set. |
| 1141 |
* @param {number|string} alpha Opacity between 0 and 1 or an empty string |
| 1142 |
* {@code ''} to clear the opacity. |
| 1143 |
*/ |
| 1144 |
goog.style.setOpacity = function(el, alpha) { |
| 1145 |
goog.asserts.assert(el); |
| 1146 |
var style = el.style; |
| 1147 |
if ('opacity' in style) { |
| 1148 |
style.opacity = alpha; |
| 1149 |
} else if ('MozOpacity' in style) { |
| 1150 |
style.MozOpacity = alpha; |
| 1151 |
} else if ('filter' in style) { |
| 1152 |
// TODO(arv): Overwriting the filter might have undesired side effects. |
| 1153 |
if (alpha === '') { |
| 1154 |
style.filter = ''; |
| 1155 |
} else { |
| 1156 |
style.filter = 'alpha(opacity=' + (Number(alpha) * 100) + ')'; |
| 1157 |
} |
| 1158 |
} |
| 1159 |
}; |
| 1160 |
|
| 1161 |
|
| 1162 |
/** |
| 1163 |
* Sets the background of an element to a transparent image in a browser- |
| 1164 |
* independent manner. |
| 1165 |
* |
| 1166 |
* This function does not support repeating backgrounds or alternate background |
| 1167 |
* positions to match the behavior of Internet Explorer. It also does not |
| 1168 |
* support sizingMethods other than crop since they cannot be replicated in |
| 1169 |
* browsers other than Internet Explorer. |
| 1170 |
* |
| 1171 |
* @param {Element} el The element to set background on. |
| 1172 |
* @param {string} src The image source URL. |
| 1173 |
*/ |
| 1174 |
goog.style.setTransparentBackgroundImage = function(el, src) { |
| 1175 |
var style = el.style; |
| 1176 |
// It is safe to use the style.filter in IE only. In Safari 'filter' is in |
| 1177 |
// style object but access to style.filter causes it to throw an exception. |
| 1178 |
// Note: IE8 supports images with an alpha channel. |
| 1179 |
if (goog.userAgent.IE && !goog.userAgent.isVersionOrHigher('8')) { |
| 1180 |
// See TODO in setOpacity. |
| 1181 |
style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(' + |
| 1182 |
'src="' + src + '", sizingMethod="crop")'; |
| 1183 |
} else { |
| 1184 |
// Set style properties individually instead of using background shorthand |
| 1185 |
// to prevent overwriting a pre-existing background color. |
| 1186 |
style.backgroundImage = 'url(' + src + ')'; |
| 1187 |
style.backgroundPosition = 'top left'; |
| 1188 |
style.backgroundRepeat = 'no-repeat'; |
| 1189 |
} |
| 1190 |
}; |
| 1191 |
|
| 1192 |
|
| 1193 |
/** |
| 1194 |
* Clears the background image of an element in a browser independent manner. |
| 1195 |
* @param {Element} el The element to clear background image for. |
| 1196 |
*/ |
| 1197 |
goog.style.clearTransparentBackgroundImage = function(el) { |
| 1198 |
var style = el.style; |
| 1199 |
if ('filter' in style) { |
| 1200 |
// See TODO in setOpacity. |
| 1201 |
style.filter = ''; |
| 1202 |
} else { |
| 1203 |
// Set style properties individually instead of using background shorthand |
| 1204 |
// to prevent overwriting a pre-existing background color. |
| 1205 |
style.backgroundImage = 'none'; |
| 1206 |
} |
| 1207 |
}; |
| 1208 |
|
| 1209 |
|
| 1210 |
/** |
| 1211 |
* Shows or hides an element from the page. Hiding the element is done by |
| 1212 |
* setting the display property to "none", removing the element from the |
| 1213 |
* rendering hierarchy so it takes up no space. To show the element, the default |
| 1214 |
* inherited display property is restored (defined either in stylesheets or by |
| 1215 |
* the browser's default style rules.) |
| 1216 |
* |
| 1217 |
* Caveat 1: if the inherited display property for the element is set to "none" |
| 1218 |
* by the stylesheets, that is the property that will be restored by a call to |
| 1219 |
* showElement(), effectively toggling the display between "none" and "none". |
| 1220 |
* |
| 1221 |
* Caveat 2: if the element display style is set inline (by setting either |
| 1222 |
* element.style.display or a style attribute in the HTML), a call to |
| 1223 |
* showElement will clear that setting and defer to the inherited style in the |
| 1224 |
* stylesheet. |
| 1225 |
* @param {Element} el Element to show or hide. |
| 1226 |
* @param {*} display True to render the element in its default style, |
| 1227 |
* false to disable rendering the element. |
| 1228 |
* @deprecated Use goog.style.setElementShown instead. |
| 1229 |
*/ |
| 1230 |
goog.style.showElement = function(el, display) { |
| 1231 |
goog.style.setElementShown(el, display); |
| 1232 |
}; |
| 1233 |
|
| 1234 |
|
| 1235 |
/** |
| 1236 |
* Shows or hides an element from the page. Hiding the element is done by |
| 1237 |
* setting the display property to "none", removing the element from the |
| 1238 |
* rendering hierarchy so it takes up no space. To show the element, the default |
| 1239 |
* inherited display property is restored (defined either in stylesheets or by |
| 1240 |
* the browser's default style rules). |
| 1241 |
* |
| 1242 |
* Caveat 1: if the inherited display property for the element is set to "none" |
| 1243 |
* by the stylesheets, that is the property that will be restored by a call to |
| 1244 |
* setElementShown(), effectively toggling the display between "none" and |
| 1245 |
* "none". |
| 1246 |
* |
| 1247 |
* Caveat 2: if the element display style is set inline (by setting either |
| 1248 |
* element.style.display or a style attribute in the HTML), a call to |
| 1249 |
* setElementShown will clear that setting and defer to the inherited style in |
| 1250 |
* the stylesheet. |
| 1251 |
* @param {Element} el Element to show or hide. |
| 1252 |
* @param {*} isShown True to render the element in its default style, |
| 1253 |
* false to disable rendering the element. |
| 1254 |
*/ |
| 1255 |
goog.style.setElementShown = function(el, isShown) { |
| 1256 |
el.style.display = isShown ? '' : 'none'; |
| 1257 |
}; |
| 1258 |
|
| 1259 |
|
| 1260 |
/** |
| 1261 |
* Test whether the given element has been shown or hidden via a call to |
| 1262 |
* {@link #setElementShown}. |
| 1263 |
* |
| 1264 |
* Note this is strictly a companion method for a call |
| 1265 |
* to {@link #setElementShown} and the same caveats apply; in particular, this |
| 1266 |
* method does not guarantee that the return value will be consistent with |
| 1267 |
* whether or not the element is actually visible. |
| 1268 |
* |
| 1269 |
* @param {Element} el The element to test. |
| 1270 |
* @return {boolean} Whether the element has been shown. |
| 1271 |
* @see #setElementShown |
| 1272 |
*/ |
| 1273 |
goog.style.isElementShown = function(el) { |
| 1274 |
return el.style.display != 'none'; |
| 1275 |
}; |
| 1276 |
|
| 1277 |
|
| 1278 |
/** |
| 1279 |
* Installs the style sheet into the window that contains opt_node. If |
| 1280 |
* opt_node is null, the main window is used. |
| 1281 |
* @param {!goog.html.SafeStyleSheet} safeStyleSheet The style sheet to install. |
| 1282 |
* @param {?Node=} opt_node Node whose parent document should have the |
| 1283 |
* styles installed. |
| 1284 |
* @return {!HTMLStyleElement|!StyleSheet} In IE<11, a StyleSheet object with no |
| 1285 |
* owning <style> tag (this is how IE creates style sheets). In every other |
| 1286 |
* browser, a <style> element with an attached style. This doesn't return a |
| 1287 |
* StyleSheet object so that setSafeStyleSheet can replace it (otherwise, if |
| 1288 |
* you pass a StyleSheet to setSafeStyleSheet, it will make a new StyleSheet |
| 1289 |
* and leave the original StyleSheet orphaned). |
| 1290 |
*/ |
| 1291 |
goog.style.installSafeStyleSheet = function(safeStyleSheet, opt_node) { |
| 1292 |
var dh = goog.dom.getDomHelper(opt_node); |
| 1293 |
|
| 1294 |
// IE < 11 requires createStyleSheet. Note that doc.createStyleSheet will be |
| 1295 |
// undefined as of IE 11. |
| 1296 |
var doc = dh.getDocument(); |
| 1297 |
if (goog.userAgent.IE && doc.createStyleSheet) { |
| 1298 |
var styleSheet = doc.createStyleSheet(); |
| 1299 |
goog.style.setSafeStyleSheet(styleSheet, safeStyleSheet); |
| 1300 |
return styleSheet; |
| 1301 |
} else { |
| 1302 |
var head = dh.getElementsByTagNameAndClass(goog.dom.TagName.HEAD)[0]; |
| 1303 |
|
| 1304 |
// In opera documents are not guaranteed to have a head element, thus we |
| 1305 |
// have to make sure one exists before using it. |
| 1306 |
if (!head) { |
| 1307 |
var body = dh.getElementsByTagNameAndClass(goog.dom.TagName.BODY)[0]; |
| 1308 |
head = dh.createDom(goog.dom.TagName.HEAD); |
| 1309 |
body.parentNode.insertBefore(head, body); |
| 1310 |
} |
| 1311 |
var el = dh.createDom(goog.dom.TagName.STYLE); |
| 1312 |
// NOTE(user): Setting styles after the style element has been appended |
| 1313 |
// to the head results in a nasty Webkit bug in certain scenarios. Please |
| 1314 |
// refer to https://bugs.webkit.org/show_bug.cgi?id=26307 for additional |
| 1315 |
// details. |
| 1316 |
goog.style.setSafeStyleSheet(el, safeStyleSheet); |
| 1317 |
dh.appendChild(head, el); |
| 1318 |
return el; |
| 1319 |
} |
| 1320 |
}; |
| 1321 |
|
| 1322 |
|
| 1323 |
/** |
| 1324 |
* Removes the styles added by {@link #installStyles}. |
| 1325 |
* @param {Element|StyleSheet} styleSheet The value returned by |
| 1326 |
* {@link #installStyles}. |
| 1327 |
*/ |
| 1328 |
goog.style.uninstallStyles = function(styleSheet) { |
| 1329 |
var node = styleSheet.ownerNode || styleSheet.owningElement || |
| 1330 |
/** @type {Element} */ (styleSheet); |
| 1331 |
goog.dom.removeNode(node); |
| 1332 |
}; |
| 1333 |
|
| 1334 |
|
| 1335 |
/** |
| 1336 |
* Sets the content of a style element. The style element can be any valid |
| 1337 |
* style element. This element will have its content completely replaced by |
| 1338 |
* the safeStyleSheet. |
| 1339 |
* @param {!Element|!StyleSheet} element A stylesheet element as returned by |
| 1340 |
* installStyles. |
| 1341 |
* @param {!goog.html.SafeStyleSheet} safeStyleSheet The new content of the |
| 1342 |
* stylesheet. |
| 1343 |
*/ |
| 1344 |
goog.style.setSafeStyleSheet = function(element, safeStyleSheet) { |
| 1345 |
var stylesString = goog.html.SafeStyleSheet.unwrap(safeStyleSheet); |
| 1346 |
if (goog.userAgent.IE && goog.isDef(element.cssText)) { |
| 1347 |
// Adding the selectors individually caused the browser to hang if the |
| 1348 |
// selector was invalid or there were CSS comments. Setting the cssText of |
| 1349 |
// the style node works fine and ignores CSS that IE doesn't understand. |
| 1350 |
// However IE >= 11 doesn't support cssText any more, so we make sure that |
| 1351 |
// cssText is a defined property and otherwise fall back to innerHTML. |
| 1352 |
element.cssText = stylesString; |
| 1353 |
} else { |
| 1354 |
// Setting textContent doesn't work in Safari, see b/29340337. |
| 1355 |
element.innerHTML = stylesString; |
| 1356 |
} |
| 1357 |
}; |
| 1358 |
|
| 1359 |
|
| 1360 |
/** |
| 1361 |
* Sets 'white-space: pre-wrap' for a node (x-browser). |
| 1362 |
* |
| 1363 |
* There are as many ways of specifying pre-wrap as there are browsers. |
| 1364 |
* |
| 1365 |
* CSS3/IE8: white-space: pre-wrap; |
| 1366 |
* Mozilla: white-space: -moz-pre-wrap; |
| 1367 |
* Opera: white-space: -o-pre-wrap; |
| 1368 |
* IE6/7: white-space: pre; word-wrap: break-word; |
| 1369 |
* |
| 1370 |
* @param {Element} el Element to enable pre-wrap for. |
| 1371 |
*/ |
| 1372 |
goog.style.setPreWrap = function(el) { |
| 1373 |
var style = el.style; |
| 1374 |
if (goog.userAgent.IE && !goog.userAgent.isVersionOrHigher('8')) { |
| 1375 |
style.whiteSpace = 'pre'; |
| 1376 |
style.wordWrap = 'break-word'; |
| 1377 |
} else if (goog.userAgent.GECKO) { |
| 1378 |
style.whiteSpace = '-moz-pre-wrap'; |
| 1379 |
} else { |
| 1380 |
style.whiteSpace = 'pre-wrap'; |
| 1381 |
} |
| 1382 |
}; |
| 1383 |
|
| 1384 |
|
| 1385 |
/** |
| 1386 |
* Sets 'display: inline-block' for an element (cross-browser). |
| 1387 |
* @param {Element} el Element to which the inline-block display style is to be |
| 1388 |
* applied. |
| 1389 |
* @see ../demos/inline_block_quirks.html |
| 1390 |
* @see ../demos/inline_block_standards.html |
| 1391 |
*/ |
| 1392 |
goog.style.setInlineBlock = function(el) { |
| 1393 |
var style = el.style; |
| 1394 |
// Without position:relative, weirdness ensues. Just accept it and move on. |
| 1395 |
style.position = 'relative'; |
| 1396 |
|
| 1397 |
if (goog.userAgent.IE && !goog.userAgent.isVersionOrHigher('8')) { |
| 1398 |
// IE8 supports inline-block so fall through to the else |
| 1399 |
// Zoom:1 forces hasLayout, display:inline gives inline behavior. |
| 1400 |
style.zoom = '1'; |
| 1401 |
style.display = 'inline'; |
| 1402 |
} else { |
| 1403 |
// Opera, Webkit, and Safari seem to do OK with the standard inline-block |
| 1404 |
// style. |
| 1405 |
style.display = 'inline-block'; |
| 1406 |
} |
| 1407 |
}; |
| 1408 |
|
| 1409 |
|
| 1410 |
/** |
| 1411 |
* Returns true if the element is using right to left (rtl) direction. |
| 1412 |
* @param {Element} el The element to test. |
| 1413 |
* @return {boolean} True for right to left, false for left to right. |
| 1414 |
*/ |
| 1415 |
goog.style.isRightToLeft = function(el) { |
| 1416 |
return 'rtl' == goog.style.getStyle_(el, 'direction'); |
| 1417 |
}; |
| 1418 |
|
| 1419 |
|
| 1420 |
/** |
| 1421 |
* The CSS style property corresponding to an element being |
| 1422 |
* unselectable on the current browser platform (null if none). |
| 1423 |
* Opera and IE instead use a DOM attribute 'unselectable'. MS Edge uses |
| 1424 |
* the Webkit prefix. |
| 1425 |
* @type {?string} |
| 1426 |
* @private |
| 1427 |
*/ |
| 1428 |
goog.style.unselectableStyle_ = goog.userAgent.GECKO ? |
| 1429 |
'MozUserSelect' : |
| 1430 |
goog.userAgent.WEBKIT || goog.userAgent.EDGE ? 'WebkitUserSelect' : null; |
| 1431 |
|
| 1432 |
|
| 1433 |
/** |
| 1434 |
* Returns true if the element is set to be unselectable, false otherwise. |
| 1435 |
* Note that on some platforms (e.g. Mozilla), even if an element isn't set |
| 1436 |
* to be unselectable, it will behave as such if any of its ancestors is |
| 1437 |
* unselectable. |
| 1438 |
* @param {Element} el Element to check. |
| 1439 |
* @return {boolean} Whether the element is set to be unselectable. |
| 1440 |
*/ |
| 1441 |
goog.style.isUnselectable = function(el) { |
| 1442 |
if (goog.style.unselectableStyle_) { |
| 1443 |
return el.style[goog.style.unselectableStyle_].toLowerCase() == 'none'; |
| 1444 |
} else if (goog.userAgent.IE || goog.userAgent.OPERA) { |
| 1445 |
return el.getAttribute('unselectable') == 'on'; |
| 1446 |
} |
| 1447 |
return false; |
| 1448 |
}; |
| 1449 |
|
| 1450 |
|
| 1451 |
/** |
| 1452 |
* Makes the element and its descendants selectable or unselectable. Note |
| 1453 |
* that on some platforms (e.g. Mozilla), even if an element isn't set to |
| 1454 |
* be unselectable, it will behave as such if any of its ancestors is |
| 1455 |
* unselectable. |
| 1456 |
* @param {Element} el The element to alter. |
| 1457 |
* @param {boolean} unselectable Whether the element and its descendants |
| 1458 |
* should be made unselectable. |
| 1459 |
* @param {boolean=} opt_noRecurse Whether to only alter the element's own |
| 1460 |
* selectable state, and leave its descendants alone; defaults to false. |
| 1461 |
*/ |
| 1462 |
goog.style.setUnselectable = function(el, unselectable, opt_noRecurse) { |
| 1463 |
// TODO(attila): Do we need all of TR_DomUtil.makeUnselectable() in Closure? |
| 1464 |
var descendants = !opt_noRecurse ? el.getElementsByTagName('*') : null; |
| 1465 |
var name = goog.style.unselectableStyle_; |
| 1466 |
if (name) { |
| 1467 |
// Add/remove the appropriate CSS style to/from the element and its |
| 1468 |
// descendants. |
| 1469 |
var value = unselectable ? 'none' : ''; |
| 1470 |
// MathML elements do not have a style property. Verify before setting. |
| 1471 |
if (el.style) { |
| 1472 |
el.style[name] = value; |
| 1473 |
} |
| 1474 |
if (descendants) { |
| 1475 |
for (var i = 0, descendant; descendant = descendants[i]; i++) { |
| 1476 |
if (descendant.style) { |
| 1477 |
descendant.style[name] = value; |
| 1478 |
} |
| 1479 |
} |
| 1480 |
} |
| 1481 |
} else if (goog.userAgent.IE || goog.userAgent.OPERA) { |
| 1482 |
// Toggle the 'unselectable' attribute on the element and its descendants. |
| 1483 |
var value = unselectable ? 'on' : ''; |
| 1484 |
el.setAttribute('unselectable', value); |
| 1485 |
if (descendants) { |
| 1486 |
for (var i = 0, descendant; descendant = descendants[i]; i++) { |
| 1487 |
descendant.setAttribute('unselectable', value); |
| 1488 |
} |
| 1489 |
} |
| 1490 |
} |
| 1491 |
}; |
| 1492 |
|
| 1493 |
|
| 1494 |
/** |
| 1495 |
* Gets the border box size for an element. |
| 1496 |
* @param {Element} element The element to get the size for. |
| 1497 |
* @return {!goog.math.Size} The border box size. |
| 1498 |
*/ |
| 1499 |
goog.style.getBorderBoxSize = function(element) { |
| 1500 |
return new goog.math.Size( |
| 1501 |
/** @type {!HTMLElement} */ (element).offsetWidth, |
| 1502 |
/** @type {!HTMLElement} */ (element).offsetHeight); |
| 1503 |
}; |
| 1504 |
|
| 1505 |
|
| 1506 |
/** |
| 1507 |
* Sets the border box size of an element. This is potentially expensive in IE |
| 1508 |
* if the document is CSS1Compat mode |
| 1509 |
* @param {Element} element The element to set the size on. |
| 1510 |
* @param {goog.math.Size} size The new size. |
| 1511 |
*/ |
| 1512 |
goog.style.setBorderBoxSize = function(element, size) { |
| 1513 |
var doc = goog.dom.getOwnerDocument(element); |
| 1514 |
var isCss1CompatMode = goog.dom.getDomHelper(doc).isCss1CompatMode(); |
| 1515 |
|
| 1516 |
if (goog.userAgent.IE && !goog.userAgent.isVersionOrHigher('10') && |
| 1517 |
(!isCss1CompatMode || !goog.userAgent.isVersionOrHigher('8'))) { |
| 1518 |
var style = element.style; |
| 1519 |
if (isCss1CompatMode) { |
| 1520 |
var paddingBox = goog.style.getPaddingBox(element); |
| 1521 |
var borderBox = goog.style.getBorderBox(element); |
| 1522 |
style.pixelWidth = size.width - borderBox.left - paddingBox.left - |
| 1523 |
paddingBox.right - borderBox.right; |
| 1524 |
style.pixelHeight = size.height - borderBox.top - paddingBox.top - |
| 1525 |
paddingBox.bottom - borderBox.bottom; |
| 1526 |
} else { |
| 1527 |
style.pixelWidth = size.width; |
| 1528 |
style.pixelHeight = size.height; |
| 1529 |
} |
| 1530 |
} else { |
| 1531 |
goog.style.setBoxSizingSize_(element, size, 'border-box'); |
| 1532 |
} |
| 1533 |
}; |
| 1534 |
|
| 1535 |
|
| 1536 |
/** |
| 1537 |
* Gets the content box size for an element. This is potentially expensive in |
| 1538 |
* all browsers. |
| 1539 |
* @param {Element} element The element to get the size for. |
| 1540 |
* @return {!goog.math.Size} The content box size. |
| 1541 |
*/ |
| 1542 |
goog.style.getContentBoxSize = function(element) { |
| 1543 |
var doc = goog.dom.getOwnerDocument(element); |
| 1544 |
var ieCurrentStyle = goog.userAgent.IE && element.currentStyle; |
| 1545 |
if (ieCurrentStyle && goog.dom.getDomHelper(doc).isCss1CompatMode() && |
| 1546 |
ieCurrentStyle.width != 'auto' && ieCurrentStyle.height != 'auto' && |
| 1547 |
!ieCurrentStyle.boxSizing) { |
| 1548 |
// If IE in CSS1Compat mode than just use the width and height. |
| 1549 |
// If we have a boxSizing then fall back on measuring the borders etc. |
| 1550 |
var width = goog.style.getIePixelValue_( |
| 1551 |
element, /** @type {string} */ (ieCurrentStyle.width), 'width', |
| 1552 |
'pixelWidth'); |
| 1553 |
var height = goog.style.getIePixelValue_( |
| 1554 |
element, /** @type {string} */ (ieCurrentStyle.height), 'height', |
| 1555 |
'pixelHeight'); |
| 1556 |
return new goog.math.Size(width, height); |
| 1557 |
} else { |
| 1558 |
var borderBoxSize = goog.style.getBorderBoxSize(element); |
| 1559 |
var paddingBox = goog.style.getPaddingBox(element); |
| 1560 |
var borderBox = goog.style.getBorderBox(element); |
| 1561 |
return new goog.math.Size( |
| 1562 |
borderBoxSize.width - borderBox.left - paddingBox.left - |
| 1563 |
paddingBox.right - borderBox.right, |
| 1564 |
borderBoxSize.height - borderBox.top - paddingBox.top - |
| 1565 |
paddingBox.bottom - borderBox.bottom); |
| 1566 |
} |
| 1567 |
}; |
| 1568 |
|
| 1569 |
|
| 1570 |
/** |
| 1571 |
* Sets the content box size of an element. This is potentially expensive in IE |
| 1572 |
* if the document is BackCompat mode. |
| 1573 |
* @param {Element} element The element to set the size on. |
| 1574 |
* @param {goog.math.Size} size The new size. |
| 1575 |
*/ |
| 1576 |
goog.style.setContentBoxSize = function(element, size) { |
| 1577 |
var doc = goog.dom.getOwnerDocument(element); |
| 1578 |
var isCss1CompatMode = goog.dom.getDomHelper(doc).isCss1CompatMode(); |
| 1579 |
if (goog.userAgent.IE && !goog.userAgent.isVersionOrHigher('10') && |
| 1580 |
(!isCss1CompatMode || !goog.userAgent.isVersionOrHigher('8'))) { |
| 1581 |
var style = element.style; |
| 1582 |
if (isCss1CompatMode) { |
| 1583 |
style.pixelWidth = size.width; |
| 1584 |
style.pixelHeight = size.height; |
| 1585 |
} else { |
| 1586 |
var paddingBox = goog.style.getPaddingBox(element); |
| 1587 |
var borderBox = goog.style.getBorderBox(element); |
| 1588 |
style.pixelWidth = size.width + borderBox.left + paddingBox.left + |
| 1589 |
paddingBox.right + borderBox.right; |
| 1590 |
style.pixelHeight = size.height + borderBox.top + paddingBox.top + |
| 1591 |
paddingBox.bottom + borderBox.bottom; |
| 1592 |
} |
| 1593 |
} else { |
| 1594 |
goog.style.setBoxSizingSize_(element, size, 'content-box'); |
| 1595 |
} |
| 1596 |
}; |
| 1597 |
|
| 1598 |
|
| 1599 |
/** |
| 1600 |
* Helper function that sets the box sizing as well as the width and height |
| 1601 |
* @param {Element} element The element to set the size on. |
| 1602 |
* @param {goog.math.Size} size The new size to set. |
| 1603 |
* @param {string} boxSizing The box-sizing value. |
| 1604 |
* @private |
| 1605 |
*/ |
| 1606 |
goog.style.setBoxSizingSize_ = function(element, size, boxSizing) { |
| 1607 |
var style = element.style; |
| 1608 |
if (goog.userAgent.GECKO) { |
| 1609 |
style.MozBoxSizing = boxSizing; |
| 1610 |
} else if (goog.userAgent.WEBKIT) { |
| 1611 |
style.WebkitBoxSizing = boxSizing; |
| 1612 |
} else { |
| 1613 |
// Includes IE8 and Opera 9.50+ |
| 1614 |
style.boxSizing = boxSizing; |
| 1615 |
} |
| 1616 |
|
| 1617 |
// Setting this to a negative value will throw an exception on IE |
| 1618 |
// (and doesn't do anything different than setting it to 0). |
| 1619 |
style.width = Math.max(size.width, 0) + 'px'; |
| 1620 |
style.height = Math.max(size.height, 0) + 'px'; |
| 1621 |
}; |
| 1622 |
|
| 1623 |
|
| 1624 |
/** |
| 1625 |
* IE specific function that converts a non pixel unit to pixels. |
| 1626 |
* @param {Element} element The element to convert the value for. |
| 1627 |
* @param {string} value The current value as a string. The value must not be |
| 1628 |
* ''. |
| 1629 |
* @param {string} name The CSS property name to use for the converstion. This |
| 1630 |
* should be 'left', 'top', 'width' or 'height'. |
| 1631 |
* @param {string} pixelName The CSS pixel property name to use to get the |
| 1632 |
* value in pixels. |
| 1633 |
* @return {number} The value in pixels. |
| 1634 |
* @private |
| 1635 |
*/ |
| 1636 |
goog.style.getIePixelValue_ = function(element, value, name, pixelName) { |
| 1637 |
// Try if we already have a pixel value. IE does not do half pixels so we |
| 1638 |
// only check if it matches a number followed by 'px'. |
| 1639 |
if (/^\d+px?$/.test(value)) { |
| 1640 |
return parseInt(value, 10); |
| 1641 |
} else { |
| 1642 |
var oldStyleValue = element.style[name]; |
| 1643 |
var oldRuntimeValue = element.runtimeStyle[name]; |
| 1644 |
// set runtime style to prevent changes |
| 1645 |
element.runtimeStyle[name] = element.currentStyle[name]; |
| 1646 |
element.style[name] = value; |
| 1647 |
var pixelValue = element.style[pixelName]; |
| 1648 |
// restore |
| 1649 |
element.style[name] = oldStyleValue; |
| 1650 |
element.runtimeStyle[name] = oldRuntimeValue; |
| 1651 |
return +pixelValue; |
| 1652 |
} |
| 1653 |
}; |
| 1654 |
|
| 1655 |
|
| 1656 |
/** |
| 1657 |
* Helper function for getting the pixel padding or margin for IE. |
| 1658 |
* @param {Element} element The element to get the padding for. |
| 1659 |
* @param {string} propName The property name. |
| 1660 |
* @return {number} The pixel padding. |
| 1661 |
* @private |
| 1662 |
*/ |
| 1663 |
goog.style.getIePixelDistance_ = function(element, propName) { |
| 1664 |
var value = goog.style.getCascadedStyle(element, propName); |
| 1665 |
return value ? |
| 1666 |
goog.style.getIePixelValue_(element, value, 'left', 'pixelLeft') : |
| 1667 |
0; |
| 1668 |
}; |
| 1669 |
|
| 1670 |
|
| 1671 |
/** |
| 1672 |
* Gets the computed paddings or margins (on all sides) in pixels. |
| 1673 |
* @param {Element} element The element to get the padding for. |
| 1674 |
* @param {string} stylePrefix Pass 'padding' to retrieve the padding box, |
| 1675 |
* or 'margin' to retrieve the margin box. |
| 1676 |
* @return {!goog.math.Box} The computed paddings or margins. |
| 1677 |
* @private |
| 1678 |
*/ |
| 1679 |
goog.style.getBox_ = function(element, stylePrefix) { |
| 1680 |
if (goog.userAgent.IE) { |
| 1681 |
var left = goog.style.getIePixelDistance_(element, stylePrefix + 'Left'); |
| 1682 |
var right = goog.style.getIePixelDistance_(element, stylePrefix + 'Right'); |
| 1683 |
var top = goog.style.getIePixelDistance_(element, stylePrefix + 'Top'); |
| 1684 |
var bottom = |
| 1685 |
goog.style.getIePixelDistance_(element, stylePrefix + 'Bottom'); |
| 1686 |
return new goog.math.Box(top, right, bottom, left); |
| 1687 |
} else { |
| 1688 |
// On non-IE browsers, getComputedStyle is always non-null. |
| 1689 |
var left = goog.style.getComputedStyle(element, stylePrefix + 'Left'); |
| 1690 |
var right = goog.style.getComputedStyle(element, stylePrefix + 'Right'); |
| 1691 |
var top = goog.style.getComputedStyle(element, stylePrefix + 'Top'); |
| 1692 |
var bottom = goog.style.getComputedStyle(element, stylePrefix + 'Bottom'); |
| 1693 |
|
| 1694 |
// NOTE(arv): Gecko can return floating point numbers for the computed |
| 1695 |
// style values. |
| 1696 |
return new goog.math.Box( |
| 1697 |
parseFloat(top), parseFloat(right), parseFloat(bottom), |
| 1698 |
parseFloat(left)); |
| 1699 |
} |
| 1700 |
}; |
| 1701 |
|
| 1702 |
|
| 1703 |
/** |
| 1704 |
* Gets the computed paddings (on all sides) in pixels. |
| 1705 |
* @param {Element} element The element to get the padding for. |
| 1706 |
* @return {!goog.math.Box} The computed paddings. |
| 1707 |
*/ |
| 1708 |
goog.style.getPaddingBox = function(element) { |
| 1709 |
return goog.style.getBox_(element, 'padding'); |
| 1710 |
}; |
| 1711 |
|
| 1712 |
|
| 1713 |
/** |
| 1714 |
* Gets the computed margins (on all sides) in pixels. |
| 1715 |
* @param {Element} element The element to get the margins for. |
| 1716 |
* @return {!goog.math.Box} The computed margins. |
| 1717 |
*/ |
| 1718 |
goog.style.getMarginBox = function(element) { |
| 1719 |
return goog.style.getBox_(element, 'margin'); |
| 1720 |
}; |
| 1721 |
|
| 1722 |
|
| 1723 |
/** |
| 1724 |
* A map used to map the border width keywords to a pixel width. |
| 1725 |
* @type {!Object} |
| 1726 |
* @private |
| 1727 |
*/ |
| 1728 |
goog.style.ieBorderWidthKeywords_ = { |
| 1729 |
'thin': 2, |
| 1730 |
'medium': 4, |
| 1731 |
'thick': 6 |
| 1732 |
}; |
| 1733 |
|
| 1734 |
|
| 1735 |
/** |
| 1736 |
* Helper function for IE to get the pixel border. |
| 1737 |
* @param {Element} element The element to get the pixel border for. |
| 1738 |
* @param {string} prop The part of the property name. |
| 1739 |
* @return {number} The value in pixels. |
| 1740 |
* @private |
| 1741 |
*/ |
| 1742 |
goog.style.getIePixelBorder_ = function(element, prop) { |
| 1743 |
if (goog.style.getCascadedStyle(element, prop + 'Style') == 'none') { |
| 1744 |
return 0; |
| 1745 |
} |
| 1746 |
var width = goog.style.getCascadedStyle(element, prop + 'Width'); |
| 1747 |
if (width in goog.style.ieBorderWidthKeywords_) { |
| 1748 |
return goog.style.ieBorderWidthKeywords_[width]; |
| 1749 |
} |
| 1750 |
return goog.style.getIePixelValue_(element, width, 'left', 'pixelLeft'); |
| 1751 |
}; |
| 1752 |
|
| 1753 |
|
| 1754 |
/** |
| 1755 |
* Gets the computed border widths (on all sides) in pixels |
| 1756 |
* @param {Element} element The element to get the border widths for. |
| 1757 |
* @return {!goog.math.Box} The computed border widths. |
| 1758 |
*/ |
| 1759 |
goog.style.getBorderBox = function(element) { |
| 1760 |
if (goog.userAgent.IE && !goog.userAgent.isDocumentModeOrHigher(9)) { |
| 1761 |
var left = goog.style.getIePixelBorder_(element, 'borderLeft'); |
| 1762 |
var right = goog.style.getIePixelBorder_(element, 'borderRight'); |
| 1763 |
var top = goog.style.getIePixelBorder_(element, 'borderTop'); |
| 1764 |
var bottom = goog.style.getIePixelBorder_(element, 'borderBottom'); |
| 1765 |
return new goog.math.Box(top, right, bottom, left); |
| 1766 |
} else { |
| 1767 |
// On non-IE browsers, getComputedStyle is always non-null. |
| 1768 |
var left = goog.style.getComputedStyle(element, 'borderLeftWidth'); |
| 1769 |
var right = goog.style.getComputedStyle(element, 'borderRightWidth'); |
| 1770 |
var top = goog.style.getComputedStyle(element, 'borderTopWidth'); |
| 1771 |
var bottom = goog.style.getComputedStyle(element, 'borderBottomWidth'); |
| 1772 |
|
| 1773 |
return new goog.math.Box( |
| 1774 |
parseFloat(top), parseFloat(right), parseFloat(bottom), |
| 1775 |
parseFloat(left)); |
| 1776 |
} |
| 1777 |
}; |
| 1778 |
|
| 1779 |
|
| 1780 |
/** |
| 1781 |
* Returns the font face applied to a given node. Opera and IE should return |
| 1782 |
* the font actually displayed. Firefox returns the author's most-preferred |
| 1783 |
* font (whether the browser is capable of displaying it or not.) |
| 1784 |
* @param {Element} el The element whose font family is returned. |
| 1785 |
* @return {string} The font family applied to el. |
| 1786 |
*/ |
| 1787 |
goog.style.getFontFamily = function(el) { |
| 1788 |
var doc = goog.dom.getOwnerDocument(el); |
| 1789 |
var font = ''; |
| 1790 |
// The moveToElementText method from the TextRange only works if the element |
| 1791 |
// is attached to the owner document. |
| 1792 |
if (doc.body.createTextRange && goog.dom.contains(doc, el)) { |
| 1793 |
var range = doc.body.createTextRange(); |
| 1794 |
range.moveToElementText(el); |
| 1795 |
|
| 1796 |
try { |
| 1797 |
font = range.queryCommandValue('FontName'); |
| 1798 |
} catch (e) { |
| 1799 |
// This is a workaround for a awkward exception. |
| 1800 |
// On some IE, there is an exception coming from it. |
| 1801 |
// The error description from this exception is: |
| 1802 |
// This window has already been registered as a drop target |
| 1803 |
// This is bogus description, likely due to a bug in ie. |
| 1804 |
font = ''; |
| 1805 |
} |
| 1806 |
} |
| 1807 |
if (!font) { |
| 1808 |
// Note if for some reason IE can't derive FontName with a TextRange, we |
| 1809 |
// fallback to using currentStyle |
| 1810 |
font = goog.style.getStyle_(el, 'fontFamily'); |
| 1811 |
} |
| 1812 |
|
| 1813 |
// Firefox returns the applied font-family string (author's list of |
| 1814 |
// preferred fonts.) We want to return the most-preferred font, in lieu of |
| 1815 |
// the *actually* applied font. |
| 1816 |
var fontsArray = font.split(','); |
| 1817 |
if (fontsArray.length > 1) font = fontsArray[0]; |
| 1818 |
|
| 1819 |
// Sanitize for x-browser consistency: |
| 1820 |
// Strip quotes because browsers aren't consistent with how they're |
| 1821 |
// applied; Opera always encloses, Firefox sometimes, and IE never. |
| 1822 |
return goog.string.stripQuotes(font, '"\''); |
| 1823 |
}; |
| 1824 |
|
| 1825 |
|
| 1826 |
/** |
| 1827 |
* Regular expression used for getLengthUnits. |
| 1828 |
* @type {RegExp} |
| 1829 |
* @private |
| 1830 |
*/ |
| 1831 |
goog.style.lengthUnitRegex_ = /[^\d]+$/; |
| 1832 |
|
| 1833 |
|
| 1834 |
/** |
| 1835 |
* Returns the units used for a CSS length measurement. |
| 1836 |
* @param {string} value A CSS length quantity. |
| 1837 |
* @return {?string} The units of measurement. |
| 1838 |
*/ |
| 1839 |
goog.style.getLengthUnits = function(value) { |
| 1840 |
var units = value.match(goog.style.lengthUnitRegex_); |
| 1841 |
return units && units[0] || null; |
| 1842 |
}; |
| 1843 |
|
| 1844 |
|
| 1845 |
/** |
| 1846 |
* Map of absolute CSS length units |
| 1847 |
* @type {!Object} |
| 1848 |
* @private |
| 1849 |
*/ |
| 1850 |
goog.style.ABSOLUTE_CSS_LENGTH_UNITS_ = { |
| 1851 |
'cm': 1, |
| 1852 |
'in': 1, |
| 1853 |
'mm': 1, |
| 1854 |
'pc': 1, |
| 1855 |
'pt': 1 |
| 1856 |
}; |
| 1857 |
|
| 1858 |
|
| 1859 |
/** |
| 1860 |
* Map of relative CSS length units that can be accurately converted to px |
| 1861 |
* font-size values using getIePixelValue_. Only units that are defined in |
| 1862 |
* relation to a font size are convertible (%, small, etc. are not). |
| 1863 |
* @type {!Object} |
| 1864 |
* @private |
| 1865 |
*/ |
| 1866 |
goog.style.CONVERTIBLE_RELATIVE_CSS_UNITS_ = { |
| 1867 |
'em': 1, |
| 1868 |
'ex': 1 |
| 1869 |
}; |
| 1870 |
|
| 1871 |
|
| 1872 |
/** |
| 1873 |
* Returns the font size, in pixels, of text in an element. |
| 1874 |
* @param {Element} el The element whose font size is returned. |
| 1875 |
* @return {number} The font size (in pixels). |
| 1876 |
*/ |
| 1877 |
goog.style.getFontSize = function(el) { |
| 1878 |
var fontSize = goog.style.getStyle_(el, 'fontSize'); |
| 1879 |
var sizeUnits = goog.style.getLengthUnits(fontSize); |
| 1880 |
if (fontSize && 'px' == sizeUnits) { |
| 1881 |
// NOTE(user): This could be parseFloat instead, but IE doesn't return |
| 1882 |
// decimal fractions in getStyle_ and Firefox reports the fractions, but |
| 1883 |
// ignores them when rendering. Interestingly enough, when we force the |
| 1884 |
// issue and size something to e.g., 50% of 25px, the browsers round in |
| 1885 |
// opposite directions with Firefox reporting 12px and IE 13px. I punt. |
| 1886 |
return parseInt(fontSize, 10); |
| 1887 |
} |
| 1888 |
|
| 1889 |
// In IE, we can convert absolute length units to a px value using |
| 1890 |
// goog.style.getIePixelValue_. Units defined in relation to a font size |
| 1891 |
// (em, ex) are applied relative to the element's parentNode and can also |
| 1892 |
// be converted. |
| 1893 |
if (goog.userAgent.IE) { |
| 1894 |
if (String(sizeUnits) in goog.style.ABSOLUTE_CSS_LENGTH_UNITS_) { |
| 1895 |
return goog.style.getIePixelValue_(el, fontSize, 'left', 'pixelLeft'); |
| 1896 |
} else if ( |
| 1897 |
el.parentNode && el.parentNode.nodeType == goog.dom.NodeType.ELEMENT && |
| 1898 |
String(sizeUnits) in goog.style.CONVERTIBLE_RELATIVE_CSS_UNITS_) { |
| 1899 |
// Check the parent size - if it is the same it means the relative size |
| 1900 |
// value is inherited and we therefore don't want to count it twice. If |
| 1901 |
// it is different, this element either has explicit style or has a CSS |
| 1902 |
// rule applying to it. |
| 1903 |
var parentElement = /** @type {!Element} */ (el.parentNode); |
| 1904 |
var parentSize = goog.style.getStyle_(parentElement, 'fontSize'); |
| 1905 |
return goog.style.getIePixelValue_( |
| 1906 |
parentElement, fontSize == parentSize ? '1em' : fontSize, 'left', |
| 1907 |
'pixelLeft'); |
| 1908 |
} |
| 1909 |
} |
| 1910 |
|
| 1911 |
// Sometimes we can't cleanly find the font size (some units relative to a |
| 1912 |
// node's parent's font size are difficult: %, smaller et al), so we create |
| 1913 |
// an invisible, absolutely-positioned span sized to be the height of an 'M' |
| 1914 |
// rendered in its parent's (i.e., our target element's) font size. This is |
| 1915 |
// the definition of CSS's font size attribute. |
| 1916 |
var sizeElement = goog.dom.createDom(goog.dom.TagName.SPAN, { |
| 1917 |
'style': 'visibility:hidden;position:absolute;' + |
| 1918 |
'line-height:0;padding:0;margin:0;border:0;height:1em;' |
| 1919 |
}); |
| 1920 |
goog.dom.appendChild(el, sizeElement); |
| 1921 |
fontSize = sizeElement.offsetHeight; |
| 1922 |
goog.dom.removeNode(sizeElement); |
| 1923 |
|
| 1924 |
return fontSize; |
| 1925 |
}; |
| 1926 |
|
| 1927 |
|
| 1928 |
/** |
| 1929 |
* Parses a style attribute value. Converts CSS property names to camel case. |
| 1930 |
* @param {string} value The style attribute value. |
| 1931 |
* @return {!Object} Map of CSS properties to string values. |
| 1932 |
*/ |
| 1933 |
goog.style.parseStyleAttribute = function(value) { |
| 1934 |
var result = {}; |
| 1935 |
goog.array.forEach(value.split(/\s*;\s*/), function(pair) { |
| 1936 |
var keyValue = pair.match(/\s*([\w-]+)\s*\:(.+)/); |
| 1937 |
if (keyValue) { |
| 1938 |
var styleName = keyValue[1]; |
| 1939 |
var styleValue = goog.string.trim(keyValue[2]); |
| 1940 |
result[goog.string.toCamelCase(styleName.toLowerCase())] = styleValue; |
| 1941 |
} |
| 1942 |
}); |
| 1943 |
return result; |
| 1944 |
}; |
| 1945 |
|
| 1946 |
|
| 1947 |
/** |
| 1948 |
* Reverse of parseStyleAttribute; that is, takes a style object and returns the |
| 1949 |
* corresponding attribute value. Converts camel case property names to proper |
| 1950 |
* CSS selector names. |
| 1951 |
* @param {Object} obj Map of CSS properties to values. |
| 1952 |
* @return {string} The style attribute value. |
| 1953 |
*/ |
| 1954 |
goog.style.toStyleAttribute = function(obj) { |
| 1955 |
var buffer = []; |
| 1956 |
goog.object.forEach(obj, function(value, key) { |
| 1957 |
buffer.push(goog.string.toSelectorCase(key), ':', value, ';'); |
| 1958 |
}); |
| 1959 |
return buffer.join(''); |
| 1960 |
}; |
| 1961 |
|
| 1962 |
|
| 1963 |
/** |
| 1964 |
* Sets CSS float property on an element. |
| 1965 |
* @param {Element} el The element to set float property on. |
| 1966 |
* @param {string} value The value of float CSS property to set on this element. |
| 1967 |
*/ |
| 1968 |
goog.style.setFloat = function(el, value) { |
| 1969 |
el.style[goog.userAgent.IE ? 'styleFloat' : 'cssFloat'] = value; |
| 1970 |
}; |
| 1971 |
|
| 1972 |
|
| 1973 |
/** |
| 1974 |
* Gets value of explicitly-set float CSS property on an element. |
| 1975 |
* @param {Element} el The element to get float property of. |
| 1976 |
* @return {string} The value of explicitly-set float CSS property on this |
| 1977 |
* element. |
| 1978 |
*/ |
| 1979 |
goog.style.getFloat = function(el) { |
| 1980 |
return el.style[goog.userAgent.IE ? 'styleFloat' : 'cssFloat'] || ''; |
| 1981 |
}; |
| 1982 |
|
| 1983 |
|
| 1984 |
/** |
| 1985 |
* Returns the scroll bar width (represents the width of both horizontal |
| 1986 |
* and vertical scroll). |
| 1987 |
* |
| 1988 |
* @param {string=} opt_className An optional class name (or names) to apply |
| 1989 |
* to the invisible div created to measure the scrollbar. This is necessary |
| 1990 |
* if some scrollbars are styled differently than others. |
| 1991 |
* @return {number} The scroll bar width in px. |
| 1992 |
*/ |
| 1993 |
goog.style.getScrollbarWidth = function(opt_className) { |
| 1994 |
// Add two hidden divs. The child div is larger than the parent and |
| 1995 |
// forces scrollbars to appear on it. |
| 1996 |
// Using overflow:scroll does not work consistently with scrollbars that |
| 1997 |
// are styled with ::-webkit-scrollbar. |
| 1998 |
var outerDiv = goog.dom.createElement(goog.dom.TagName.DIV); |
| 1999 |
if (opt_className) { |
| 2000 |
outerDiv.className = opt_className; |
| 2001 |
} |
| 2002 |
outerDiv.style.cssText = 'overflow:auto;' + |
| 2003 |
'position:absolute;top:0;width:100px;height:100px'; |
| 2004 |
var innerDiv = goog.dom.createElement(goog.dom.TagName.DIV); |
| 2005 |
goog.style.setSize(innerDiv, '200px', '200px'); |
| 2006 |
outerDiv.appendChild(innerDiv); |
| 2007 |
goog.dom.appendChild(goog.dom.getDocument().body, outerDiv); |
| 2008 |
var width = outerDiv.offsetWidth - outerDiv.clientWidth; |
| 2009 |
goog.dom.removeNode(outerDiv); |
| 2010 |
return width; |
| 2011 |
}; |
| 2012 |
|
| 2013 |
|
| 2014 |
/** |
| 2015 |
* Regular expression to extract x and y translation components from a CSS |
| 2016 |
* transform Matrix representation. |
| 2017 |
* |
| 2018 |
* @type {!RegExp} |
| 2019 |
* @const |
| 2020 |
* @private |
| 2021 |
*/ |
| 2022 |
goog.style.MATRIX_TRANSLATION_REGEX_ = new RegExp( |
| 2023 |
'matrix\\([0-9\\.\\-]+, [0-9\\.\\-]+, ' + |
| 2024 |
'[0-9\\.\\-]+, [0-9\\.\\-]+, ' + |
| 2025 |
'([0-9\\.\\-]+)p?x?, ([0-9\\.\\-]+)p?x?\\)'); |
| 2026 |
|
| 2027 |
|
| 2028 |
/** |
| 2029 |
* Returns the x,y translation component of any CSS transforms applied to the |
| 2030 |
* element, in pixels. |
| 2031 |
* |
| 2032 |
* @param {!Element} element The element to get the translation of. |
| 2033 |
* @return {!goog.math.Coordinate} The CSS translation of the element in px. |
| 2034 |
*/ |
| 2035 |
goog.style.getCssTranslation = function(element) { |
| 2036 |
var transform = goog.style.getComputedTransform(element); |
| 2037 |
if (!transform) { |
| 2038 |
return new goog.math.Coordinate(0, 0); |
| 2039 |
} |
| 2040 |
var matches = transform.match(goog.style.MATRIX_TRANSLATION_REGEX_); |
| 2041 |
if (!matches) { |
| 2042 |
return new goog.math.Coordinate(0, 0); |
| 2043 |
} |
| 2044 |
return new goog.math.Coordinate( |
| 2045 |
parseFloat(matches[1]), parseFloat(matches[2])); |
| 2046 |
}; |