Skip to content

Commit

Permalink
Fix CSS vendor-prefixed property serialization (#7704)
Browse files Browse the repository at this point in the history
Corrects how wp.element.renderToString() serializes a vendor-prefixed
property such as MozTransform. These properties should be turned into
kebab-case and prefixed with a '-', e.g. '-moz-transform'.
  • Loading branch information
noisysocks committed Jul 5, 2018
1 parent 28985e3 commit 7c44995
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
14 changes: 10 additions & 4 deletions packages/element/src/serialize.js
Expand Up @@ -360,19 +360,25 @@ function getNormalAttributeName( attribute ) {
}

/**
* Returns the normal form of the style property name for HTML. Converts
* property names to kebab-case (e.g. 'backgroundColor' → 'background-color'),
* unless the property name begins with a '-' (e.g. '-webkit-overflow-scroll').
* Returns the normal form of the style property name for HTML.
*
* - Converts property names to kebab-case, e.g. 'backgroundColor' → 'background-color'
* - Leaves custom attributes alone, e.g. '--myBackgroundColor' → '--myBackgroundColor'
* - Converts vendor-prefixed property names to -kebab-case, e.g. 'MozTransform' → '-moz-transform'
*
* @param {string} property Property name.
*
* @return {string} Normalized property name.
*/
function getNormalStylePropertyName( property ) {
if ( startsWith( property, '-' ) ) {
if ( startsWith( property, '--' ) ) {
return property;
}

if ( hasPrefix( property, [ 'ms', 'O', 'Moz', 'Webkit' ] ) ) {
return '-' + kebabCase( property );
}

return kebabCase( property );
}

Expand Down
9 changes: 6 additions & 3 deletions packages/element/src/test/serialize.js
Expand Up @@ -566,12 +566,15 @@ describe( 'renderStyle()', () => {
expect( result ).toBe( '--myBackgroundColor:palegoldenrod' );
} );

it( 'should not kebab-case properties with a vendor prefix', () => {
it( 'should -kebab-case style properties with a vendor prefix', () => {
const result = renderStyle( {
'-webkit-overflow-scrolling': 'touch',
msTransform: 'none',
OTransform: 'none',
MozTransform: 'none',
WebkitTransform: 'none',
} );

expect( result ).toBe( '-webkit-overflow-scrolling:touch' );
expect( result ).toBe( '-ms-transform:none;-o-transform:none;-moz-transform:none;-webkit-transform:none' );
} );

describe( 'value unit', () => {
Expand Down

0 comments on commit 7c44995

Please sign in to comment.