Skip to content

Conversation

@yashjawale
Copy link
Contributor

@yashjawale yashjawale commented Jul 21, 2025

What?

Closes #70542

Why?

This PR handles pasted links to avoid nested anchor tags, which earlier led to invalid HTML markup

How?

While pasting links, if the target is already enclosed in <a tags, anchor tags are stripped from the copied text to avoid pasting anchor tags again

Similar approach can be used to tackle other HTML tags, but since anchor tags lead to invalid HTML, it is targetted first

Approach will be expanded based on discussion in #70542

Testing Instructions

  1. Open a new post
  2. Create two linked text
  3. Copy one linked word
  4. Paste it in middle of the other linked word
  5. Observe markup in Code Editor

Screenshots or screencast

Before

Screen.Recording.2025-08-04.at.7.02.43.PM.mov

After

Screen.Recording.2025-08-04.at.7.05.42.PM.mov

@t-hamano t-hamano added [Type] Bug An existing feature does not function as intended [Feature] Paste labels Jul 23, 2025
- Replace DOM-based approach with string-level regex preprocessing
- Add shared flattenNestedTags utility to eliminate code duplication
- Fix both external paste (pasteHandler) and internal copy-paste (rich-text event listeners)
- Add comprehensive test coverage for utility function and integration scenarios
- Handle nested strong and anchor tags before DOM parsing to prevent browser auto-correction issues
- Add logic to detect when pasted content contains links
- Filter out link formats from activeFormats when pasting linked content
- Prevent nested <a> tags when pasting links into existing links
- Add null check and validation in flattenNestedTags utility
- Export flattenNestedTags from @wordpress/blocks for reuse
- Remove debug console logging
- All tests passing (16/16)
- Remove unnecessary complex string-based regex processing
- Remove flattenNestedTags function and related code
- Keep only the essential fix: filter link formats from activeFormats when pasting linked content
- This prevents nested <a> tags with minimal code changes
- All existing tests still pass
@yashjawale yashjawale marked this pull request as ready for review August 4, 2025 13:42
@yashjawale yashjawale requested a review from ellatrix as a code owner August 4, 2025 13:42
@github-actions
Copy link

github-actions bot commented Aug 4, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: yashjawale <yashjawale@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

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

Labels

[Feature] Paste [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RichText: Combine nested strong tags to one

2 participants