/* Copyright (c) 2004-2006, The Dojo Foundation All Rights Reserved. Licensed under the Academic Free License version 2.1 or above OR the modified BSD license. For more information on Dojo licensing, see: http://dojotoolkit.org/community/licensing.shtml */ dojo.provide("dojo.style"); dojo.require("dojo.graphics.color"); dojo.require("dojo.uri.Uri"); dojo.require("dojo.lang.common"); (function(){ var h = dojo.render.html; var ds = dojo.style; var db = document["body"]||document["documentElement"]; ds.boxSizing = { MARGIN_BOX: "margin-box", BORDER_BOX: "border-box", PADDING_BOX: "padding-box", CONTENT_BOX: "content-box" }; var bs = ds.boxSizing; ds.getBoxSizing = function(node){ if((h.ie)||(h.opera)){ var cm = document["compatMode"]; if((cm == "BackCompat")||(cm == "QuirksMode")){ return bs.BORDER_BOX; }else{ return bs.CONTENT_BOX; } }else{ if(arguments.length == 0){ node = document.documentElement; } var sizing = ds.getStyle(node, "-moz-box-sizing"); if(!sizing){ sizing = ds.getStyle(node, "box-sizing"); } return (sizing ? sizing : bs.CONTENT_BOX); } } /* The following several function use the dimensions shown below +-------------------------+ | margin | | +---------------------+ | | | border | | | | +-----------------+ | | | | | padding | | | | | | +-------------+ | | | | | | | content | | | | | | | +-------------+ | | | | | +-|-------------|-+ | | | +-|-|-------------|-|-+ | +-|-|-|-------------|-|-|-+ | | | | | | | | | | | |<- content ->| | | | | |<------ inner ------>| | |<-------- outer -------->| +-------------------------+ * content-box |m|b|p| |p|b|m| | |<------ offset ----->| | | | |<---- client --->| | | | | | |<-- width -->| | | | * border-box |m|b|p| |p|b|m| | |<------ offset ----->| | | | |<---- client --->| | | | |<------ width ------>| | */ /* Notes: General: - Uncomputable values are returned as NaN. - setOuterWidth/Height return *false* if the outer size could not be computed, otherwise *true*. - (sjmiles) knows no way to find the calculated values for auto-margins. - All returned values are floating point in 'px' units. If a non-zero computed style value is not specified in 'px', NaN is returned. FF: - styles specified as '0' (unitless 0) show computed as '0pt'. IE: - clientWidth/Height are unreliable (0 unless the object has 'layout'). - margins must be specified in px, or 0 (in any unit) for any sizing function to work. Otherwise margins detect as 'auto'. - padding can be empty or, if specified, must be in px, or 0 (in any unit) for any sizing function to work. Safari: - Safari defaults padding values to 'auto'. See the unit tests for examples of (un)computable values in a given browser. */ // FIXME: these work for some elements (e.g. DIV) but not others (e.g. TABLE, TEXTAREA) ds.isBorderBox = function(node){ return (ds.getBoxSizing(node) == bs.BORDER_BOX); } ds.getUnitValue = function(node, cssSelector, autoIsZero){ var s = ds.getComputedStyle(node, cssSelector); if((!s)||((s == 'auto')&&(autoIsZero))){ return { value: 0, units: 'px' }; } if(dojo.lang.isUndefined(s)){return ds.getUnitValue.bad;} // FIXME: is regex inefficient vs. parseInt or some manual test? var match = s.match(/(\-?[\d.]+)([a-z%]*)/i); if (!match){return ds.getUnitValue.bad;} return { value: Number(match[1]), units: match[2].toLowerCase() }; } // FIXME: 'bad' value should be 0? ds.getUnitValue.bad = { value: NaN, units: '' }; ds.getPixelValue = function(node, cssSelector, autoIsZero){ var result = ds.getUnitValue(node, cssSelector, autoIsZero); // FIXME: there is serious debate as to whether or not this is the right solution if(isNaN(result.value)){ return 0; } // FIXME: code exists for converting other units to px (see Dean Edward's IE7) // but there are cross-browser complexities if((result.value)&&(result.units != 'px')){ return NaN; } return result.value; } // FIXME: deprecated ds.getNumericStyle = function() { dojo.deprecated('dojo.(style|html).getNumericStyle', 'in favor of dojo.(style|html).getPixelValue', '0.4'); return ds.getPixelValue.apply(this, arguments); } ds.setPositivePixelValue = function(node, selector, value){ if(isNaN(value)){return false;} node.style[selector] = Math.max(0, value) + 'px'; return true; } ds._sumPixelValues = function(node, selectors, autoIsZero){ var total = 0; for(var x=0; x *downloaded cssText* ds.insertCssFile = function(URI, doc, checkDuplicates){ if(!URI){ return; } if(!doc){ doc = document; } var cssStr = dojo.hostenv.getText(URI); cssStr = ds.fixPathsInCssText(cssStr, URI); if(checkDuplicates){ var styles = doc.getElementsByTagName("style"); var cssText = ""; for(var i = 0; i= 1.0){ if(h.ie){ ds.clearOpacity(node); return; }else{ opacity = 0.999999; } }else if( opacity < 0.0){ opacity = 0; } } if(h.ie){ if(node.nodeName.toLowerCase() == "tr"){ // FIXME: is this too naive? will we get more than we want? var tds = node.getElementsByTagName("td"); for(var x=0; x= 0.999999 ? 1.0 : Number(opac); } ds.clearOpacity = function clearOpacity(node){ node = dojo.byId(node); var ns = node.style; if(h.ie){ try { if( node.filters && node.filters.alpha ){ ns.filter = ""; // FIXME: may get rid of other filter effects } } catch(e) { /* * IE7 gives error if node.filters not set; * don't know why or how to workaround (other than this) */ } }else if(h.moz){ ns.opacity = 1; ns.MozOpacity = 1; }else if(h.safari){ ns.opacity = 1; ns.KhtmlOpacity = 1; }else{ ns.opacity = 1; } } /** * Set the given style attributes for the node. * Patch submitted by Wolfram Kriesing, 22/03/2006. * * Ie. dojo.style.setStyleAttributes(myNode, "position:absolute; left:10px; top:10px;") * This just makes it easier to set a style directly without the need to * override it completely (as node.setAttribute() would). * If there is a dojo-method for an attribute, like for "opacity" there * is setOpacity, the dojo method is called instead. * For example: dojo.style.setStyleAttributes(myNode, "opacity: .4"); * * Additionally all the dojo.style.set* methods can also be used. * Ie. when attributes contains "outer-height: 10;" it will call dojo.style.setOuterHeight("10"); * * @param object The node to set the style attributes for. * @param string Ie. "position:absolute; left:10px; top:10px;" */ ds.setStyleAttributes = function(node, attributes) { var methodMap={ "opacity":dojo.style.setOpacity, "content-height":dojo.style.setContentHeight, "content-width":dojo.style.setContentWidth, "outer-height":dojo.style.setOuterHeight, "outer-width":dojo.style.setOuterWidth } var splittedAttribs=attributes.replace(/(;)?\s*$/, "").split(";"); for(var i=0; i 4 ) { coords.pop(); } var ret = coords; } else { // coords is an dom object (or dom object id); return it's coordinates var node = dojo.byId(coords); var pos = ds.getAbsolutePosition(node, includeScroll); var ret = [ pos.x, pos.y, ds.getBorderBoxWidth(node), ds.getBorderBoxHeight(node) ]; } ret.x = ret[0]; ret.y = ret[1]; ret.w = ret[2]; ret.h = ret[3]; return ret; }; })();