Clean HTML

What users paste is rarely what you want to store. VEditor turns office markup into plain HTML in the browser, and the PHP sanitizer checks the result against the same rules on save.

A Word document pasted into VEditor: numbered and bulleted lists, bold text and a table, without office formatting

What happens when you paste

  • Word lists

    mso-list paragraphs become nested ul and ol with the right numbering type and start number. Numbered headings become list items too.

  • Google Docs

    Lists nested directly in lists are repaired and dir="ltr" noise is dropped.

  • Tables from Word and Excel

    Paragraphs in cells become line breaks, tfoot joins the body, nested tables are flattened into the cell text and colgroup is removed.

  • Formatting from styles

    Bold, italic, underline and strikethrough set with <span style> become strong, em, u and s.

  • Dangerous addresses

    javascript:, data: and every scheme other than http, https, mailto and tel are removed from links, images and media, also when hidden with control characters.

  • Scripts and handlers

    script, style, on* attributes and unknown elements never reach the content.

  • Embeds

    Only YouTube (no-cookie) and Vimeo iframes inside a figure, with sandbox and a referrer policy.

  • Your styles stay

    Classes from your styles option are allowed; random office classes and inline CSS outside the whitelist are not.

  • Images from the clipboard

    Image files are uploaded through your upload function instead of being embedded as base64. Images copied from Word together with text are left out.

  • Spaces and empty blocks

    In the saved HTML a single non-breaking space between words becomes a normal one. Empty paragraphs at the end, empty captions and the line breaks Google Docs puts between paragraphs are dropped.

One content model, two sanitizers

The editor’s schema in JavaScript and `HtmlSanitizer` in PHP implement the same algorithm and the same whitelist.

  • Same rulesTags, attributes with their patterns, classes and CSS properties with allowed values.
  • Byte-identical outputValid editor content passes the PHP sanitizer unchanged, so a difference means something was tampered with.
  • The server decidesCleaning in the browser is a convenience. HtmlSanitizer on the server is the security boundary, built on DOMDocument with no libraries.
  • Styles in one placeThe same styles array feeds the editor’s “Style” list and allowStyles() in PHP.
  • Size and encodingLengthException above maxLength (1 MB by default), invalid UTF-8 cleaned before parsing.
  • Integrations includedsanitize() in the Laravel, Symfony and Yii2 integrations uses your styles automatically.

Sanitize on every save

One class, no configuration needed for the editor’s default content.

save.php
use VEditor\HtmlSanitizer;

$styles = [['name' => 'Lead', 'element' => 'p', 'classes' => ['lead']]];

try {
    $clean = (new HtmlSanitizer())
        ->allowStyles($styles)          // the same array as the editor's styles option
        ->sanitize((string) $_POST['content']);
} catch (LengthException $e) {
    // larger than maxLength (1 MB by default)
}

Every option of the sanitizer and the editor schema is described in the documentation.

Try it in your browser

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