-
Notifications
You must be signed in to change notification settings - Fork 151
/
serialize.ts
99 lines (94 loc) · 4.11 KB
/
serialize.ts
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
/**
* Copyright 2018 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { HydrateableNode, NodeType } from '../transfer/TransferrableNodes';
import { TransferrableKeys } from '../transfer/TransferrableKeys';
import { NumericBoolean } from '../utils';
import { WorkerDOMConfiguration, HydrationFilterPredicate } from './configuration';
import { applyDefaultInputListener, sendValueChangeOnAttributeMutation } from './commands/event-subscription';
import { WorkerContext } from './worker';
const NODES_ALLOWED_TO_TRANSMIT_TEXT_CONTENT = [NodeType.COMMENT_NODE, NodeType.TEXT_NODE];
/**
* Serializes a DOM element for transport to the worker.
* @param element
* @param minimizeString Function for minimizing strings for optimized ferrying across postMessage.
*/
function createHydrateableNode(
element: RenderableElement,
minimizeString: (value: string) => number,
hydrateFilter: HydrationFilterPredicate,
workerContext: WorkerContext,
): HydrateableNode {
const filteredChildNodes = [].slice.call(element.childNodes).filter(hydrateFilter);
const hydrated: HydrateableNode = {
[TransferrableKeys.index]: element._index_,
[TransferrableKeys.transferred]: NumericBoolean.FALSE,
[TransferrableKeys.nodeType]: element.nodeType,
[TransferrableKeys.localOrNodeName]: minimizeString(element.localName || element.nodeName),
[TransferrableKeys.childNodes]: filteredChildNodes.map((child: RenderableElement) =>
createHydrateableNode(child, minimizeString, hydrateFilter, workerContext),
),
[TransferrableKeys.attributes]: [].map.call(element.attributes || [], (attribute: Attr) => [
minimizeString(attribute.namespaceURI || 'null'),
minimizeString(attribute.name),
minimizeString(attribute.value),
]),
};
if (element.namespaceURI != null) {
hydrated[TransferrableKeys.namespaceURI] = minimizeString(element.namespaceURI);
}
if (NODES_ALLOWED_TO_TRANSMIT_TEXT_CONTENT.includes(element.nodeType) && (element as Text).textContent !== null) {
hydrated[TransferrableKeys.textContent] = minimizeString(element.textContent as string);
}
applyDefaultInputListener(workerContext, element);
sendValueChangeOnAttributeMutation(workerContext, element);
return hydrated;
}
/**
* @param element
*/
export function createHydrateableRootNode(
element: RenderableElement,
config: WorkerDOMConfiguration,
workerContext: WorkerContext,
): { skeleton: HydrateableNode; strings: Array<string> } {
const hydrateFilter: HydrationFilterPredicate = config.hydrateFilter || (() => true);
const strings: Array<string> = [];
const stringMap: Map<string, number> = new Map();
const storeString = (value: string): number => {
if (stringMap.has(value)) {
// Safe to cast since we verified the mapping contains the value.
return stringMap.get(value) as number;
}
const count = strings.length;
stringMap.set(value, count);
strings.push(value);
return count;
};
const skeleton = createHydrateableNode(element, storeString, hydrateFilter, workerContext);
return { skeleton, strings };
}
/**
* @param element
*/
export function createReadableHydrateableRootNode(
element: RenderableElement,
config: WorkerDOMConfiguration,
workerContext: WorkerContext,
): HydrateableNode {
// "Readable" variant doesn't do any string minimization so we can output it for debugging purposes.
// Note that this intentionally breaks the type contract of createHydrateableNode() and HydrateableNode.
return createHydrateableNode(element, ((value: string): string => value) as any, config.hydrateFilter || (() => true), workerContext);
}