-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheditv2.ts
More file actions
114 lines (99 loc) · 2.96 KB
/
editv2.ts
File metadata and controls
114 lines (99 loc) · 2.96 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/** Intent to `parent.insertBefore(node, reference)` */
export type Insert = {
parent: Node;
node: Node;
reference: Node | null;
};
/** Intent to remove a `node` from its `ownerDocument` */
export type Remove = {
node: Node;
};
/** Intent to set the `textContent` of `element` */
export type SetTextContent = {
element: Element;
textContent: string;
};
/** Record from attribute names to attribute values */
export type AttributesV2 = Partial<Record<string, string | null>>;
/** Record from namespace URIs to `Attributes` records */
export type AttributesNS = Partial<Record<string, AttributesV2>>;
/** Intent to set or remove (if `null`) `attributes`(-`NS`) on `element` */
export type SetAttributes = {
element: Element;
attributes?: AttributesV2;
attributesNS?: AttributesNS;
};
/** Intent to change some XMLDocuments */
export type EditV2 =
| Insert
| SetAttributes
| SetTextContent
| Remove
| EditV2[];
export function isAttributesV2(
attributes: unknown,
): attributes is AttributesV2 {
if (typeof attributes !== 'object' || attributes === null) {
return false;
}
return Object.entries(attributes).every(
([key, value]) =>
typeof key === 'string' && (value === null || typeof value === 'string'),
);
}
export function isAttributesNS(
attributesNS: unknown,
): attributesNS is AttributesNS {
if (typeof attributesNS !== 'object' || attributesNS === null) {
return false;
}
return Object.entries(attributesNS).every(
([namespace, attributes]) =>
typeof namespace === 'string' &&
isAttributesV2(attributes as Record<string, string | null>),
);
}
export function isComplexEditV2(edit: unknown): edit is EditV2[] {
return edit instanceof Array && edit.every(e => isEditV2(e));
}
export function isSetTextContent(edit: unknown): edit is SetTextContent {
return (
(edit as SetTextContent).element instanceof Element &&
typeof (edit as SetTextContent).textContent === 'string'
);
}
export function isRemove(edit: unknown): edit is Remove {
return (
(edit as Insert).parent === undefined &&
(edit as Remove).node instanceof Node
);
}
export function isSetAttributes(edit: unknown): edit is SetAttributes {
const setAttrs = edit as SetAttributes;
return (
setAttrs.element instanceof Element &&
(isAttributesV2(setAttrs.attributes) ||
isAttributesNS(setAttrs.attributesNS))
);
}
export function isInsert(edit: unknown): edit is Insert {
return (
((edit as Insert).parent instanceof Element ||
(edit as Insert).parent instanceof Document ||
(edit as Insert).parent instanceof DocumentFragment) &&
(edit as Insert).node instanceof Node &&
((edit as Insert).reference instanceof Node ||
(edit as Insert).reference === null)
);
}
export function isEditV2(edit: unknown): edit is EditV2 {
if (isComplexEditV2(edit)) {
return true;
}
return (
isSetAttributes(edit) ||
isSetTextContent(edit) ||
isInsert(edit) ||
isRemove(edit)
);
}