Engineering

Why your editor changes the HTML you give it

Load HTML, change nothing, read it back, and it differs. Four separate causes: the browser's parser, the editor's model, the sanitizer and the serializer.

6 min read

You load some HTML into a rich text editor, change nothing, and read it back. The result is different. A<div> became a paragraph, an attribute is gone, or a colour is written another way. This is the most common complaint about WYSIWYG editors, and it has four separate causes. Knowing which one you hit tells you whether to change a setting, change your content, or keep that content out of the editor.

1. The browser repairs the markup first

An editor works on a live document, so your HTML goes through the browser’s parser before the editor sees it. The parser follows the HTML standard, and the standard says how to repair markup that breaks the nesting rules. A paragraph cannot contain a block element, so the parser closes the paragraph early:

Code example
<!-- what you loaded -->
<p>Intro <div>boxed text</div> more</p>

<!-- what the parser built -->
<p>Intro </p><div>boxed text</div> more<p></p>

The same happens with a missing closing tag, a table row with no <tbody>, and a list item outside a list. No editor setting changes this, because it happens before the editor runs. The fix is valid markup in the content.

2. The editor fits content into what it can edit

Every editor has a set of structures it knows how to edit: paragraphs, headings, lists, tables, links, images and so on. Content outside that set is a problem for it. Where should the cursor go inside an element the editor has never heard of? What should Enter do there? Editors answer in one of two ways.

Neither is wrong. If your content has custom elements, classes or data attributes that must survive, test that exact content in the editor before you choose it, and look for the setting that declares extra allowed elements and attributes.

3. The sanitizer removes what can run

An editor that accepted every tag would be a way to store an attack. So editors remove script elements, event handler attributes such as onclick, and javascript: addresses. Many also remove<iframe>, <form> and <style> unless you allow them. If a tracking snippet or an embed disappears, this is usually the cause.

Do not turn the sanitizer off to keep one embed. Allow the one element or the one trusted source you need, and still sanitize on the server, because a request can skip the browser. The stored XSS article covers the server side, and the content security demo shows what a sanitizer stops.

4. The same document is written back differently

When you read the content back, the document is turned into text again. Many different strings describe the same document, and the serializer picks one. These differences change nothing on the page:

What to do about it

  1. Do not compare HTML as strings. To detect a real edit, use the editor’s change event, or compare after loading both versions through the same editor.
  2. Round-trip your real content before you commit. Load a sample of stored documents, read them back without editing, and review the differences. Sort each one into the four causes above.
  3. Declare what must survive. If custom elements, classes or attributes matter, allow them in the editor’s configuration and in the server sanitizer, with the same list in both.
  4. Keep non-editable content out. A full page template with scripts and a layout is not rich text. Edit the text regions, and assemble the page on the server.
  5. Store what the editor produces. Once content has been through the editor, it is stable: loading and saving it again should not change it. Check that it is, and report it to the vendor if it is not.

Pasted content is a fifth source of surprises, with its own rules: see cleaning pasted HTML. For what to store and where, see how to store rich text.


RichTextEditor is a perpetual-licence JavaScript editor — one purchase, self-hosted, no metered editor loads. Download the evaluation or see how it compares.