Best JavaScript code snippet using playwright-internal
vdom.js
Source:vdom.js
1function isObject(obj) {2 return obj !== null && typeof obj === 'object';3}4function isArray(arr) {5 return Array.isArray(arr);6}7function isString(str) {8 return typeof str === 'string';9}10// æ¯æå¤ç§åæ³11// 1. h('div', 'hello');12// 2. h('div', [h(â¦â¦), h(â¦â¦)]);13// 3. h('div', {class: 'app'}, 'hello');14// 4. h('div', {class: 'app'}, [h(â¦â¦), h(â¦â¦)]);15// ç¨js对象表示DOMæ 16function h(tag, props, children) {17 if (isArray(props) || !isObject(props)) {18 children = props;19 props = {};20 }21 return { tag, props, children };22}23// æè½½24// ç¨jsæ æ建çå®çDOMæ ï¼å¹¶æå
¥å°é¡µé¢ææ¡£ä¸ã25function mount(vDom, container, refVNode) {26 if (isString(container)) {27 container = document.querySelector(container);28 }29 const { tag, props, children } = vDom;30 // ä¿åvDom.elï¼æ¹ä¾¿diff31 const el = (vDom.el = document.createElement(tag));32 // å±æ§å¤ç33 if (props) {34 Object.keys(props).forEach((k) => {35 let v = props[k];36 if (k.startsWith('on')) {37 // äºä»¶å¤ç38 el.addEventListener(k.slice(2).toLowerCase(), v);39 } else {40 // å±æ§å¤ç41 el.setAttribute(k, props[k]);42 }43 });44 }45 // åèç¹å¤ç46 if (children) {47 if (isString(children)) {48 el.textContent = children;49 } else if (isArray(children)) {50 children.forEach((child) => {51 if (isString(child)) {52 el.textContent = child;53 } else {54 // éå½55 mount(child, el);56 }57 });58 }59 }60 if (!refVNode) {61 container.appendChild(el);62 } else {63 if (refVNode.el.parentNode === container) {64 container.insertBefore(el, refVNode.el);65 }66 }67}68/*69 old:70 <div id="app">71 <ul class="list">72 <li class="item">item1</li>73 <li class="item">item2</li>74 <li class="item">item3</li>75 </ul>76 </div>77 new1:78 <div id="root" class="root">79 <ul class="list">80 <li class="item">item3</li>81 <li class="item">item2</li>82 <li class="item">item4</li>83 <li class="item">item5</li>84 </ul>85 <p>Hello, P!</p>86 </div>87 new2:88 <div id="root" class="root">89 <p class="list" id="list">90 <a class="item">item3</a>91 <a class="item">item2</a>92 <a class="item">item4</a>93 <a class="item">item6</a>94 <a class="item">item5</a>95 </p>96 <p>Hello, P!</p>97 </div>98 new3: 99 <p>Hello, World!</p>100*/101// å表æ¯è¾102function updateChildren(oldCh, newCh) {103 if (!isArray(oldCh) || !isArray(newCh)) {104 return;105 }106 let oldStartIdx = 0;107 let oldEndIdx = oldCh.length - 1;108 let oldStartVNode = oldCh[oldStartIdx];109 let oldEndVNode = oldCh[oldEndIdx];110 let startIdx = 0;111 let endIdx = newCh.length - 1;112 let startVNode = newCh[startIdx];113 let endVNode = newCh[endIdx];114 let parentEl = oldStartVNode.el.parentNode;115 while (oldStartIdx <= oldEndIdx && startIdx <= endIdx) {116 //? 为ä»ä¹è¦è¿æ ·å¤æï¼è¾¹çå¤çï¼117 if (!oldStartVNode) {118 oldStartVNode = oldCh[++oldStartIdx];119 } else if (!oldEndVNode) {120 oldEndVNode = oldCh[--oldEndIdx];121 } else if (!startVNode) {122 startVNode = newCh[++startIdx];123 } else if (!endVNode) {124 endVNode = newCh[--endIdx];125 }126 // æ§é¦ãæ°é¦æ¯è¾127 if (isSameVNode(oldStartVNode, startVNode)) {128 // å¦æ两个èç¹ç¸åï¼å³å¯å¤ç¨129 patchVNode(oldStartVNode, startVNode);130 oldStartVNode = oldCh[++oldStartIdx];131 startVNode = newCh[++startIdx];132 }133 // æ§å°¾ãæ°å°¾æ¯è¾134 else if (isSameVNode(oldEndVNode, endVNode)) {135 // å¦æ两个èç¹ç¸åï¼å³å¯å¤ç¨136 patchVNode(oldEndVNode, endVNode);137 oldEndVNode = oldCh[--oldEndIdx];138 endVNode = newCh[--endIdx];139 }140 // æ§é¦ãæ°å°¾æ¯è¾141 else if (isSameVNode(oldStartVNode, endVNode)) {142 // æ´æ°æ§é¦vnode143 patchVNode(oldStartVNode, endVNode);144 // 移å¨æ´æ°åçèç¹å°éå°¾145 parentEl.insertBefore(oldStartVNode.el, oldEndVNode.el.nextSibling);146 oldStartVNode = oldCh[++oldStartIdx];147 endVNode = newCh[--endIdx];148 }149 // æ§å°¾ãæ°é¦æ¯è¾150 else if (isSameVNode(oldEndVNode, startVNode)) {151 // æ´æ°æ§å°¾vnode152 patchVNode(oldEndVNode, startVNode);153 // 移å¨æ§å°¾vnodeå°éé¦154 parentEl.insertBefore(oldEndVNode.el, oldStartVNode.el);155 }156 // å¦æ没æ¾å°ç¸åçï¼åä»å©ä½ï¼æ°ï¼vnodeå表åéé¦vnodeï¼å»å©ä½oldVNodeå表ä¸ä¸æ¥æ¾ï¼æ¯å¦åå¨ç¸åçã157 else {158 const idxInOld = findIdxInOld(startVNode, oldCh, oldStartIdx, oldEndIdx);159 // 没ææ¾å°å¯æç¨çvNode160 if (!idxInOld) {161 // 1. å建æ°èç¹162 // 2. æå
¥å°oldStartVNodeåé¢163 mount(startVNode, parentEl, oldStartVNode);164 } else {165 patchVNode(oldCh[idxInOld], startVNode);166 }167 startVNode = newCh[++startIdx];168 }169 }170 // å½æ°èç¹å表è¿æå©ä½æ¶ï¼è¯´ææ¯æ°å¢çï¼éè¦æ°å¢ã171 // å½æ§èç¹å表è¿æå©ä½æ¶ï¼è¯´ææ¯å¤ä½çéè¦å é¤æ172 if (oldStartIdx > oldEndIdx) {173 if (startIdx <= endIdx) {174 // startIdx ~ endIdx ä¹é´çvnodeé½éè¦æ°å¢175 for (let i = startIdx; i <= endIdx; i++) {176 mount(newCh[i], parentEl);177 }178 }179 } else {180 for (let i = oldStartIdx; i <= oldEndIdx; i++) {181 oldCh[i].el.remove();182 }183 }184}185function findIdxInOld(vNode, oldCh, oldStartIdx, oldEndIdx) {186 for (let i = oldStartIdx; i < oldEndIdx; i++) {187 if (isSameVNode(vNode, oldCh[i])) {188 return i;189 }190 }191}192// tagç¸åï¼è®¤ä¸ºnodeç¸åï¼å¯ä»¥å¤ç¨ã193function isSameVNode(oldVNode, vNode) {194 // ææ¶æ²¡èèkey195 return oldVNode.tag === vNode.tag;196}197// æ°æ§èæDOMæ 对æ¯æ´æ°198// æ ¹æ®åæï¼DOMæ æä½æ以ä¸ç¹ç¹ï¼199// 1. å¾å°ä¼è·¨è¶å±çº§å°ç§»å¨DOMå
ç´ ï¼æ以éæ©åå±çº§å
ç´ æ¯è¾çæ¹æ¡ï¼éä½ç®æ³å¤æ度ã200// 2. éç¨æ·±åº¦ä¼å
éåç®æ³201function patch(oldVNode, newVNode) {202 const el = (newVNode.el = oldVNode.el); // å°elåå¨å¨203 // å¦æèç¹tagç¸å204 if (oldVNode.tag === newVNode.tag) {205 patchVNode(oldVNode, newVNode);206 } else {207 // æå
¥æ°èç¹208 mount(newVNode, el.parentNode);209 // å é¤æ§èç¹ã210 el.remove();211 }212}213function patchVNode(oldVNode, newVNode) {214 const el = (newVNode.el = oldVNode.el); // å°elåå¨å¨215 // æ´æ°props216 const oldProps = oldVNode.props;217 const newProps = newVNode.props;218 // æ°æ§propsé½åå¨219 if (oldProps && newProps) {220 const oldPropKeys = Object.keys(oldProps);221 const newPropKeys = Object.keys(newProps);222 for (const ok in oldPropKeys) {223 // æ§propsæçkeyï¼æ°props没æï¼åå é¤ã224 if (!newProps[ok]) {225 el.removeAttribute(ok);226 }227 // if (!newPropKeys.hasOwnProperty(ok)) {228 // el.removeAttribute(ok);229 // }230 }231 for (const nk in newPropKeys) {232 const newValue = newProps[nk];233 const oldValue = oldProps[nk];234 // æ°propsæçkeyï¼æ§propsæï¼ä½å¼ä¸åï¼åæ´æ°ã235 if (oldValue !== newValue) {236 el.setAttribute(nk, newValue);237 }238 }239 }240 // æ°propsåå¨ï¼æ§propsä¸åå¨ï¼åæ·»å ã241 else if (newProps) {242 for (const key in newProps) {243 el.setAttribute(key, newProps[key]);244 }245 }246 // æ°propsä¸åå¨ï¼æ§propsåå¨ï¼åå é¤ã247 else if (oldProps) {248 for (const key in oldProps) {249 el.removeAttribute(key);250 }251 }252 // æ´æ°children253 const oldChildren = oldVNode.children;254 const newChildren = newVNode.children;255 // æ°æ§childrené½åå¨ï¼åæ¯è¾children256 if (newChildren && oldChildren) {257 // æ°children为ææ¬èç¹258 if (typeof newChildren === 'string') {259 el.innerHTML = '';260 el.textContent = newChildren;261 }262 // æ§children为ææ¬èç¹263 else if (typeof oldChildren === 'string') {264 el.textContent = '';265 if (isArray(newChildren)) {266 newChildren.forEach((ch) => {267 mount(ch, el);268 });269 }270 }271 // æ°æ§childrené½ä¸ä¸ºææ¬èç¹272 else {273 updateChildren(oldChildren, newChildren);274 }275 }276 // æ°childrenåå¨ï¼æ§childrenä¸åå¨ï¼åæ·»å ã277 else if (newChildren) {278 newChildren.forEach((ch) => {279 mount(ch, el);280 });281 }282 // æ°childrenä¸åå¨ï¼æ§childrenåå¨ï¼åå é¤ã283 else if (oldChildren) {284 oldChildren.forEach((ch) => {285 el.removeChild(ch.el);286 });287 }...
snabbdomDemo.js
Source:snabbdomDemo.js
...70 // 1.æ°å å æ§å71 else if (sameVnode(oldStartVnode, newStartVnode)) {72 console.log('1å½ä¸')73 // è°ç¨ patchVnode 对æ¯ä¸¤ä¸ªèç¹ç 对象 ææ¬ children74 patchVnode(oldStartVnode, newStartVnode)75 // æé移å¨76 newStartVnode = newCh[++newStartIdx]77 oldStartVnode = oldCh[++oldStartIdx]78 } // 2.æ°å å æ§å79 else if (sameVnode(oldEndVnode, newEndVnode)) {80 console.log('2å½ä¸')81 // è°ç¨ patchVnode 对æ¯ä¸¤ä¸ªèç¹ç 对象 ææ¬ children82 patchVnode(oldEndVnode, newEndVnode)83 // æé移å¨84 newEndVnode = newCh[--newEndIdx]85 oldEndVnode = oldCh[--oldEndIdx]86 } // 3.æ°å å æ§å87 else if (sameVnode(oldStartVnode, newEndVnode)) {88 console.log('3å½ä¸')89 // è°ç¨ patchVnode 对æ¯ä¸¤ä¸ªèç¹ç 对象 ææ¬ children90 patchVnode(oldStartVnode, newEndVnode)91 // çç¥3æ¯éè¦ç§»å¨èç¹ç ææ§åèç¹ ç§»å¨å° æ§å ä¹å92 // insertBefore å¦æåç
§èç¹ä¸ºç©ºï¼å°±æå
¥å°æå å appendChildä¸æ ·93 parentElm.insertBefore(oldStartVnode.elm, oldEndVnode.elm.nextSibling)94 // æé移å¨95 newEndVnode = newCh[--newEndIdx]96 oldStartVnode = oldCh[++oldStartIdx]97 }98 // 4.æ°å å æ§å99 else if (sameVnode(oldEndVnode, newStartVnode)) {100 console.log('4å½ä¸')101 // è°ç¨ patchVnode 对æ¯ä¸¤ä¸ªèç¹ç 对象 ææ¬ children102 patchVnode(oldEndVnode, newStartVnode)103 // çç¥4æ¯ä¹éè¦ç§»å¨èç¹ç ææ§åèç¹ ç§»å¨å° æ§å ä¹å104 parentElm.insertBefore(oldEndVnode.elm, oldStartVnode.elm)105 // æé移å¨106 newStartVnode = newCh[++newStartIdx]107 oldEndVnode = oldCh[--oldEndIdx]108 }109 else {110 console.log('diffåç§ä¼åçç¥é½æ²¡å½ä¸')111 // å½åç§çç¥é½æ²¡æå½ä¸112 // keyMap 为ç¼åï¼è¿æ ·å°±ä¸ç¨æ¯æ¬¡é½éåè对象113 if (!keyMap) {114 // åå§å keyMap115 keyMap = {}116 // ä»oldStartIdxå°oldEndIdxè¿è¡éå117 for (let i = oldStartIdx; i < oldEndIdx; i++) {118 // æ¿ä¸ªæ¯ä¸ªå对象 ç key119 const key = oldCh[i].data.key120 // å¦æ key ä¸ä¸º undefined æ·»å å°ç¼åä¸121 if (!key) keyMap[key] = i122 }123 }124 // å¤æå½å项æ¯å¦åå¨ keyMap ä¸ ,å½åé¡¹æ¶ æ°å(newStartVnode)125 let idInOld = keyMap[newStartIdx.data]126 ? keyMap[newStartIdx.data.key]127 : undefined128 // åå¨çè¯å°±æ¯ç§»å¨æä½129 if (idInOld) {130 console.log('移å¨èç¹')131 // ä» èåèç¹ ååºè¦ç§»å¨ç项132 let moveElm = oldCh[idInOld]133 // è°ç¨ patchVnode è¿è¡å¯¹æ¯ ä¿®æ¹134 patchVnode(moveElm, newStartVnode)135 // å°è¿ä¸é¡¹è®¾ç½®ä¸º undefined136 oldCh[idInOld] = undefined137 // ç§»å¨ èç¹ ,对äºåå¨çèç¹ä½¿ç¨ insertBefore移å¨138 // 移å¨ç æ§å ä¹å ï¼å 为 æ§å ä¸ æ§å ä¹é´çè¦è¢«å é¤139 parentElm.insertBefore(moveElm.elm, oldStartVnode.elm)140 } else {141 console.log('æ·»å æ°èç¹')142 // ä¸åå¨å°±æ¯è¦æ°å¢ç项143 // æ·»å çèç¹è¿æ¯èæèç¹è¦éè¿ createElm è¿è¡å建 DOM144 // åæ ·æ·»å å° æ§å ä¹å145 parentElm.insertBefore(createElm(newStartVnode), oldStartVnode.elm)146 }147 // å¤çå®ä¸é¢çæ·»å åç§»å¨ æä»¬è¦ æ°å æé继ç»åä¸èµ°148 newStartVnode = newCh[++newStartIdx]...
index.js
Source:index.js
...58 oldVnode = vNode(oldVnode.tagName.toLowerCase(), {}, undefined, undefined, oldVnode)59 }60 if (isSameVnode(oldVnode, newVnode)) {61 // ç¸åèç¹62 patchVnode(oldVnode, newVnode)63 } else {64 // ä¸ç¸åï¼åæ´åå é¤65 const dom = createElement(newVnode)66 const parent = oldVnode.elm.parentNode67 parent.insertBefore(dom, oldVnode.elm)68 parent.removeChild(oldVnode.elm)69 }70}71/**72 * @param {*} oldVnode 73 * @param {*} newVnode 74 * 对æ¯èæDOM75 */76function patchVnode(oldVnode, newVnode) {77 // ç¸åèç¹78 if (oldVnode == newVnode) {79 return80 }81 // æ°vnodeåèç¹æ¯ææ¬çæ
åµ82 if (typeof newVnode.text === 'string') {83 if (newVnode.text !== oldVnode.text) {84 oldVnode.elm.innerText = newVnode.text85 }86 } else {87 // æ°vnodeåèç¹æ¯å¯¹è±¡88 // æ§vnodeåèç¹æ¯ææ¬çæ
åµ89 if (typeof oldVnode.text === 'string') {90 // æ¸
空åèç¹91 oldVnode.elm.innerHTML = ''92 // æ°èç¹ä¸æ 93 for (let i = 0; i < newVnode.children.length; i++) {94 const child = newVnode.children[i];95 const dom = createElement(child)96 oldVnode.elm.appendChild(dom)97 }98 } else {99 // æ°æ§vnodeåèç¹é½æ¯å¯¹è±¡100 updateChildren(oldVnode.elm, oldVnode.children, newVnode.children)101 }102 }103}104/**105 * 106 * @param {*} parentElm æ§ççå®DOM107 * @param {*} oldCh æ§çèæDOMåèç¹108 * @param {*} newCh æ°çèæDOMåèç¹109 * 对æ¯æ°æ§èç¹é½æ¯æ°ç»çæ
åµ110 * åç§å½ä¸æ¹å¼111 * â æ°åä¸æ§å112 * â¡æ°åä¸æ§å113 * â¢æ°åä¸æ§å114 * â£æ°åä¸æ§å115 */116function updateChildren(parentElm, oldCh, newCh) {117 // æ°åæé118 let newStartIdx = 0119 // æ°åæé120 let newEndIdx = newCh.length - 1121 // æ§åæé122 let oldStartIdx = 0123 // æ§åæé124 let oldEndIdx = oldCh.length - 1125 // æ°åèæDOM126 let newStartVnode = newCh[newStartIdx]127 // æ°åèæDOM128 let newEndVnode = newCh[newEndIdx]129 // æ§åèæDOM130 let oldStartVnode = oldCh[oldStartIdx]131 // æ§åèæDOM132 let oldEndVnode = oldCh[oldEndIdx]133 // ç¼å没æå½ä¸çèç¹ï¼è¿æ ·å°±å¯ä»¥ä¸ç¨å¤æ¬¡éå134 let keyMap = null135 while (newStartIdx <= newEndIdx && oldStartIdx <= oldEndIdx) {136 // è¦å
忽ç¥å·²ç»æ¯undefinedçä¸è¥¿137 if(newStartIdx===null || newCh[newStartIdx]===undefined){138 newStartVnode = newCh[++newStartIdx]139 }else if(newEndIdx===null||newCh[newEndIdx]===undefined){140 newEndVnode = newCh[--newEndIdx]141 }else if(oldStartIdx===null||oldCh[oldStartIdx]===undefined){142 oldStartVnode=oldCh[++oldStartIdx]143 }else if(oldEndIdx===null||oldCh[oldEndIdx]===undefined){144 oldEndVnode=oldCh[--oldEndIdx]145 }else if (isSameVnode(newStartVnode, oldStartVnode)) {146 // æ°åä¸æ§å147 patchVnode(oldStartVnode, newStartVnode)148 newStartVnode = newCh[++newStartIdx]149 oldStartVnode = oldCh[++oldStartIdx]150 } else if (isSameVnode(newEndVnode, oldEndVnode)) {151 // æ°åä¸æ§å152 patchVnode(oldEndVnode, newEndVnode)153 newEndVnode = newCh[--newEndIdx]154 oldEndVnode = oldCh[--oldEndIdx]155 } else if (isSameVnode(newEndVnode, oldStartVnode)) {156 // æ°åä¸æ§å157 patchVnode(oldStartVnode, newEndVnode)158 // æ¤æ¶è¦ç§»å¨èç¹ï¼ç§»å¨æ°åæåçè¿ä¸ªèç¹å°èèç¹çæ§åçåé¢159 parentElm.insertBefore(oldStartVnode.elm, oldEndVnode.elm.nextSibling)160 newEndVnode = newCh[--newEndIdx]161 oldStartVnode = oldCh[++oldStartIdx]162 } else if (isSameVnode(newStartVnode, oldEndVnode)) {163 // æ°åä¸æ§å164 patchVnode(oldEndVnode, newStartVnode)165 // æ¤æ¶è¦ç§»å¨èç¹ï¼ç§»å¨æ°åæåçè¿ä¸ªèç¹å°èèç¹çæ§åçåé¢166 parentElm.insertBefore(oldEndVnode.elm, oldStartVnode.elm)167 newStartVnode = newCh[++newStartIdx]168 oldEndVnode = oldCh[--oldEndIdx]169 }else{170 // ä¸é¢åç§é½æ²¡æå½ä¸171 if(!keyMap){172 keyMap = {}173 // ç¼åæ§èç¹æ²¡æå½ä¸ç174 for (let i = oldStartIdx; i <= oldEndIdx; i++) {175 const key = oldCh[i].key176 if(key!== undefined){177 keyMap[key] = i178 }179 }180 }181 182 const oldIdx = keyMap[newStartVnode.key]183 if(oldIdx===null || oldIdx===undefined){184 // å
¨æ°çï¼éè¦æ°å¢ï¼å³æ¯æ°å¢çæ
åµ185 parentElm.insertBefore(createElement(newStartVnode),oldStartVnode.elm)186 }else{187 // ä¸æ¯å
¨æ°çï¼éè¦ç§»å¨188 const elmToMove = oldCh[oldIdx]189 patchVnode(elmToMove,newStartVnode)190 oldCh[oldIdx]=undefined191 }192 // 移å¨æé193 newStartVnode = newCh[++newStartIdx]194 }195 }196 if(newStartIdx<=newEndIdx){197 // æ§èç¹å
éåå®æ¯ï¼æ°èç¹è¿æå©ä½ï¼å³ä½¿æ°å¢æ
åµ198 const before = newCh[newEndIdx+1]?newCh[newEndIdx+1].elm:null199 for (let i = newStartIdx; i <= newEndIdx; i++) {200 // insertBeforeå¯èªå¨è¯å«nullï¼ä¸ºnullæ¯èªå¨æå
¥å°æ«å°¾ï¼è·appendChildä¸æ ·201 if(oldStartVnode){202 parentElm.insertBefore(createElement(newCh[i]),oldStartVnode.elm)203 }else{...
updateChildren.js
Source:updateChildren.js
...40 newEndVnode = newCh[--newEndInx]41 } else if (checkSameVnode(oldStartVnode, newStartVnode)) {42 // å½ä¸ æ°ååæ§å43 // ä¸è´å°±è°ç¨ patchVnode æ¥å¯¹æ¯æ°èèç¹44 patchVnode(oldStartVnode, newStartVnode)45 // æ°ååæ§åèç¹é½å¼å§å移46 oldStartVnode = oldCh[++oldStartInx]47 newStartVnode = newCh[++newStartInx]48 } else if (checkSameVnode(oldEndVnode, newEndVnode)) {49 // å½ä¸æ°ååæ§å50 patchVnode(oldEndVnode, newEndVnode)51 // æ°ååæ§åå¼å§å移52 oldEndVnode = oldCh[--oldEndInx]53 newEndVnode = newCh[--newEndInx]54 } else if (checkSameVnode(oldStartVnode, newEndVnode)) {55 // å½ä¸æ°ååæ§å56 patchVnode(oldStartVnode, newEndVnode)57 // ææ°åæåçèç¹æå
¥å°æ§èç¹çæå58 parentElm.insertBefore(oldStartVnode.elm, oldEndVnode.elm.nextSibling)59 oldStartVnode = oldCh[++oldStartInx]60 newEndVnode = newCh[--newEndInx]61 } else if (checkSameVnode(oldEndVnode, newStartVnode)) {62 // å½ä¸æ°ååæ§å63 patchVnode(oldEndVnode, newStartVnode)64 // ææ§åèç¹ç§»å¨å°æ§åçåé¢65 parentElm.insertBefore(oldEndVnode.elm, oldStartVnode.elm)66 oldEndVnode = oldCh[--oldEndInx]67 newStartVnode = newCh[++newStartInx]68 } else {69 // åç§æ
åµé½æ²¡æå½ä¸70 // å¶ä½ä¸ä¸ªkeyMapçæ å°å¯¹è±¡ï¼è¿æ ·å°±ä¸ç¨éåè对象äº71 if (!keyMap) {72 keyMap = {}73 // ä»oldStartInxå°oldEndInxå¼å§å¾ªç¯74 for (let i = oldStartInx; i <= oldEndInx; i++) {75 const key = oldch[i].key76 if (key != undefined) {77 keyMap[key] = i78 }79 }80 }81 // oldIndex 为å½å项çèèç¹åºå·82 const oldIndex = keyMap[newStartVnode.key]83 if (oldIndex == undefined) {84 // å¤æï¼å¦æidxInOldæ¯undefined表示å®æ¯å
¨æ°ç项85 // 被å å
¥ç项ï¼å°±æ¯newStartVnodeè¿é¡¹)ç°ä¸æ¯çæ£çDOMèç¹86 parentElm.insertBefore(createElement(newStartVnode), oldStartVnode.elm);87 } else {88 // å¦æä¸æ¯undefined å°±éè¦æè¿é¡¹ç§»å¨89 const elmToMove = oldCh[oldIndex]90 patchVnode(elmToMove, newStartVnode)91 // æå½å项设置为undefined 表示已ç»å¤çè¿äº92 oldCh[oldIndex] = undefined93 // 移å¨elmToMove94 parentElm.insertBefore(elmToMove.elm, oldStartVnode.elm)95 }96 // 让æéå¾ä¸ç§»å¨ æ¾å°æ°çæ°èç¹é¡¹97 newStartVnode = newCh[++newStartInx]98 }99 }100 // å½å¾ªç¯æ¥åæ¥çæ¯å¦æå©ä½çèç¹101 if (newStartInx <= newEndInx) {102 // éåå©ä½çèç¹å¹¶ä¸å建èç¹åæå
¥èç¹103 for (let i = newStartInx; i <= newEndInx; i++) {104 // insertBefore æ¹æ³ å¦ææå
¥çå
ç´ ä¸ºnull åæ¯é»è®¤æ¯æå
¥æåä¸ä¸ª...
patch.js
Source:patch.js
...11 // å¤æoldVnodeånewVnodeæ¯å¦æ¯åä¸ä¸ªèç¹12 if (oldVnode.key == newVnode.key && oldVnode.sel == newVnode.sel) {13 console.log("æ¯åä¸ä¸ªèç¹");14 // å¤ææ°æ§èç¹æ¯å¦æ¯åä¸ä¸ªå¯¹è±¡15 patchVnode(oldVnode,newVnode);16 } else {17 console.log("ä¸æ¯åä¸ä¸ªèç¹ï¼æ´åæå
¥å¿å¾èç¹ï¼å é¤æ§ç");18 let newVnodeElm = createElement(newVnode);19 // æå
¥èèç¹ä¹å20 if (oldVnode.elm.parentNode != undefined && newVnodeElm) {21 oldVnode.elm.parentNode.insertBefore(newVnodeElm, oldVnode.elm)22 }23 // å æèèç¹24 oldVnode.elm.parentNode.removeChild(oldVnode.elm);25 }...
Using AI Code Generation
1const {patchVnode} = require('playwright');2const {patchVnode} = require('playwright');3const {patchVnode} = require('playwright');4const {patchVnode} = require('playwright');5const {patchVnode} = require('playwright');6const {patchVnode} = require('playwright');7patchVnode((vnode, oldVnode) => {8 if (vnode.elm && vnode.elm.tagName === 'INPUT') {9 vnode.elm.setAttribute('data-vnode', 'true');10 }11});12const {patchVnode} = require('playwright');13const {patchVnode} = require('playwright');14const {patchVnode} = require('playwright');15const {patchVnode} = require('playwright');16const {patchVnode} = require('playwright');17const {patchVnode} = require('playwright');18const {patchVnode} = require('playwright');19patchVnode((vnode, oldVnode) => {20 if (vnode.elm && vnode.elm.tagName === 'INPUT') {21 vnode.elm.setAttribute('data-vnode', 'true');22 }23});24test('My test', async ({ page }) => {25 await page.click('inpu
Using AI Code Generation
1const { patchVnode } = require('playwright/lib/server/domPatch');2const { JSDOM } = require('jsdom');3const dom = new JSDOM(`<!DOCTYPE html><p>Hello world</p>`);4const vnode = dom.window.document.querySelector('p');5patchVnode(vnode, 'Hello world', 'Hello world, again');6console.log(dom.serialize());7const { patchVnode } = require('playwright/lib/server/domPatch');8const { JSDOM } = require('jsdom');9const dom = new JSDOM(`<!DOCTYPE html><p>Hello world</p>`);10const vnode = dom.window.document.querySelector('p');11patchVnode(vnode, 'Hello world', 'Hello world, again');12console.log(dom.serialize());13 OS: Linux 5.4 Ubuntu 20.04.3 LTS (Focal Fossa)
Using AI Code Generation
1const { patchVnode } = require('playwright/lib/server/domPatchVnode');2const { h } = require('playwright/lib/server/domPatchVnode');3const { patch } = require('playwright/lib/server/domPatchVnode');4const { VNode } = require('playwright/lib/server/domPatchVnode');5const { createVNode } = require('playwright/lib/server/domPatchVnode');6const vnode = createVNode('div', { id: 'foo' }, [7 createVNode('span', { id: 'bar' }, 'Hello'),8 createVNode('span', { id: 'baz' }, 'World'),9]);10const vnode1 = createVNode('div', { id: 'foo' }, [11 createVNode('span', { id: 'bar' }, 'Hello'),12 createVNode('span', { id: 'baz' }, 'World'),13]);14const vnode2 = createVNode('div', { id: 'foo' }, [15 createVNode('span', { id: 'bar' }, 'Hello'),16 createVNode('span', { id: 'baz' }, 'World'),17]);18const vnode3 = createVNode('div', { id: 'foo' }, [19 createVNode('span', { id: 'bar' }, 'Hello'),20 createVNode('span', { id: 'baz' }, 'World'),21]);22const vnode4 = createVNode('div', { id: 'foo' }, [23 createVNode('span', { id: 'bar' }, 'Hello'),24 createVNode('span', { id: 'baz' }, 'World'),25]);26const vnode5 = createVNode('div', { id: 'foo' }, [27 createVNode('span', { id: 'bar' }, 'Hello'),28 createVNode('span', { id: 'baz' }, 'World'),29]);30const vnode6 = createVNode('div
Using AI Code Generation
1const { patchVnode } = require('playwright-core/lib/server/domPatch');2const { parseHTML } = require('playwright-core/lib/server/domPatch');3const { createVNode } = require('playwright-core/lib/server/domPatch');4const { createDocument } = require('playwright-core/lib/server/domPatch');5const { createVNodeWithText } = require('playwright-core/lib/server/domPatch');6const { createTextVNode } = require('playwright-core/lib/server/domPatch');7const { createVNodeWithComment } = require('playwright-core/lib/server/domPatch');8const { createCommentVNode } = require('playwright-core/lib/server/domPatch');9const { createVNodeWithDocument } = require('playwright-core/lib/server/domPatch');10const { createVNodeWithDocumentType } = require('playwright-core/lib/server/domPatch');11const { createDocumentTypeVNode } = require('playwright-core/lib/server/domPatch');12const { createVNodeWithElement } = require('playwright-core/lib/server/domPatch');13const { createElementVNode } = require('playwright-core/lib/server/domPatch');14const { createVNodeWithFragment } = require('playwright-core/lib/server/domPatch');15const { createFragmentVNode } = require('playwright-core/lib/server/domPatch');16const { createVNodeWithShadowRoot } = require('playwright-core/lib/server/domPatch');17const { createShadowRootVNode } = require('playwright-core/lib/server/domPatch');18const { createVNodeWithTextContent } = require('playwright-core/lib/server/domPatch');19const { createVNodeWithElementAttribute } = require('playwright-core/lib/server/domPatch');20const { createVNodeWithElementProperty } = require('playwright-core/lib/server/domPatch');21const { createVNodeWithElementEvent } = require('playwright-core/lib/server/domPatch');22const { createVNodeWithElementEventProperty } = require('playwright-core/lib/server/domPatch');23const { createVNodeWithElementEventOptions } = require('playwright-core/lib/server/domPatch');24const { createVNodeWithElementStyle } = require('playwright-core/lib/server/domPatch');25const { createVNodeWithElementClass } = require('playwright-core/lib/server/domPatch');26const { createVNodeWithElementChild } = require('playwright-core/lib/server/domPatch');27const { createVNodeWith
Using AI Code Generation
1const { patchVnode } = require('playwright-core/lib/server/supplements/recorder/recorderSupplement.js');2patchVnode(vnode, context, callback);3const { patchVnode } = require('playwright-core/lib/server/supplements/recorder/recorderSupplement.js');4patchVnode(vnode, context, callback);5const { patchVnode } = require('playwright-core/lib/server/supplements/recorder/recorderSupplement.js');6patchVnode(vnode, context, callback);7const { patchVnode } = require('playwright-core/lib/server/supplements/recorder/recorderSupplement.js');8patchVnode(vnode, context, callback);9const { patchVnode } = require('playwright-core/lib/server/supplements/recorder/recorderSupplement.js');10patchVnode(vnode, context, callback);11const { patchVnode } = require('playwright-core/lib/server/supplements/recorder/recorderSupplement.js');12patchVnode(vnode, context, callback);13const { patchVnode } = require('playwright-core/lib/server/supplements/recorder/recorderSupplement.js');14patchVnode(vnode, context, callback);15const { patchVnode } = require('playwright-core/lib/server
Using AI Code Generation
1const { patchVnode } = require('playwright/lib/server/domPatch');2const { parse } = require('playwright/lib/server/domParser');3const html = `<div id="parent"><div id="child">Child</div></div>`;4const dom = parse(html);5const parent = dom.querySelector('#parent');6const child = dom.querySelector('#child');7patchVnode(child, { attributes: { id: 'newId' } }, dom);8console.log(dom.toString());
Using AI Code Generation
1const { patchVnode } = require('@playwright/test/lib/server/vnodePatch');2const { parse } = require('playwright-core/lib/server/inspector/dom.js');3const vNode = parse('<div class="test">Hello World</div>');4const newVNode = parse('<div class="test">Hello World</div>');5const patch = patchVnode(vNode, newVNode);6console.log(patch);7const { patchVnode } = require('@playwright/test/lib/server/vnodePatch');8const { parse } = require('playwright-core/lib/server/inspector/dom.js');9const vNode = parse('<div class="test">Hello World</div>');10const newVNode = parse('<div class="test">Hello World</div>');11const patch = patchVnode(vNode, newVNode);12console.log(patch);13const { patchVnode } = require('@playwright/test/lib/server/vnodePatch');14const { parse } = require('playwright-core/lib/server/inspector/dom.js');15const vNode = parse('<div class="test">Hello World</div>');16const newVNode = parse('<div class="test">Hello World</div>');17const patch = patchVnode(vNode, newVNode);18console.log(patch);19const { patchVnode }
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!!