Question presented to candidate:
"Build a basic rich text editor for release notes: a toolbar with Bold, Italic, Underline, a bulleted list and a numbered list, plus Ctrl+B, Ctrl+I and Ctrl+U. Do not use document.execCommand, and the HTML it produces must be safe to store and show to other users."
Requirements:
- A
contenteditablearea that starts with a few paragraphs, and a toolbar whose buttons showaria-pressedfor the current selection. - Bold, Italic and Underline toggle on the selection; with only a caret, they format the word the caret is in.
- A selection that is partly bold becomes fully bold; toggling again removes bold.
- List buttons turn the selected blocks into list items, switch between list types, and turn them back into paragraphs.
- The selection survives every command, and clicking a toolbar button does not steal it.
- Paste inserts plain text only. The editor emits normalised HTML that contains only
p,ul,ol,li,strong,em,uandbr.
What a strong answer should cover:
- Why
execCommandis out: deprecated, inconsistent output across browsers, and no control over the markup. - A small document model (blocks of text runs with marks) with pure commands, and a parse step from the DOM and a render step back to HTML.
- Mapping the DOM selection to model positions (block index and character offset) and restoring it after re-rendering.
- Not letting the framework render children into the editable element, so typing and framework patches do not fight.
- Sanitising by construction: parse keeps only known tags, and paste is plain text.
Clarifying questions expected:
- "Which formats are needed, and is nesting such as lists inside lists in scope?"
- "Should paste keep formatting from Word or Google Docs, or is plain text acceptable?"
- "Do we need undo and redo beyond what the browser gives us?"
Code / implementation expected: Yes. The model, the commands and selection mapping, and the toolbar UI.