MCPcopy Create free account
hub / github.com/Tencent/kbone / replaceChild

Method replaceChild

packages/miniprogram-render/src/node/element.js:891–936  ·  view source on GitHub ↗
(node, old)

Source from the content-addressed store, hash-verified

889 }
890
891 replaceChild(node, old) {
892 if (!(node instanceof Node) || !(old instanceof Node)) return
893
894 let nodes
895 let hasUpdate = false
896
897 if (node.nodeType === Node.DOCUMENT_FRAGMENT_NODE) {
898 // documentFragment
899 nodes = []
900 for (let i = node.childNodes.length - 1; i >= 0; i--) {
901 // 因为是逐个插入,所以需要逆序
902 nodes.push(node.childNodes[i])
903 }
904 } else {
905 nodes = [node]
906 }
907
908 const replaceIndex = this.$_children.indexOf(old)
909 if (replaceIndex !== -1) this.$_children.splice(replaceIndex, 1)
910
911 for (const node of nodes) {
912 if (node === this) continue
913 if (node.parentNode) node.parentNode.removeChild(node)
914
915 if (replaceIndex === -1) {
916 // 插入到末尾
917 this.$_children.push(node)
918 } else {
919 // 替换到 old
920 this.$_children.splice(replaceIndex, 0, node)
921 }
922
923 node.$$updateParent(this) // 设置 parentNode
924
925 // 更新映射表
926 this.$_updateChildrenExtra(node)
927 this.$_updateChildrenExtra(old, true)
928
929 hasUpdate = true
930 }
931
932 // 触发 webview 端更新
933 if (hasUpdate) this.$_triggerMeUpdate()
934
935 return old
936 }
937
938 hasChildNodes() {
939 return this.$_children.length > 0

Callers 5

document.test.jsFile · 0.80
element.test.jsFile · 0.80
chooseImageMethod · 0.80
index.test.jsFile · 0.80
jquery-3.6.0.jsFile · 0.80

Calls 4

$_updateChildrenExtraMethod · 0.95
$_triggerMeUpdateMethod · 0.95
removeChildMethod · 0.80
$$updateParentMethod · 0.80

Tested by

no test coverage detected