Skip to content

Commit

Permalink
CSS: Support overwriting rules with !important priority
Browse files Browse the repository at this point in the history
Fixes trac-14394
Ref gh-1385
  • Loading branch information
mgol committed Mar 4, 2019
1 parent 6ced263 commit 6c0c64c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
12 changes: 12 additions & 0 deletions src/core/kebabCase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
define( function() {
"use strict";

var rbigLetter = /[A-Z]/g;

// Changes camelCase to kebab-case.
function kebabCase( value ) {
return value.replace( rbigLetter, "-$&" ).toLowerCase();
}

return kebabCase;
} );
15 changes: 9 additions & 6 deletions src/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ define( [
"./core",
"./core/access",
"./core/camelCase",
"./core/kebabCase",
"./var/rcssNum",
"./css/var/rnumnonpx",
"./css/var/cssExpand",
Expand All @@ -16,7 +17,7 @@ define( [
"./core/init",
"./core/ready",
"./selector" // contains
], function( jQuery, access, camelCase, rcssNum, rnumnonpx, cssExpand,
], function( jQuery, access, camelCase, kebabCase, rcssNum, rnumnonpx, cssExpand,
getStyles, swap, curCSS, adjustCSS, addGetHookIf, support, finalPropName ) {

"use strict";
Expand All @@ -27,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]).+)/,
rmsPropPrefix = /^ms[A-Z]/,
rcustomProp = /^--/,
cssShow = { position: "absolute", visibility: "hidden", display: "block" },
cssNormalTransform = {
Expand Down Expand Up @@ -274,11 +276,12 @@ jQuery.extend( {
if ( !hooks || !( "set" in hooks ) ||
( value = hooks.set( elem, value, extra ) ) !== undefined ) {

if ( isCustomProp ) {
style.setProperty( name, value );
} else {
style[ name ] = value;
}
style.setProperty(
isCustomProp ?
name :
kebabCase( name.replace( rmsPropPrefix, "-$&" ) ),
value
);
}

} else {
Expand Down
8 changes: 4 additions & 4 deletions src/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ define( [
"./core",
"./core/access",
"./core/camelCase",
"./core/kebabCase",
"./data/var/dataPriv",
"./data/var/dataUser"
], function( jQuery, access, camelCase, dataPriv, dataUser ) {
], function( jQuery, access, camelCase, kebabCase, dataPriv, dataUser ) {

"use strict";

Expand All @@ -18,8 +19,7 @@ define( [
// 5. Avoid exposing implementation details on user objects (eg. expando properties)
// 6. Provide a clear path for implementation upgrade to WeakMap in 2014

var rbrace = /^(?:\{[\w\W]*\}|\[[\w\W]*\])$/,
rmultiDash = /[A-Z]/g;
var rbrace = /^(?:\{[\w\W]*\}|\[[\w\W]*\])$/;

function getData( data ) {
if ( data === "true" ) {
Expand Down Expand Up @@ -52,7 +52,7 @@ function dataAttr( elem, key, data ) {
// If nothing was found internally, try to fetch any
// data from the HTML5 data-* attribute
if ( data === undefined && elem.nodeType === 1 ) {
name = "data-" + key.replace( rmultiDash, "-$&" ).toLowerCase();
name = "data-" + kebabCase( key );
data = elem.getAttribute( name );

if ( typeof data === "string" ) {
Expand Down
7 changes: 7 additions & 0 deletions test/unit/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -1501,6 +1501,13 @@ QUnit.test( "Reset the style if set to an empty string", function( assert ) {
"The style can be reset by setting to an empty string" );
} );

QUnit.test( "Override !important when changing styles (#14394)", function( assert ) {
assert.expect( 1 );

var el = jQuery( "<div style='display: block !important;'></div>" ).css( "display", "none" );
assert.equal( el.css( "display" ), "none", "New style replaced !important" );
} );

QUnit.test(
"Clearing a Cloned Element's Style Shouldn't Clear the Original Element's Style (#8908)",
function( assert ) {
Expand Down

0 comments on commit 6c0c64c

Please sign in to comment.