Project

General

Profile

Actions

Bug #23169

closed

Copy and pasting an HTML page into a classic block consistently loses first anchor

Added by Raffi Khatchadourian 4 months ago. Updated 4 months ago.

Status:
Resolved
Priority name:
Low
Assignee:
-
Category name:
WordPress (misc)
Target version:
-
Start date:
2025-08-14
Due date:
% Done:

0%

Estimated time:
Deployment actions:

Description

I have a problem related to the problem outlined at https://stackoverflow.com/questions/64990808/switching-between-wordpress-text-visual-editor-makes-anchor-tags-disappear.

When I copy and paste an HTML page into a classic block, I always lose the first anchor. I always have to go back, edit the block in HTML, and manually add the anchor back. It's only the first one; all the others are fine.

The problematic block is https://khatchad.commons.gc.cuny.edu/wp-admin/post.php?action=edit&post=358#block-52d9abe6-634e-4a9b-9a98-6007fc7f287a. I've attached the HTML page that I copy and paste into that block.


Files

all.html (33.4 KB) all.html HTML page I copy into the classic block. Raffi Khatchadourian, 2025-08-14 10:16 AM
Actions #1

Updated by Boone Gorges 4 months ago

Hi Raffi - This is unfortunate, but I don't know that there's anything we can do to fix it. It's likely a behavior caused by the sanitization features of TinyMCE, which is beyond the scope of things that the Commons team is able to address. Moreover, the Classic block is not a commonly used tool on the site, so we don't have the resources to devote to a deep dive into the internals.

I realize that you're using HTML that's generated by another application, and ideally you'd be able to use it directly without modification. But have you considered trying the workaround from that StackOverflow thread, namely adding a non-breaking space as the content of the anchor node?

Actions #2

Updated by Raffi Khatchadourian 4 months ago

Hi Boone,

Sorry, I read that post a little more carefully. While it is a similar problem, it's not exactly the same. Specifically, my problem happens even without switching between visual and non-visual editor modes. And, this is something I have only noticed with the block editor (I need the block editor because I have other blocks on that page). It's quite off that only the first one isn't retained; all of the others are fine. And, the reason I use the "classic" block is that, last I checked, the paragraph block doesn't retain any anchors.

Actions #3

Updated by Raffi Khatchadourian 4 months ago

Yup, just tried it again. The paragraph block has no anchors at all.

I also believe I use the "classic" block here also because this page was originally a "classic" page.

Actions #4

Updated by Boone Gorges 4 months ago

I see. As noted, we don't have the resources to do a deep dive into the way that the Classic Block's implementation of TinyMCE works.

From what I can see, the "official" way to use anchors in the block editor is to give IDs to block-level elements, rather than using <a name="..."> tags. See eg https://www.inmotionhosting.com/support/edu/wordpress/gutenberg/add-anchor-links-with-gutenberg-wordpress/. You could thus write some sort of post-processing script that transforms your generated HTML so that this:

<p><a name="Khatchadourian2025"></a>

becomes this

<p id="Khatchadourian2025">

I'm afraid this is the best we can offer at this time.

Actions #5

Updated by Raffi Khatchadourian 4 months ago

Ah yes. Like you said, that HTML comes from another (older) tool. OK thanks, I'll try that!

Actions #6

Updated by Raffi Khatchadourian 4 months ago

That seems to work. Thanks!

Actions #7

Updated by Raffi Khatchadourian 4 months ago

My command, just for reference:

sed -i 's@<p><a name=\(.*\)></a>@<p id=\1>@' all.html
Actions #8

Updated by Raffi Khatchadourian 4 months ago

  • Status changed from New to Resolved
Actions

Also available in: Atom PDF