How to paste from Word into a CMS and get clean HTML

To paste text from Word into a CMS without unnecessary code, you need to keep the document’s structure – headings, lists, tables, links and emphasis – and remove everything Word adds for its own layout: Mso classes, mso- styles, fonts, colours and empty tags. This is best done by an editor that c…

This post is also available in: Polski

To paste text from Word into a CMS without unnecessary code, you need to keep the document’s structure – headings, lists, tables, links and emphasis – and remove everything Word adds for its own layout: Mso* classes, mso-* styles, fonts, colours and empty tags. This is best done by an editor that converts Word formatting into semantic HTML at paste time, while the server still checks the result before saving. Pasting “as plain text” is safe, but it loses the structure, which the editor then has to rebuild by hand.

TL;DR

  • Word puts HTML on the clipboard that describes how the document looks, not what it means: MsoNormal classes, mso-* properties, fonts, non-breaking spaces and paragraphs pretending to be lists.
  • Keep the semantics (h2, ul/ol, table, a, strong, em) and leave the appearance to the site’s stylesheet.
  • “Paste as plain text” is an emergency exit, not a method – it loses lists, links and headings.
  • Cleaning in the browser is a convenience; the security boundary is the server-side sanitizer.
  • Check the result in the source view and after reopening the saved article, not just “by eye” in the editor.

What does Word put on the clipboard?

Word describes a document with editing and printing in mind. When you copy a fragment, the clipboard receives several versions of the same content, including HTML, which the browser passes on to the editor. This HTML recreates how the document looks in Word, so it typically contains:

  • MsoNormal, MsoListParagraphCxSpFirst and similar classes;
  • style declarations with mso-* properties, font family and size, margins in points;
  • conditional comments (`) and tags from the Office namespace, e.g. <o:p></o:p>`;
  • lists written as ordinary paragraphs, with the number typed into the text and the level hidden in mso-list;
  • non-breaking spaces (&nbsp;) between words and empty paragraphs used as spacing.

An illustrative example (simplified, not a literal clipboard dump):

<p class="MsoNormal" style="margin-bottom:6.0pt;line-height:115%">
  <span style="font-family:Calibri;mso-bidi-font-weight:bold"><b>Important information</b></span>
  <span lang="PL">for&nbsp;the editors</span><o:p></o:p>
</p>
<p class="MsoListParagraphCxSpFirst" style="text-indent:-18.0pt;mso-list:l0 level1 lfo1">
  <span style="mso-list:Ignore">1.</span>First step
</p>

Code like this can be displayed, but it is hard to style, and every subsequent edit adds more layers. The page template no longer controls the appearance, because inline styles win over CSS.

What to keep and what to remove

The rule is simple: whatever carries meaning stays, whatever describes the look in Word goes.

Element from WordDecisionTarget HTML
“Heading 1–3” styleskeeph2, h3 (level consistent with the page hierarchy)
Bulleted and numbered listskeep and rebuildul/ol with li, nested, with a start number
Bold, italicskeep as semanticsstrong, em
Linkskeep, check the URLsa href with an allowed scheme
Tableskeep the structuretable, rows, cells, headers
Font family, size and colourremoveappearance from the site’s CSS
Mso* classes, mso-* styles, o:premove–
Empty paragraphs, &nbsp; between wordsremove or normalisespacing from CSS, ordinary spaces
Images from the clipboardupload as filesimg src with a file URL, not base64

A bold paragraph in a larger font is sometimes a subheading in the document and sometimes just decoration. Automation cannot guess which, so good editorial practice is to write in Word using styles (“Heading 2”, “Numbered list”) rather than manual formatting. The details of mapping headings and lists – including levels, numbering and mso-list – are covered in Word → CMS: how to keep lists and headings.

Why “paste as plain text” is not enough

Pasting as plain text removes the code problem entirely, but the structure goes with it: lists become paragraphs, links become bare text, headings become ordinary sentences. For a short note this is acceptable. For a how-to with numbered steps, a table of parameters and several links, the editor will spend more time rebuilding the structure than writing.

It makes sense in two situations: when the source document is so damaged that its structure is worthless, and when you are pasting a fragment into a field that accepts only text anyway. In all other cases, an editor that cleans pasted HTML instead of throwing it away is the better choice.

How to paste from Word into a CMS step by step

  1. Prepare the document. Mark subheadings with heading styles and lists with Word’s list buttons. Remove manual spacing made of empty paragraphs.
  2. Copy a representative fragment and paste it into the editor. For the first test, do not start with an entire 40-page report.
  3. Look at the source view. Lists should have <ol>/<ul> and <li>, subheadings h2/h3, and the code should contain no class="Mso…" or style="mso-…".
  4. Fix heading levels. If the page title is h1, the content starts at h2, without skipped levels.
  5. Check images and links. An image should point to a file on the server and have alternative text; a link should have the right URL, not just a correct label.
  6. Save, reopen and compare. Only the HTML after saving and server-side sanitization is what readers will get.

Tables from spreadsheets have their own pitfalls – merged cells, headers, widths – described in the guide Excel tables in a CMS. Google documents behave differently from Word; see how to clean HTML from Google Docs.

Example: HTML before and after cleaning

The same fragment after proper cleaning:

<p><strong>Important information</strong> for the editors</p>
<ol>
  <li>First step</li>
</ol>

The classes, styles, conditional comments and non-breaking space are gone, while the bold text and the list remain as tags. The paragraph’s appearance, spacing and numbering are now styled by the site’s CSS, so the same text looks right in any template.

Cleaning on the developer side: a parser, not regex

It is tempting to “clean up Word” with a few regular expressions that remove class="Mso…" and style="…". That is fragile: HTML is a tree, not text, so global substitutions break nesting, leave orphaned tags or let through attributes written in an unusual way. A better pattern is:

  1. normalisation – transforming the structure (mso-list paragraphs → lists, a span with bold → strong) on the DOM tree;
  2. sanitization – passing the result through an allowlist of elements, attributes and URL schemes.

These are two different goals and it is worth keeping them separate, so that you can establish why something disappeared. Specific transformation rules and a regression test set for importing from Word are described in Word → HTML transformation rules.

Most importantly: cleaning in the browser does not replace checks on the server. Anyone can send arbitrary HTML to the save endpoint, bypassing the editor, so the PHP-side sanitizer must always run – more in the guide how to secure HTML from a WYSIWYG editor in PHP.

How to avoid cluttering the content again

Cleaning on paste solves the problem once. To keep the code from accumulating clutter with subsequent edits, limit what the editor can add:

  • instead of arbitrary colours and fonts, provide a short list of approved styles based on CSS classes;
  • allow the same list of classes in the sanitizer, so that the editor and the server share one content model;
  • do not allow free-form style attributes – alignment or indentation can also be expressed with a class.

We write more about keeping code consistent across the whole site in how to keep HTML clean and consistent in a CMS.

Checklist after pasting from Word

  • No class="Mso…", style="mso-…", <o:p> or conditional comments in the source.
  • Lists are real ul/ol, with nesting and start number preserved.
  • Headings have logical levels without skips.
  • Links point to the right URLs; the link text describes the target.
  • Images are files on the server, not base64; they have alternative text or a deliberately empty alt.
  • Tables have headers; there are no nested tables inside cells.
  • The result after saving and reopening is the same as in the editor.

How VEditor handles it

VEditor cleans content pasted from Word in the editor itself. Word list paragraphs (mso-list) become real nested ul/ol lists with a start number and letter or Roman numbering for sublists. Bold, italics, underline, subscript and superscript become tags, and formatting written as <span style> is turned into strong, em, u and s. Fonts, colours, Word classes and empty tags are removed; text alignment is kept. Single non-breaking spaces between words become ordinary spaces in the saved HTML. Empty paragraphs used as spacing in Word remain as empty paragraphs, so it is worth removing them before copying.

Tables from Word keep their structure: paragraphs in cells become line breaks, nested tables are flattened to the cell’s text, and colgroup is removed. An image pasted from the clipboard as a file is sent through the configured upload function rather than embedded as base64; without it, the image is not inserted. Images copied from Word together with text (pointing to local files or base64) are skipped – they are added separately. The toolbar also has a “Paste as plain text” toggle for situations where the structure is not needed.

On the server side, the PHP HtmlSanitizer applies the same content model as the editor: valid content from the editor passes through unchanged, and anything the editor would not generate – scripts, event handlers, javascript: links, styles outside the allowlist – is removed. Custom styles from the “Style” list feed the same allowlist. The accessibility review panel will additionally point out images without alternative text, skipped heading levels and tables without headers. This does not mean every document will keep its Word appearance – the goal is a predictable, semantic structure.

FAQ

Is pasting as plain text safer?

It gives the simplest result, but security is provided by the server-side sanitizer either way. Plain text makes sense for short notes; for longer material it costs you lists, links and headings.

Should the article on the site look like the Word document?

It does not have to. The site has its own typography and layout. What matters is that the meaning is preserved: the heading hierarchy, the order of steps and the emphasis.

What should be done with images pasted from the clipboard?

Upload them as files to controlled storage, check their type and size, and leave the file URL in the HTML. Base64 images bloat the database and bypass upload checks.

How can I check that cleaning has not lost any content?

Compare the text, the number of links, and the structure of lists and tables before pasting and after saving. A good test is a document with a three-level list, numbering starting from 5, a table and a link.

Will Word footnotes survive pasting?

Do not count on it. Footnotes are best moved deliberately – as a “Footnotes” section at the end of the text with links to anchors – or turned into ordinary links.

Try VEditor in practice

The quickest way to evaluate the cleaning is to paste your own Word document – with multi-level lists and a table – into the editor and look at the source view. Then it is worth running the same HTML through the sanitizer and comparing the result.

Clean HTML, accessibility and SEO

How to clean HTML copied from Google Docs

HTML copied from Google Docs contains a style on almost every fragment of text, a <b id="docs-internal-guid-…"> wrapper around the whole thing, and dir, role and aria-level attributes. Cleaning it means removing that wrapper, turning styled span elements into strong and em, keeping headings, list…

AI in the content editor

How to integrate OpenAI with a WYSIWYG editor?

To integrate OpenAI with a WYSIWYG editor, you need an endpoint on your own server that accepts an action name and text from the editor, checks permissions, adds the API key and sends a cleaned result back to the preview. The OpenAI key never reaches the browser, and you treat the model’s respons…

HTML security

Why filtering HTML in the browser is not enough

Because the server does not know whether a request came from your editor. Anyone who can save content can also send arbitrary HTML with their own HTTP client, a modified form or another endpoint – and then the JavaScript filter never runs at all. Cleaning in the browser improves convenience and c…

Try it in your browser

The demo has the full toolbar and shows what the PHP sanitizer returns for your content.