-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathbefore-mount.js
More file actions
96 lines (75 loc) · 2.16 KB
/
before-mount.js
File metadata and controls
96 lines (75 loc) · 2.16 KB
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
import componentWillUnmount from './component-will-unmount';
import { invokeRef, invokeRefsForVTree } from './invoke-refs';
import diff from '../util/binding';
import { Transaction } from '../util/types';
const { NodeCache, PATCH_TYPE, decodeEntities, createNode } = diff.Internals;
const uppercaseEx = /[A-Z]/g;
/**
* @param {Transaction} transaction
*/
export default transaction => {
if (transaction.aborted) {
return;
}
const { patches } = transaction;
const { length } = patches;
let i = 0;
while (true) {
const patchType = patches[i];
// Exhausted remaining patches.
if (i === length) {
break;
}
switch(patchType) {
case PATCH_TYPE.SET_ATTRIBUTE: {
const vTree = patches[i + 1];
const name = patches[i + 2];
const value = patches[i + 3];
uppercaseEx.lastIndex = 0;
// Normalize uppercase attributes.
if (uppercaseEx.test(name)) {
uppercaseEx.lastIndex = 0;
const newName = name.replace(
uppercaseEx,
(/** @type {string} */ ch) => `-${ch.toLowerCase()}`,
);
if (value && typeof value === 'string') {
const decodedValue = decodeEntities(value);
if (NodeCache.has(vTree)) {
/** @type {HTMLElement} */(NodeCache.get(vTree)).setAttribute(newName, decodedValue);
}
}
}
// TBD Remove because invokeRefs is handled in afterMount
if (name === 'ref') {
invokeRef(createNode(vTree), vTree);
}
i += 4;
break;
}
case PATCH_TYPE.REMOVE_ATTRIBUTE: {
i += 3;
break;
}
case PATCH_TYPE.REPLACE_CHILD: {
const oldTree = patches[i + 2];
invokeRefsForVTree(oldTree, null);
componentWillUnmount(oldTree);
i += 3;
break;
}
case PATCH_TYPE.NODE_VALUE:
case PATCH_TYPE.INSERT_BEFORE: {
i += 4;
break;
}
case PATCH_TYPE.REMOVE_CHILD: {
const vTree = patches[i + 1];
invokeRefsForVTree(vTree, null);
componentWillUnmount(vTree);
i += 2;
break;
}
}
}
};