diff options
author | Connor Atherton <c.liam.atherton@gmail.com> | 2016-06-25 10:21:00 -0700 |
---|---|---|
committer | Michał Gołębiowski <m.goleb@gmail.com> | 2017-03-07 14:52:08 +0100 |
commit | 619bf98d5b479f9582dbc40259b666f1c5a83146 (patch) | |
tree | 6215a0c018d7aaf0718881dc2a1eef71529cee92 /src | |
parent | be041e4da46153465ab4cdc65a4d90245a383a4c (diff) | |
download | jquery-619bf98d5b479f9582dbc40259b666f1c5a83146.tar.gz jquery-619bf98d5b479f9582dbc40259b666f1c5a83146.zip |
CSS: Support custom properties
Fixes gh-3144
Closes gh-3199
Closes gh-3557
Diffstat (limited to 'src')
-rw-r--r-- | src/css.js | 39 | ||||
-rw-r--r-- | src/css/curCSS.js | 5 |
2 files changed, 35 insertions, 9 deletions
diff --git a/src/css.js b/src/css.js index c17265ced..7a225d9a9 100644 --- a/src/css.js +++ b/src/css.js @@ -28,6 +28,7 @@ var // except "table", "table-cell", or "table-caption" // See here for display values: https://developer.mozilla.org/en-US/docs/CSS/display rdisplayswap = /^(none|table(?!-c[ea]).+)/, + rcustomProp = /^--/, cssShow = { position: "absolute", visibility: "hidden", display: "block" }, cssNormalTransform = { letterSpacing: "0", @@ -57,6 +58,16 @@ function vendorPropName( name ) { } } +// Return a property mapped along what jQuery.cssProps suggests or to +// a vendor prefixed property. +function finalPropName( name ) { + var ret = jQuery.cssProps[ name ]; + if ( !ret ) { + ret = jQuery.cssProps[ name ] = vendorPropName( name ) || name; + } + return ret; +} + function setPositiveNumber( elem, value, subtract ) { // Any relative (+/-) values have already been @@ -218,10 +229,15 @@ jQuery.extend( { // Make sure that we're working with the right name var ret, type, hooks, origName = jQuery.camelCase( name ), + isCustomProp = rcustomProp.test( name ), style = elem.style; - name = jQuery.cssProps[ origName ] || - ( jQuery.cssProps[ origName ] = vendorPropName( origName ) || origName ); + // Make sure that we're working with the right name. We don't + // want to query the value if it is a CSS custom property + // since they are user-defined. + if ( !isCustomProp ) { + name = finalPropName( origName ); + } // Gets hook for the prefixed version, then unprefixed version hooks = jQuery.cssHooks[ name ] || jQuery.cssHooks[ origName ]; @@ -257,7 +273,11 @@ jQuery.extend( { if ( !hooks || !( "set" in hooks ) || ( value = hooks.set( elem, value, extra ) ) !== undefined ) { - style[ name ] = value; + if ( isCustomProp ) { + style.setProperty( name, value ); + } else { + style[ name ] = value; + } } } else { @@ -276,11 +296,15 @@ jQuery.extend( { css: function( elem, name, extra, styles ) { var val, num, hooks, - origName = jQuery.camelCase( name ); + origName = jQuery.camelCase( name ), + isCustomProp = rcustomProp.test( name ); - // Make sure that we're working with the right name - name = jQuery.cssProps[ origName ] || - ( jQuery.cssProps[ origName ] = vendorPropName( origName ) || origName ); + // Make sure that we're working with the right name. We don't + // want to modify the value if it is a CSS custom property + // since they are user-defined. + if ( !isCustomProp ) { + name = finalPropName( origName ); + } // Try prefixed name followed by the unprefixed name hooks = jQuery.cssHooks[ name ] || jQuery.cssHooks[ origName ]; @@ -305,6 +329,7 @@ jQuery.extend( { num = parseFloat( val ); return extra === true || isFinite( num ) ? num || 0 : val; } + return val; } } ); diff --git a/src/css/curCSS.js b/src/css/curCSS.js index 313da4222..de5333030 100644 --- a/src/css/curCSS.js +++ b/src/css/curCSS.js @@ -15,8 +15,9 @@ function curCSS( elem, name, computed ) { computed = computed || getStyles( elem ); - // Support: IE <=9 only - // getPropertyValue is only needed for .css('filter') (#12537) + // getPropertyValue is needed for: + // .css('filter') (IE 9 only, #12537) + // .css('--customProperty) (#3144) if ( computed ) { ret = computed.getPropertyValue( name ) || computed[ name ]; |