Skip to content

Conversation

@francesmcmullin
Copy link

@francesmcmullin francesmcmullin commented Mar 28, 2025

Fixes for TryGhost/Ghost#21721

Hi 👋🏻 this bug turned into a bit of a rabbit hole, I'm happy to split this into smaller PRs or make any other adjustments as needed. I found 3 separate, but connected problems:

  • Typing '@' in Firefox doesn't open the internal linking popover. This is because of timing, in Firefox the event is triggering before the editor state has updated. To solve this I've used editor.registerUpdateListener to catch the next update, and then immediately deregistered that listener. I explored and considered a few other solutions such as setTimeout, upgrading to Lexical 17.0 or higher and taking advantage of the editor.read method to force a state reconciliation, but this seemed like a better compromise with the existing code.
  • Crashing/infinite loops (Chrome and Firefox) - these are described in Link popup in editor not working in Firefox Ghost#22421 and I'm still not fully certain of the cause. Essentially, we have an update listener which tries to guarantee the cursor/selection never sits on the ZWNJNode, but rather in the SearchNode instead, but sometimes something else will move the selection back to the ZWNJNode and so it loops forever. My only solution here was to identify the situation by examining prevEditorState and avoid looping.
  • For the first character typed in Firefox, the cursor would jump backward. This happened because the AtLink node transform behaves just slightly differently if it finds [ZWNJNode, SearchNode, ExtendedTextNode] (Firefox) instead of [ExtendedTextNode] (Chrome). In Firefox, the existing node is deleted and text copied to another node - this causes the cursor to move left (before the text entered). I've updated the transform code to preserve any existing non-empty node if possible, fixing this issue.

I've also gratefully added the tests already written in the spike code.

In certain circumstances, mostly with Firefox, our plugin would adjust
selection from a ZWNJ to the adjacent search node, and something else
would adjust selection back to the ZWNJ and we would loop forever.

See TryGhost/Ghost#22421
@coderabbitai
Copy link

coderabbitai bot commented Mar 28, 2025

Walkthrough

The changes refactor the handling of the '@' character in the editor by introducing a new function, handleAtDetected. This function encapsulates the logic previously embedded within handleAtInsert, performing checks on the current selection and text node conditions, deleting the '@' character, creating the at-link node, and repositioning the cursor. Modifications have also been made to the handleAtBeforeInput function and the update listener to ensure that state changes are managed correctly and to prevent infinite loops. Additionally, a new suite of end-to-end tests for Firefox has been added, covering scenarios such as bookmark card visibility, metadata display, search functionality, URL query handling for various protocols (HTTP, anchor, relative, mailto), and at-link interactions including backspacing and pasting.

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

packages/koenig-lexical/test/e2e/internal-linking-firefox-test.js

Oops! Something went wrong! :(

ESLint: 8.57.0

ESLint couldn't find the config "react-app" to extend from. Please check that the name of the config is correct.

The config "react-app" was referenced from the config file in "/packages/koenig-lexical/.eslintrc.cjs".

If you still have problems, please stop by https://eslint.org/chat/help to chat with the team.

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
packages/koenig-lexical/test/e2e/internal-linking-firefox-test.js (1)

189-189: Empty test suite for Link toolbar.

The Link toolbar test suite is defined but contains no tests. This might be intended if there were no Firefox-specific issues with the link toolbar.

Consider either adding a comment explaining why this section is empty or removing it if no tests are planned.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 23047d3 and 8bccd30.

📒 Files selected for processing (1)
  • packages/koenig-lexical/test/e2e/internal-linking-firefox-test.js (1 hunks)
🔇 Additional comments (13)
packages/koenig-lexical/test/e2e/internal-linking-firefox-test.js (13)

1-3: Appropriate imports for testing the internal linking functionality.

The imports include all necessary utilities for the e2e tests, such as HTML assertion, editor focus, and initialization helpers.


4-18: Well-structured test setup with proper lifecycle hooks.

The test suite follows best practices by:

  • Using separate browser page for Firefox tests
  • Properly initializing with the internal linking lab feature enabled
  • Including appropriate beforeAll/beforeEach/afterAll hooks
  • Cleaning up resources after tests complete

19-58: Good test coverage for bookmark card default options.

The test thoroughly validates the bookmark card functionality by:

  • Checking visibility of the dropdown
  • Asserting the complete HTML structure matches expectations
  • Verifying that default options appear correctly

60-89: Comprehensive testing of metadata display on selected items.

The test effectively validates that:

  • Metadata is shown for the selected item
  • Metadata is removed when an item is deselected
  • Metadata appears for the newly selected item
  • Different metadata types (members only, specific tiers) are correctly displayed

91-101: Effective test for search highlighting functionality.

The test correctly verifies that search terms are highlighted in the results using the bold font style.


102-110: Important regression test for special character handling.

This test ensures the application doesn't crash when using regexp characters in search, which is particularly important given the PR's focus on fixing Firefox-specific issues.


112-186: Comprehensive tests for URL query handling across different protocols.

These tests methodically verify that URL queries work correctly for:

  • HTTP URLs
  • Anchor links
  • Relative paths
  • Mailto links

This is particularly valuable for ensuring consistent behavior across browsers.


191-236: Good test for at-linking default behavior.

The test properly verifies:

  • The at-linking node structure when typing '@'
  • The popup appears with default options
  • The placeholder text is correctly shown

238-270: Thorough test for at-linking search functionality.

The test effectively:

  • Validates the node structure during search
  • Waits appropriately for search results
  • Verifies highlighting of search terms in results
  • Checks the correct category header is shown ("Posts")

272-284: Important test for no-results handling.

This test correctly verifies that:

  • The "No results found" message appears when no matches exist
  • Pressing Enter with no results converts the at-link to plain text

286-313: Thorough backspace handling test.

The test effectively validates that:

  • Backspacing within the at-link input works correctly
  • Backspacing when the input is empty reverts to plain text
  • The DOM structure is correct at each step

This is particularly important given the PR's focus on fixing Firefox-specific cursor and selection issues.


315-350: Important test for bookmark creation via at-linking.

The test properly:

  • Types '@' followed by search text
  • Waits for search results
  • Selects and confirms a result
  • Verifies the bookmark card is created with correct content

352-371: Good Firefox-specific clipboard handling test.

The test correctly uses a Firefox-compatible approach for pasting content into an at-link node. The comment on line 356 is helpful in explaining why a different approach is needed for Firefox.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant