Skip to content

Commit

Permalink
Check for duplication in addGeneratedClassName (#4422)
Browse files Browse the repository at this point in the history
* Check for duplication in addGeneratedClassName

* Coding Standards

* Update Syntax

remove the classnames utility and added tests

* Code Cleanup

* Code Cleanup

* New fixture for whatever reason

* Handle unexpected whitespaces
  • Loading branch information
Luehrsen authored and aduth committed Jan 30, 2018
1 parent d56af63 commit b5e2ae7
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 14 deletions.
21 changes: 14 additions & 7 deletions blocks/hooks/generated-class-name.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import classnames from 'classnames';
import { uniq } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -26,13 +26,20 @@ import { hasBlockSupport, getBlockDefaultClassname } from '../api';
export function addGeneratedClassName( extraProps, blockType ) {
// Adding the generated className
if ( hasBlockSupport( blockType, 'className', true ) ) {
const updatedClassName = classnames(
getBlockDefaultClassname( blockType.name ),
extraProps.className,
);
extraProps.className = updatedClassName;
}
if ( typeof extraProps.className === 'string' ) {
// We have some extra classes and want to add the default classname
// We use uniq to prevent duplicate classnames

extraProps.className = uniq( [
getBlockDefaultClassname( blockType.name ),
...extraProps.className.split( ' ' ),
] ).join( ' ' ).trim();
} else {
// There is no string in the className variable,
// so we just dump the default name in there
extraProps.className = getBlockDefaultClassname( blockType.name );
}
}
return extraProps;
}

Expand Down
7 changes: 7 additions & 0 deletions blocks/hooks/test/generated-class-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,12 @@ describe( 'generated className', () => {

expect( extraProps.className ).toBe( 'wp-block-chicken-ribs foo' );
} );

it( 'should not inject duplicates into className', () => {
const attributes = { className: 'bar' };
const extraProps = addSaveProps( { className: 'foo wp-block-chicken-ribs' }, blockSettings, attributes );

expect( extraProps.className ).toBe( 'wp-block-chicken-ribs foo' );
} );
} );
} );
4 changes: 2 additions & 2 deletions blocks/test/fixtures/core__cover-image.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
"Guten Berg!"
],
"url": "https://cldup.com/uuUqE_dXzy.jpg",
"contentAlign": "center",
"hasParallax": false,
"dimRatio": 40,
"contentAlign": "center"
"dimRatio": 40
},
"originalContent": "<section class=\"wp-block-cover-image has-background-dim-40 has-background-dim\" style=\"background-image:url(https://cldup.com/uuUqE_dXzy.jpg)\">\n <h2>Guten Berg!</h2>\n</section>"
}
Expand Down
4 changes: 2 additions & 2 deletions blocks/test/fixtures/core__image.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"isValid": true,
"attributes": {
"url": "https://cldup.com/uuUqE_dXzy.jpg",
"caption": [],
"alt": ""
"alt": "",
"caption": []
},
"originalContent": "<figure class=\"wp-block-image\"><img src=\"https://cldup.com/uuUqE_dXzy.jpg\" alt=\"\" /></figure>"
}
Expand Down
4 changes: 2 additions & 2 deletions blocks/test/fixtures/core__image__center-caption.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
"isValid": true,
"attributes": {
"url": "https://cldup.com/YLYhpou2oq.jpg",
"alt": "",
"caption": [
"Give it a try. Press the \"really wide\" button on the image toolbar."
],
"align": "center",
"alt": ""
"align": "center"
},
"originalContent": "<figure class=\"wp-block-image aligncenter\"><img src=\"https://cldup.com/YLYhpou2oq.jpg\" alt=\"\" /><figcaption>Give it a try. Press the &quot;really wide&quot; button on the image toolbar.</figcaption></figure>"
}
Expand Down
3 changes: 2 additions & 1 deletion blocks/test/fixtures/core__subhead.parsed.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[
{
"blockName": "core/subhead",
"attrs": null,"innerBlocks": [],
"attrs": null,
"innerBlocks": [],
"innerHTML": "\n<p class=\"wp-block-subhead\">This is a <em>subhead</em>.</p>\n"
},
{
Expand Down

0 comments on commit b5e2ae7

Please sign in to comment.