Tinymce 4.6.6 粘贴文本格式继承异常问题技术求助
Hey there, sorry to hear you're hitting this frustrating formatting snag with TinyMCE 4.6.6. Let's walk through what might be causing this and some potential fixes to try out:
Why This Might Be Occurring
TinyMCE's paste handling logic in older versions (like 4.6.6) has known quirks when it comes to style inheritance. When you paste content near bolded text, the editor might be incorrectly pulling the parent element's bold styling onto the pasted content, instead of respecting the original formatting of the text you copied.
Potential Fixes to Implement
Tweak Paste Configuration Settings
You can adjust TinyMCE's init options to force stricter preservation of original paste formatting. Add these lines to your editor setup:tinymce.init({ // ... your existing settings paste_retain_style_properties: "all", paste_auto_cleanup_on_paste: true, paste_preprocess: function(plugin, args) { // Wrap pasted content in a span to isolate its original styles const tempDiv = document.createElement('div'); tempDiv.innerHTML = args.content; const contentElements = tempDiv.querySelectorAll('*'); contentElements.forEach(el => { if (!el.style.fontWeight) { el.style.fontWeight = 'normal'; } }); args.content = tempDiv.innerHTML; } });The
paste_preprocesshook here explicitly sets unbolded content to have a normal font weight, preventing it from inheriting the surrounding bold style.Leverage the "Paste as Plain Text" Workaround
For immediate relief, you can guide users to use TinyMCE's "Paste as Plain Text" option (found in the paste dropdown menu). After pasting plain text, they can manually reapply the underline formatting. While this isn't a permanent fix, it's a quick way to avoid the style inheritance issue.Upgrade to a Newer TinyMCE Version
Version 4.6.6 is quite outdated (released back in 2017), and many paste-handling bugs have been resolved in subsequent updates. If your project allows, upgrading to the latest stable release of TinyMCE 4.x (or even migrating to 5.x/6.x) would likely eliminate this issue entirely, as the paste engine has been significantly refined in newer versions.
Additional Context
You noted this doesn't happen in MS Word or other editors, which makes sense—those tools prioritize preserving the copied content's original formatting over inheriting styles from the target location. TinyMCE's older default behavior leans the other way, which is why we need to adjust its settings.
Hope these suggestions help you get this sorted out! Feel free to ask if you need help implementing any of these fixes.
内容的提问来源于stack exchange,提问作者Abhishek Kumar

