patch.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import { isArray, isObject } from '../core/util.js';
  2. import { createElement, createVNode, XMLNS, XML_NAMESPACE, XLINKNS } from './core.js';
  3. import * as api from './domapi.js';
  4. var colonChar = 58;
  5. var xChar = 120;
  6. var emptyNode = createVNode('', '');
  7. function isUndef(s) {
  8. return s === undefined;
  9. }
  10. function isDef(s) {
  11. return s !== undefined;
  12. }
  13. function createKeyToOldIdx(children, beginIdx, endIdx) {
  14. var map = {};
  15. for (var i = beginIdx; i <= endIdx; ++i) {
  16. var key = children[i].key;
  17. if (key !== undefined) {
  18. if (process.env.NODE_ENV !== 'production') {
  19. if (map[key] != null) {
  20. console.error("Duplicate key " + key);
  21. }
  22. }
  23. map[key] = i;
  24. }
  25. }
  26. return map;
  27. }
  28. function sameVnode(vnode1, vnode2) {
  29. var isSameKey = vnode1.key === vnode2.key;
  30. var isSameTag = vnode1.tag === vnode2.tag;
  31. return isSameTag && isSameKey;
  32. }
  33. function createElm(vnode) {
  34. var i;
  35. var children = vnode.children;
  36. var tag = vnode.tag;
  37. if (isDef(tag)) {
  38. var elm = (vnode.elm = createElement(tag));
  39. updateAttrs(emptyNode, vnode);
  40. if (isArray(children)) {
  41. for (i = 0; i < children.length; ++i) {
  42. var ch = children[i];
  43. if (ch != null) {
  44. api.appendChild(elm, createElm(ch));
  45. }
  46. }
  47. }
  48. else if (isDef(vnode.text) && !isObject(vnode.text)) {
  49. api.appendChild(elm, api.createTextNode(vnode.text));
  50. }
  51. }
  52. else {
  53. vnode.elm = api.createTextNode(vnode.text);
  54. }
  55. return vnode.elm;
  56. }
  57. function addVnodes(parentElm, before, vnodes, startIdx, endIdx) {
  58. for (; startIdx <= endIdx; ++startIdx) {
  59. var ch = vnodes[startIdx];
  60. if (ch != null) {
  61. api.insertBefore(parentElm, createElm(ch), before);
  62. }
  63. }
  64. }
  65. function removeVnodes(parentElm, vnodes, startIdx, endIdx) {
  66. for (; startIdx <= endIdx; ++startIdx) {
  67. var ch = vnodes[startIdx];
  68. if (ch != null) {
  69. if (isDef(ch.tag)) {
  70. var parent_1 = api.parentNode(ch.elm);
  71. api.removeChild(parent_1, ch.elm);
  72. }
  73. else {
  74. api.removeChild(parentElm, ch.elm);
  75. }
  76. }
  77. }
  78. }
  79. export function updateAttrs(oldVnode, vnode) {
  80. var key;
  81. var elm = vnode.elm;
  82. var oldAttrs = oldVnode && oldVnode.attrs || {};
  83. var attrs = vnode.attrs || {};
  84. if (oldAttrs === attrs) {
  85. return;
  86. }
  87. for (key in attrs) {
  88. var cur = attrs[key];
  89. var old = oldAttrs[key];
  90. if (old !== cur) {
  91. if (cur === true) {
  92. elm.setAttribute(key, '');
  93. }
  94. else if (cur === false) {
  95. elm.removeAttribute(key);
  96. }
  97. else {
  98. if (key.charCodeAt(0) !== xChar) {
  99. elm.setAttribute(key, cur);
  100. }
  101. else if (key === 'xmlns:xlink' || key === 'xmlns') {
  102. elm.setAttributeNS(XMLNS, key, cur);
  103. }
  104. else if (key.charCodeAt(3) === colonChar) {
  105. elm.setAttributeNS(XML_NAMESPACE, key, cur);
  106. }
  107. else if (key.charCodeAt(5) === colonChar) {
  108. elm.setAttributeNS(XLINKNS, key, cur);
  109. }
  110. else {
  111. elm.setAttribute(key, cur);
  112. }
  113. }
  114. }
  115. }
  116. for (key in oldAttrs) {
  117. if (!(key in attrs)) {
  118. elm.removeAttribute(key);
  119. }
  120. }
  121. }
  122. function updateChildren(parentElm, oldCh, newCh) {
  123. var oldStartIdx = 0;
  124. var newStartIdx = 0;
  125. var oldEndIdx = oldCh.length - 1;
  126. var oldStartVnode = oldCh[0];
  127. var oldEndVnode = oldCh[oldEndIdx];
  128. var newEndIdx = newCh.length - 1;
  129. var newStartVnode = newCh[0];
  130. var newEndVnode = newCh[newEndIdx];
  131. var oldKeyToIdx;
  132. var idxInOld;
  133. var elmToMove;
  134. var before;
  135. while (oldStartIdx <= oldEndIdx && newStartIdx <= newEndIdx) {
  136. if (oldStartVnode == null) {
  137. oldStartVnode = oldCh[++oldStartIdx];
  138. }
  139. else if (oldEndVnode == null) {
  140. oldEndVnode = oldCh[--oldEndIdx];
  141. }
  142. else if (newStartVnode == null) {
  143. newStartVnode = newCh[++newStartIdx];
  144. }
  145. else if (newEndVnode == null) {
  146. newEndVnode = newCh[--newEndIdx];
  147. }
  148. else if (sameVnode(oldStartVnode, newStartVnode)) {
  149. patchVnode(oldStartVnode, newStartVnode);
  150. oldStartVnode = oldCh[++oldStartIdx];
  151. newStartVnode = newCh[++newStartIdx];
  152. }
  153. else if (sameVnode(oldEndVnode, newEndVnode)) {
  154. patchVnode(oldEndVnode, newEndVnode);
  155. oldEndVnode = oldCh[--oldEndIdx];
  156. newEndVnode = newCh[--newEndIdx];
  157. }
  158. else if (sameVnode(oldStartVnode, newEndVnode)) {
  159. patchVnode(oldStartVnode, newEndVnode);
  160. api.insertBefore(parentElm, oldStartVnode.elm, api.nextSibling(oldEndVnode.elm));
  161. oldStartVnode = oldCh[++oldStartIdx];
  162. newEndVnode = newCh[--newEndIdx];
  163. }
  164. else if (sameVnode(oldEndVnode, newStartVnode)) {
  165. patchVnode(oldEndVnode, newStartVnode);
  166. api.insertBefore(parentElm, oldEndVnode.elm, oldStartVnode.elm);
  167. oldEndVnode = oldCh[--oldEndIdx];
  168. newStartVnode = newCh[++newStartIdx];
  169. }
  170. else {
  171. if (isUndef(oldKeyToIdx)) {
  172. oldKeyToIdx = createKeyToOldIdx(oldCh, oldStartIdx, oldEndIdx);
  173. }
  174. idxInOld = oldKeyToIdx[newStartVnode.key];
  175. if (isUndef(idxInOld)) {
  176. api.insertBefore(parentElm, createElm(newStartVnode), oldStartVnode.elm);
  177. }
  178. else {
  179. elmToMove = oldCh[idxInOld];
  180. if (elmToMove.tag !== newStartVnode.tag) {
  181. api.insertBefore(parentElm, createElm(newStartVnode), oldStartVnode.elm);
  182. }
  183. else {
  184. patchVnode(elmToMove, newStartVnode);
  185. oldCh[idxInOld] = undefined;
  186. api.insertBefore(parentElm, elmToMove.elm, oldStartVnode.elm);
  187. }
  188. }
  189. newStartVnode = newCh[++newStartIdx];
  190. }
  191. }
  192. if (oldStartIdx <= oldEndIdx || newStartIdx <= newEndIdx) {
  193. if (oldStartIdx > oldEndIdx) {
  194. before = newCh[newEndIdx + 1] == null ? null : newCh[newEndIdx + 1].elm;
  195. addVnodes(parentElm, before, newCh, newStartIdx, newEndIdx);
  196. }
  197. else {
  198. removeVnodes(parentElm, oldCh, oldStartIdx, oldEndIdx);
  199. }
  200. }
  201. }
  202. function patchVnode(oldVnode, vnode) {
  203. var elm = (vnode.elm = oldVnode.elm);
  204. var oldCh = oldVnode.children;
  205. var ch = vnode.children;
  206. if (oldVnode === vnode) {
  207. return;
  208. }
  209. updateAttrs(oldVnode, vnode);
  210. if (isUndef(vnode.text)) {
  211. if (isDef(oldCh) && isDef(ch)) {
  212. if (oldCh !== ch) {
  213. updateChildren(elm, oldCh, ch);
  214. }
  215. }
  216. else if (isDef(ch)) {
  217. if (isDef(oldVnode.text)) {
  218. api.setTextContent(elm, '');
  219. }
  220. addVnodes(elm, null, ch, 0, ch.length - 1);
  221. }
  222. else if (isDef(oldCh)) {
  223. removeVnodes(elm, oldCh, 0, oldCh.length - 1);
  224. }
  225. else if (isDef(oldVnode.text)) {
  226. api.setTextContent(elm, '');
  227. }
  228. }
  229. else if (oldVnode.text !== vnode.text) {
  230. if (isDef(oldCh)) {
  231. removeVnodes(elm, oldCh, 0, oldCh.length - 1);
  232. }
  233. api.setTextContent(elm, vnode.text);
  234. }
  235. }
  236. export default function patch(oldVnode, vnode) {
  237. if (sameVnode(oldVnode, vnode)) {
  238. patchVnode(oldVnode, vnode);
  239. }
  240. else {
  241. var elm = oldVnode.elm;
  242. var parent_2 = api.parentNode(elm);
  243. createElm(vnode);
  244. if (parent_2 !== null) {
  245. api.insertBefore(parent_2, vnode.elm, api.nextSibling(elm));
  246. removeVnodes(parent_2, [oldVnode], 0, 0);
  247. }
  248. }
  249. return vnode;
  250. }