dom/tests/mochitest/ajax/mochikit/MochiKit/Style.js

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /***
michael@0 2
michael@0 3 MochiKit.Style 1.4
michael@0 4
michael@0 5 See <http://mochikit.com/> for documentation, downloads, license, etc.
michael@0 6
michael@0 7 (c) 2005-2006 Bob Ippolito, Beau Hartshorne. All rights Reserved.
michael@0 8
michael@0 9 ***/
michael@0 10
michael@0 11 if (typeof(dojo) != 'undefined') {
michael@0 12 dojo.provide('MochiKit.Style');
michael@0 13 dojo.require('MochiKit.Base');
michael@0 14 dojo.require('MochiKit.DOM');
michael@0 15 }
michael@0 16 if (typeof(JSAN) != 'undefined') {
michael@0 17 JSAN.use('MochiKit.Base', []);
michael@0 18 JSAN.use('MochiKit.DOM', []);
michael@0 19 }
michael@0 20
michael@0 21 try {
michael@0 22 if (typeof(MochiKit.Base) == 'undefined') {
michael@0 23 throw '';
michael@0 24 }
michael@0 25 } catch (e) {
michael@0 26 throw 'MochiKit.Style depends on MochiKit.Base!';
michael@0 27 }
michael@0 28
michael@0 29 try {
michael@0 30 if (typeof(MochiKit.DOM) == 'undefined') {
michael@0 31 throw '';
michael@0 32 }
michael@0 33 } catch (e) {
michael@0 34 throw 'MochiKit.Style depends on MochiKit.DOM!';
michael@0 35 }
michael@0 36
michael@0 37
michael@0 38 if (typeof(MochiKit.Style) == 'undefined') {
michael@0 39 MochiKit.Style = {};
michael@0 40 }
michael@0 41
michael@0 42 MochiKit.Style.NAME = 'MochiKit.Style';
michael@0 43 MochiKit.Style.VERSION = '1.4';
michael@0 44 MochiKit.Style.__repr__ = function () {
michael@0 45 return '[' + this.NAME + ' ' + this.VERSION + ']';
michael@0 46 };
michael@0 47 MochiKit.Style.toString = function () {
michael@0 48 return this.__repr__();
michael@0 49 };
michael@0 50
michael@0 51 MochiKit.Style.EXPORT_OK = [];
michael@0 52
michael@0 53 MochiKit.Style.EXPORT = [
michael@0 54 'setStyle',
michael@0 55 'setOpacity',
michael@0 56 'getStyle',
michael@0 57 'getElementDimensions',
michael@0 58 'elementDimensions', // deprecated
michael@0 59 'setElementDimensions',
michael@0 60 'getElementPosition',
michael@0 61 'elementPosition', // deprecated
michael@0 62 'setElementPosition',
michael@0 63 'setDisplayForElement',
michael@0 64 'hideElement',
michael@0 65 'showElement',
michael@0 66 'getViewportDimensions',
michael@0 67 'getViewportPosition',
michael@0 68 'Dimensions',
michael@0 69 'Coordinates'
michael@0 70 ];
michael@0 71
michael@0 72
michael@0 73 /*
michael@0 74
michael@0 75 Dimensions
michael@0 76
michael@0 77 */
michael@0 78 /** @id MochiKit.Style.Dimensions */
michael@0 79 MochiKit.Style.Dimensions = function (w, h) {
michael@0 80 this.w = w;
michael@0 81 this.h = h;
michael@0 82 };
michael@0 83
michael@0 84 MochiKit.Style.Dimensions.prototype.__repr__ = function () {
michael@0 85 var repr = MochiKit.Base.repr;
michael@0 86 return '{w: ' + repr(this.w) + ', h: ' + repr(this.h) + '}';
michael@0 87 };
michael@0 88
michael@0 89 MochiKit.Style.Dimensions.prototype.toString = function () {
michael@0 90 return this.__repr__();
michael@0 91 };
michael@0 92
michael@0 93
michael@0 94 /*
michael@0 95
michael@0 96 Coordinates
michael@0 97
michael@0 98 */
michael@0 99 /** @id MochiKit.Style.Coordinates */
michael@0 100 MochiKit.Style.Coordinates = function (x, y) {
michael@0 101 this.x = x;
michael@0 102 this.y = y;
michael@0 103 };
michael@0 104
michael@0 105 MochiKit.Style.Coordinates.prototype.__repr__ = function () {
michael@0 106 var repr = MochiKit.Base.repr;
michael@0 107 return '{x: ' + repr(this.x) + ', y: ' + repr(this.y) + '}';
michael@0 108 };
michael@0 109
michael@0 110 MochiKit.Style.Coordinates.prototype.toString = function () {
michael@0 111 return this.__repr__();
michael@0 112 };
michael@0 113
michael@0 114
michael@0 115 MochiKit.Base.update(MochiKit.Style, {
michael@0 116
michael@0 117 /** @id MochiKit.Style.getStyle */
michael@0 118 getStyle: function (elem, cssProperty) {
michael@0 119 var dom = MochiKit.DOM;
michael@0 120 var d = dom._document;
michael@0 121
michael@0 122 elem = dom.getElement(elem);
michael@0 123 cssProperty = MochiKit.Base.camelize(cssProperty);
michael@0 124
michael@0 125 if (!elem || elem == d) {
michael@0 126 return undefined;
michael@0 127 }
michael@0 128 if (cssProperty == 'opacity' && elem.filters) {
michael@0 129 var opacity = (MochiKit.Style.getStyle(elem, 'filter') || '').match(/alpha\(opacity=(.*)\)/);
michael@0 130 if (opacity && opacity[1]) {
michael@0 131 return parseFloat(opacity[1]) / 100;
michael@0 132 }
michael@0 133 return 1.0;
michael@0 134 }
michael@0 135 var value = elem.style ? elem.style[cssProperty] : null;
michael@0 136 if (!value) {
michael@0 137 if (d.defaultView && d.defaultView.getComputedStyle) {
michael@0 138 var css = d.defaultView.getComputedStyle(elem, null);
michael@0 139 cssProperty = cssProperty.replace(/([A-Z])/g, '-$1'
michael@0 140 ).toLowerCase(); // from dojo.style.toSelectorCase
michael@0 141 value = css ? css.getPropertyValue(cssProperty) : null;
michael@0 142 } else if (elem.currentStyle) {
michael@0 143 value = elem.currentStyle[cssProperty];
michael@0 144 }
michael@0 145 }
michael@0 146 if (cssProperty == 'opacity') {
michael@0 147 value = parseFloat(value);
michael@0 148 }
michael@0 149
michael@0 150 if (/Opera/.test(navigator.userAgent) && (MochiKit.Base.find(['left', 'top', 'right', 'bottom'], cssProperty) != -1)) {
michael@0 151 if (MochiKit.Style.getStyle(elem, 'position') == 'static') {
michael@0 152 value = 'auto';
michael@0 153 }
michael@0 154 }
michael@0 155
michael@0 156 return value == 'auto' ? null : value;
michael@0 157 },
michael@0 158
michael@0 159 /** @id MochiKit.Style.setStyle */
michael@0 160 setStyle: function (elem, style) {
michael@0 161 elem = MochiKit.DOM.getElement(elem);
michael@0 162 for (var name in style) {
michael@0 163 if (name == 'opacity') {
michael@0 164 MochiKit.Style.setOpacity(elem, style[name]);
michael@0 165 } else {
michael@0 166 elem.style[MochiKit.Base.camelize(name)] = style[name];
michael@0 167 }
michael@0 168 }
michael@0 169 },
michael@0 170
michael@0 171 /** @id MochiKit.Style.setOpacity */
michael@0 172 setOpacity: function (elem, o) {
michael@0 173 elem = MochiKit.DOM.getElement(elem);
michael@0 174 var self = MochiKit.Style;
michael@0 175 if (o == 1) {
michael@0 176 var toSet = /Gecko/.test(navigator.userAgent) && !(/Konqueror|AppleWebKit|KHTML/.test(navigator.userAgent));
michael@0 177 elem.style["opacity"] = toSet ? 0.999999 : 1.0;
michael@0 178 if (/MSIE/.test(navigator.userAgent)) {
michael@0 179 elem.style['filter'] =
michael@0 180 self.getStyle(elem, 'filter').replace(/alpha\([^\)]*\)/gi, '');
michael@0 181 }
michael@0 182 } else {
michael@0 183 if (o < 0.00001) {
michael@0 184 o = 0;
michael@0 185 }
michael@0 186 elem.style["opacity"] = o;
michael@0 187 if (/MSIE/.test(navigator.userAgent)) {
michael@0 188 elem.style['filter'] =
michael@0 189 self.getStyle(elem, 'filter').replace(/alpha\([^\)]*\)/gi, '') + 'alpha(opacity=' + o * 100 + ')';
michael@0 190 }
michael@0 191 }
michael@0 192 },
michael@0 193
michael@0 194 /*
michael@0 195
michael@0 196 getElementPosition is adapted from YAHOO.util.Dom.getXY v0.9.0.
michael@0 197 Copyright: Copyright (c) 2006, Yahoo! Inc. All rights reserved.
michael@0 198 License: BSD, http://developer.yahoo.net/yui/license.txt
michael@0 199
michael@0 200 */
michael@0 201
michael@0 202 /** @id MochiKit.Style.getElementPosition */
michael@0 203 getElementPosition: function (elem, /* optional */relativeTo) {
michael@0 204 var self = MochiKit.Style;
michael@0 205 var dom = MochiKit.DOM;
michael@0 206 elem = dom.getElement(elem);
michael@0 207
michael@0 208 if (!elem ||
michael@0 209 (!(elem.x && elem.y) &&
michael@0 210 (!elem.parentNode === null ||
michael@0 211 self.getStyle(elem, 'display') == 'none'))) {
michael@0 212 return undefined;
michael@0 213 }
michael@0 214
michael@0 215 var c = new self.Coordinates(0, 0);
michael@0 216 var box = null;
michael@0 217 var parent = null;
michael@0 218
michael@0 219 var d = MochiKit.DOM._document;
michael@0 220 var de = d.documentElement;
michael@0 221 var b = d.body;
michael@0 222
michael@0 223 if (!elem.parentNode && elem.x && elem.y) {
michael@0 224 /* it's just a MochiKit.Style.Coordinates object */
michael@0 225 c.x += elem.x || 0;
michael@0 226 c.y += elem.y || 0;
michael@0 227 } else if (elem.getBoundingClientRect) { // IE shortcut
michael@0 228 /*
michael@0 229
michael@0 230 The IE shortcut can be off by two. We fix it. See:
michael@0 231 http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/getboundingclientrect.asp
michael@0 232
michael@0 233 This is similar to the method used in
michael@0 234 MochiKit.Signal.Event.mouse().
michael@0 235
michael@0 236 */
michael@0 237 box = elem.getBoundingClientRect();
michael@0 238
michael@0 239 c.x += box.left +
michael@0 240 (de.scrollLeft || b.scrollLeft) -
michael@0 241 (de.clientLeft || 0);
michael@0 242
michael@0 243 c.y += box.top +
michael@0 244 (de.scrollTop || b.scrollTop) -
michael@0 245 (de.clientTop || 0);
michael@0 246
michael@0 247 } else if (elem.offsetParent) {
michael@0 248 c.x += elem.offsetLeft;
michael@0 249 c.y += elem.offsetTop;
michael@0 250 parent = elem.offsetParent;
michael@0 251
michael@0 252 if (parent != elem) {
michael@0 253 while (parent) {
michael@0 254 c.x += parent.offsetLeft;
michael@0 255 c.y += parent.offsetTop;
michael@0 256 parent = parent.offsetParent;
michael@0 257 }
michael@0 258 }
michael@0 259
michael@0 260 /*
michael@0 261
michael@0 262 Opera < 9 and old Safari (absolute) incorrectly account for
michael@0 263 body offsetTop and offsetLeft.
michael@0 264
michael@0 265 */
michael@0 266 var ua = navigator.userAgent.toLowerCase();
michael@0 267 if ((typeof(opera) != 'undefined' &&
michael@0 268 parseFloat(opera.version()) < 9) ||
michael@0 269 (ua.indexOf('AppleWebKit') != -1 &&
michael@0 270 self.getStyle(elem, 'position') == 'absolute')) {
michael@0 271
michael@0 272 c.x -= b.offsetLeft;
michael@0 273 c.y -= b.offsetTop;
michael@0 274
michael@0 275 }
michael@0 276 }
michael@0 277
michael@0 278 if (typeof(relativeTo) != 'undefined') {
michael@0 279 relativeTo = arguments.callee(relativeTo);
michael@0 280 if (relativeTo) {
michael@0 281 c.x -= (relativeTo.x || 0);
michael@0 282 c.y -= (relativeTo.y || 0);
michael@0 283 }
michael@0 284 }
michael@0 285
michael@0 286 if (elem.parentNode) {
michael@0 287 parent = elem.parentNode;
michael@0 288 } else {
michael@0 289 parent = null;
michael@0 290 }
michael@0 291
michael@0 292 while (parent) {
michael@0 293 var tagName = parent.tagName.toUpperCase();
michael@0 294 if (tagName === 'BODY' || tagName === 'HTML') {
michael@0 295 break;
michael@0 296 }
michael@0 297 var disp = self.getStyle(parent, 'display');
michael@0 298 // Handle strange Opera bug for some display
michael@0 299 if (disp != 'inline' && disp != 'table-row') {
michael@0 300 c.x -= parent.scrollLeft;
michael@0 301 c.y -= parent.scrollTop;
michael@0 302 }
michael@0 303 if (parent.parentNode) {
michael@0 304 parent = parent.parentNode;
michael@0 305 } else {
michael@0 306 parent = null;
michael@0 307 }
michael@0 308 }
michael@0 309
michael@0 310 return c;
michael@0 311 },
michael@0 312
michael@0 313 /** @id MochiKit.Style.setElementPosition */
michael@0 314 setElementPosition: function (elem, newPos/* optional */, units) {
michael@0 315 elem = MochiKit.DOM.getElement(elem);
michael@0 316 if (typeof(units) == 'undefined') {
michael@0 317 units = 'px';
michael@0 318 }
michael@0 319 var newStyle = {};
michael@0 320 var isUndefNull = MochiKit.Base.isUndefinedOrNull;
michael@0 321 if (!isUndefNull(newPos.x)) {
michael@0 322 newStyle['left'] = newPos.x + units;
michael@0 323 }
michael@0 324 if (!isUndefNull(newPos.y)) {
michael@0 325 newStyle['top'] = newPos.y + units;
michael@0 326 }
michael@0 327 MochiKit.DOM.updateNodeAttributes(elem, {'style': newStyle});
michael@0 328 },
michael@0 329
michael@0 330 /** @id MochiKit.Style.getElementDimensions */
michael@0 331 getElementDimensions: function (elem) {
michael@0 332 var self = MochiKit.Style;
michael@0 333 var dom = MochiKit.DOM;
michael@0 334 if (typeof(elem.w) == 'number' || typeof(elem.h) == 'number') {
michael@0 335 return new self.Dimensions(elem.w || 0, elem.h || 0);
michael@0 336 }
michael@0 337 elem = dom.getElement(elem);
michael@0 338 if (!elem) {
michael@0 339 return undefined;
michael@0 340 }
michael@0 341 var disp = self.getStyle(elem, 'display');
michael@0 342 // display can be empty/undefined on WebKit/KHTML
michael@0 343 if (disp != 'none' && disp !== '' && typeof(disp) != 'undefined') {
michael@0 344 return new self.Dimensions(elem.offsetWidth || 0,
michael@0 345 elem.offsetHeight || 0);
michael@0 346 }
michael@0 347 var s = elem.style;
michael@0 348 var originalVisibility = s.visibility;
michael@0 349 var originalPosition = s.position;
michael@0 350 s.visibility = 'hidden';
michael@0 351 s.position = 'absolute';
michael@0 352 s.display = '';
michael@0 353 var originalWidth = elem.offsetWidth;
michael@0 354 var originalHeight = elem.offsetHeight;
michael@0 355 s.display = 'none';
michael@0 356 s.position = originalPosition;
michael@0 357 s.visibility = originalVisibility;
michael@0 358 return new self.Dimensions(originalWidth, originalHeight);
michael@0 359 },
michael@0 360
michael@0 361 /** @id MochiKit.Style.setElementDimensions */
michael@0 362 setElementDimensions: function (elem, newSize/* optional */, units) {
michael@0 363 elem = MochiKit.DOM.getElement(elem);
michael@0 364 if (typeof(units) == 'undefined') {
michael@0 365 units = 'px';
michael@0 366 }
michael@0 367 var newStyle = {};
michael@0 368 var isUndefNull = MochiKit.Base.isUndefinedOrNull;
michael@0 369 if (!isUndefNull(newSize.w)) {
michael@0 370 newStyle['width'] = newSize.w + units;
michael@0 371 }
michael@0 372 if (!isUndefNull(newSize.h)) {
michael@0 373 newStyle['height'] = newSize.h + units;
michael@0 374 }
michael@0 375 MochiKit.DOM.updateNodeAttributes(elem, {'style': newStyle});
michael@0 376 },
michael@0 377
michael@0 378 /** @id MochiKit.Style.setDisplayForElement */
michael@0 379 setDisplayForElement: function (display, element/*, ...*/) {
michael@0 380 var elements = MochiKit.Base.extend(null, arguments, 1);
michael@0 381 var getElement = MochiKit.DOM.getElement;
michael@0 382 for (var i = 0; i < elements.length; i++) {
michael@0 383 element = getElement(elements[i]);
michael@0 384 if (element) {
michael@0 385 element.style.display = display;
michael@0 386 }
michael@0 387 }
michael@0 388 },
michael@0 389
michael@0 390 /** @id MochiKit.Style.getViewportDimensions */
michael@0 391 getViewportDimensions: function () {
michael@0 392 var d = new MochiKit.Style.Dimensions();
michael@0 393
michael@0 394 var w = MochiKit.DOM._window;
michael@0 395 var b = MochiKit.DOM._document.body;
michael@0 396
michael@0 397 if (w.innerWidth) {
michael@0 398 d.w = w.innerWidth;
michael@0 399 d.h = w.innerHeight;
michael@0 400 } else if (b.parentElement.clientWidth) {
michael@0 401 d.w = b.parentElement.clientWidth;
michael@0 402 d.h = b.parentElement.clientHeight;
michael@0 403 } else if (b && b.clientWidth) {
michael@0 404 d.w = b.clientWidth;
michael@0 405 d.h = b.clientHeight;
michael@0 406 }
michael@0 407 return d;
michael@0 408 },
michael@0 409
michael@0 410 /** @id MochiKit.Style.getViewportPosition */
michael@0 411 getViewportPosition: function () {
michael@0 412 var c = new MochiKit.Style.Coordinates(0, 0);
michael@0 413 var d = MochiKit.DOM._document;
michael@0 414 var de = d.documentElement;
michael@0 415 var db = d.body;
michael@0 416 if (de && (de.scrollTop || de.scrollLeft)) {
michael@0 417 c.x = de.scrollLeft;
michael@0 418 c.y = de.scrollTop;
michael@0 419 } else if (db) {
michael@0 420 c.x = db.scrollLeft;
michael@0 421 c.y = db.scrollTop;
michael@0 422 }
michael@0 423 return c;
michael@0 424 },
michael@0 425
michael@0 426 __new__: function () {
michael@0 427 var m = MochiKit.Base;
michael@0 428
michael@0 429 this.elementPosition = this.getElementPosition;
michael@0 430 this.elementDimensions = this.getElementDimensions;
michael@0 431
michael@0 432 this.hideElement = m.partial(this.setDisplayForElement, 'none');
michael@0 433 this.showElement = m.partial(this.setDisplayForElement, 'block');
michael@0 434
michael@0 435 this.EXPORT_TAGS = {
michael@0 436 ':common': this.EXPORT,
michael@0 437 ':all': m.concat(this.EXPORT, this.EXPORT_OK)
michael@0 438 };
michael@0 439
michael@0 440 m.nameFunctions(this);
michael@0 441 }
michael@0 442 });
michael@0 443
michael@0 444 MochiKit.Style.__new__();
michael@0 445 MochiKit.Base._exportSymbols(this, MochiKit.Style);

mercurial