jQuery UI Widgets › Forums › General Discussions › What is the best method to sanitize pasted content from Word in jqxEditor and st

This topic contains 1 reply, has 2 voices, and was last updated by  admin 3 weeks ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author

  • vitorio
    Participant

    Hi everyone, I am working on a SaaS settings panel with Editor.

    I am currently trying to solve this: What is the best method to sanitize pasted content from Word in jqxEditor and strip unwanted inline styles?

    So far, I can reproduce it consistently in a small demo, but state gets out of sync after user interactions.

    What approach would you recommend here?


    admin
    Keymaster

    Hi vitorio,

    For jqxEditor, I’d recommend handling pasted Word content at the paste boundary, rather than trying to clean up the editor’s HTML after every interaction.

    The important part is to keep the editor’s internal state and the displayed content synchronized. If you manipulate the DOM directly after a paste, the editor may retain a different internal representation, which can cause problems when users continue editing, undo changes, or switch between formatting options.
    Recommended approach
    1. Intercept pasted content before it enters the editor, if the version of jqxEditor you use exposes an appropriate paste event or hook.
    2. Sanitize the incoming HTML with an HTML parser and an allowlist of permitted tags and attributes. Remove unwanted Word-specific markup, such as Mso* classes, mso-* CSS properties, and unnecessary inline styles.
    3. Preserve intentional formatting such as bold, italic, lists, links, and headings, if those are supported by your application.
    4. Let the editor apply the cleaned content through its supported API, rather than changing its editable DOM behind its back.
    5. Test undo/redo and subsequent edits, not just the initial paste. These are often where state synchronization problems become visible.
    Avoid using a single regular expression to sanitize HTML. Word-generated HTML can contain nested elements, conditional markup, and CSS that is difficult to remove safely with regex alone.
    Why state gets out of sync
    The most likely cause is that the cleanup runs after the browser or editor has already processed the paste. Directly replacing innerHTML can bypass the editor’s normal change tracking and selection management.

    For a minimal reproduction, I’d check these cases:
    – Paste Word HTML, then type immediately.
    – Paste, then undo and redo.
    – Select existing text and paste over it.
    – Paste a list or formatted table.
    – Trigger toolbar commands after pasting.
    – Read the editor’s value after each operation and compare it with the visible content.
    The jqxEditor package is described as supporting paste cleanup, so I’d first verify the available API in your specific version before adding a custom event handler

    Best regards,
    Peter

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.