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 | 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 52x 52x 52x 52x 52x 52x 52x 190x 190x 395x 395x 395x 395x 156x 156x 10x 10x 17x 17x 10x 146x 132x 132x 110x 22x 22x 132x 156x 223x 223x 16x 16x 16x 16x 16x 16x 16x 16x 76x 76x 162x 162x 162x 53x 109x 109x 109x 68x 9x 59x 49x 7x 42x 7x 35x 10x 10x 8x 2x 68x 32x 32x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 16x 16x 16x 10x 9x 9x 16x 16x 16x 4x 12x 12x 12x 7x 7x 5x 4x 4x 4x 1x 1x 1x 1x 1x 1x 9x 4x 40x 40x 40x 11x 11x 2x 9x 9x 11x | import { isPrimitive } from "../utils/shared";
import createTemplate, { NodePatcherRuleTypes } from "./createTemplate";
import areEquivalentValues from "./areEquivalentValues";
import getGlobalFunction from "../custom-element/helpers/getGlobalFunction";
import createNodePatcherRules, { NodePatcherRule } from "./createNodePatcherRules";
import setAttribute from "./dom/setAttribute";
import removeLeftSiblings from "./dom/removeLeftSiblings";
import removeLeftSibling from "./dom/removeLeftSibling";
import replaceChild from "./dom/replaceChild";
import mountNodes from "./mountNodes";
import createNodes from "./createNodes";
import updateNodes from "./updateNodes";
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;
/**
* Whether the content of the template has a single element
*/
isSingleElement: boolean;
constructor(strings: TemplateStringsArray) {
const {
templateString,
template,
keyIndex
} = createTemplate(strings);
this.templateString = templateString; // To make debugging easier
this.template = template;
const childNodes = template.content.childNodes;
this.isSingleElement = childNodes.length === 1 &&
childNodes[0].nodeType === Node.ELEMENT_NODE;
this.rules = createNodePatcherRules(template.content);
this.keyIndex = keyIndex;
}
firstPatch(parentNode: Node, rules: CompiledNodePatcherRule[], values: any[] = []) {
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
let value = values[i];
const rule = rules[i];
const {
type,
name,
node
} = rule;
switch (type) {
case NodePatcherRuleTypes.PATCH_NODE:
{
const { parentNode } = node;
if (Array.isArray(value)) {
const df = document.createDocumentFragment();
value.forEach(v => {
Iif (isPrimitive(v)) {
const n = document.createTextNode(value.toString());
parentNode.insertBefore(n, node);
}
else {
mountNodes(df, v);
}
});
parentNode.insertBefore(df, node);
}
else if (value !== undefined &&
value !== null) {
let n = value;
if (isPrimitive(value)) {
n = document.createTextNode(value.toString());
}
else Eif ((value as NodePatchingData).patcher !== undefined) {
n = createNodes(value as NodePatchingData);
}
parentNode.insertBefore(n, node);
}
}
break;
case NodePatcherRuleTypes.PATCH_ATTRIBUTE:
{
setAttribute(node as HTMLElement, name, value);
}
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
Iif (typeof value === 'string') {
value = getGlobalFunction(value);
}
Eif (value !== undefined) {
node.addEventListener(eventName, value, useCapture);
}
// Remove the attribute from the HTML
(node as HTMLElement).removeAttribute(name);
}
break;
default: throw Error(`firstPatch is not implemented for rule type: ${type}`);
}
}
}
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:
{
if (Array.isArray(newValue)) {
patchChildren(node, oldValue, newValue);
}
else { // Single node
if (newValue !== undefined &&
newValue !== null) {
if (oldValue === undefined ||
oldValue === null) {
insertBefore(node, newValue, rules);
}
else {
if (oldValue.patcher !== undefined &&
oldValue.patcher === newValue.patcher) {
updateNodes(node, oldValue, newValue);
}
else {
replaceChild(node, newValue, oldValue);
}
}
}
else { // newValue === undefined || null
Eif (oldValue !== undefined &&
oldValue !== null) {
if (Array.isArray(oldValue) || // Several nodes to remove
oldValue.patcher !== undefined) {
removeLeftSiblings(node);
}
else { // Only one node to remove
removeLeftSibling(node);
}
}
}
}
}
break;
case NodePatcherRuleTypes.PATCH_ATTRIBUTE:
{
setAttribute(node as HTMLElement, name, 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
Iif (typeof newValue === 'string') {
newValue = getGlobalFunction(newValue);
}
Iif (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 patchChildren(markerNode: Node, oldChildren: any = [], newChildren: any = []): void {
oldChildren = oldChildren || [];
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];
let key = getKey(oldChild);
if (key !== null) {
keyedNodes.set(key, oldChild);
}
}
const { length: newChildrenCount } = newChildren;
for (let i = 0; i < newChildrenCount; ++i) {
const newChild = newChildren[i];
const oldChild = oldChildren[i];
if (oldChild === undefined) {
insertBefore(markerNode, newChild, null);
}
else { // oldChild !== undefined
const oldChildKey = getKey(oldChild);
const newChildKey = getKey(newChild);
if (newChildKey === oldChildKey) {
Iif (isPrimitive(oldChild)) {
replaceChild(markerNode, newChild, oldChild)
}
else {
updateNodes((oldChild as any).node, oldChild as any, newChild as any);
}
}
else { // newChildKey !== oldChildKey
if (keyedNodes.has(newChildKey)) { // Find an existing keyed node
const keyedOldNode = keyedNodes.get(newChildKey);
Eif (oldChildrenCount >= newChildrenCount) {
replaceChild(markerNode, newChild, oldChild);
}
else {
insertBefore(markerNode, keyedOldNode, null);
++oldChildrenCount; // The newNode is added to the container
}
}
else { // No keyed node found, set the new child at the current index
const { parentNode } = markerNode;
let newNode: Node;
Iif (isPrimitive(newChild)) {
newNode = document.createTextNode(newChild.toString());
}
else Eif ((newChild as NodePatchingData).patcher !== undefined) {
newNode = createNodes(newChild as NodePatchingData);
}
// else { newChild is never an actual node?
// newNode = newChild;
// }
const existingChild = parentNode.childNodes[i + 1]; // Skip the begin marker node
existingChild.replaceWith(newNode);
// insertBefore(existingChild, newChild, null);
// ++oldChildrenCount; // Update the count of extra nodes to remove
}
}
}
}
// Remove the extra nodes
for (let i = oldChildrenCount - 1; i >= newChildrenCount; --i) {
removeLeftSibling(markerNode);
}
}
/**
* Retrieves the key from the patching data
* @param patchingData
* @returns
*/
function getKey(patchingData: NodePatchingData) {
const {
patcher,
values
} = patchingData as NodePatchingData;
const {
keyIndex
} = patcher;
return keyIndex !== undefined ?
values[keyIndex] :
null;
}
function insertBefore(markerNode: Node, newChild: Node | NodePatchingData, rules: CompiledNodePatcherRule[]): void {
const { parentNode } = markerNode;
if (isPrimitive(newChild)) {
newChild = document.createTextNode(newChild.toString());
}
else Eif ((newChild as NodePatchingData).patcher !== undefined) {
newChild = createNodes(newChild as NodePatchingData);
}
parentNode.insertBefore(newChild as Node, markerNode);
} |