Trending

World technology news

→
Cloning independent maintenance rows with the HTML5 template element
Software Engineering · 2014
Software Engineering2014

Cloning independent maintenance rows with the HTML5 template element

Use an HTML template to declare the shape of a maintenance row. A browser test verifies independent clones, an untouched template and a description that stays plain text.

Archive retrospective · Topic year 2014

Developer's JournalPublished: Dec 4, 20266 min read
Reading progress0%
In this article
  1. A common shape for a maintenance record
  2. Copying the fragment and populating it
  3. Running a two-record browser fixture
  4. Keeping markup and maintenance data distinct
  5. Maintaining the row during later changes

A common shape for a maintenance record

A maintenance panel receives a cable report and a filter inspection. Each has an independent identifier and description, but both need the same list structure, a visible code and selectable prose. A designer should be able to inspect that structure in HTML before records are available. The W3C HTML5 Recommendation dated 28 October 2014 describes the template element for declaring markup intended for later use. The browser parses its contents into a DocumentFragment accessible through the content property. The list item inside that fragment does not appear in the maintenance list until the application provides an instance. This makes one part of the design review more concrete: the template defines the elements and their relationships, while the application decides what each slot should say. Our example uses a native ordered list and plain browser APIs. The same approach can accommodate an interactive row, which introduces its own focus and accessible naming requirements.

Copying the fragment and populating it

The template element and its content serve different roles. In this fixture, document.importNode makes a deep copy of the content fragment in the current document. It includes each descendant and the attributes used to find the code and description. A separate clone is created for every record. When the fragment is appended, its child nodes move into the list, and the fragment can no longer be treated as a second filled row. Keeping data slots as data attributes avoids copying one document-wide identifier onto every instance. A real row that has a label and input must generate a distinct ID and update the label reference together. The destination still determines structural requirements. Here the ordered list must contain list items, so an extra wrapper inside the template would change the intended structure. The browser check reads the direct children of the final list, which exposes that mistake even if styling seems to hide it.

Frontend data and event flowConceptual sketch · summary of the article's technical flow

Running a two-record browser fixture

Load the HTML file as a standalone document. Before any rows are added, record the child count and test whether querying the template element itself returns the li inside its content. The script then copies the fragment twice, adds text to each copy and appends its children to the ordered list. On Edge 158.0.4370.45 the initial list had zero children, and the template query did not return an li. The two printed strings were A17 Cable <unplugged> and B42 Air filter checked. The strong within the original content still had empty text, and the list contained no unplugged element. Together these observations verify an untouched template, two independent visible clones and plain text insertion at the description field. The W3C Recommendation establishes the historical design. We executed this fixture in a modern browser and did not use a browser build from 2014.

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>Template clone demonstration</title>
<template id="fault">
  <li><strong data-code></strong> <span data-description></span></li>
</template>
<ol id="faults"></ol>
<script>
const template = document.querySelector('#fault');
const list = document.querySelector('#faults');
const before = [list.children.length, template.querySelector('li') === null];
for (const [code, message] of [['A17','Cable <unplugged>'],['B42','Air filter checked']]) {
  const fragment = document.importNode(template.content, true);
  fragment.querySelector('[data-code]').textContent = code;
  fragment.querySelector('[data-description]').textContent = message;
  list.append(fragment);
}
window.result = {
  before,
  after: [...list.children].map(row => row.textContent.trim()),
  templateEmpty: template.content.querySelector('strong').textContent === '',
  injectedElement: list.querySelector('unplugged') !== null
};
</script>
</html>

Keeping markup and maintenance data distinct

A backend service can return prose containing angle brackets or text copied from a model number. Treating that prose as markup might change what is visible and create elements that were never intended. The textContent assignments in the fixture demonstrate how plain text behaves for both fields. An image URL, button action or embedded inline control introduces separate constraints that the test does not cover. In production, decide how the list signals a missing description and how it handles acquisition errors. Avoid counting a status placeholder as a completed inspection. Decide also which fields can update an existing row, and how the panel matches a change to a stable record identifier. A template helps render the row, while the application owns those data decisions. If a large inspection list performs poorly, measure actual rendering, reordering and focus behavior on target devices before adopting a more complex rendering approach.

Maintaining the row during later changes

Focus becomes a measurable requirement when filtering replaces the current list. An operator might open details on the second row, filter out the first record and return to the controls. Test that sequence in addition to visual appearance. If delegated events are used, verify that a newly appended copy still points to the correct maintenance identifier, and test an event coming from a descendant within its button. A presentation change can also add a wrapper that violates the list’s original shape without changing the visible colors. Preserve the direct-child assertion and the untouched-template assertion through that refactor. They help distinguish data mutations from template structure changes. The fixture deliberately uses two different descriptions, which allows a regression to show up as a repeated value. Once localization is added, test a long description and an empty one as additional cases, and inspect any control names separately.

596 reads ✎ Write a comment

Reader discussion

Discussion prompt

What is the most important unanswered question about this topic?

No comments yet. Be the first to contribute.

Write a comment

Comments are real visitor contributions. Repeated, link-heavy or abusive submissions may be limited.