You can check if a node is terminal (has no children) by examining its child nodes. Here are several ways to do this in JavaScript:
Method 1: Check childNodes.length (Most Common)
function isTerminalNode(node) {
return node.childNodes.length === 0;
}
// Usage example:
if (isTerminalNode(someNode)) {
console.log('This node has no children');
}
Method 2: Check children.length (Element Children Only)
function hasNoElementChildren(node) {
return node.children.length === 0;
}
Important difference:
childNodes includes ALL child nodes (elements, text nodes, comments, etc.)
children includes only element nodes
Method 3: Check hasChildNodes() Method
function isTerminalNode(node) {
return !node.hasChildNodes();
}
Method 4: Check firstChild Property
function isTerminalNode(node) {
return node.firstChild === null;
}
Integrating into Your Existing Code
Here's how you could modify your assignUniqueIds function to handle terminal nodes differently:
function assignUniqueIds() {
const container = document.getElementById('container');
if (!container) {
console.error('Container element not found');
return;
}
const tagCounters = {};
function isTerminalNode(node) {
return node.childNodes.length === 0;
}
function processNode(node) {
if (node.nodeType !== Node.ELEMENT_NODE) {
return;
}
if (node === container) {
return;
}
// Check if this is a terminal node
if (isTerminalNode(node)) {
console.log(`Terminal node found: ${node.tagName}`);
// You can handle terminal nodes specially here
}
if (!node.id) {
let parentId = node.parentNode && node.parentNode.id ? node.parentNode.id : '';
if (parentId) {
const tagName = node.tagName.toLowerCase();
if (!tagCounters[tagName]) {
tagCounters[tagName] = 0;
}
tagCounters[tagName]++;
const newId = `${parentId}-${tagName}${tagCounters[tagName]}`;
node.id = newId;
}
}
// Only process children if the node is not terminal
if (!isTerminalNode(node)) {
const children = node.childNodes;
for (let i = 0; i < children.length; i++) {
processNode(children[i]);
}
}
}
const children = container.childNodes;
for (let i = 0; i < children.length; i++) {
processNode(children[i]);
}
}
Considerations for Different Node Types
Text Nodes and Whitespace
Be aware that text nodes (including whitespace) count as children:
<div>
<span>Text</span> <!-- This div has 3 children: text node, span, text node -->
</div>
<div><span>Text</span></div> <!-- This div has 1 child: span -->
Checking for "Meaningful" Terminal Nodes
If you want to ignore whitespace-only text nodes:
function isTerminalNode(node) {
// Check if node has no children at all
if (node.childNodes.length === 0) {
return true;
}
// Check if all children are whitespace-only text nodes
for (let child of node.childNodes) {
if (child.nodeType === Node.ELEMENT_NODE) {
return false; // Has element children
}
if (child.nodeType === Node.TEXT_NODE && child.textContent.trim() !== '') {
return false; // Has meaningful text content
}
}
return true; // Only whitespace text nodes or empty
}
Example Usage with Your HTML
Given your example HTML:
<li id="drw-item1">
<strong>Fusion F7 Power</strong>: Aktuell bei
<strong style="color:#dc4d01;">27 %</strong>.
<em style="color:#dc4d01;">8:31:30</em>
</li>
- The
<li> element is not terminal (has children)
- The
<strong> elements are not terminal (contain text nodes)
- The
<em> element is not terminal (contains text nodes)
- The text nodes inside the elements are terminal (no children)
The most commonly used approach is Method 1 (childNodes.length === 0) as it's simple, clear, and covers all cases.