01 / Reproduce
Run the offline preview lab
Download the practice ZIP below and extract all three files into a fresh folder: preview.html, preview.js and README.md. Keep the JavaScript file beside the HTML file. Double-click preview.html, or open PowerShell in that folder and run:
Start-Process -FilePath .\preview.html
A current browser with JavaScript enabled is enough. There is no package installation, server, account or model download. The lab uses local files and makes no network requests.
Choose A button tag. The source string is <button type="button">Approve</button>. The left pane shows an Approve control. The right pane shows the angle brackets and tag text. Clicking the lab button does nothing: it has no handler, form or business operation.
Now select the other three fixtures. Our observed results:
Fixture HTML elements Text elements
button 1 0
emphasis 1 0
entity 0 0
lines 0 0
entity / HTML: Research & review
entity / text: Research & review
The entity case matters even when no control appears. The source contains the literal characters &. HTML parsing displays an ampersand; the text preview preserves the source characters. The two-line fixture stays on two lines in both panes because the lab deliberately uses white-space: pre-wrap.
02 / Inspect
Inspect the preview boundary
Open preview.js in a text editor. Inside renderSample, these are the two assignments:
htmlPreview.innerHTML = draft; // Demonstration only: fixed, inert fixtures above.
textPreview.textContent = draft;
MDN documents that innerHTML parses the assigned string as HTML. The lab's first assignment therefore creates an element from the button fixture. This is a demonstration line, not a preview implementation to connect to model responses. Its input is limited to four hardcoded strings.
For an ordinary div intended to display plain text, textContent sets textual content. It replaces the element's current children. Tags in the string remain text instead of becoming new page elements. That is the behavior the right pane demonstrates.
A request such as “return plain text” can describe what you want from a model. Your rendering choice still determines how the returned string enters the page. Inspect that handoff in a draft viewer, agent activity log or support-note preview you control.
03 / Adapt
Make it your own
In the lab's text-only preview editor, type this fictional note:
Please review <strong>before sending</strong>.
The literal tags should stay visible. No strong element should appear in that preview. Clear the editor and enter a different note: the current preview should be replaced, not appended. We checked both behaviors.
This is the complete editable-preview function from the supplied kit:
function renderCustom() {
customPreview.textContent = draftInput.value;
}
To adapt it, use a dedicated ordinary display element for your draft, and assign your response string to that element's textContent. The input event in the kit updates the display as you type. Keep application buttons outside the draft display, with their behavior defined by your own interface.
If your application needs paragraphs or other structure, create those known elements in application code with document.createElement, then put draft text inside them. If you need rendered Markdown or HTML, design and review that richer rendering policy separately. This exercise does not supply a sanitizer.
04 / Evidence
Keep the evidence in scope
We ran the extracted kit as local files in Chromium and WebKit at mobile and desktop viewport sizes. All four fixture strings, element counts, literal text, entity behavior, custom-note adaptation, empty replacement and line-preserving CSS passed. We observed no HTTP requests or page errors, and the source files stayed unchanged. These were automated browser checks with emulated viewport sizes, not physical-device tests.
OWASP recommends textContent for displaying untrusted text in the DOM. This guide applies that narrow boundary to a fictional AI draft preview. It does not make arbitrary output trustworthy or validate the draft's claims.
Use an ordinary display element. This is not guidance for script or style contents, attributes, links, event handlers, or text that you later reinterpret as HTML. We did not test malicious payload coverage, a production integration, rich-text sanitization, authorization, model compliance, accuracy or performance. A button appearing in a preview also does not establish that any action was authorized or executed.
05 / Sources
Read the primary sources
Checked November 27, 2026. The browser documentation supports the rendering distinction; the measured results above come from our authored kit.
- MDN: Element.innerHTML — HTML parsing and security considerations.
- MDN: Node.textContent — textual content and child replacement.
- MDN: Document.createElement — creating known elements in application code.
- OWASP: DOM-based XSS prevention — choosing text-oriented DOM operations for untrusted text.
06 / Take it with you
Get the practice and posting files
The practice ZIP contains the offline lab and instructions. The posting ZIP contains the original graphic, teaser, alt text and canonical article URL. Social content is prepared for manual posting; it is not scheduled or published.

