MCPcopy Index your code
hub / github.com/Tencent/kbone / insertBefore

Method insertBefore

packages/miniprogram-render/src/node/element.js:844–889  ·  view source on GitHub ↗
(node, ref)

Source from the content-addressed store, hash-verified

842 }
843
844 insertBefore(node, ref) {
845 if (!(node instanceof Node)) return
846 if (ref && !(ref instanceof Node)) return
847
848 let nodes
849 let hasUpdate = false
850
851 if (node.nodeType === Node.DOCUMENT_FRAGMENT_NODE) {
852 // documentFragment
853 nodes = []
854 for (let i = 0; i < node.childNodes.length; i++) {
855 // 因为是逐个插入,所以需要逆序
856 nodes.push(node.childNodes[i])
857 }
858 } else {
859 nodes = [node]
860 }
861
862 for (const node of nodes) {
863 if (node === this) continue
864 if (node.parentNode) node.parentNode.removeChild(node)
865
866 const insertIndex = ref ? this.$_children.indexOf(ref) : -1
867
868 if (insertIndex === -1) {
869 // 插入到末尾
870 this.$_children.push(node)
871 } else {
872 // 插入到 ref 之前
873 this.$_children.splice(insertIndex, 0, node)
874 }
875
876 node.$$updateParent(this) // 设置 parentNode
877
878 // 更新映射表
879 this.$_updateChildrenExtra(node)
880
881 hasUpdate = true
882 }
883
884
885 // 触发 webview 端更新
886 if (hasUpdate) this.$_triggerMeUpdate()
887
888 return node
889 }
890
891 replaceChild(node, old) {
892 if (!(node instanceof Node) || !(old instanceof Node)) return

Callers 6

document.test.jsFile · 0.80
element.test.jsFile · 0.80
jquery-3.6.0.jsFile · 0.80
dFunction · 0.80
wFunction · 0.80

Calls 4

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

Tested by

no test coverage detected