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

Hosting Command Palette: Trim double quotes from selected items if present #84373

Merged
merged 4 commits into from
Nov 23, 2023

Conversation

katinthehatsite
Copy link
Contributor

@katinthehatsite katinthehatsite commented Nov 21, 2023

Related to https://github.com/Automattic/dotcom-forge/issues/4550

Proposed Changes

This PR trims the double quotes from the selected site title before it is processed by the document.querySelector. This ensures that the command palette does not break if the site titles include double quotes.

This is what the error looks like if there are double quotes present before they are being processed by the document.querySelector in the command palette:

Screenshot 2023-11-21 at 3 16 33 PM

Testing Instructions

  • Ensure that you have an Atomic site
  • Set the site title under Settings > General on an Atomic site to include double quotes e.g. "My Site"
  • First, test on https://wpcalypso.wordpress.com/sites by triggering cmd + k on Mac and ctrl + k on Windows
  • Navigate to one of the SSH commands e.g. Open SSH details
  • Try navigating to the site with double quotes in the site title
  • Confirm this breaks the command palette
  • Next, navigate to Calypso Live Link generated in this PR
  • Trigger command palette with cmd + k on Mac and ctrl + k on Windows
  • Try navigating to the site with double quotes in the site title
  • Confirm that the command executes as expected

Pre-merge Checklist

  • Has the general commit checklist been followed? (PCYsg-hS-p2)
  • https://wpcalypso.wordpress.com/devdocs/docs/testing/index.md for your changes?
  • Have you tested the feature in Simple (P9HQHe-k8-p2), Atomic (P9HQHe-jW-p2), and self-hosted Jetpack sites (PCYsg-g6b-p2)?
  • Have you checked for TypeScript, React or other console errors?
  • Have you used memoizing on expensive computations? More info in Memoizing with create-selector and Using memoizing selectors and Our Approach to Data
  • Have we added the "[Status] String Freeze" label as soon as any new strings were ready for translation (p4TIVU-5Jq-p2)?
  • For changes affecting Jetpack: Have we added the "[Status] Needs Privacy Updates" label if this pull request changes what data or activity we track or use (p4TIVU-ajp-p2)?

Copy link

github-actions bot commented Nov 21, 2023

@matticbot
Copy link
Contributor

matticbot commented Nov 21, 2023

Here is how your PR affects size of JS and CSS bundles shipped to the user's browser:

Sections (~44 bytes added 📈 [gzipped])

name                     parsed_size           gzip_size
sites-dashboard               +318 B  (+0.1%)      +57 B  (+0.0%)
import-hosted-site-flow       +318 B  (+0.0%)      +72 B  (+0.0%)
import-flow                   +318 B  (+0.0%)      +72 B  (+0.0%)

Sections contain code specific for a given set of routes. Is downloaded and parsed only when a particular route is navigated to.

Legend

What is parsed and gzip size?

Parsed Size: Uncompressed size of the JS and CSS files. This much code needs to be parsed and stored in memory.
Gzip Size: Compressed size of the JS and CSS files. This much data needs to be downloaded over network.

Generated by performance advisor bot at iscalypsofastyet.com.

@katinthehatsite katinthehatsite requested a review from a team November 21, 2023 14:34
@matticbot matticbot added the [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. label Nov 21, 2023
Copy link
Member

@sejas sejas left a comment

Choose a reason for hiding this comment

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

Wow!, great catch @katinthehatsite . 🙌

This PR solves the issue that I was able to replicate on production. I also tried adding other characters besides the quote, like [, ], but they worked fine.

We should upstream this change.
https://github.com/WordPress/gutenberg/blob/trunk/packages/commands/src/components/command-menu.js#L161C17-L164

I confirm that having a site with quotes or other weird characters don't break the command palette. And I was able to navigate to the SSH details. 👍

Screencast

quotes.on.command.palette.mp4

@@ -69,16 +69,23 @@ interface CommandInputProps {
function CommandInput( { isOpen, search, setSearch }: CommandInputProps ) {
const commandMenuInput = useRef< HTMLInputElement >( null );
const _value = useCommandState( ( state ) => state.value );
const sanitizedValue = useMemo( () => {
const removedQuotesValue = _value.replace( /"/g, '' ); // Remove double quotes from any selected items before processing input
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we use escapeAttribute from @wordpress/escape-html? https://developer.wordpress.org/block-editor/reference-guides/packages/packages-escape-html/#escapeattribute

Or how do we match the sanitization the library is already applying, to make sure our value exactly matches what's rendered on the page?

Also, what other characters might cause problems?

Copy link
Member

@sejas sejas Nov 22, 2023

Choose a reason for hiding this comment

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

Also, what other characters might cause problems?

Yesterday reviewing the PR I found that the double quotes character is problematic. The rest worked fine.

@sejas
Copy link
Member

sejas commented Nov 22, 2023

@katinthehatsite, let's pair on this.
All this logic is for accessibility improvements and should be implemented directly on cmdk. I think we can get the id withtout accessing the DOM elements at all.

@sejas
Copy link
Member

sejas commented Nov 22, 2023

For context:
The accessibility issue was fixed on cmdk but there hasn't been a new release since March. Therefore those have the same risk.

@sejas
Copy link
Member

sejas commented Nov 22, 2023

Hey Kat, I created this PR using yours as a base. We are taking a different/better approach from cmdk and @wordpress/commands #84412

…ters (#84412)

* Use the value slugified for item id and activedesdcendant
* calculate slug only when the value changes
onSelect={ () => command.callback( { close, setSearch } ) }
id={ command.name }
id={ cleanForSlug( itemValue ) }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@katinthehatsite
Copy link
Contributor Author

@sejas - thanks for all the research and the refactor. I think if we can make the fix in Gutenberg, this is definitely the approach that we should be taking!

@sejas sejas merged commit f916efc into trunk Nov 23, 2023
12 checks passed
@sejas sejas deleted the fix/trim-double-quotes-in-command-palette branch November 23, 2023 11:41
@github-actions github-actions bot removed the [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. label Nov 23, 2023
cpapazoglou pushed a commit that referenced this pull request Dec 11, 2023
…esent (#84373)

* Use the  slugified value for item id and activedesdcendant

---------

Co-authored-by: Kateryna Kodonenko <kateryna@automattic.com>
Co-authored-by: Antonio Sejas <antonio@sejas.es>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants