(node, old)
| 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 |
no test coverage detected