Bug #23169
closedCopy and pasting an HTML page into a classic block consistently loses first anchor
0%
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
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?
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.
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.
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.
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!
Updated by Raffi Khatchadourian 4 months ago
My command, just for reference:
sed -i 's@<p><a name=\(.*\)></a>@<p id=\1>@' all.html
Updated by Raffi Khatchadourian 4 months ago
- Status changed from New to Resolved