Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Replace all direct instantiation of PIE.Length and PIE.Color objects …

…with calls to PIE.getLength and PIE.getColor factories, which return shared singleton instances per unique input value. Effect on speed and memory usage is negligible if any, but should prevent some unnecessary garbage collection.
  • Loading branch information...
commit 0227926830e6b95418cafc972aac1fb9c2656c00 1 parent 43ebe57
Jason Johnston authored
View
10 sources/BackgroundStyleInfo.js
@@ -68,7 +68,7 @@ PIE.BackgroundStyleInfo = PIE.StyleInfoBase.newStyleInfo( {
}
function sizeToken( token ) {
- return ( token.isLengthOrPercent() && new PIE.Length( token.value ) ) || ( token.value === 'auto' && 'auto' );
+ return ( token.isLengthOrPercent() && PIE.getLength( token.value ) ) || ( token.value === 'auto' && 'auto' );
}
// If the CSS3-specific -pie-background property is present, parse it
@@ -111,12 +111,12 @@ PIE.BackgroundStyleInfo = PIE.StyleInfoBase.newStyleInfo( {
}
stop = {
- color: new PIE.Color( tokVal )
+ color: PIE.getColor( tokVal )
};
// check for offset following color
token = tokenizer.next();
if( token.isLengthOrPercent() ) {
- stop.offset = new PIE.Length( token.value );
+ stop.offset = PIE.getLength( token.value );
} else {
tokenizer.prev();
}
@@ -172,7 +172,7 @@ PIE.BackgroundStyleInfo = PIE.StyleInfoBase.newStyleInfo( {
}
}
else if( tokType & type_color && !props.color ) {
- props.color = new PIE.Color( tokVal );
+ props.color = PIE.getColor( tokVal );
}
else if( tokType & type_operator ) {
// background size
@@ -218,7 +218,7 @@ PIE.BackgroundStyleInfo = PIE.StyleInfoBase.newStyleInfo( {
props = {};
if( color !== 'transparent' ) {
- props.color = new PIE.Color( color )
+ props.color = PIE.getColor( color )
}
if( img !== 'none' ) {
props.images = [ {
View
8 sources/BgPosition.js
@@ -5,7 +5,7 @@
*/
PIE.BgPosition = (function() {
- var length_fifty = new PIE.Length( '50%' ),
+ var length_fifty = PIE.getLength( '50%' ),
vert_idents = { 'top': 1, 'center': 1, 'bottom': 1 },
horiz_idents = { 'left': 1, 'center': 1, 'right': 1 };
@@ -27,7 +27,7 @@ PIE.BgPosition = (function() {
var tokens = this.tokens,
len = tokens.length,
identType = PIE.Tokenizer.Type,
- length_zero = PIE.Length.ZERO,
+ length_zero = PIE.getLength( '0' ),
type_ident = identType.IDENT,
type_length = identType.LENGTH,
type_percent = identType.PERCENT,
@@ -55,7 +55,7 @@ PIE.BgPosition = (function() {
}
}
else if( tokens[0].isLengthOrPercent() ) {
- vals[1] = new PIE.Length( tokens[0].value );
+ vals[1] = PIE.getLength( tokens[0].value );
}
if( tokens[1].type & type_ident ) {
if( tokens[1].value === 'center' ) {
@@ -65,7 +65,7 @@ PIE.BgPosition = (function() {
}
}
else if( tokens[1].isLengthOrPercent() ) {
- vals[3] = new PIE.Length( tokens[1].value );
+ vals[3] = PIE.getLength( tokens[1].value );
}
}
View
14 sources/BorderImageStyleInfo.js
@@ -122,22 +122,22 @@ PIE.BorderImageStyleInfo = PIE.StyleInfoBase.newStyleInfo( {
}
p.slice = distributeSides( slices, function( tok ) {
- return new PIE.Length( ( tok.type & NUMBER ) ? tok.value + 'px' : tok.value );
+ return PIE.getLength( ( tok.type & NUMBER ) ? tok.value + 'px' : tok.value );
} );
p.width = widths && widths.length > 0 ?
distributeSides( widths, function( tok ) {
- return tok.type & ( LENGTH | PERCENT ) ? new PIE.Length( tok.value ) : tok.value;
+ return tok.type & ( LENGTH | PERCENT ) ? PIE.getLength( tok.value ) : tok.value;
} ) :
( cs = this.targetElement.currentStyle ) && {
- t: new PIE.Length( cs.borderTopWidth ),
- r: new PIE.Length( cs.borderRightWidth ),
- b: new PIE.Length( cs.borderBottomWidth ),
- l: new PIE.Length( cs.borderLeftWidth )
+ t: PIE.getLength( cs.borderTopWidth ),
+ r: PIE.getLength( cs.borderRightWidth ),
+ b: PIE.getLength( cs.borderBottomWidth ),
+ l: PIE.getLength( cs.borderLeftWidth )
};
p.outset = distributeSides( outsets || [ 0 ], function( tok ) {
- return tok.type & LENGTH ? new PIE.Length( tok.value ) : tok.value;
+ return tok.type & LENGTH ? PIE.getLength( tok.value ) : tok.value;
} );
}
View
10 sources/BorderRadiusStyleInfo.js
@@ -15,17 +15,13 @@ PIE.BorderRadiusStyleInfo = PIE.StyleInfoBase.newStyleInfo( {
tokenizer, token, length,
hasNonZero = false;
- function newLength( v ) {
- return new PIE.Length( v );
- }
-
if( css ) {
tokenizer = new PIE.Tokenizer( css );
function collectLengths() {
var arr = [], num;
while( ( token = tokenizer.next() ) && token.isLengthOrPercent() ) {
- length = newLength( token.value );
+ length = PIE.getLength( token.value );
num = length.getNumber();
if( num < 0 ) {
return null;
@@ -65,8 +61,8 @@ PIE.BorderRadiusStyleInfo = PIE.StyleInfoBase.newStyleInfo( {
}
} );
-var ZERO = PIE.Length.ZERO,
- zeros = { 'tl': ZERO, 'tr': ZERO, 'br': ZERO, 'bl': ZERO };
+var zero = PIE.getLength( '0' ),
+ zeros = { 'tl': zero, 'tr': zero, 'br': zero, 'bl': zero };
PIE.BorderRadiusStyleInfo.ALL_ZERO = { x: zeros, y: zeros };
})();
View
4 sources/BorderRenderer.js
@@ -112,8 +112,8 @@ PIE.BorderRenderer = PIE.RendererBase.newRenderer( {
for( i = sides.length; i--; ) {
side = sides[ i ];
rs[ 'padding' + side ] = '';
- rs[ 'padding' + side ] = ( new PIE.Length( cs[ 'padding' + side ] ) ).pixels( el ) +
- ( new PIE.Length( cs[ 'border' + side + 'Width' ] ) ).pixels( el ) +
+ rs[ 'padding' + side ] = ( PIE.getLength( cs[ 'padding' + side ] ) ).pixels( el ) +
+ ( PIE.getLength( cs[ 'border' + side + 'Width' ] ) ).pixels( el ) +
( !PIE.ieVersion === 8 && i % 2 ? 1 : 0 ); //needs an extra horizontal pixel to counteract the extra "inner border" going away
}
rs.borderWidth = 0;
View
4 sources/BorderStyleInfo.js
@@ -43,9 +43,9 @@ PIE.BorderStyleInfo = PIE.StyleInfoBase.newStyleInfo( {
lastColor = color;
lastWidth = width;
- c[ ltr ] = new PIE.Color( color );
+ c[ ltr ] = PIE.getColor( color );
- width = w[ ltr ] = new PIE.Length( s[ ltr ] === 'none' ? '0' : ( this.namedWidths[ width ] || width ) );
+ width = w[ ltr ] = PIE.getLength( s[ ltr ] === 'none' ? '0' : ( this.namedWidths[ width ] || width ) );
if( width.pixels( this.targetElement ) > 0 ) {
active = true;
}
View
12 sources/BoxShadowStyleInfo.js
@@ -10,7 +10,7 @@ PIE.BoxShadowStyleInfo = PIE.StyleInfoBase.newStyleInfo( {
parseCss: function( css ) {
var props,
- Length = PIE.Length,
+ getLength = PIE.getLength,
Type = PIE.Tokenizer.Type,
tokenizer;
@@ -48,11 +48,11 @@ PIE.BoxShadowStyleInfo = PIE.StyleInfoBase.newStyleInfo( {
len = lengths && lengths.length;
if( len > 1 && len < 5 ) {
( inset ? props.inset : props.outset ).push( {
- xOffset: new Length( lengths[0].value ),
- yOffset: new Length( lengths[1].value ),
- blur: new Length( lengths[2] ? lengths[2].value : '0' ),
- spread: new Length( lengths[3] ? lengths[3].value : '0' ),
- color: new PIE.Color( color || 'currentColor' )
+ xOffset: getLength( lengths[0].value ),
+ yOffset: getLength( lengths[1].value ),
+ blur: getLength( lengths[2] ? lengths[2].value : '0' ),
+ spread: getLength( lengths[3] ? lengths[3].value : '0' ),
+ color: PIE.getColor( color || 'currentColor' )
} );
return true;
}
View
15 sources/Color.js
@@ -1,9 +1,12 @@
/**
- * Abstraction for colors values. Allows detection of rgba values.
+ * Abstraction for colors values. Allows detection of rgba values. A singleton instance per unique
+ * value is returned from PIE.getColor() - always use that instead of instantiating directly.
* @constructor
* @param {string} val The raw CSS string value for the color
*/
PIE.Color = (function() {
+ var instances = {};
+
function Color( val ) {
this.val = val;
}
@@ -110,5 +113,15 @@ PIE.Color = (function() {
}
};
+
+ /**
+ * Retrieve a PIE.Color instance for the given value. A shared singleton instance is returned for each unique value.
+ * @param {string} val The CSS string representing the color. It is assumed that this will already have
+ * been validated as a valid color syntax.
+ */
+ PIE.getColor = function(val) {
+ return instances[ val ] || ( instances[ val ] = new Color( val ) );
+ };
+
return Color;
})();
View
57 sources/Length.js
@@ -1,23 +1,33 @@
/**
- * Wrapper for length and percentage style values
+ * Wrapper for length and percentage style values. The value is immutable. A singleton instance per unique
+ * value is returned from PIE.getLength() - always use that instead of instantiating directly.
* @constructor
* @param {string} val The CSS string representing the length. It is assumed that this will already have
* been validated as a valid length or percentage syntax.
*/
PIE.Length = (function() {
-
var lengthCalcEl = doc.createElement( 'length-calc' ),
+ parent = doc.documentElement,
s = lengthCalcEl.style,
- numCache = {},
- unitCache = {};
+ conversions = {},
+ units = [ 'mm', 'cm', 'in', 'pt', 'pc' ],
+ i = units.length,
+ instances = {};
+
s.position = 'absolute';
- s.top = s.left = -9999;
+ s.top = s.left = '-9999px';
+
+ parent.appendChild( lengthCalcEl );
+ while( i-- ) {
+ lengthCalcEl.style.width = '100' + units[i];
+ conversions[ units[i] ] = lengthCalcEl.offsetWidth / 100;
+ }
+ parent.removeChild( lengthCalcEl );
function Length( val ) {
this.val = val;
}
-
Length.prototype = {
/**
* Regular expression for matching the length unit
@@ -30,10 +40,10 @@ PIE.Length = (function() {
* @return {number} The value
*/
getNumber: function() {
- var num = numCache[ this.val ],
+ var num = this.num,
UNDEF;
if( num === UNDEF ) {
- num = numCache[ this.val ] = parseFloat( this.val );
+ num = this.num = parseFloat( this.val );
}
return num;
},
@@ -43,10 +53,11 @@ PIE.Length = (function() {
* @return {string} The unit
*/
getUnit: function() {
- var unit = unitCache[ this.val ], m;
+ var unit = this.unit,
+ m;
if( !unit ) {
m = this.val.match( this.unitRE );
- unit = unitCache[ this.val ] = ( m && m[0] ) || 'px';
+ unit = this.unit = ( m && m[0] ) || 'px';
}
return unit;
},
@@ -78,7 +89,7 @@ PIE.Length = (function() {
case "ex":
return num * this.getEmPixels( el ) / 2;
default:
- return num * Length.conversions[ unit ];
+ return num * conversions[ unit ];
}
},
@@ -109,23 +120,15 @@ PIE.Length = (function() {
}
};
- Length.conversions = (function() {
- var units = [ 'mm', 'cm', 'in', 'pt', 'pc' ],
- vals = {},
- parent = doc.documentElement,
- i = units.length, unit;
-
- parent.appendChild( lengthCalcEl );
- while( i-- ) {
- unit = units[i];
- lengthCalcEl.style.width = '100' + unit;
- vals[ unit ] = lengthCalcEl.offsetWidth / 100;
- }
- parent.removeChild( lengthCalcEl );
- return vals;
- })();
- Length.ZERO = new Length( '0' );
+ /**
+ * Retrieve a PIE.Length instance for the given value. A shared singleton instance is returned for each unique value.
+ * @param {string} val The CSS string representing the length. It is assumed that this will already have
+ * been validated as a valid length or percentage syntax.
+ */
+ PIE.getLength = function( val ) {
+ return instances[ val ] || ( instances[ val ] = new Length( val ) );
+ };
return Length;
})();
Please sign in to comment.
Something went wrong with that request. Please try again.