Vanilla Breeze v0.7.4

Vanilla Breeze v0.7.4 released

0.7.4 gives the Ambient pack weather. A page names one feed URL on its own site, and the pack sets data-weather from it, uses its sunrise and sunset for dawn and dusk, and never shows a stale observation. Helpers for producing the feed from Open-Meteo are included. Ambient p…

Read the release notes

contenteditable

Let users edit an element's content in place, as rich text or plain text only. Covers the roles and labels you must add, sanitising saved HTML, and VB's markdown-editable enhancement.

Overview

The contenteditable attribute makes an element's content editable in place. The user can click into it, type, delete and paste, and the DOM changes as they do. It is the base of inline editors, note fields and rich-text editors.

An editable element is not a form control. It has no name or value, isn't submitted with a form, has no built-in validation, and gets no textbox role. You add those parts yourself.

Applies to: any HTML element (it is a global attribute).

Values

ValueMeaning
true, or empty (contenteditable / contenteditable="")Editable, rich text. Pasted HTML keeps its formatting, and some browsers apply formatting shortcuts.
plaintext-onlyEditable, text only. Pasted content arrives as plain text and formatting shortcuts do nothing.
falseNot editable, even inside an editable ancestor.
Absent, or an invalid valueInherit: editable if the parent is.

Editability is inherited: every descendant of an editable element is editable unless it sets contenteditable="false". In script, el.contentEditable reads and sets "true", "false", "plaintext-only" or "inherit", and el.isContentEditable reports the result after inheritance.

Examples

Inline markdown with data-markdown-editable

VB's data-markdown-editable turns an element into an inline markdown field: raw markdown while focused, rendered and sanitised HTML after blur.

Controlling edits with beforeinput

document.execCommand() is deprecated. To control what an edit does, listen for beforeinput: its inputType says what is about to happen (insertText, insertParagraph, formatBold, insertFromPaste…), and preventDefault() cancels most of them.

Saving the content

Read textContent (or innerText, which keeps line breaks) for plain text. Only read innerHTML when you need the formatting, and treat it as untrusted (see Security).

In Vanilla Breeze

  • No styling. VB has no [contenteditable] rule, so an editable region looks like the element it is. Give it a visible edge, as the demo's own CSS does. Focus shows the browser's default outline.
  • data-markdown-editable (src/utils/markdown-editable-init.js, loaded on demand) sets contenteditable="true", role="textbox" and aria-multiline="false" on the element. While focused it shows the raw markdown and sets data-md-editing (monospace text and an outline, from src/utils/markdown-editable.css). On blur it renders the inline markdown, sanitises it, and stores the source in data-markdown-value. It fires markdown-editable:change with { value, html }. On a <textarea> or <input> it adds a preview <output> instead.
  • Sanitising. Rendered markdown goes through VB's internal sanitiser (src/lib/sanitize-html.js), which inserts nodes rather than re-parsing a string. It isn't a public export; for your own editors see Security.
  • Inserting at the caret. <emoji-picker for="id"> inserts into a contenteditable target at the caret and fires an input event.
  • Keyboard shortcuts stay out of the way. VB's shared hotkey binder ignores keys typed into contenteditable regions (and inputs) unless a binding is global, so data-hotkey single-key shortcuts don't fire while someone types.
  • For whole documents, use <markdown-editor>, which wraps a real <textarea> and so takes part in forms.

Accessibility

  • No implicit role. contenteditable doesn't change an element's role: an editable <p> is still announced as a paragraph, with an editable state. For a field-like region add role="textbox", plus aria-multiline="true" when Enter adds a line.
  • Name it. A textbox needs an accessible name: aria-labelledby pointing at a visible heading or label, or aria-label. A <label for> doesn't work, because the element isn't labelable.
  • Editable regions are focusable and in the tab order. Don't add tabindex.
  • Add aria-required, aria-invalid and aria-describedby yourself if the region acts as a required or validated field.
  • Keep the editing controls keyboard-reachable. Formatting shortcuts differ between browsers and platforms, so offer buttons too.
  • Prefer <textarea> (with data-grow) when you don't need rich text: it brings a role, a label, form submission and validation for free.

Security

  • Pasted HTML is untrusted. With contenteditable="true" a user can paste markup from anywhere, and innerHTML hands it back to you. Storing it and rendering it to other people is a cross-site scripting risk.
  • Prefer plaintext-only and textContent / innerText for user text.
  • When you must keep HTML, sanitise it before you store it and again before you render it. Element.setHTML() parses and sanitises in one step (Chrome 146, Firefox 148; not in Safari as of November 2026); elsewhere use a maintained library such as DOMPurify. Never write user HTML back with innerHTML.

Browser support

contenteditable="true" works in every browser. plaintext-only: Baseline 2025 (Chrome 51, Safari 5, Firefox 136).

Gotchas

  • Not submitted. Copy the content into a hidden input on submit, or use a form-associated custom element.
  • Markup varies by browser. Enter in rich mode may create a <div>, a <p> or a <br>, and pasted content brings its own elements and styles. Normalise what you save.
  • Empty regions collapse. An element with no text has no height. Give it a minimum block size.
  • Placeholders aren't built in. :empty only matches with no child nodes at all, and browsers often leave a <br> behind after deleting.
  • Child components stay editable. Mark embedded widgets contenteditable="false" so the user can't type into them.

Related

Which to choose

  • <textarea> + data-grow — plain text in a form. The default choice.
  • contenteditable="plaintext-only" — plain text edited in place, inside the page's own typography (titles, captions, notes).
  • data-markdown-editable — short inline text with bold, italic, code and links, stored as markdown.
  • <markdown-editor> — longer documents, with a toolbar and preview, submitted with a form.
  • contenteditable="true" — real rich text, when you are prepared to normalise and sanitise the HTML.
↑