Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block Editor: Update Block Editor classNames to convention #14420

Merged
merged 1 commit into from Mar 14, 2019

Conversation

@aduth
Copy link
Member

@aduth aduth commented Mar 13, 2019

Previously: #14112

This nightmare of a pull request seeks to update class names for components moved from the editor package to block-editor, in order to continue respecting the CSS class naming guidelines which prescribe that each component be prefixed by its package directory. It does so without breaking compatibility by keeping the .editor- prefix as a compatibility mechanism.

In its current incarnation, it is as conservative as possible to keeping full compatibility. Unlike my previous comment by which I'd jynxed myself, automated transforms are highly fraught with issues (dealing with classnames, string template interpolation, etc). Conversely, I chose not to get into specifics of what's "worth" keeping for compatibility, for the sake of keeping the effort short-lived and as non-contentious as possible. In both of these instances, I think revisiting in the future may be appropriate.

There is an argument to be made that we make a specific exception to the CSS naming guidelines for the block-editor module, allowing both editor and block-editor components to share the editor- prefix for the sake of avoiding this tedious transition. My cautions against this are that (a) exceptions are bad because they break expectations and (b) it introduces the risk of unintentional conflict when working with components across the two otherwise distinct modules.

Implementation notes:

The changes here were partly RegEx-assisted mass string replacements, with a supplementary (sometimes involved) manual curation.

Specific replacement patterns:

  1. Replace string occurrences, exempting icon slugs:
    • Replace: (['"])editor-(?!aligncenter|alignleft|alignright|bold|break|code|contract|customchar|expand|help|indent|insertmore|italic|justify|kitchensink|ltr|ol-rtl|ol|outdent|paragraph|paste-text|paste-word|quote|removeformatting|rtl|spellcheck|strikethrough|table|textcolor|ul|underline|unlink|video)([^ '"]+)
    • With: $1editor-$2 block-editor-$2
    • Note: In initial passes, this missed some components where the string does not begin with editor- (example)
  2. Remove editor- occurrence for functions expecting a singular argument
    • Replace: (hasClass|contains)\( 'editor-.*?
    • With: $1( '
  3. Remove editor- occurrence for IDs
    • Replace: (id|htmlFor|aria-.+?)(="|': ')editor-.*?
    • With: $1$2
    • Note: There is no backwards-compatibility for IDs. They are largely used exclusively for the in-app label association.
  4. Replace CSS selector occurrences for deprecated components
    • Replace: \.editor-(autocomplete|alignment-toolbar|block-alignment-toolbar|block-controls|block-edit|block-format-controls|block-icon|block-inspector|block-list|block-mover|block-navigation-dropdown|block-selection-clearer|block-settings-menu|block-title|block-toolbar|color-palette|contrast-checker|copy-handler|default-block-appender|inserter|inner-blocks|inspector-advanced-controls|inspector-controls|panel-color-settings|plain-text|rich-text|media-placeholder|media-upload|multi-blocks-switcher|multi-select-scroll-into-view|navigable-toolbar|observe-typing|preserve-scroll-in-reorder|skip-to-selected-block|url-input|url-popover|warning|writing-flow)
    • With: .block-editor-$1
    • Note: This does not cover all migrated components, only those which had been exported publicly. Additional searches for .editor- were manually performed (guided in some cases by "post" exception \.editor-(?!post-)).

Notable exceptions:

  • We have some occurrences of clever concatenation (example). Template string interpolation in general proved tricky.
  • Native components bypassed compatibility and upgraded the class names directly (example)

Testing Instructions:

Give it a thorough shake, paying very close attention to visual regressions.

End-to-end tests should also reveal some issues, if present.

npm run test-e2e

Most useful review will be in considering what may have been overlooked or wrongly revised.

@aduth
Copy link
Member Author

@aduth aduth commented Mar 13, 2019

It took all of 20 minutes for this to receive its first merge conflict 😩

@aduth aduth force-pushed the update/block-editor-classnames branch from 216328f to bdad16a Mar 13, 2019
@aduth
Copy link
Member Author

@aduth aduth commented Mar 13, 2019

More notes:

  • Previously mentioned patterns often didn't accommodate for XPath selectors (example)
  • In general, there's lots of references to editor modules in e2e-tests and e2e-test-utils. Ultimately, I took to just reviewing every occurrence of the plaintext term editor- within the directories.

@youknowriad
Copy link
Contributor

@youknowriad youknowriad commented Mar 14, 2019

Looks like there's a valid e2e test failure but yeah let's not let this PR linger too much, so I'm approving.

@aduth
Copy link
Member Author

@aduth aduth commented Mar 14, 2019

The current failure is related to a few editor- class names applied outside the context of the editor or block editor modules:

className="editor-format-toolbar__link-container-content"

const linkClassName = classnames( 'editor-format-toolbar__link-container-value', {

className="editor-format-toolbar__link-container-content"

These shouldn't be assigned here, and could probably be abstracted to a component offered by BlockEditor. I won't assume to "correct" it here, for the purpose of keeping this short-lived, but I will update the class names to add the additional block-editor- prefixed class name. This will ensure consistency with what I assume is otherwise meant to extend the <FormatToolbar /> component.

cc @ellatrix

@aduth
Copy link
Member Author

@aduth aduth commented Mar 14, 2019

The above prompted me to do another search pass for the "editor-" plaintext term across all packages excluding packages/editor, packages/edit-post (I'd also previously done an audit yesterday for e2e-tests and e2e-test-utils).

There are several other erroneous occurrences:

<div className="editor-format-toolbar__selection-position" style={ style }>

<div className="editor-block-settings-menu__separator" />

className="editor-block-settings-menu__control"

className="editor-format-toolbar__image-container-content"

className="editor-format-toolbar__image-container-value"

As seen, they're all related to the format toolbar and block settings menu.

Noted previously, FormatToolbar is a block-editor component, so I think it's reasonable to update all these class names to include the block-editor- prefix.

editor-block-settings-menu, however, as best I can tell never had anything to do with the editor module, but rather plugin integrations in edit-post (source). I expect these were either always wrongly-named or neglected as part of a previous effort to extract components out of editor to edit-post. I will not update them.

@aduth aduth force-pushed the update/block-editor-classnames branch from 776c0e1 to 790fa23 Mar 14, 2019
@aduth
Copy link
Member Author

@aduth aduth commented Mar 14, 2019

Another useful pattern for auditing block-editor module using look-behinds, supported in the superior Python-based Sublime Text mass search:

(?<!block-)editor-

Copy link
Member Author

@aduth aduth left a comment

One thing we should consider in a subsequent pull request is performing an audit of excessive code line length, refactoring some of the very-long lines made yet-further-longer as of the changes here.

@@ -23,8 +23,8 @@ class BlockCompare extends Component {

return difference.map( ( item, pos ) => {
const classes = classnames( {
'editor-block-compare__added': item.added,
'editor-block-compare__removed': item.removed,
'editor-block-compare__added block-editor-block-compare__added': item.added,
Copy link
Member Author

@aduth aduth Mar 14, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of my own curiosity, I did confirm that classnames is capable of handling the assigning of two classes with respect to a single boolean property:

⇒ node       
> var cn = require( 'classnames' )
undefined
> cn( { 'foo bar': true } )
'foo bar'

@@ -1,4 +1,4 @@
.editor-visual-editor__block[data-type="core/nextpage"] {
.block-editor-block-list__block[data-type="core/nextpage"] {
Copy link
Member Author

@aduth aduth Mar 14, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noting that this was previously very broken, .editor-visual-editor__block doesn't exist anywhere before or after this set of changes. It should be corrected here now.

@aduth aduth merged commit 7964f38 into master Mar 14, 2019
1 check passed
@aduth aduth deleted the update/block-editor-classnames branch Mar 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

2 participants