Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 9140x 9140x 7177x 7177x 7177x 2x 2x 7175x 1835x 1835x 7177x 9140x 9140x 9140x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 12262x 12262x 12262x 12262x 12262x 12262x 12262x 2101x 2101x 10161x 10161x 10161x 10161x 600x 600x 600x 600x 600x 600x 736x 736x 736x 736x 600x 600x 600x 12262x 12262x 12262x 2701x 2701x 9561x 9561x 9561x 9561x 9561x 9561x 9561x 12262x 397x 397x 397x 397x 397x 38x 38x 38x 38x 397x 397x 397x 397x 397x 397x 397x 397x 27x 27x 27x 27x 27x 2x 2x 2x 25x 25x 25x 27x 397x 397x 397x 358x 358x 358x 190x 190x 190x 190x 358x 359x 359x 359x 359x 368x 15x 15x 359x 359x 359x 359x 359x 359x 359x 397x 344x 344x 344x 397x 397x 397x 397x 397x 397x 397x 397x 397x 397x 12262x 12262x 12262x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 8x 8x 26x 26x 26x 12262x 12262x 12262x 1x 1x 9138x 9138x 9138x 9138x 9138x 9138x 9138x 9138x 10438x 9126x 9126x 9126x 12262x 12262x 12262x 12262x 12262x 12262x 9138x 9138x 9138x 9138x 9138x 9138x 9138x 12262x 12262x 12262x 12262x 12262x 12262x 802x 802x 802x 802x 802x 802x 802x 12262x 1440x 1440x 12262x 1x 1x 6895x 6895x 6895x 12262x 12262x 12262x 12262x 955x 1249x 1249x 1249x 1249x 1x 1x 1248x 1248x 1248x 1248x 1248x 1248x 1240x 1240x 1248x 1248x 1248x 1248x 1249x 955x 12262x 12262x 1815x 1815x 1815x 1815x 5x 5x 5x 1815x 12262x 12262x 12262x 12262x 12262x 2x 2x 12262x 12262x 12262x 90x 90x 12262x 12262x 12262x 153x 153x 153x 2x 2x 153x 153x 12262x 12262x 12262x | import Pool from '../util/pool'; import { VTree, VTreeLike, ValidInput, EMPTY, NODE_TYPE, NodeCache, CreateTreeHookCache, } from '../util/types'; const { isArray } = Array; const { memory } = Pool; const fragmentName = '#document-fragment'; const textName = '#text'; /** * Will flatten fragments that should remain invisible. If a fragment was * intentionally created to be diffed, such as a component, the rawNodeName * will not match and will be preserved. * * @param {VTree[]} vTrees * @param {VTree[]} retVal * * @return {VTree[]} */ function flatten(vTrees, retVal = []) { for (let i = 0; i < vTrees.length; i++) { const vTree = vTrees[i]; if (vTree && vTree.rawNodeName === fragmentName) { flatten(vTree.childNodes, retVal); } else if (vTree) { retVal.push(vTree); } } return retVal; } /** * Typically passed either a single or list of DOM Nodes or a VTreeLike object. * * @param {ValidInput=} input * @param {any=} attributes * @param {any=} childNodes * @param {...any} rest * * @return {VTree} */ export default function createTree(input, attributes, childNodes, ...rest) { /** @type {VTree | null} */ let entry = null; // Reuse a VTree if it has already been created and in the pool. This is an // optimization and reliability check to ensure repeated calls to this // function with the same reference produces consistent results. if (memory.protected.has(input) || memory.allocated.has(input)) { entry = /** @type {VTree } */ (input); } // A fragment is used whenever an array is passed directly into createTree. // This is also what is returned when no input is passed. else if (!input || isArray(input)) { const length = input ? input.length : 0; childNodes = []; // When using an Array copy the Nodes in and ensure a valid top-level tree. for (let i = 0; i < length; i++) { const hasInput = input && !input[i]; if (hasInput) continue; input && childNodes.push(input[i]); } entry = createTree(fragmentName, null, childNodes); } // If a return value was found, end this function early. if (entry) { return entry; } const isObject = typeof input === 'object'; const inputAsHTMLEl = /** @type {HTMLElement} */ (input); // If the input passed has an 'ownerDocument' property, then assume it is a // DOM-like Node object. This means we need to synchronize the DOM and this is // an expensive operation. if (input && isObject && 'ownerDocument' in inputAsHTMLEl) { const { nodeType } = inputAsHTMLEl; // When passed a text node, simply migrate the value over into the new VTree // associate in the NodeCache. if (nodeType === NODE_TYPE.TEXT) { const vTree = createTree(textName, inputAsHTMLEl.nodeValue); NodeCache.set(vTree, inputAsHTMLEl); return vTree; } attributes = {}; childNodes = []; const inputAttrs = inputAsHTMLEl.attributes; // We only scrape attributes from element nodes if they are available. if (inputAsHTMLEl.nodeType === NODE_TYPE.ELEMENT && inputAttrs && inputAttrs.length) { for (let i = 0; i < inputAttrs.length; i++) { const { name, value } = inputAttrs[i]; // If the attribute's value is empty, seek out the property instead. if (value === EMPTY.STR && name in inputAsHTMLEl) { attributes[name] = /** @type {any} */ (input)[name]; continue; } attributes[name] = value; } } // Get the child nodes from an Element or Fragment/Shadow Root. if (inputAsHTMLEl.nodeType === NODE_TYPE.ELEMENT || inputAsHTMLEl.nodeType === NODE_TYPE.FRAGMENT) { childNodes = []; for (let i = 0; i < inputAsHTMLEl.childNodes.length; i++) { /** @type {ValidInput} */ const childNodeElement = (inputAsHTMLEl.childNodes[i]); childNodes.push(createTree(childNodeElement)); } } // FIXME This is going to hurt performance. Is there a better way to find a // VTree from a DOM Node? NodeCache.forEach((domNode, vTree) => { if (domNode === input) { entry = vTree; } }); /** * If no VTree was previously bound this to DOM Node, create a brand new * tree. * * @type {VTree} */ entry = entry || createTree( inputAsHTMLEl.nodeName, attributes, childNodes, ); entry.attributes = { ...entry.attributes, ...attributes }; // Use childNodes that are passed in, otherwise keep the existing nodes. entry.childNodes = childNodes; NodeCache.set(entry, inputAsHTMLEl); return entry; } // Assume any remaining objects are VTree-like. if (isObject) { /** @type {VTreeLike} */ const { rawNodeName, nodeName, nodeValue, attributes, childNodes, children, } = (/** @type {any} */(input)); const treeName = rawNodeName || nodeName; // The priority of a VTreeLike input is nodeValue above all else. If this // value is present, we assume a text-based element and that the intentions // are setting the children to this value. const vTree = createTree( treeName, attributes || null, children || childNodes, ); // Ensure nodeValue is properly copied over. if (nodeValue) { vTree.nodeValue = nodeValue; } return vTree; } // Support JSX-style children being passed. if (rest.length) { childNodes = [childNodes, ...rest]; } // Allocate a new VTree from the pool. entry = Pool.get(); const isTextNode = input === textName; const isString = typeof input === 'string'; // This is a standard HTML element. if (isString) { entry.rawNodeName = input; entry.nodeName = entry.rawNodeName.toLowerCase(); } // Otherwise treat this as a fragment, since we have no idea what type of // element it is. else { entry.rawNodeName = input; entry.nodeName = fragmentName; } // Clear out and reset the remaining VTree attributes. entry.nodeValue = EMPTY.STR; entry.key = EMPTY.STR; entry.childNodes.length = 0; entry.attributes = {}; const useAttributes = isArray(attributes) || typeof attributes !== 'object'; const useNodes = useAttributes ? attributes : childNodes; const allNodes = flatten(isArray(useNodes) ? useNodes : [useNodes]); // Ensure nodeType is set correctly, and if this is a text node, return early. if (isTextNode) { const nodeValue = allNodes.join(EMPTY.STR); entry.nodeType = NODE_TYPE.TEXT; entry.nodeValue = String(nodeValue); return entry; } else if (entry.nodeName === fragmentName) { entry.nodeType = NODE_TYPE.FRAGMENT; } else if (input === '#comment') { entry.nodeType = NODE_TYPE.COMMENT; } else { entry.nodeType = NODE_TYPE.ELEMENT; } // Parse out the child nodes if they exist and are not overwritten by an // attribute. if (useNodes && allNodes.length && (!attributes || !attributes.childNodes)) { for (let i = 0; i < allNodes.length; i++) { const newNode = allNodes[i]; // Merge in arrays. if (isArray(newNode)) { entry.childNodes.push(...newNode); } // Skip over `null` nodes. else if (!newNode) { continue; } // Merge in true fragments, but not components or unknowns. else if (newNode.nodeType === NODE_TYPE.FRAGMENT && typeof newNode.rawNodeName === 'string') { entry.childNodes.push(...newNode.childNodes); } // Assume objects are vTrees. else if (newNode && typeof newNode === 'object') { entry.childNodes.push(createTree(newNode)); } // Last resort treat as text. else { entry.childNodes.push(createTree(textName, null, newNode)); } } } if (attributes && typeof attributes === 'object' && !isArray(attributes)) { entry.attributes = { ...attributes }; // Use childNodes directly from the attributes. if (attributes.childNodes) { const isObject = typeof attributes.childNodes === 'object'; entry.childNodes.push(isObject ? createTree(attributes.childNodes) : createTree('#text', attributes.childNodes)); } } // If is a script tag and has a src attribute, key off that. We have a special // handling of scripts to avoid accidentally re-executing a script when // shifting position. if (entry.nodeName === 'script' && entry.attributes.src) { entry.key = String(entry.attributes.src); } // Set the `key` prop if passed as an attr, overrides `script[src]`. if (entry.attributes && 'key' in entry.attributes) { entry.key = String(entry.attributes.key); } // Only run the `forEach` if there are hooks to run. if (CreateTreeHookCache.size) { CreateTreeHookCache.forEach((fn, retVal) => { // If the hook returns a value, replace the active entry. if (retVal = fn(entry)) { entry = createTree(retVal); } }); } return entry; } |