Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Enhancement refactor element.style #2414

Closed
wants to merge 3 commits into from

3 participants

@ibolmo
Owner

Don't merge. It's no where near done. Just wanted to get some feedback.

A couple of things.

  1. I unrolled the Element.ShortStyles which significantly increased Element.Styles. I still think there's room to play with this. Keeping in mind, however, that we want to keep it simple. Let compilers and gzip do the hard work.

  2. Getter and setters are separated, but there may be collisions and a lot of repetition (element.getComputedStyle).

  3. There's also some normalization in setStyle and getStyle ... how are we going to handle this? Per getter/setter or against all results.

@ibolmo
Owner

Oh and this is definitely untested.. :D ala 1.0.x style (a "kami commit")

@arian arian commented on the diff
Source/Element/Element.Style.js
@@ -110,45 +122,29 @@ Element.implement({
},
getStyle: function(property){
- if (property == 'opacity') return getOpacity(this);
- property = (property == 'float' ? floatName : property).camelCase();
+
+ property = (property == 'float') ? floatName : property.camelCase();
+
+ var getter = getGetter(property);
@arian Owner
arian added a note

there isn't a getGetter function yet.

@ibolmo Owner
ibolmo added a note

hehe good call. Must have forgotten. I'll fix soon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@arian arian commented on the diff
Source/Element/Element.Style.js
@@ -213,23 +200,232 @@ Element.Styles = new Hash(Element.Styles);
//</1.2compat>
-Element.ShortStyles = {margin: {}, padding: {}, border: {}, borderWidth: {}, borderStyle: {}, borderColor: {}};
+Element.Styles = {
+ left: {map: '@px'},
@arian Owner
arian added a note

why not keep left: '@px' by default, only for special cases use an object. Is smaller and probably better for backward compat.

@ibolmo Owner
ibolmo added a note

We could reduce this by adding more logic to the getGetter, getMap, and getStyle, but I'm also trying to encourage a simple hook mechanism. See the following comment.

@ibolmo Owner
ibolmo added a note

oh and there is a simple byte reduction I had thought of, but I'm not implementing yet. give me your 2c:

'left,top,bottom,right,width,height,letterSpacing,lineHeight,...'.split(',').forEach(function(property){
    Element.Styles[property] = {map: '@px'};
});
@ibolmo Owner
ibolmo added a note

Even further:

{
  '@px': 'left,top,bottom,right,width,height,letterSpacing,lineHeight,...',
  '@': 'zIndex',
  ...
}

And just have the initializer code once.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@arian arian commented on the diff
Source/Element/Element.Style.js
((10 lines not shown))
+ width: {map: '@px'},
+ height: {map: '@px'},
+ letterSpacing: {map: '@px'},
+ lineHeight: {map: '@px'},
+ clip: {map: 'rect(@px @px @px @px)'},
+ zIndex: {
+ map: '@',
+ get: function(element){
+ return element.getComputedStyle('zIndex');
+ }
+ },
+ 'zoom': {map: '@'},
+ textIndent: {map: '@px'},
+ opacity: {
+ map: '@',
+ get: function(element){
@arian Owner
arian added a note
get: getOpacity,
set: setOpacity

should be able to handle it..

@ibolmo Owner
ibolmo added a note

actually I'm considering moving that logic inside the get and set. Just haven't gotten to it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@arian arian commented on the diff
Source/Element/Element.Style.js
((11 lines not shown))
+ height: {map: '@px'},
+ letterSpacing: {map: '@px'},
+ lineHeight: {map: '@px'},
+ clip: {map: 'rect(@px @px @px @px)'},
+ zIndex: {
+ map: '@',
+ get: function(element){
+ return element.getComputedStyle('zIndex');
+ }
+ },
+ 'zoom': {map: '@'},
+ textIndent: {map: '@px'},
+ opacity: {
+ map: '@',
+ get: function(element){
+ return getOpacity(this);
@arian Owner
arian added a note

also you're using this here, instead of element.

@ibolmo Owner
ibolmo added a note

right, forgot to update. nice catch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@arian arian commented on the diff
Source/Element/Element.Style.js
((180 lines not shown))
+ borderRightStyle: '@px',
+ borderBottomStyle: '@px',
+ borderLeftStyle: '@px'
+ },
+ borderColor: {
+ borderTopColor: '@px',
+ borderRightColor: '@px',
+ borderBottomColor: '@px',
+ borderLeftColor: '@px'
+ }
+};
+
+
+// todo(ibolmo): collisions?
+
+if (hasBackgroundPositionXY){
@arian Owner
arian added a note

maybe also move the feature detections here, closer where it's actually used.

@ibolmo Owner
ibolmo added a note

agreed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@arian
Owner

Nice work, good start! :+1:

@gonchuki gonchuki commented on the diff
Source/Element/Element.Style.js
((12 lines not shown))
var result = this.style[property];
- if (!result || property == 'zIndex'){
- if (Element.ShortStyles.hasOwnProperty(property)){
- result = [];
- for (var s in Element.ShortStyles[property]) result.push(this.getStyle(s));
- return result.join(' ');
- }
+
+ if (!result && Element.ShortStyles.hasOwnProperty(property)){
+ result = [];
+ for (var s in Element.ShortStyles[property]) result.push(this.getStyle(s));
+ return result.join(' ');
+ } else {
@gonchuki Collaborator

code will wrongly enter here when result && !Element.ShortStyles.hasOwnProperty(property), you only need to run the getComputedStyle part if we got no result from the this.style[property]

Even better, I'm also not sure if doing: var result = this.style[property] || this.getComputedStyle(property); would break anything, current specs pass but that doesn't say much. Any reason to give ShortStyles precedence over trying to getComputedStyle on the element?

@ibolmo Owner
ibolmo added a note

You're right the change is not 1:1 with the previous logic. I'll modify soon.

Regards to your suggest change, again you're right. It's not clear why we can't default to getComputedStyle. If style getter doesn't work.

My other thought is, why can't we always use getComputedStyle? Was the original idea used to save CPU cycles?

@gonchuki Collaborator

saving CPU cycles is a sure thing, remember than only IE has .currentStyle so other browsers will go though the longer function-call-heavy path. Considering that getStyle is used a lot on animations, we should always have the fastest possible path here to avoid jerkiness.

That being said, if it's safe to do style || getComputedStyle on this part of the code then we could integrate this default behavior right inside getComputedStyle if (this.style[property]) return this.style[property]; etc. as the rest of the uses of getComputedStyle are always as a default when the element.style cannot be retrieved (getOpacity, setFilter on #2411, etc)

@gonchuki Collaborator

investigated this, it's not safe to just integrate the element style inside getComputedStyle. Several tests break on different browsers so it seems it is like that for a reason. Removing the element.style querying altogether is neither an option, once again several tests break. Keeping in line with the comment I left on #2417 I think this part of the code should stay with the same logic flow unless we get way more specs to cover all bases.

edit: also a small correction to my comment above: seems that Opera also has currentStyle and it has a serious bug on getComputedStyle where if you set something as a %, the getComputedStyle call rounds the result to the nearest pixel integer instead of returning the exact sub-pixel value as Firefox/IE9/IE10 does (this unit conversion from % to px was addressed on #2160, some browsers convert from % to px, others don't).

Take this as misc info to reinforce the fact that current logic flow is mostly ok, even though I would still like to have getComputedStyle used if available instead of currentStyle as that's the standard and I will probably get up a PR later with these changes. I still think that doing small incremental changes gets us in a position where we can ship code now without requiring a ton of new specs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@ibolmo
Owner

The reason I unrolled the https://github.com/mootools/mootoolscore/blob/cb327bf3/Source/Element/Element.Style.js#L216-232 is simply that it wasn't easy to read.

My logic is that we're no longer in 2000 when JS compilers were non-existant and that most people didn't bother to gzip .. CDNs are prolific now.

Lastly, I'm not done with Element.ShortStyle and Element.Styles. I think there's a lot of room to explore. To explore, I thought let's see all properties unrolled.

I have some ideas with increased effort:

  1. Instead of defaulting to '@' we could default to '@px' in line: https://github.com/mootools/mootools-core/pull/2414/files#L0R86. That'll save a lot of bytes. I'm not sure, however, of the consequences. Need more test coverage.

  2. I was thinking we could do a BoxModel class that we can use to say contain this stuff. Especially rules about accepted input and marginTop-like (, borderTopWidth...) definitions that would be implicit to the model. Again, this is very vapor but I thought it might be interesting to see if it'll simplify development.

  3. Finally, if we decide that we don't need developers to understand the code we can save a lot of bytes by having a script that scrapes the w3 css definition and comes up with a literally mapping of the accepted input and the expected output. Notice that sometimes we map some outputs to px or convert rgba to rgb. That's really bad. or how we round floats ow.

@ibolmo ibolmo modified the milestone: 1.6, 1.5
@ibolmo
Owner

Closing due to no longer relevant. If at any point we'll refactor this, we'll do so then.

@ibolmo ibolmo closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 260 additions and 64 deletions.
  1. +260 −64 Source/Element/Element.Style.js
View
324 Source/Element/Element.Style.js
@@ -1,3 +1,4 @@
+
/*
---
@@ -75,6 +76,16 @@ var floatName = (html.style.cssFloat == null) ? 'styleFloat' : 'cssFloat',
namedPositions = {left: '0%', top: '0%', center: '50%', right: '100%', bottom: '100%'},
hasBackgroundPositionXY = (html.style.backgroundPositionX != null);
+function getSetter(property){
+ property = Element.Styles[property];
+ return property && property.set;
+}
+
+function getMap(property){
+ property = Element.Styles[property];
+ return property && property.map || '@';
+}
+
Element.implement({
getComputedStyle: function(property){
@@ -85,14 +96,11 @@ Element.implement({
},
setStyle: function(property, value){
- if (property == 'opacity'){
- if (value != null) value = parseFloat(value);
- setOpacity(this, value);
- return this;
- }
- property = (property == 'float' ? floatName : property).camelCase();
+
+ property = (property == 'float') ? floatName : property.camelCase();
+
if (typeOf(value) != 'string'){
- var map = (Element.Styles[property] || '@').split(' ');
+ var map = getMap(property).split(' ');
value = Array.from(value).map(function(val, i){
if (!map[i]) return '';
return (typeOf(val) == 'number') ? map[i].replace('@', Math.round(val)) : val;
@@ -100,7 +108,11 @@ Element.implement({
} else if (value == String(Number(value))){
value = Math.round(value);
}
- this.style[property] = value;
+
+ var setter = getSetter(property);
+ if (setter) setter(element, value);
+ else this.style[property] = value;
+
//<ltIE9>
if ((value == '' || value == null) && doesNotRemoveStyles && this.style.removeAttribute){
this.style.removeAttribute(property);
@@ -110,45 +122,29 @@ Element.implement({
},
getStyle: function(property){
- if (property == 'opacity') return getOpacity(this);
- property = (property == 'float' ? floatName : property).camelCase();
+
+ property = (property == 'float') ? floatName : property.camelCase();
+
+ var getter = getGetter(property);
@arian Owner
arian added a note

there isn't a getGetter function yet.

@ibolmo Owner
ibolmo added a note

hehe good call. Must have forgotten. I'll fix soon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ if (getter) return getter(property);
+
var result = this.style[property];
- if (!result || property == 'zIndex'){
- if (Element.ShortStyles.hasOwnProperty(property)){
- result = [];
- for (var s in Element.ShortStyles[property]) result.push(this.getStyle(s));
- return result.join(' ');
- }
+
+ if (!result && Element.ShortStyles.hasOwnProperty(property)){
+ result = [];
+ for (var s in Element.ShortStyles[property]) result.push(this.getStyle(s));
+ return result.join(' ');
+ } else {
@gonchuki Collaborator

code will wrongly enter here when result && !Element.ShortStyles.hasOwnProperty(property), you only need to run the getComputedStyle part if we got no result from the this.style[property]

Even better, I'm also not sure if doing: var result = this.style[property] || this.getComputedStyle(property); would break anything, current specs pass but that doesn't say much. Any reason to give ShortStyles precedence over trying to getComputedStyle on the element?

@ibolmo Owner
ibolmo added a note

You're right the change is not 1:1 with the previous logic. I'll modify soon.

Regards to your suggest change, again you're right. It's not clear why we can't default to getComputedStyle. If style getter doesn't work.

My other thought is, why can't we always use getComputedStyle? Was the original idea used to save CPU cycles?

@gonchuki Collaborator

saving CPU cycles is a sure thing, remember than only IE has .currentStyle so other browsers will go though the longer function-call-heavy path. Considering that getStyle is used a lot on animations, we should always have the fastest possible path here to avoid jerkiness.

That being said, if it's safe to do style || getComputedStyle on this part of the code then we could integrate this default behavior right inside getComputedStyle if (this.style[property]) return this.style[property]; etc. as the rest of the uses of getComputedStyle are always as a default when the element.style cannot be retrieved (getOpacity, setFilter on #2411, etc)

@gonchuki Collaborator

investigated this, it's not safe to just integrate the element style inside getComputedStyle. Several tests break on different browsers so it seems it is like that for a reason. Removing the element.style querying altogether is neither an option, once again several tests break. Keeping in line with the comment I left on #2417 I think this part of the code should stay with the same logic flow unless we get way more specs to cover all bases.

edit: also a small correction to my comment above: seems that Opera also has currentStyle and it has a serious bug on getComputedStyle where if you set something as a %, the getComputedStyle call rounds the result to the nearest pixel integer instead of returning the exact sub-pixel value as Firefox/IE9/IE10 does (this unit conversion from % to px was addressed on #2160, some browsers convert from % to px, others don't).

Take this as misc info to reinforce the fact that current logic flow is mostly ok, even though I would still like to have getComputedStyle used if available instead of currentStyle as that's the standard and I will probably get up a PR later with these changes. I still think that doing small incremental changes gets us in a position where we can ship code now without requiring a ton of new specs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
result = this.getComputedStyle(property);
}
- if (hasBackgroundPositionXY && /^backgroundPosition[XY]?$/.test(property)){
- return result.replace(/(top|right|bottom|left)/g, function(position){
- return namedPositions[position];
- }) || '0px';
- }
- if (!result && property == 'backgroundPosition') return '0px 0px';
+
+ // todo(ibolmo): normalization is needed for which properties? and when
if (result){
result = String(result);
var color = result.match(/rgba?\([\d\s,]+\)/);
if (color) result = result.replace(color[0], color[0].rgbToHex());
}
- if (Browser.opera || Browser.ie){
- if ((/^(height|width)$/).test(property) && !(/px$/.test(result))){
- var values = (property == 'width') ? ['left', 'right'] : ['top', 'bottom'], size = 0;
- values.each(function(value){
- size += this.getStyle('border-' + value + '-width').toInt() + this.getStyle('padding-' + value).toInt();
- }, this);
- return this['offset' + property.capitalize()] - size + 'px';
- }
- if ((/^border(.+)Width|margin|padding/).test(property) && isNaN(parseFloat(result))){
- return '0px';
- }
- //<ltIE9>
- if (returnsBordersInWrongOrder && /^border(Top|Right|Bottom|Left)?$/.test(property) && /^#/.test(result)){
- return result.replace(/^(.+)\s(.+)\s(.+)$/, '$2 $3 $1');
- }
- //</ltIE9>
- }
+
return result;
},
@@ -167,15 +163,6 @@ Element.implement({
});
-Element.Styles = {
- left: '@px', top: '@px', bottom: '@px', right: '@px',
- width: '@px', height: '@px', maxWidth: '@px', maxHeight: '@px', minWidth: '@px', minHeight: '@px',
- backgroundColor: 'rgb(@, @, @)', backgroundPosition: '@px @px', color: 'rgb(@, @, @)',
- fontSize: '@px', letterSpacing: '@px', lineHeight: '@px', clip: 'rect(@px @px @px @px)',
- margin: '@px @px @px @px', padding: '@px @px @px @px', border: '@px @ rgb(@, @, @) @px @ rgb(@, @, @) @px @ rgb(@, @, @)',
- borderWidth: '@px @px @px @px', borderStyle: '@ @ @ @', borderColor: 'rgb(@, @, @) rgb(@, @, @) rgb(@, @, @) rgb(@, @, @)',
- zIndex: '@', 'zoom': '@', fontWeight: '@', textIndent: '@px', opacity: '@'
-};
//<1.3compat>
@@ -213,23 +200,232 @@ Element.Styles = new Hash(Element.Styles);
//</1.2compat>
-Element.ShortStyles = {margin: {}, padding: {}, border: {}, borderWidth: {}, borderStyle: {}, borderColor: {}};
+Element.Styles = {
+ left: {map: '@px'},
@arian Owner
arian added a note

why not keep left: '@px' by default, only for special cases use an object. Is smaller and probably better for backward compat.

@ibolmo Owner
ibolmo added a note

We could reduce this by adding more logic to the getGetter, getMap, and getStyle, but I'm also trying to encourage a simple hook mechanism. See the following comment.

@ibolmo Owner
ibolmo added a note

oh and there is a simple byte reduction I had thought of, but I'm not implementing yet. give me your 2c:

'left,top,bottom,right,width,height,letterSpacing,lineHeight,...'.split(',').forEach(function(property){
    Element.Styles[property] = {map: '@px'};
});
@ibolmo Owner
ibolmo added a note

Even further:

{
  '@px': 'left,top,bottom,right,width,height,letterSpacing,lineHeight,...',
  '@': 'zIndex',
  ...
}

And just have the initializer code once.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ top: {map: '@px'},
+ bottom: {map: '@px'},
+ right: {map: '@px'},
+ width: {map: '@px'},
+ height: {map: '@px'},
+ letterSpacing: {map: '@px'},
+ lineHeight: {map: '@px'},
+ clip: {map: 'rect(@px @px @px @px)'},
+ zIndex: {
+ map: '@',
+ get: function(element){
+ return element.getComputedStyle('zIndex');
+ }
+ },
+ 'zoom': {map: '@'},
+ textIndent: {map: '@px'},
+ opacity: {
+ map: '@',
+ get: function(element){
@arian Owner
arian added a note
get: getOpacity,
set: setOpacity

should be able to handle it..

@ibolmo Owner
ibolmo added a note

actually I'm considering moving that logic inside the get and set. Just haven't gotten to it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ return getOpacity(this);
@arian Owner
arian added a note

also you're using this here, instead of element.

@ibolmo Owner
ibolmo added a note

right, forgot to update. nice catch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ },
+ set: function(element, value){
+ if (value != null) value = parseFloat(value);
+ setOpacity(this, value);
+ return this;
+ }
+ },
+ color: {map: 'rgb(@, @, @)'},
+
+ maxWidth: {map: '@px'},
+ maxHeight: {map: '@px'},
-['Top', 'Right', 'Bottom', 'Left'].each(function(direction){
- var Short = Element.ShortStyles;
- var All = Element.Styles;
- ['margin', 'padding'].each(function(style){
- var sd = style + direction;
- Short[style][sd] = All[sd] = '@px';
+ minWidth: {map: '@px'},
+ minHeight: {map: '@px'},
+
+ backgroundColor: {map: 'rgb(@, @, @)'},
+ backgroundPosition: {
+ map: '@px @px',
+ get: function(element){
+ return element.getComputedStyle('backgroundPosition') || '0px 0px';
+ }
+ },
+
+ fontWeight: {map: '@'},
+ fontSize: {map: '@px'},
+
+ margin: {map: '@px @px @px @px'},
+ marginTop: {map: '@px'},
+ marginRight: {map: '@px'},
+ marginBottom: {map: '@px'},
+ marginLeft: {map: '@px'},
+
+ padding: {map: '@px @px @px @px'},
+ paddingTop: {map: '@px'},
+ paddingRight: {map: '@px'},
+ paddingBottom: {map: '@px'},
+ paddingLeft: {map: '@px'},
+
+ border: {map: '@px @ rgb(@, @, @) @px @ rgb(@, @, @) @px @ rgb(@, @, @)'},
+ borderWidth: {map: '@px @px @px @px'},
+ borderStyle: {map: '@ @ @ @'},
+ borderColor: {map: 'rgb(@, @, @) rgb(@, @, @) rgb(@, @, @) rgb(@, @, @)'},
+
+ borderTop: {map: '@px @ rgb(@, @, @)'},
+ borderTopWidth: {map: '@px'},
+ borderTopColor: {map: '@px'},
+ borderTopStyle: {map: '@px'},
+
+ borderRight: {map: '@px @ rgb(@, @, @)'},
+ borderRightWidth: {map: '@px'},
+ borderRightStyle: {map: '@px'},
+ borderRightColor: {map: '@px'},
+
+ borderBottom: {map: '@px @ rgb(@, @, @)'},
+ borderBottomStyle: {map: '@px'},
+ borderBottomColor: {map: '@px'},
+ borderBottomWidth: {map: '@px'},
+
+ borderLeft: {map: '@px @ rgb(@, @, @)'},
+ borderLeftWidth: {map: '@px'},
+ borderLeftStyle: {map: '@px'},
+ borderLeftColor: {map: '@px'}
+};
+
+Element.ShortStyles = {
+ margin: {
+ marginTop: '@px',
+ marginRight: '@px',
+ marginBottom: '@px',
+ marginLeft: '@px'
+ },
+ padding: {
+ paddingTop: '@px',
+ paddingRight: '@px',
+ paddingBottom: '@px',
+ paddingLeft: '@px'
+ },
+ border: {
+ borderTop: '@px @ rgb(@, @, @)',
+ borderRight: '@px @ rgb(@, @, @)',
+ borderBottom: '@px @ rgb(@, @, @)',
+ borderLeft: '@px @ rgb(@, @, @)'
+ },
+ borderTop: {
+ borderTopWidth: '@px',
+ borderTopColor: '@px',
+ borderTopStyle: '@px',
+ borderRightWidth: '@px',
+ borderRightStyle: '@px',
+ borderRightColor: '@px',
+ borderBottomStyle: '@px',
+ borderBottomColor: '@px',
+ borderBottomWidth: '@px',
+ borderLeftWidth: '@px',
+ borderLeftStyle: '@px',
+ borderLeftColor: '@px'
+ },
+ borderRight: {
+ borderTopWidth: '@px',
+ borderTopColor: '@px',
+ borderTopStyle: '@px',
+ borderRightWidth: '@px',
+ borderRightStyle: '@px',
+ borderRightColor: '@px',
+ borderBottomStyle: '@px',
+ borderBottomColor: '@px',
+ borderBottomWidth: '@px',
+ borderLeftWidth: '@px',
+ borderLeftStyle: '@px',
+ borderLeftColor: '@px'
+ },
+ borderBottom: {
+ borderTopWidth: '@px',
+ borderTopColor: '@px',
+ borderTopStyle: '@px',
+ borderRightWidth: '@px',
+ borderRightStyle: '@px',
+ borderRightColor: '@px',
+ borderBottomStyle: '@px',
+ borderBottomColor: '@px',
+ borderBottomWidth: '@px',
+ borderLeftWidth: '@px',
+ borderLeftStyle: '@px',
+ borderLeftColor: '@px'
+ },
+ borderLeft: {
+ borderTopWidth: '@px',
+ borderTopColor: '@px',
+ borderTopStyle: '@px',
+ borderRightWidth: '@px',
+ borderRightStyle: '@px',
+ borderRightColor: '@px',
+ borderBottomStyle: '@px',
+ borderBottomColor: '@px',
+ borderBottomWidth: '@px',
+ borderLeftWidth: '@px',
+ borderLeftStyle: '@px',
+ borderLeftColor: '@px'
+ },
+ borderWidth: {
+ borderTopWidth: '@px',
+ borderRightWidth: '@px',
+ borderBottomWidth: '@px',
+ borderLeftWidth: '@px'
+ },
+ borderStyle: {
+ borderTopStyle: '@px', q
+ borderRightStyle: '@px',
+ borderBottomStyle: '@px',
+ borderLeftStyle: '@px'
+ },
+ borderColor: {
+ borderTopColor: '@px',
+ borderRightColor: '@px',
+ borderBottomColor: '@px',
+ borderLeftColor: '@px'
+ }
+};
+
+
+// todo(ibolmo): collisions?
+
+if (hasBackgroundPositionXY){
@arian Owner
arian added a note

maybe also move the feature detections here, closer where it's actually used.

@ibolmo Owner
ibolmo added a note

agreed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ ['backgroundPositionX', 'backgroundPositionY'].forEach(function(property){
+ Element.Styles[property] = {get: function(element){
+ return element.getComputedStyle(property).replace(/(top|right|bottom|left)/g, function(position){
+ return namedPosition[position];
+ }) || '0px';
+ }};
});
- var bd = 'border' + direction;
- Short.border[bd] = All[bd] = '@px @ rgb(@, @, @)';
- var bdw = bd + 'Width', bds = bd + 'Style', bdc = bd + 'Color';
- Short[bd] = {};
- Short.borderWidth[bdw] = Short[bd][bdw] = All[bdw] = '@px';
- Short.borderStyle[bds] = Short[bd][bds] = All[bds] = '@';
- Short.borderColor[bdc] = Short[bd][bdc] = All[bdc] = 'rgb(@, @, @)';
-});
+ Element.ShortStyles.backgroundPosition = {backgroundPositionX: '@', backgroundPositionY: '@'};
+}
+
+if (Browser.opera || Browser.ie){
+ Object.each({
+ width: ['borderTopWidth', 'borderBottomWidth', 'paddingTop', 'paddingBottom'],
+ height: ['borderLeftWidth', 'borderRightWidth', 'paddingLeft', 'paddingRight']
+ }, function(styles, property){
+ Element.Styles[property].get = function(element){
+ var result = element.getComputedStyle(property), size = 0;
+ if (result.substr(-2) == 'px') return result;
+
+ Object.forEach(element.getStyles(styles), function(value){ size += value; });
+ return (element['offset' + property.capitalize()] - size) + 'px';
+ };
+ });
+
+ for (var property in Element.Styles) if (/^border(.+)Width|margin|padding/.test(property)){
+ Element.Styles[property].get = function(element){
+ var result = element.getComputedStyle(property);
+ return isNaN(parseFloat(result)) ? '0px' : result;
+ }
+ }
+}
+
+//<ltIE9>
+if (returnsBordersInWrongOrder){
+ for (var property in Element.Styles) if (/^border(Top|Right|Bottom|Left)?$/.test(property)){
+ Element.Styles[property].get = function(element){
+ var result = element.getComputedStyle(property);
+ return /^#/.test(result) ? result.replace(/^(.+)\s(.+)\s(.+)$/, '$2 $3 $1') : result;
+ };
+ }
+}
+//</ltIE9>
-if (hasBackgroundPositionXY) Element.ShortStyles.backgroundPosition = {backgroundPositionX: '@', backgroundPositionY: '@'};
})();
Something went wrong with that request. Please try again.