DEV Community

Cover image for Render the answer summary as text instead of HTML
The Doctor
The Doctor

Posted on

Render the answer summary as text instead of HTML

The painting planner stores a surface selection such as Peeling or visible damage. The current choices are predefined, but the summary renderer still assigns labels and values with textContent. That keeps display data in the DOM as text. The distinction becomes more consequential if a later release allows custom room names or notes.

Keep the renderer’s contract small

The shared script clears the summary and uses a definition list. For each pair it creates a dt for the term and a dd for the answer. The browser handles text and markup differently. Building the nodes explicitly gives the renderer no reason to interpret a label as element syntax.

const term = document.createElement('dt');
const detail = document.createElement('dd');
term.textContent = label;
detail.textContent = value;
summary.append(term, detail);
Enter fullscreen mode Exit fullscreen mode

Check semantics and replacement

For the current browser path, choose Interior, A few rooms and Peeling or visible damage. After finishing, scope to the summary and assert that the term Surface is followed by the selected text. Return to the question and revise the answer. Finish again and assert that there is still one Surface row. That exercises the clear-and-rebuild logic alongside the answer state.

For any future free-text variant, add a renderer test with a plain string containing angle brackets, such as Room . Assert that the complete value appears as text and that the dd contains no new elements. Keep this check at the output boundary. A future input length limit or persistence layer would have separate tests and would not change how the summary should display text.

Separately test that old rows are removed when the summary is rebuilt. It is possible to render perfectly safe text and still display a stale answer. The revision check should fail if a future change removes replaceChildren.

Perth Quotes painting planner is the live preview. Online enquiries are closed.

Top comments (0)