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 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 | 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 24x 24x 24x 24x 24x 5x 5x 4x 4x 4x 1x 3x 3x 3x 3x 3x 2x 2x 1x 1x 88x 88x 88x 16x 16x 72x 16x 56x 56x 32x 56x 64x 56x 32x 32x 45x 45x 19x 19x 26x 6x 6x 32x 2x 2x 2x 2x 2x 2x | import { isPrimitive } from "../utils/shared";
import createTemplate, { attributeMarkerPrefix, eventMarkerPrefix, nodeMarker } from "./createTemplate";
import { createNode } from "./createNode";
import areEquivalentValues from "./areEquivalentValues";
import getGlobalFunction from "../custom-element/helpers/getGlobalFunction";
export enum NodePatcherRuleTypes {
PATCH_NODE = 1, // Patches either a single node or a collection of nodes
PATCH_ATTRIBUTE,
PATCH_EVENT
}
/**
* The rule to "compile" to patch the DOM node
*/
export interface NodePatcherRule {
/**
* The type of patching to do to that node
*/
type: NodePatcherRuleTypes;
/**
* The path to locate the node
*/
path: number[];
/**
* The name of the attribute to patch
* Only populated if it is an attribute or an event
*/
name?: string;
}
export interface CompiledNodePatcherRule {
/**
* The type of patching to do to that node
*/
type: NodePatcherRuleTypes;
/**
* The node the rule is acting upon
*/
node: Node;
/**
* The name of the attribute to patch
* Only populated if it is an attribute or an event
*/
name?: string,
}
/**
* The patching data with the information to patch a node
*/
export interface NodePatchingData {
/**
* The node to be patched (it does not exist until it is created if needed)
*/
node?: Node;
/**
* The patcher to patch the node with the values according to the rules
*/
patcher: NodePatcher;
/**
* The rules to use to patch the node
*/
rules: CompiledNodePatcherRule[];
/**
* The values used to feed the rules
*/
values: any[];
}
export class NodePatcher {
/**
* The inner HTML of the template content to help debugging
*/
templateString: string;
/**
* The template to clone and generate the node from
*/
template: HTMLTemplateElement;
/**
* The rules to be cloned (compiled)to execute the patching
*/
rules: NodePatcherRule[];
/**
* The index of the dynamic property where the key is
*/
keyIndex: number;
constructor(strings: TemplateStringsArray) {
const {
templateString,
template,
keyIndex
} = createTemplate(strings);
this.templateString = templateString; // To make debugging easier
this.template = template;
this.rules = createNodePatcherRules(template.content);
this.keyIndex = keyIndex;
}
patchNode(parentNode: Node, rules: CompiledNodePatcherRule[], oldValues: any[] = [], newValues: any[] = [], compareValues: boolean = true) {
const {
length
} = rules;
for (let i = 0; i < length; ++i) { // The index of the values of the rules match 1 to 1 with the number of rules
const oldValue = oldValues[i];
let newValue = newValues[i];
if (compareValues === true && // False when the node is created to remove placeholders from the attributes
areEquivalentValues(oldValue, newValue)) {
continue;
}
const rule = rules[i];
const {
type,
name,
node
} = rule;
switch (type) {
case NodePatcherRuleTypes.PATCH_NODE:
{
Iif (Array.isArray(newValue)) {
patchChildren(node, oldValue, newValue);
}
else { // Single node
if (newValue !== null) {
Eif (oldValue === undefined ||
oldValue === null) {
insertBefore(node, newValue, rules);
}
else {
replaceChild(node, newValue, oldValue);
}
}
else { // newValue === undefined || null
Iif (oldValue !== undefined &&
oldValue !== null) {
removeLeftSibling(node);
}
}
}
}
break;
case NodePatcherRuleTypes.PATCH_ATTRIBUTE:
{
patchAttribute(node as HTMLElement, name, oldValue, newValue);
}
break;
case NodePatcherRuleTypes.PATCH_EVENT:
{
const eventName: string = name.slice(2).toLowerCase();
const nameParts = eventName.split('_'); // Just in case it has the capture parameter in the event
const useCapture: boolean = nameParts[1]?.toLowerCase() === 'capture'; // The convention is: eventName_capture for capture. Example onClick_capture
if (typeof newValue === 'string') {
newValue = getGlobalFunction(newValue);
}
if (oldValue === undefined &&
newValue !== undefined) {
node.addEventListener(eventName, newValue, useCapture);
}
else {
node.removeEventListener(eventName, newValue, useCapture);
}
// Remove the attribute from the HTML
(node as HTMLElement).removeAttribute(name);
}
break;
default: throw Error(`patch is not implemented for rule type: ${type}`);
}
}
}
}
function createNodePatcherRules(node: Node, path: number[] = [], rules: NodePatcherRule[] = []): NodePatcherRule[] {
const {
childNodes
} = node;
const {
length
} = childNodes;
if (node.nodeType === Node.COMMENT_NODE &&
(node as Text).data === nodeMarker) {
rules.push({
type: NodePatcherRuleTypes.PATCH_NODE,
path: [...path]
});
return rules; // Comments do not have attributes and children so we are done
}
else if (node.nodeType === Node.TEXT_NODE) {
return rules; // No need to create rules for a text literal
}
else {
const attributes = (node as HTMLElement).attributes;
if (attributes !== undefined) {
rules = createAttributePatcherRules(attributes, path, rules);
}
}
for (let i = 0; i < length; ++i) {
rules = createNodePatcherRules(childNodes[i], [...path, i], rules);
}
return rules;
}
function createAttributePatcherRules(attributes: NamedNodeMap, path: number[], rules: NodePatcherRule[]): NodePatcherRule[] {
const {
length
} = attributes;
for (let i = 0; i < length; ++i) {
const value = attributes[i].value;
if (value.startsWith(attributeMarkerPrefix)) {
const name = value.split(':')[1];
rules.push({
type: NodePatcherRuleTypes.PATCH_ATTRIBUTE,
path,
name
});
}
else if (value.startsWith(eventMarkerPrefix)) {
const name = value.split(':')[1];
rules.push({
type: NodePatcherRuleTypes.PATCH_EVENT,
path,
name
});
}
}
return rules;
}
function patchAttribute(node: HTMLElement, name: string, oldValue: string, newValue: string): boolean {
if (newValue === undefined) { // It was removed in the new virtual node
node.removeAttribute(name);
}
else {
setAttribute(node, name, newValue);
}
return true;
}
function setAttribute(node: HTMLElement, key: string, value: string) {
if (value === 'undefined' ||
value === 'null' ||
value === '' ||
value === 'false') {
node.removeAttribute(key);
(node as any)[key] = value; // Reset the value in any case
}
else {
if (value === 'true') {
node.setAttribute(key, '');
}
else {
const type = typeof value;
if (type === 'function') {
node.removeAttribute(key); // It is similar to an event. Do not show as attribute
(node as any)[key] = value; // Bypass the stringification of the attribute
}
else if (type === 'object') {
value = JSON.stringify(value);
node.setAttribute(key, value);
}
else { // Any other type
node.setAttribute(key, value);
}
}
}
}
function patchChildren(markerNode: Node, oldChildren: any = [], newChildren: any = []): void {
let { length: oldChildrenCount } = oldChildren;
// Map the keyed nodes from the old children nodes
const keyedNodes = new Map<any, Node>();
for (let i = 0; i < oldChildrenCount; ++i) {
const oldChild = oldChildren[i] as HTMLElement;
let key = oldChild.getAttribute?.('key') || null;
if (key !== null) {
keyedNodes.set(key, oldChild);
}
}
const { length: newChildrenCount } = newChildren;
for (let i = 0; i < newChildrenCount; ++i) {
const newChild = newChildren[i] as HTMLElement;
const oldChild = oldChildren[i] as HTMLElement;
if (oldChild === undefined) {
insertBefore(markerNode, newChild, null);
}
else { // oldChild !== undefined
const oldChildKey = oldChild.getAttribute?.('key') || null;
const newChildKey = newChild.getAttribute?.('key') || null;
if (newChildKey === oldChildKey) {
if (oldChild.nodeType === newChild.nodeType &&
oldChild.tagName === newChild.tagName) { // Nodes match
//patch(oldChild, newChild);
}
else { //Nodes do not match
replaceChild(markerNode, newChild, oldChild);
}
}
else { // newChildKey !== oldChildKey
if (keyedNodes.has(newChildKey)) { // Find an existing keyed node
const keyedOldNode = keyedNodes.get(newChildKey);
// if (vnode.patchDom(newNode as any) === true) {
// updated = true;
// }
if (oldChildrenCount >= newChildrenCount) {
insertBefore(markerNode, keyedOldNode, null);
--oldChildrenCount; // The oldNode is removed from the container
}
else {
insertBefore(markerNode, keyedOldNode, null);
++oldChildrenCount; // The newNode is added to the container
}
}
else { // No keyed node found, set the new child
insertBefore(markerNode, newChild, null);
++oldChildrenCount; // Update the count of extra nodes to remove
}
}
}
// if (newChildrenCount > oldChildrenCount) { // Insert the children
// appendChildren(markerNode, newChildren);
// }
// else { // There are old children
// throw new Error('patchChildren is not implemented');
// }
// if (childNode === undefined) { // The node does not have any child at that path
// childNode = node as HTMLElement;
// }
// const parentNode = (childNode.parentNode !== null ?
// childNode.parentNode :
// childNode) as HTMLElement;
// let { childNodes } = parentNode;
// if (Array.isArray(newValue)) { // Collection of nodes
// if (
// childNode.nodeType === Node.COMMENT_NODE &&
// (childNode as Comment).data === nodeMarker
// ) {
// parentNode.replaceChildren(...newValue); // Replace the marker with the children
// // If the newValue is an array of document fragments, they will lose their children after replacement
// // Ensure the children have the instance of the patching data
// childNodes = parentNode.childNodes;
// const length = childNodes.length;
// for (let i = 0; i < length; ++i) {
// const instancePatchingData = newValue[i].__instancePatchingData__;
// if (instancePatchingData !== undefined) {
// (childNodes[i] as any).__instancePatchingData__ = instancePatchingData;
// }
// }
// // Restore the values
// newValuesHolder.newValues = [Array.from(childNodes)];
// }
// else { // Parent node has children
// patchChildren(parentNode, newValue); // Patch the existing children
// }
// }
// else { // Single value or node
// if (newValue instanceof DocumentFragment) {
// parentNode.replaceChild(newValue, childNode);
// }
// else {
// parentNode.replaceChild(document.createTextNode(newValue), childNode);
// }
}
// Remove the extra nodes
for (let i = oldChildrenCount - 1; i >= newChildrenCount; --i) {
removeLeftSibling(markerNode);
}
}
function insertBefore(markerNode: Node, newChild: Node | NodePatchingData, rules: CompiledNodePatcherRule[]): void {
const { parentNode } = markerNode;
Iif (isPrimitive(newChild)) {
newChild = document.createTextNode(newChild.toString());
}
else Eif ((newChild as NodePatchingData).patcher !== undefined) {
// Set the compiled rules
Eif (rules !== null) {
(newChild as NodePatchingData).rules = rules;
}
newChild = createNode(/*parentNode, */newChild as NodePatchingData);
// Transfer the patching data from the document fragment to the parent node
(parentNode as any)._$patchingData = (newChild as any)._$patchingData;
}
parentNode.insertBefore(newChild as Node, markerNode);
}
function replaceChild(markerNode: Node, newChild: Node, oldChild: Node) {
const {
parentNode
} = markerNode;
if (isPrimitive(newChild) &&
isPrimitive(oldChild)) {
// Find the node whose old value matches the old child
const oldChildNode = findPreviousSibling(markerNode,
node => node instanceof Text &&
(node as Text).textContent === oldChild.toString());
(oldChildNode as Text).textContent = newChild.toString();
}
else if ((oldChild as any).patcher !== undefined) { // Patching data
// Find the node whose patching data matches this one
let oldChildNode = null;
findPreviousSibling(
markerNode,
node => testThisOrAnyParent(node, n => {
if (n._$patchingData === undefined) {
return false;
}
const {
patcher,
values
} = n._$patchingData;
const {
patcher: otherPatcher,
values: otherValues
} = oldChild as any;
const r = patcher === otherPatcher &&
values === otherValues;
if (r === true) {
oldChildNode = n;
}
return r;
}));
const {
patcher: oldPatcher,
rules,
values: oldValues
} = (oldChildNode as any)._$patchingData;
const {
patcher,
values
} = (newChild as any);
if (patcher === oldPatcher) {
if ((newChild as any).rules === null) {
(newChild as any).rules = rules; // Transfer the compiled rules
}
oldPatcher.patchNode(oldChildNode, rules, oldValues, values);
(oldChildNode as any)._$patchingData.values = values; // Update the latest values
}
else {
throw new Error('Not implemented');
}
}
else {
parentNode.replaceChild(newChild, oldChild);
}
}
function findPreviousSibling(markerNode: Node, predicate: (node: any) => boolean): Node {
let {
previousSibling
} = markerNode;
while (previousSibling !== null &&
(previousSibling as Comment).textContent !== nodeMarker) {
if (predicate(previousSibling) === true) {
return previousSibling;
}
previousSibling = previousSibling.previousSibling;
}
return null;
}
function testThisOrAnyParent(node: Node, predicate: (n: any) => boolean): boolean {
let parentNode = node;
while (parentNode !== null) {
if (predicate(parentNode) === true) {
return true;
}
parentNode = parentNode.parentNode;
}
return false;
}
function removeLeftSibling(markerNode: Node) {
const {
parentNode,
previousSibling
} = markerNode;
parentNode.removeChild(previousSibling);
}
// function findParentNode(parentNode: Node, predicate: (node: any) => boolean) : Node {
// for (let node = parentNode; node !== null; node = node.parentNode) {
// if (predicate(node) === true) {
// return node;
// }
// }
// return null;
// }
|