Best JavaScript code snippet using playwright-internal
vueInner.js
Source:vueInner.js
...249}250/**251 * removeVnodes ä¼æ¹éè°ç¨removeNode移é¤èç¹252 */253function removeVnodes(parentElm, vnodes, startIdx, endIdx) {254 for (; startIdx <= endIdx; ++startIdx) {255 const ch = vnodes[startIdx];256 if (ch) {257 removeNode(ch.elm);258 }259 }260}261// patch(æè¡¥ä¸)262// patchçæ ¸å¿diffç®æ³,æ们ç¨diffç®æ³å¯¹æ¯åºä¸¤æ£µæ çå·®å¼263// diffç®æ³æ¯éè¿åå±çæ èç¹è¿è¡æ¯è¾èé对æ è¿è¡éå±æç´¢éåçæ¹å¼,æ以æ¶é´å¤æ度åªæ264// O(n),æ¯ä¸ç§ç¸å½é«æçç®æ³265function patch (oldVnode, vnode, parentElm) {266 if (!oldVnode) {267 addVnodes(parentElm, null, vnode, 0, vnode.length - 1);268 } else if (!vnode) {269 removeVnodes(parentElm, oldVnode, 0, oldVnode.length - 1)270 } else {271 if (sameVnode(oldVNode, vnode)) {272 patchVnode(oldVNode, vnode);273 } else {274 removeVnodes(parentElm, oldVnode, 0, oldVnode.length - 1);275 addVnodes(parentElm, null, vnode, 0, vnode.length - 1);276 }277 }278}279/**** 第ä¸ç« æ¹éå¼æ¥æ´æ°çç¥ånextTickåç *****/...
2.diff.js
Source:2.diff.js
...52 nodeOps.removeChild(parent, el);53 }54}55// removeVnodes ä¼æ¹éè°ç¨ removeNode 移é¤èç¹ã56function removeVnodes(parentElm, vnodes, startIdx, endIdx) {57 for (; startIdx <= endIdx; ++startIdx) {58 const ch = vnodes[startIdx];59 if (ch) {60 removeNode(ch.elm);61 }62 }63}64// diff ç®æ³æ¯éè¿åå±çæ èç¹è¿è¡æ¯è¾èé对æ è¿è¡éå±æç´¢éåçæ¹å¼65// æ以æ¶é´å¤æ度åªæ O(n)ï¼æ¯ä¸ç§ç¸å½é«æçç®æ³66function patch(oldVnode, vnode, parentElm) {67 if (!oldVnode) {68 // 没æèèç¹ï¼æå
¥æ°èç¹69 addVnodes(parentElm, null, vnode, 0, vnode.length - 1);70 } else if (!vnode) {71 // 没ææ°èç¹ï¼å é¤èèç¹72 removeVnodes(parentElm, oldVnode, 0, oldVnode.length - 1);73 } else {74 // å¦ææ¯åä¸ä¸ªèç¹ï¼patchVnode75 if (sameVnode(oldVNode, vnode)) {76 patchVnode(oldVNode, vnode);77 } else {78 // ä¸æ¯ä¸ä¸ªèç¹ï¼å é¤èèç¹ï¼æ°å¢æ°èç¹79 removeVnodes(parentElm, oldVnode, 0, oldVnode.length - 1);80 addVnodes(parentElm, null, vnode, 0, vnode.length - 1);81 }82 }83}84// ä»ä¹æ
åµä¸ä¸¤ä¸ª VNode ä¼å±äº sameVnode ï¼ç¸åçèç¹ï¼å¢ï¼85function sameVnode() {86 return (87 a.key === b.key &&88 a.tag === b.tag &&89 a.isComment === b.isComment &&90 !!a.data === !!b.data &&91 sameInputType(a, b)92 );93}94function sameInputType(a, b) {95 if (a.tag !== "input") return true;96 let i;97 const typeA = (i = a.data) && (i = i.attrs) && i.type;98 const typeB = (i = b.data) && (i = i.attrs) && i.type;99 return typeA === typeB;100}101/**102 * æ ¸å¿æ¹æ³103 * @param {*} oldVnode104 * @param {*} vnode105 * @returns106 */107function patchVnode(oldVnode, vnode) {108 // æ°è VNode èç¹ç¸åçæ
åµä¸ï¼å°±ä¸éè¦åä»»ä½æ¹åäºï¼ç´æ¥ return æ109 if (oldVnode === vnode) {110 return;111 }112 // å¨å½æ°è VNode èç¹é½æ¯ isStaticï¼éæçï¼ï¼å¹¶ä¸ key ç¸åæ¶ï¼113 // åªè¦å° componentInstance ä¸ elm ä»è VNode èç¹âæ¿è¿æ¥âå³å¯ã114 // è¿éç isStatic ä¹å°±æ¯åé¢æå°è¿çãç¼è¯ãçæ¶åä¼å°éæèç¹æ è®°åºæ¥ï¼è¿æ ·å°±å¯ä»¥è·³è¿æ¯å¯¹çè¿ç¨ã115 if (vnode.isStatic && oldVnode.isStatic && vnode.key === oldVnode.key) {116 vnode.elm = oldVnode.elm;117 vnode.componentInstance = oldVnode.componentInstance;118 return;119 }120 const elm = (vnode.elm = oldVnode.elm);121 const oldCh = oldVnode.children;122 const ch = vnode.children;123 // å½æ° VNode èç¹æ¯ææ¬èç¹çæ¶åï¼ç´æ¥ç¨ setTextContent æ¥è®¾ç½® text124 if (vnode.text) {125 nodeOps.setTextContent(elm, vnode.text);126 } else {127 // oldCh ä¸ ch é½åå¨ä¸ä¸ç¸åæ¶ï¼ä½¿ç¨ updateChildren å½æ°æ¥æ´æ°åèç¹128 if (oldCh && ch && oldCh !== ch) {129 updateChildren(elm, oldCh, ch);130 } else if (ch) {131 // å¦æåªæ ch åå¨çæ¶å132 // å¦æèèç¹æ¯ææ¬èç¹åå
å°èç¹çææ¬æ¸
é¤133 if (oldVnode.text) nodeOps.setTextContent(elm, "");134 // ç¶åå° ch æ¹éæå
¥å°èç¹elmä¸ã135 addVnodes(elm, null, ch, 0, ch.length - 1);136 } else if (oldCh) {137 // åçå½åªæ oldch åå¨æ¶ï¼è¯´æéè¦å°èèç¹éè¿ removeVnodes å
¨é¨æ¸
é¤138 removeVnodes(elm, oldCh, 0, oldCh.length - 1);139 } else if (oldVnode.text) {140 // æåä¸ç§æ
åµæ¯å½åªæèèç¹æ¯ææ¬èç¹çæ¶åï¼æ¸
é¤å
¶èç¹ææ¬å
容ã141 nodeOps.setTextContent(elm, "");142 }143 }144}145function updateChildren(parentElm, oldCh, newCh) {146 let oldStartIdx = 0;147 let newStartIdx = 0;148 let oldEndIdx = oldCh.length - 1;149 let oldStartVnode = oldCh[0];150 let oldEndVnode = oldCh[oldEndIdx];151 let newEndIdx = newCh.length - 1;152 let newStartVnode = newCh[0];153 let newEndVnode = newCh[newEndIdx];154 let oldKeyToIdx, idxInOld, elmToMove, refElm;155 // while 循ç¯ï¼å¨è¿è¿ç¨ä¸ï¼oldStartIdxãnewStartIdxãoldEndIdx 以å newEndIdx ä¼éæ¸åä¸é´é æ¢156 while (oldStartIdx <= oldEndIdx && newStartIdx <= newEndIdx) {157 if (!oldStartVnode) {158 oldStartVnode = oldCh[++oldStartIdx];159 } else if (!oldEndVnode) {160 oldEndVnode = oldCh[--oldEndIdx];161 } else if (sameVnode(oldStartVnode, newStartVnode)) {162 patchVnode(oldStartVnode, newStartVnode);163 oldStartVnode = oldCh[++oldStartIdx];164 newStartVnode = newCh[++newStartIdx];165 } else if (sameVnode(oldEndVnode, newEndVnode)) {166 patchVnode(oldEndVnode, newEndVnode);167 oldEndVnode = oldCh[--oldEndIdx];168 newEndVnode = newCh[--newEndIdx];169 } else if (sameVnode(oldStartVnode, newEndVnode)) {170 patchVnode(oldStartVnode, newEndVnode);171 nodeOps.insertBefore(172 parentElm,173 oldStartVnode.elm,174 nodeOps.nextSibling(oldEndVnode.elm)175 );176 oldStartVnode = oldCh[++oldStartIdx];177 newEndVnode = newCh[--newEndIdx];178 } else if (sameVnode(oldEndVnode, newStartVnode)) {179 patchVnode(oldEndVnode, newStartVnode);180 nodeOps.insertBefore(parentElm, oldEndVnode.elm, oldStartVnode.elm);181 oldEndVnode = oldCh[--oldEndIdx];182 newStartVnode = newCh[++newStartIdx];183 } else {184 let elmToMove = oldCh[idxInOld];185 if (!oldKeyToIdx)186 oldKeyToIdx = createKeyToOldIdx(oldCh, oldStartIdx, oldEndIdx);187 idxInOld = newStartVnode.key ? oldKeyToIdx[newStartVnode.key] : null;188 // å½ånewStartVnodeå¨oldè¿æªéåå®ç项ç®ä¸ï¼æ¯å¦åå¨189 if (!idxInOld) {190 createElm(newStartVnode, parentElm);191 newStartVnode = newCh[++newStartIdx];192 } else {193 elmToMove = oldCh[idxInOld];194 if (sameVnode(elmToMove, newStartVnode)) {195 patchVnode(elmToMove, newStartVnode);196 oldCh[idxInOld] = undefined;197 nodeOps.insertBefore(parentElm, newStartVnode.elm, oldStartVnode.elm);198 newStartVnode = newCh[++newStartIdx];199 } else {200 createElm(newStartVnode, parentElm);201 newStartVnode = newCh[++newStartIdx];202 }203 }204 }205 }206 // å½ while 循ç¯ç»æ以åï¼å¦æ oldStartIdx > oldEndIdxï¼è¯´æèèç¹æ¯å¯¹å®äº207 // ä½æ¯æ°èç¹è¿æå¤çï¼éè¦å°æ°èç¹æå
¥å°çå® DOM ä¸å»ï¼è°ç¨ addVnodes å°è¿äºèç¹æå
¥å³å¯208 if (oldStartIdx > oldEndIdx) {209 refElm = newCh[newEndIdx + 1] ? newCh[newEndIdx + 1].elm : null;210 addVnodes(parentElm, refElm, newCh, newStartIdx, newEndIdx);211 } else if (newStartIdx > newEndIdx) {212 // å¦æ满足 newStartIdx > newEndIdx æ¡ä»¶ï¼è¯´ææ°èç¹æ¯å¯¹å®äºï¼èèç¹è¿æå¤213 // å°è¿äºæ ç¨çèèç¹éè¿ removeVnodes æ¹éå é¤å³å¯ã214 removeVnodes(parentElm, oldCh, oldStartIdx, oldEndIdx);215 }216}217function createKeyToOldIdx(children, beginIdx, endIdx) {218 let i, key;219 const map = {};220 for (i = beginIdx; i <= endIdx; ++i) {221 key = children[i].key;222 if (isDef(key)) map[key] = i;223 }224 return map;...
index.js
Source:index.js
...3 // ä¸åå¨æ§çnodeï¼å°æ°çnodeç´æ¥æå
¥ç¶èç¹4 if (!oldVnode) {5 addVnodes(parentElm, null, vnode, 0, vnode.length - 1)6 } else if (!vnode) { // ä¸åå¨æ°çnodeï¼ç§»é¤å°±çèç¹7 removeVnodes(parentElm, oldVnode, 0, oldVnode.length - 1)8 } else {9 // åå¨åç¸å 10 if (sameVnode(oldVNode, vnode)) {11 patchVnode(oldVNode, vnode)12 } else {13 // åå¨ä¸åï¼å°æ°èç¹æ¿æ¢ä¹
èç¹14 removeVnodes(parentElm, oldVnode, 0, oldVnode.length - 1)15 addVnodes(parentElm, null, vnode, 0, vnode.length - 1)16 }17 }18}19// ç¸åèç¹çæ¯è¾20function patchVnode(oldVnode, vnode) {21 // å®æç¸åï¼ç´æ¥è¿å22 if (oldVnode === vnode) {23 return24 }25 // éæçèç¹ï¼ç´æ¥æ¿æ¢26 if (vnode.isStatic && oldVnode.isStatic && vnode.key === oldVnode.key) {27 vnode.elm = oldVnode.elm28 vnode.componentInstance = oldVnode.componentInstance29 return30 }31 // å¤æåèç¹32 const elm = vnode.elm = oldVnode.elm33 const oldCh = oldVnode.children34 const ch = vnode.children35 // æ¯ææ¬èç¹ï¼ç´æ¥æ¿æ¢36 if (vnode.text) {37 nodeOps.setTextContent(elm, vnode.text)38 } else {39 if (oldCh && ch) { //åæ¶åå¨40 if (oldCh !== ch) { // ä¸ç¸åï¼æ´æ°åèç¹41 updateChildren(elm, oldCh, ch)42 }43 } else if (ch) { 44 if (oldVnode.text) nodeOps.setTextContent(elm, '')45 addVnodes(elm, null, ch, 0, ch.length - 1)46 } else if (oldCh) {47 removeVnodes(elm, oldCh, 0, oldCh.length - 1)48 } else if (oldVnode.text) {49 nodeOps.setTextContent(elm, '')50 }51 }52}53// å¤æinputçç±»å54function sameInputType(a, b) {55 if (a.tag !== 'input') return true56 let i57 const typeA = (i = a.data) && (i = i.attrs) && i.type58 const typeB = (i = b.data) && (i = i.attrs) && i.type59 return typeA === typeB60}61// å¤æç¸åçnode62function sameVnode() {63 return (64 a.key === b.key &&65 a.tag === b.tag &&66 a.isComment === b.isComment &&67 !!a.data === !!b.data &&68 sameInputType(a, b)69 )70}71// å°elæå
¥å°parentä¸72function insert(parent, elm, ref) {73 if (parent) {74 // å¦ææ¯ref 75 if (ref) {76 if (ref.parentNode === parent) {77 nodeOps.insertBefore(parent, elm, ref)78 }79 } else {80 nodeOps.appendChild(parent, elm)81 }82 }83}84// å建å
ç´ 85function createElm(vnode, parentElm, refElm) {86 if (vnode.tag) {87 insert(parentElm, nodeOps.createElement(vnode.tag), refElm)88 } else {89 insert(parentElm, nodeOps.createTextNode(vnode.text), refElm)90 }91}92// æ¹éæ·»å node93function addVnodes(parentElm, refElm, vnodes, startIdx, endIdx) {94 for (; startIdx <= endIdx; ++startIdx) {95 createElm(vnodes[startIdx], parentElm, refElm)96 }97}98// 移é¤node99function removeNode(el) {100 const parent = nodeOps.parentNode(el)101 if (parent) {102 nodeOps.removeChild(parent, el)103 }104}105// æ¹é移é¤node106function removeVnodes(parentElm, vnodes, startIdx, endIdx) {107 for (; startIdx <= endIdx; ++startIdx) {108 const ch = vnodes[startIdx]109 if (ch) {110 removeNode(ch.elm)111 }112 }113}114// å°key map 转åææ°ç»115function createKeyToOldIdx(children, beginIdx, endIdx) {116 let i, key117 const map = {}118 for (i = beginIdx; i <= endIdx; ++i) {119 key = children[i].key120 if (isDef(key)) map[key] = i121 }122 return map123}124// æ´æ°åèç¹125function updateChildren(parentElm, oldCh, newCh) {126 let oldStartIdx = 0 // æ§èç¹å¼å§127 let newStartIdx = 0 // 128 let oldEndIdx = oldCh.length - 1129 let oldStartVnode = oldCh[0]130 let oldEndVnode = oldCh[oldEndIdx] // æ§èç¹çnodeï¼ ä¸indexåæ¥131 let newEndIdx = newCh.length - 1132 let newStartVnode = newCh[0]133 let newEndVnode = newCh[newEndIdx]134 let oldKeyToIdx, idxInOld, elmToMove, refElm135 while (oldStartIdx <= oldEndIdx && newStartIdx <= newEndIdx) {136 // æ§èç¹ä¸åå¨ï¼ç´æ¥æ¿æ¢137 if (!oldStartVnode) {138 oldStartVnode = oldCh[++oldStartIdx]139 } else if (!oldEndVnode) {140 oldEndVnode = oldCh[--oldEndIdx]141 } else if (sameVnode(oldStartVnode, newStartVnode)) { // æ°æ§ç¸åçåç§æ
åµ142 patchVnode(oldStartVnode, newStartVnode)143 oldStartVnode = oldCh[++oldStartIdx]144 newStartVnode = newCh[++newStartIdx]145 } else if (sameVnode(oldEndVnode, newEndVnode)) {146 patchVnode(oldEndVnode, newEndVnode)147 oldEndVnode = oldCh[--oldEndIdx]148 newEndVnode = newCh[--newEndIdx]149 } else if (sameVnode(oldStartVnode, newEndVnode)) {150 patchVnode(oldStartVnode, newEndVnode)151 nodeOps.insertBefore(parentElm, oldStartVnode.elm, nodeOps.nextSibling(oldEndVnode.elm))152 oldStartVnode = oldCh[++oldStartIdx]153 newEndVnode = newCh[--newEndIdx]154 } else if (sameVnode(oldEndVnode, newStartVnode)) {155 patchVnode(oldEndVnode, newStartVnode)156 nodeOps.insertBefore(parentElm, oldEndVnode.elm, oldStartVnode.elm)157 oldEndVnode = oldCh[--oldEndIdx]158 newStartVnode = newCh[++newStartIdx]159 } else { //æ°æ§ä¸ç¸å160 let elmToMove = oldCh[idxInOld]161 if (!oldKeyToIdx) oldKeyToIdx = createKeyToOldIdx(oldCh, oldStartIdx, oldEndIdx) // ææ§èç¹çkey转ææ°ç»ï¼æ¹ä¾¿æ¥æ¾162 idxInOld = newStartVnode.key ? oldKeyToIdx[newStartVnode.key] : null 163 if (!idxInOld) { // åå¨åæ°nodeç¸åçkey164 createElm(newStartVnode, parentElm)165 newStartVnode = newCh[++newStartIdx]166 } else {167 elmToMove = oldCh[idxInOld]168 if (sameVnode(elmToMove, newStartVnode)) { // å®å
¨ç¸åï¼ç´æ¥æ¿æ¢169 patchVnode(elmToMove, newStartVnode)170 oldCh[idxInOld] = undefined // é²æ¢éå¤171 nodeOps.insertBefore(parentElm, newStartVnode.elm, oldStartVnode.elm)172 newStartVnode = newCh[++newStartIdx]173 } else { //ä¸ç¸åï¼å建æ°nodeï¼ æå
¥174 createElm(newStartVnode, parentElm)175 newStartVnode = newCh[++newStartIdx]176 }177 }178 }179 }180 if (oldStartIdx > oldEndIdx) { // æ°nodeå¤ï¼ç´æ¥æå
¥181 refElm = (newCh[newEndIdx + 1]) ? newCh[newEndIdx + 1].elm : null182 addVnodes(parentElm, refElm, newCh, newStartIdx, newEndIdx)183 } else if (newStartIdx > newEndIdx) { // æ§nodeå¤ç´æ¥å é¤184 removeVnodes(parentElm, oldCh, oldStartIdx, oldEndIdx)185 }186}187// å°è£
å¤å¹³å°node api188const nodeOps = {189 // ææ¬èç¹è®¾ç½®ææ¬190 setTextContent() {191 },192 parentNode() {193 },194 removeChild() {195 },196 nextSibling() {197 },198 insertBefore() {...
2018-10-30-vue-diff.js
Source:2018-10-30-vue-diff.js
1function patch (oldVNode, vnode, parentElm) {2 if (!oldVnode) {3 addVnodes(parentElm, null, vnode, 0, vnode.length - 1);4 } else if (!vnode) {5 removeVnodes(parentElm, oldVnode, 0, oldVnode.length - 1);6 } else {7 if (sameVnode(oldVNode, vnode)) {8 patchVnode(oldVNode, vnode);9 } else {10 removeVnodes(parentElm, oldVnode, 0, oldVnode.length - 1);11 addVnodes(parentElm, null, vnode, 0, vnode.length - 1);12 }13 }14}15function patchVnode (oldVnode, vnode) {16 if (oldVnode === vnode) {17 return;18 }19 if (vnode.isStatic && oldVnode.isStatic && vnode.key === oldVnode.key) {20 vnode.elm = oldVnode.elm;21 vnode.componentInstance = oldVnode.componentInstance;22 return;23 }24 const elm = vnode.elm = oldVnode.elm;25 const oldCh = oldVnode.children;26 const ch = vnode.children;27 if (vnode.text) {28 nodeOps.setTextContent(elm, vnode.text);29 } else {30 if (oldCh && ch && (oldCh !== ch)) {31 updateChildren(elm, oldCh, ch);32 } else if (ch) {33 if (oldVnode.text) nodeOps.setTextContent(elm, '');34 addVnodes(elm, null, ch, 0, ch.length - 1);35 } else if (oldCh) {36 removeVnodes(elm, oldCh, 0, oldCh.length - 1)37 } else if (oldVnode.text) {38 nodeOps.setTextContent(elm, '')39 }40 }41}42function sameInputType (a, b) {43 if (a.tag !== 'input') return true44 let i45 const typeA = (i = a.data) && (i = i.attrs) && i.type46 const typeB = (i = b.data) && (i = i.attrs) && i.type47 return typeA === typeB48}49function sameVnode () {50 return (51 a.key === b.key &&52 a.tag === b.tag &&53 a.isComment === b.isComment &&54 !!a.data === !!b.data &&55 sameInputType(a, b)56 )57}58function insert (parent, elm, ref) {59 if (parent) {60 if (ref) {61 if (ref.parentNode === parent) {62 nodeOps.insertBefore(parent, elm, ref);63 }64 } else {65 nodeOps.appendChild(parent, elm)66 }67 }68}69function createElm (vnode, parentElm, refElm) {70 if (vnode.tag) {71 insert(parentElm, nodeOps.createElement(vnode.tag), refElm);72 } else {73 insert(parentElm, nodeOps.createTextNode(vnode.text), refElm);74 }75}76function addVnodes (parentElm, refElm, vnodes, startIdx, endIdx) {77 for (; startIdx <= endIdx; ++startIdx) {78 createElm(vnodes[startIdx], parentElm, refElm);79 }80}81function removeNode (el) {82 const parent = nodeOps.parentNode(el);83 if (parent) {84 nodeOps.removeChild(parent, el);85 }86}87function removeVnodes (parentElm, vnodes, startIdx, endIdx) {88 for (; startIdx <= endIdx; ++startIdx) {89 const ch = vnodes[startIdx]90 if (ch) {91 removeNode(ch.elm);92 }93 }94}95function createKeyToOldIdx (children, beginIdx, endIdx) {96 let i, key97 const map = {}98 for (i = beginIdx; i <= endIdx; ++i) {99 key = children[i].key100 if (isDef(key)) map[key] = i101 }102 return map103}104function updateChildren (parentElm, oldCh, newCh) {105 let oldStartIdx = 0;106 let newStartIdx = 0;107 let oldEndIdx = oldCh.length - 1;108 let oldStartVnode = oldCh[0];109 let oldEndVnode = oldCh[oldEndIdx];110 let newEndIdx = newCh.length - 1;111 let newStartVnode = newCh[0];112 let newEndVnode = newCh[newEndIdx];113 114 let oldKeyToIdx, idxInOld, elmToMove, refElm;115 while (oldStartIdx <= oldEndIdx && newStartIdx <= newEndIdx) {116 if (!oldStartVnode) {117 oldStartVnode = oldCh[++oldStartIdx];118 } else if (!oldEndVnode) {119 oldEndVnode = oldCh[--oldEndIdx];120 } else if (sameVnode(oldStartVnode, newStartVnode)) {121 patchVnode(oldStartVnode, newStartVnode);122 oldStartVnode = oldCh[++oldStartIdx];123 newStartVnode = newCh[++newStartIdx];124 } else if (sameVnode(oldEndVnode, newEndVnode)) {125 patchVnode(oldEndVnode, newEndVnode);126 oldEndVnode = oldCh[--oldEndIdx];127 newEndVnode = newCh[--newEndIdx];128 } else if (sameVnode(oldStartVnode, newEndVnode)) {129 patchVnode(oldStartVnode, newEndVnode);130 nodeOps.insertBefore(parentElm, oldStartVnode.elm, nodeOps.nextSibling(oldEndVnode.elm));131 oldStartVnode = oldCh[++oldStartIdx];132 newEndVnode = newCh[--newEndIdx];133 } else if (sameVnode(oldEndVnode, newStartVnode)) {134 patchVnode(oldEndVnode, newStartVnode);135 nodeOps.insertBefore(parentElm, oldEndVnode.elm, oldStartVnode.elm);136 oldEndVnode = oldCh[--oldEndIdx];137 newStartVnode = newCh[++newStartIdx];138 } else {139 let elmToMove = oldCh[idxInOld];140 if (!oldKeyToIdx) oldKeyToIdx = createKeyToOldIdx(oldCh, oldStartIdx, oldEndIdx);141 idxInOld = newStartVnode.key ? oldKeyToIdx[newStartVnode.key] : null;142 if (!idxInOld) {143 createElm(newStartVnode, parentElm);144 newStartVnode = newCh[++newStartIdx];145 } else {146 elmToMove = oldCh[idxInOld];147 if (sameVnode(elmToMove, newStartVnode)) {148 patchVnode(elmToMove, newStartVnode);149 oldCh[idxInOld] = undefined;150 nodeOps.insertBefore(parentElm, newStartVnode.elm, oldStartVnode.elm);151 newStartVnode = newCh[++newStartIdx];152 } else {153 createElm(newStartVnode, parentElm);154 newStartVnode = newCh[++newStartIdx];155 }156 }157 }158 }159 if (oldStartIdx > oldEndIdx) {160 refElm = (newCh[newEndIdx + 1]) ? newCh[newEndIdx + 1].elm : null;161 addVnodes(parentElm, refElm, newCh, newStartIdx, newEndIdx);162 } else if (newStartIdx > newEndIdx) {163 removeVnodes(parentElm, oldCh, oldStartIdx, oldEndIdx);164 }165}166const nodeOps = {167 setTextContent () {168 169 },170 parentNode () {171 },172 removeChild () {173 },174 nextSibling () {175 },176 insertBefore () {177 ...
《数据状态更新时的差异 diff 及 patch 机制》.js
Source:《数据状态更新时的差异 diff 及 patch 机制》.js
1function patch(oldVNode, vnode, parentElm) {2 if (!oldVnode) {3 addVnodes(parentElm, null, vnode, 0, vnode.length - 1);4 } else if (!vnode) {5 removeVnodes(parentElm, oldVnode, 0, oldVnode.length - 1);6 } else {7 if (sameVnode(oldVNode, vnode)) {8 patchVnode(oldVNode, vnode);9 } else {10 removeVnodes(parentElm, oldVnode, 0, oldVnode.length - 1);11 addVnodes(parentElm, null, vnode, 0, vnode.length - 1);12 }13 }14}15function patchVnode(oldVnode, vnode) {16 if (oldVnode === vnode) {17 return;18 }19 if (vnode.isStatic && oldVnode.isStatic && vnode.key === oldVnode.key) {20 vnode.elm = oldVnode.elm;21 vnode.componentInstance = oldVnode.componentInstance;22 return;23 }24 const elm = vnode.elm = oldVnode.elm;25 const oldCh = oldVnode.children;26 const ch = vnode.children;27 if (vnode.text) {28 nodeOps.setTextContent(elm, vnode.text);29 } else {30 if (oldCh && ch && (oldCh !== ch)) {31 updateChildren(elm, oldCh, ch);32 } else if (ch) {33 if (oldVnode.text) nodeOps.setTextContent(elm, '');34 addVnodes(elm, null, ch, 0, ch.length - 1);35 } else if (oldCh) {36 removeVnodes(elm, oldCh, 0, oldCh.length - 1)37 } else if (oldVnode.text) {38 nodeOps.setTextContent(elm, '')39 }40 }41}42function sameInputType(a, b) {43 if (a.tag !== 'input') return true44 let i45 const typeA = (i = a.data) && (i = i.attrs) && i.type46 const typeB = (i = b.data) && (i = i.attrs) && i.type47 return typeA === typeB48}49function sameVnode() {50 return (51 a.key === b.key &&52 a.tag === b.tag &&53 a.isComment === b.isComment &&54 !!a.data === !!b.data &&55 sameInputType(a, b)56 )57}58function insert(parent, elm, ref) {59 if (parent) {60 if (ref) {61 if (ref.parentNode === parent) {62 nodeOps.insertBefore(parent, elm, ref);63 }64 } else {65 nodeOps.appendChild(parent, elm)66 }67 }68}69function createElm(vnode, parentElm, refElm) {70 if (vnode.tag) {71 insert(parentElm, nodeOps.createElement(vnode.tag), refElm);72 } else {73 insert(parentElm, nodeOps.createTextNode(vnode.text), refElm);74 }75}76function addVnodes(parentElm, refElm, vnodes, startIdx, endIdx) {77 for (; startIdx <= endIdx; ++startIdx) {78 createElm(vnodes[startIdx], parentElm, refElm);79 }80}81function removeNode(el) {82 const parent = nodeOps.parentNode(el);83 if (parent) {84 nodeOps.removeChild(parent, el);85 }86}87function removeVnodes(parentElm, vnodes, startIdx, endIdx) {88 for (; startIdx <= endIdx; ++startIdx) {89 const ch = vnodes[startIdx]90 if (ch) {91 removeNode(ch.elm);92 }93 }94}95function createKeyToOldIdx(children, beginIdx, endIdx) {96 let i, key97 const map = {}98 for (i = beginIdx; i <= endIdx; ++i) {99 key = children[i].key100 if (isDef(key)) map[key] = i101 }102 return map103}104function updateChildren(parentElm, oldCh, newCh) {105 let oldStartIdx = 0;106 let newStartIdx = 0;107 let oldEndIdx = oldCh.length - 1;108 let oldStartVnode = oldCh[0];109 let oldEndVnode = oldCh[oldEndIdx];110 let newEndIdx = newCh.length - 1;111 let newStartVnode = newCh[0];112 let newEndVnode = newCh[newEndIdx];113 let oldKeyToIdx, idxInOld, elmToMove, refElm;114 while (oldStartIdx <= oldEndIdx && newStartIdx <= newEndIdx) {115 if (!oldStartVnode) {116 oldStartVnode = oldCh[++oldStartIdx];117 } else if (!oldEndVnode) {118 oldEndVnode = oldCh[--oldEndIdx];119 } else if (sameVnode(oldStartVnode, newStartVnode)) {120 patchVnode(oldStartVnode, newStartVnode);121 oldStartVnode = oldCh[++oldStartIdx];122 newStartVnode = newCh[++newStartIdx];123 } else if (sameVnode(oldEndVnode, newEndVnode)) {124 patchVnode(oldEndVnode, newEndVnode);125 oldEndVnode = oldCh[--oldEndIdx];126 newEndVnode = newCh[--newEndIdx];127 } else if (sameVnode(oldStartVnode, newEndVnode)) {128 patchVnode(oldStartVnode, newEndVnode);129 nodeOps.insertBefore(parentElm, oldStartVnode.elm, nodeOps.nextSibling(oldEndVnode.elm));130 oldStartVnode = oldCh[++oldStartIdx];131 newEndVnode = newCh[--newEndIdx];132 } else if (sameVnode(oldEndVnode, newStartVnode)) {133 patchVnode(oldEndVnode, newStartVnode);134 nodeOps.insertBefore(parentElm, oldEndVnode.elm, oldStartVnode.elm);135 oldEndVnode = oldCh[--oldEndIdx];136 newStartVnode = newCh[++newStartIdx];137 } else {138 let elmToMove = oldCh[idxInOld];139 if (!oldKeyToIdx) oldKeyToIdx = createKeyToOldIdx(oldCh, oldStartIdx, oldEndIdx);140 idxInOld = newStartVnode.key ? oldKeyToIdx[newStartVnode.key] : null;141 if (!idxInOld) {142 createElm(newStartVnode, parentElm);143 newStartVnode = newCh[++newStartIdx];144 } else {145 elmToMove = oldCh[idxInOld];146 if (sameVnode(elmToMove, newStartVnode)) {147 patchVnode(elmToMove, newStartVnode);148 oldCh[idxInOld] = undefined;149 nodeOps.insertBefore(parentElm, newStartVnode.elm, oldStartVnode.elm);150 newStartVnode = newCh[++newStartIdx];151 } else {152 createElm(newStartVnode, parentElm);153 newStartVnode = newCh[++newStartIdx];154 }155 }156 }157 }158 if (oldStartIdx > oldEndIdx) {159 refElm = (newCh[newEndIdx + 1]) ? newCh[newEndIdx + 1].elm : null;160 addVnodes(parentElm, refElm, newCh, newStartIdx, newEndIdx);161 } else if (newStartIdx > newEndIdx) {162 removeVnodes(parentElm, oldCh, oldStartIdx, oldEndIdx);163 }164}165const nodeOps = {166 setTextContent() {167 },168 parentNode() {169 },170 removeChild() {171 },172 nextSibling() {173 },174 insertBefore() {175 }176}
vapi.js
Source:vapi.js
...59function patch(oldVnode, vnode, parentElm) {60 if (!oldVnode) {61 addVnodes(parentElm, null, vnode, 0, vnode.length - 1)62 } else if (!vnode) {63 removeVnodes(parentElm, oldVnode, 0, oldVnode.length - 1)64 } else {65 if (sameVnode(oldVnode, vnode)) {66 patchVnode(oldVnode, vnode)67 } else {68 removeVnodes(parentElm, oldVnode, 0, oldVnode.length - 1)69 addVnodes(parentElm, null, vnode, 0, vnode.length - 1)70 }71 }72}73function sameVnode(a, b) {74 return (75 a.key === b.key &&76 a.tag === b.tag &&77 a.isComment === b.isComment &&78 (!!a.data) === (!!b.data) &&79 sameInputType(a, b)80 )81}82function sameInputType(a, b) {83 if (a.tag !== 'input') return true84 let i85 const typeA = (i = a.data) && (i = i.attrs) && i.type86 const typeB = (i = b.data) && (i = i.attrs) && i.type87 return typeA === typeB88}89function patchVnode(oldVnode, vnode) {90 if (oldVnode === vnode) {91 return92 }93 if (vnode.isStatic && oldVnode.isStatic && vnode.key === oldVnode.key) {94 vnode.elm = oldVnode.elm;95 vnode.componentInstance = oldVnode.componentInstance;96 return;97 }98 const elm = vnode.elm = oldVnode.elm;99 const oldCh = oldVnode.children;100 const ch = vnode.children;101 if (vnode.text) {102 nodeOps.setTextContent(elm, vnode.text);103 } else {104 if (oldCh && ch && (oldCh !== ch)) {105 updateChildren(elm, oldCh, ch);106 } else if (ch) {107 if (oldVnode.text) nodeOps.setTextContent(elm, '');108 addVnodes(elm, null, ch, 0, ch.length - 1);109 } else if (oldCh) {110 removeVnodes(elm, oldCh, 0, oldCh.length - 1)111 } else if (oldVnode.text) {112 nodeOps.setTextContent(elm, '')113 }114 }...
patch.js
Source:patch.js
...4function patch (oldVnode, vnode, parentElm) {5 if (!oldVnode) {6 addVnodes(parentElm, null, vnode, 0, vnode.length - 1);7 } else if (!vnode) {8 removeVnodes(parentElm, oldVnode, 0, oldVnode.length - 1);9 } else {10 if (sameVnode(oldVNode, vnode)) {11 patchVnode(oldVNode, vnode);12 } else {13 removeVnodes(parentElm, oldVnode, 0, oldVnode.length - 1);14 addVnodes(parentElm, null, vnode, 0, vnode.length - 1);15 }16 }17}18function addVnodes (parentElm, refElm, vnodes, startIdx, endIdx) {19 for (; startIdx <= endIdx; ++startIdx) {20 createElm(vnodes[startIdx], parentElm, refElm);21 }22}23function createElm (vnode, parentElm, refElm) {24 if (vnode.tag) {25 insert(parentElm, nodeOps.createElement(vnode.tag), refElm);26 } else {27 insert(parentElm, nodeOps.createTextNode(vnode.text), refElm);28 }29}30function removeVnodes (parentElm, vnodes, startIdx, endIdx) {31 for (; startIdx <= endIdx; ++startIdx) {32 const ch = vnodes[startIdx]33 if (ch) {34 removeNode(ch.elm);35 }36 }37}38function removeNode (el) {39 const parent = nodeOps.parentNode(el);40 if (parent) {41 nodeOps.removeChild(parent, el);42 }43}44function sameVnode () {45 return (46 a.key === b.key &&47 a.tag === b.tag &&48 a.isComment === b.isComment &&49 (!!a.data) === (!!b.data) &&50 sameInputType(a, b)51 )52}53function sameInputType (a, b) {54 if (a.tag !== 'input') return true55 let i56 const typeA = (i = a.data) && (i = i.attrs) && i.type57 const typeB = (i = b.data) && (i = i.attrs) && i.type58 return typeA === typeB59}60function patchVnode (oldVnode, vnode) {61 if (oldVnode === vnode) {62 return;63 }64 if (vnode.isStatic && oldVnode.isStatic && vnode.key === oldVnode.key) {65 vnode.elm = oldVnode.elm;66 vnode.componentInstance = oldVnode.componentInstance;67 return;68 }69 const elm = vnode.elm = oldVnode.elm;70 const oldCh = oldVnode.children;71 const ch = vnode.children;72 if (vnode.text) {73 nodeOps.setTextContent(elm, vnode.text);74 } else {75 if (oldCh && ch && (oldCh !== ch)) {76 updateChildren(elm, oldCh, ch);77 } else if (ch) {78 if (oldVnode.text) nodeOps.setTextContent(elm, '');79 addVnodes(elm, null, ch, 0, ch.length - 1);80 } else if (oldCh) {81 removeVnodes(elm, oldCh, 0, oldCh.length - 1)82 } else if (oldVnode.text) {83 nodeOps.setTextContent(elm, '')84 }85 }...
utils.dev.js
Source:utils.dev.js
...56 // å¦ærefElm为nullï¼åä¼æ·»å å°æåä¸é¡¹ ç¸å½äºappendChild57 parentElm.insertBefore((0, _createElement["default"])(vnodes[i]), refElm);58 }59}60function removeVnodes(vnodes, startIdx, endIdx) {61 var parentElm = null;62 for (; startIdx <= endIdx; ++startIdx) {63 var ch = vnodes[startIdx];64 !parentElm && (parentElm = ch.elm.parentNode);65 parentElm && parentElm.removeChild(ch.elm);66 }...
Using AI Code Generation
1const { removeVnodes } = require('playwright/lib/protocol/dom.js');2const { chromium } = require('playwright');3(async () => {4 const browser = await chromium.launch({ headless: false });5 const context = await browser.newContext();6 const page = await context.newPage();7 const elementHandle = await page.$('h1');8 await removeVnodes(elementHandle);9 await browser.close();10})();11const { removeVnodes } = require('playwright/lib/protocol/dom.js');12const { chromium } = require('playwright');13(async () => {14 const browser = await chromium.launch({ headless: false });15 const context = await browser.newContext();16 const page = await context.newPage();17 const elementHandle = await page.$('h1');18 await removeVnodes(elementHandle, { childrenOnly: true });19 await browser.close();20})();21const { removeVnodes } = require('playwright/lib/protocol/dom.js');22const { chromium } = require('playwright');23(async () => {24 const browser = await chromium.launch({ headless: false });25 const context = await browser.newContext();26 const page = await context.newPage();27 const elementHandle = await page.$('h1');28 await removeVnodes(elementHandle, { childrenOnly: false });29 await browser.close();30})();
Using AI Code Generation
1const { removeVnodes } = require('playwright/lib/server/dom.js');2const { chromium } = require('playwright');3const { expect } = require('chai');4(async () => {5 const browser = await chromium.launch();6 const page = await browser.newPage();7 await page.setContent(`<div id="parent">8</div>`);9 const parent = await page.$('#parent');10 const child1 = await page.$('#child1');11 const child2 = await page.$('#child2');12 const child3 = await page.$('#child3');13 removeVnodes(parent, child1, child3);14 const parentHTML = await parent.innerHTML();15 expect(parentHTML).to.equal('<div id="child3"></div>');16 await browser.close();17})();
Using AI Code Generation
1const path = require('path');2const { chromium } = require('playwright');3const { removeVnodes } = require('playwright/lib/server/dom.js');4(async () => {5 const browser = await chromium.launch({ headless: false });6 const context = await browser.newContext();7 const page = await context.newPage();8 const element = await page.$('input[name="q"]');9 await removeVnodes(element);10 await page.waitForTimeout(5000);11 await browser.close();12})();
Using AI Code Generation
1const { removeVnodes } = require("playwright/lib/server/dom.js");2const { parse } = require("playwright/lib/server/common/html.js");3const html = `<html><body><div id="parent"><div id="child1"></div><div id="child2"></div></div></body></html>`;4const document = parse(html).documentElement;5const parent = document.querySelector("#parent");6const child1 = document.querySelector("#child1");7const child2 = document.querySelector("#child2");8removeVnodes(parent, child1, child2);9console.log(document.outerHTML);10const { removeVnodes } = require("playwright/lib/server/dom.js");11const { parse } = require("playwright/lib/server/common/html.js");12const html = `<html><body><div id="parent"><div id="child1"></div><div id="child2"></div></div></body></html>`;13const document = parse(html).documentElement;14const parent = document.querySelector("#parent");15const child1 = document.querySelector("#child1");16const child2 = document.querySelector("#child2");17removeVnodes(parent, child1, child2);18console.log(document.outerHTML);19const { removeVnodes } = require("playwright/lib/server/dom.js");20const { parse } = require("playwright/lib/server/common/html.js");21const html = `<html><body><div id="parent"><div id="child1"></div><div id="child2"></div></div></body></html>`;22const document = parse(html).documentElement;23const parent = document.querySelector("#parent");24const child1 = document.querySelector("#child1");25const child2 = document.querySelector("#child2");26removeVnodes(parent, child1, child2);27console.log(document.outerHTML);28const { removeVnodes } = require("playwright/lib/server/dom.js");29const { parse }
Using AI Code Generation
1const { removeVnodes } = require('playwright/lib/server/dom.js');2removeVnodes([node]);3const { removeVnodes } = require('playwright/lib/server/dom.js');4removeVnodes([node]);5const { removeVnodes } = require('playwright/lib/server/dom.js');6removeVnodes([node]);7const { removeVnodes } = require('playwright/lib/server/dom.js');8removeVnodes([node]);9const { removeVnodes } = require('playwright/lib/server/dom.js');10removeVnodes([node]);11const { removeVnodes } = require('playwright/lib/server/dom.js');12removeVnodes([node]);13const { removeVnodes } = require('playwright/lib/server/dom.js');14removeVnodes([node]);15const { removeVnodes } = require('playwright/lib/server/dom.js');16removeVnodes([node]);17const { removeVnodes } = require('playwright/lib/server/dom.js');18removeVnodes([node]);19const { removeVnodes } = require('playwright/lib/server/dom.js');20removeVnodes([node]);21const { removeVnodes } = require('playwright
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!