1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
|
/**
* Handles parsing, caching, and detecting changes to background (and -pie-background) CSS
* @constructor
* @param {Element} el the target element
*/
PIE.BackgroundStyleInfo = PIE.StyleInfoBase.newStyleInfo( {
cssProperty: PIE.CSS_PREFIX + 'background',
styleProperty: PIE.STYLE_PREFIX + 'Background',
attachIdents: { 'scroll':1, 'fixed':1, 'local':1 },
repeatIdents: { 'repeat-x':1, 'repeat-y':1, 'repeat':1, 'no-repeat':1 },
originAndClipIdents: { 'padding-box':1, 'border-box':1, 'content-box':1 },
positionIdents: { 'top':1, 'right':1, 'bottom':1, 'left':1, 'center':1 },
sizeIdents: { 'contain':1, 'cover':1 },
propertyNames: {
CLIP: 'backgroundClip',
COLOR: 'backgroundColor',
IMAGE: 'backgroundImage',
ORIGIN: 'backgroundOrigin',
POSITION: 'backgroundPosition',
REPEAT: 'backgroundRepeat',
SIZE: 'backgroundSize'
},
/**
* For background styles, we support the -pie-background property but fall back to the standard
* backround* properties. The reason we have to use the prefixed version is that IE natively
* parses the standard properties and if it sees something it doesn't know how to parse, for example
* multiple values or gradient definitions, it will throw that away and not make it available through
* currentStyle.
*
* Format of return object:
* {
* color: <PIE.Color>,
* bgImages: [
* {
* imgType: 'image',
* imgUrl: 'image.png',
* imgRepeat: <'no-repeat' | 'repeat-x' | 'repeat-y' | 'repeat'>,
* bgPosition: <PIE.BgPosition>,
* bgAttachment: <'scroll' | 'fixed' | 'local'>,
* bgOrigin: <'border-box' | 'padding-box' | 'content-box'>,
* bgClip: <'border-box' | 'padding-box'>,
* bgSize: <PIE.BgSize>,
* origString: 'url(img.png) no-repeat top left'
* },
* {
* imgType: 'linear-gradient',
* gradientStart: <PIE.BgPosition>,
* angle: <PIE.Angle>,
* stops: [
* { color: <PIE.Color>, offset: <PIE.Length> },
* { color: <PIE.Color>, offset: <PIE.Length> }, ...
* ]
* }
* ]
* }
* @param {String} css
* @override
*/
parseCss: function( css ) {
var el = this.targetElement,
cs = el.currentStyle,
tokenizer, token, image,
tok_type = PIE.Tokenizer.Type,
type_operator = tok_type.OPERATOR,
type_ident = tok_type.IDENT,
type_color = tok_type.COLOR,
tokType, tokVal,
beginCharIndex = 0,
positionIdents = this.positionIdents,
gradient, stop, width, height,
props = { bgImages: [] };
function isBgPosToken( token ) {
return token && token.isLengthOrPercent() || ( token.tokenType & type_ident && token.tokenValue in positionIdents );
}
function sizeToken( token ) {
return token && ( ( token.isLengthOrPercent() && PIE.getLength( token.tokenValue ) ) || ( token.tokenValue === 'auto' && 'auto' ) );
}
// If the CSS3-specific -pie-background property is present, parse it
if( this.getCss3() ) {
tokenizer = new PIE.Tokenizer( css );
image = {};
while( token = tokenizer.next() ) {
tokType = token.tokenType;
tokVal = token.tokenValue;
if( !image.imgType && tokType & tok_type.FUNCTION && tokVal === 'linear-gradient' ) {
gradient = { stops: [], imgType: tokVal };
stop = {};
while( token = tokenizer.next() ) {
tokType = token.tokenType;
tokVal = token.tokenValue;
// If we reached the end of the function and had at least 2 stops, flush the info
if( tokType & tok_type.CHARACTER && tokVal === ')' ) {
if( stop.color ) {
gradient.stops.push( stop );
}
if( gradient.stops.length > 1 ) {
PIE.Util.merge( image, gradient );
}
break;
}
// Color stop - must start with color
if( tokType & type_color ) {
// if we already have an angle/position, make sure that the previous token was a comma
if( gradient.angle || gradient.gradientStart ) {
token = tokenizer.prev();
if( token.tokenType !== type_operator ) {
break; //fail
}
tokenizer.next();
}
stop = {
color: PIE.getColor( tokVal )
};
// check for offset following color
token = tokenizer.next();
if( token.isLengthOrPercent() ) {
stop.offset = PIE.getLength( token.tokenValue );
} else {
tokenizer.prev();
}
}
// Angle - can only appear in first spot
else if( tokType & tok_type.ANGLE && !gradient.angle && !stop.color && !gradient.stops.length ) {
gradient.angle = new PIE.Angle( token.tokenValue );
}
else if( isBgPosToken( token ) && !gradient.gradientStart && !stop.color && !gradient.stops.length ) {
tokenizer.prev();
gradient.gradientStart = new PIE.BgPosition(
tokenizer.until( function( t ) {
return !isBgPosToken( t );
}, false )
);
}
else if( tokType & type_operator && tokVal === ',' ) {
if( stop.color ) {
gradient.stops.push( stop );
stop = {};
}
}
else {
// Found something we didn't recognize; fail without adding image
break;
}
}
}
else if( !image.imgType && tokType & tok_type.URL ) {
image.imgUrl = tokVal;
image.imgType = 'image';
}
else if( isBgPosToken( token ) && !image.bgPosition ) {
tokenizer.prev();
image.bgPosition = new PIE.BgPosition(
tokenizer.until( function( t ) {
return !isBgPosToken( t );
}, false )
);
}
else if( tokType & type_ident ) {
if( tokVal in this.repeatIdents && !image.imgRepeat ) {
image.imgRepeat = tokVal;
}
else if( tokVal in this.originAndClipIdents && !image.bgOrigin ) {
image.bgOrigin = tokVal;
if( ( token = tokenizer.next() ) && ( token.tokenType & type_ident ) &&
token.tokenValue in this.originAndClipIdents ) {
image.bgClip = token.tokenValue;
} else {
image.bgClip = tokVal;
tokenizer.prev();
}
}
else if( tokVal in this.attachIdents && !image.bgAttachment ) {
image.bgAttachment = tokVal;
}
else {
return null;
}
}
else if( tokType & type_color && !props.color ) {
props.color = PIE.getColor( tokVal );
}
else if( tokType & type_operator && tokVal === '/' && !image.bgSize && image.bgPosition ) {
// background size
token = tokenizer.next();
if( token.tokenType & type_ident && token.tokenValue in this.sizeIdents ) {
image.bgSize = new PIE.BgSize( token.tokenValue );
}
else if( width = sizeToken( token ) ) {
height = sizeToken( tokenizer.next() );
if ( !height ) {
height = width;
tokenizer.prev();
}
image.bgSize = new PIE.BgSize( width, height );
}
else {
return null;
}
}
// new layer
else if( tokType & type_operator && tokVal === ',' && image.imgType ) {
image.origString = css.substring( beginCharIndex, tokenizer.ch - 1 );
beginCharIndex = tokenizer.ch;
props.bgImages.push( image );
image = {};
}
else {
// Found something unrecognized; chuck everything
return null;
}
}
// leftovers
if( image.imgType ) {
image.origString = css.substring( beginCharIndex );
props.bgImages.push( image );
}
}
// Otherwise, use the standard background properties; let IE give us the values rather than parsing them
else {
this.withActualBg( PIE.ieDocMode < 9 ?
function() {
var propNames = this.propertyNames,
posX = cs[propNames.POSITION + 'X'],
posY = cs[propNames.POSITION + 'Y'],
img = cs[propNames.IMAGE],
color = cs[propNames.COLOR];
if( color !== 'transparent' ) {
props.color = PIE.getColor( color )
}
if( img !== 'none' ) {
props.bgImages = [ {
imgType: 'image',
imgUrl: new PIE.Tokenizer( img ).next().tokenValue,
imgRepeat: cs[propNames.REPEAT],
bgPosition: new PIE.BgPosition( new PIE.Tokenizer( posX + ' ' + posY ).all() )
} ];
}
} :
function() {
var propNames = this.propertyNames,
splitter = /\s*,\s*/,
images = cs[propNames.IMAGE].split( splitter ),
color = cs[propNames.COLOR],
repeats, positions, origins, clips, sizes, i, len, image, sizeParts;
if( color !== 'transparent' ) {
props.color = PIE.getColor( color )
}
len = images.length;
if( len && images[0] !== 'none' ) {
repeats = cs[propNames.REPEAT].split( splitter );
positions = cs[propNames.POSITION].split( splitter );
origins = cs[propNames.ORIGIN].split( splitter );
clips = cs[propNames.CLIP].split( splitter );
sizes = cs[propNames.SIZE].split( splitter );
props.bgImages = [];
for( i = 0; i < len; i++ ) {
image = images[ i ];
if( image && image !== 'none' ) {
sizeParts = sizes[i].split( ' ' );
props.bgImages.push( {
origString: image + ' ' + repeats[ i ] + ' ' + positions[ i ] + ' / ' + sizes[ i ] + ' ' +
origins[ i ] + ' ' + clips[ i ],
imgType: 'image',
imgUrl: new PIE.Tokenizer( image ).next().tokenValue,
imgRepeat: repeats[ i ],
bgPosition: new PIE.BgPosition( new PIE.Tokenizer( positions[ i ] ).all() ),
bgOrigin: origins[ i ],
bgClip: clips[ i ],
bgSize: new PIE.BgSize( sizeParts[ 0 ], sizeParts[ 1 ] )
} );
}
}
}
}
);
}
return ( props.color || props.bgImages[0] ) ? props : null;
},
/**
* Execute a function with the actual background styles (not overridden with runtimeStyle
* properties set by the renderers) available via currentStyle.
* @param fn
*/
withActualBg: function( fn ) {
var isIE9 = PIE.ieDocMode > 8,
propNames = this.propertyNames,
rs = this.targetElement.runtimeStyle,
rsImage = rs[propNames.IMAGE],
rsColor = rs[propNames.COLOR],
rsRepeat = rs[propNames.REPEAT],
rsClip, rsOrigin, rsSize, rsPosition, ret;
if( rsImage ) rs[propNames.IMAGE] = '';
if( rsColor ) rs[propNames.COLOR] = '';
if( rsRepeat ) rs[propNames.REPEAT] = '';
if( isIE9 ) {
rsClip = rs[propNames.CLIP];
rsOrigin = rs[propNames.ORIGIN];
rsPosition = rs[propNames.POSITION];
rsSize = rs[propNames.SIZE];
if( rsClip ) rs[propNames.CLIP] = '';
if( rsOrigin ) rs[propNames.ORIGIN] = '';
if( rsPosition ) rs[propNames.POSITION] = '';
if( rsSize ) rs[propNames.SIZE] = '';
}
ret = fn.call( this );
if( rsImage ) rs[propNames.IMAGE] = rsImage;
if( rsColor ) rs[propNames.COLOR] = rsColor;
if( rsRepeat ) rs[propNames.REPEAT] = rsRepeat;
if( isIE9 ) {
if( rsClip ) rs[propNames.CLIP] = rsClip;
if( rsOrigin ) rs[propNames.ORIGIN] = rsOrigin;
if( rsPosition ) rs[propNames.POSITION] = rsPosition;
if( rsSize ) rs[propNames.SIZE] = rsSize;
}
return ret;
},
getCss: PIE.StyleInfoBase.cacheWhenLocked( function() {
return this.getCss3() ||
this.withActualBg( function() {
var cs = this.targetElement.currentStyle,
propNames = this.propertyNames;
return cs[propNames.COLOR] + ' ' + cs[propNames.IMAGE] + ' ' + cs[propNames.REPEAT] + ' ' +
cs[propNames.POSITION + 'X'] + ' ' + cs[propNames.POSITION + 'Y'];
} );
} ),
getCss3: PIE.StyleInfoBase.cacheWhenLocked( function() {
var el = this.targetElement;
return el.style[ this.styleProperty ] || el.currentStyle.getAttribute( this.cssProperty );
} ),
/**
* Tests if style.PiePngFix or the -pie-png-fix property is set to true in IE6.
*/
isPngFix: function() {
var val = 0, el;
if( PIE.ieVersion < 7 ) {
el = this.targetElement;
val = ( '' + ( el.style[ PIE.STYLE_PREFIX + 'PngFix' ] || el.currentStyle.getAttribute( PIE.CSS_PREFIX + 'png-fix' ) ) === 'true' );
}
return val;
},
/**
* The isActive logic is slightly different, because getProps() always returns an object
* even if it is just falling back to the native background properties. But we only want
* to report is as being "active" if either the -pie-background override property is present
* and parses successfully or '-pie-png-fix' is set to true in IE6.
*/
isActive: PIE.StyleInfoBase.cacheWhenLocked( function() {
return (this.getCss3() || this.isPngFix()) && !!this.getProps();
} )
} );
|