Skip to content

Commit c44c71f

Browse files
dkayiwaCopilot
andauthored
LUI-208: Fix Node.appendChild error in search results with newer DWR util.js (#240)
The cellCreator function in OpenmrsSearch.js returned options.data directly when it was already a TD element. The newer DWR util.js (from org.openmrs.contrib) then calls cell.appendChild(data) on the returned cell, but since cell === data, this throws 'Node.appendChild: The new child is an ancestor of the parent' on pages like Find Duplicate Patients and concept search popups. Fix: Create a new TD element in cellCreator and transfer all attributes and child nodes from the original, so the returned cell is always a different object from the data. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 0f65e29 commit c44c71f

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

omod/src/main/webapp/resources/scripts/dojo/src/widget/openmrs/OpenmrsSearch.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,8 +573,33 @@ dojo.widget.defineWidget(
573573
},
574574

575575
cellCreator: function(options) {
576-
if (dwr.util._isHTMLElement(options.data, "td") == true)
577-
return options.data;
576+
if (dwr.util._isHTMLElement(options.data, "td") == true) {
577+
// LUI-208: Create a new td and transfer content from the original.
578+
// The newer DWR util.js (from org.openmrs.contrib) always tries to
579+
// appendChild(data) on the cell returned by cellCreator. When the
580+
// cell IS the data (same object), this causes "Node.appendChild:
581+
// The new child is an ancestor of the parent". By returning a
582+
// different td object with the same content, we avoid this error.
583+
var original = options.data;
584+
var td = document.createElement("td");
585+
td.className = original.className;
586+
td.id = original.id;
587+
if (original.colSpan > 1) td.colSpan = original.colSpan;
588+
td.onclick = original.onclick;
589+
td.onmouseover = original.onmouseover;
590+
td.onmouseout = original.onmouseout;
591+
while (original.firstChild) {
592+
td.appendChild(original.firstChild);
593+
}
594+
// Clear the original to avoid duplicate ids and stale state
595+
// in case DWR appends it as a nested element
596+
original.className = '';
597+
original.id = '';
598+
original.onclick = null;
599+
original.onmouseover = null;
600+
original.onmouseout = null;
601+
return td;
602+
}
578603

579604
return document.createElement("td");
580605
},

0 commit comments

Comments
 (0)