Skip to content

Commit 7713cbb

Browse files
committed
refactor(StringBasedRenderer): Consolidate logic into createNode and remove unused methods (#8617)
1 parent c46c754 commit 7713cbb

1 file changed

Lines changed: 5 additions & 60 deletions

File tree

Lines changed: 5 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,17 @@
1-
import DomAccess from '../DomAccess.mjs';
2-
31
const StringBasedRenderer = {
42
/**
5-
* @param {String} html representing a single element
3+
* @param {Object} data
4+
* @param {String} data.outerHTML The HTML string of the node to create.
65
* @returns {DocumentFragment}
76
*/
8-
htmlStringToElement(html) {
7+
createNode({outerHTML}) {
98
const template = document.createElement('template');
10-
template.innerHTML = html;
9+
template.innerHTML = outerHTML;
1110
return template.content
12-
},
13-
14-
/**
15-
* Handles string-based insertion of a new node into the DOM.
16-
* This method is called by `insertNode()` when `NeoConfig.useDomApiRenderer` is false.
17-
*
18-
* @param {Object} data
19-
* @param {Boolean} data.hasLeadingTextChildren Flag to honor leading comments.
20-
* @param {Number} data.index The index at which to insert the new node.
21-
* @param {String} data.outerHTML The HTML string of the node to insert.
22-
* @param {HTMLElement} data.parentNode The parent DOM node to insert into.
23-
* @param {Object[]} [data.postMountUpdates] Array of post-mount updates (e.g. scroll state).
24-
* @private
25-
*/
26-
insertNodeAsString({hasLeadingTextChildren, index, outerHTML, parentNode, postMountUpdates}) {
27-
let me = this;
28-
29-
// If comments detected, parse HTML string to a node and use insertBefore/appendChild on childNodes.
30-
if (hasLeadingTextChildren) {
31-
let node = me.htmlStringToElement(outerHTML);
32-
33-
if (index < parentNode.childNodes.length) {
34-
parentNode.insertBefore(node, parentNode.childNodes[index])
35-
} else {
36-
parentNode.appendChild(node)
37-
}
38-
}
39-
// If no comments detected, use insertAdjacentHTML for element nodes.
40-
else {
41-
let countChildren = parentNode.children.length; // Use `children` for `insertAdjacentHTML` context
42-
43-
if (index > 0 && index >= countChildren) {
44-
parentNode.insertAdjacentHTML('beforeend', outerHTML);
45-
} else if (countChildren > 0 && countChildren > index) {
46-
parentNode.children[index].insertAdjacentHTML('beforebegin', outerHTML)
47-
} else if (countChildren > 0) {
48-
parentNode.children[countChildren - 1].insertAdjacentHTML('afterend', outerHTML)
49-
} else {
50-
parentNode.insertAdjacentHTML('beforeend', outerHTML)
51-
}
52-
}
53-
54-
if (postMountUpdates?.length > 0) {
55-
let node;
56-
57-
postMountUpdates.forEach(update => {
58-
node = DomAccess.getElement(update.id);
59-
60-
if (node) {
61-
if (update.scrollLeft) {node.scrollLeft = update.scrollLeft}
62-
if (update.scrollTop) {node.scrollTop = update.scrollTop}
63-
}
64-
})
65-
}
6611
}
6712
};
6813

6914
const ns = Neo.ns('Neo.main.render', true);
7015
ns.StringBasedRenderer = StringBasedRenderer;
7116

72-
export default StringBasedRenderer;
17+
export default StringBasedRenderer;

0 commit comments

Comments
 (0)