-
Notifications
You must be signed in to change notification settings - Fork 190
/
Copy pathdynamic.ts
248 lines (208 loc) · 6.84 KB
/
dynamic.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
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
import {
Dict,
Environment,
Option,
ElementBuilder,
AttributeOperation,
AttributeCursor,
} from '@glimmer/interfaces';
import { warnIfStyleNotTrusted } from '@glimmer/global-context';
import { AttrNamespace, Namespace, SimpleElement } from '@simple-dom/interface';
import { normalizeStringValue } from '../../dom/normalize';
import { normalizeProperty } from '../../dom/props';
import { requiresSanitization, sanitizeAttributeValue } from '../../dom/sanitized-values';
import { DEBUG } from '@glimmer/env';
import { castToBrowser } from '@glimmer/util';
export function dynamicAttribute(
element: SimpleElement,
attr: string,
namespace: Option<AttrNamespace>,
isTrusting = false
): DynamicAttribute {
let { tagName, namespaceURI } = element;
let attribute = { element, name: attr, namespace };
if (DEBUG && attr === 'style' && !isTrusting) {
return new DebugStyleAttributeManager(attribute);
}
if (namespaceURI === Namespace.SVG) {
return buildDynamicAttribute(tagName, attr, attribute);
}
let { type, normalized } = normalizeProperty(element, attr);
if (type === 'attr') {
return buildDynamicAttribute(tagName, normalized, attribute);
} else {
return buildDynamicProperty(tagName, normalized, attribute);
}
}
function buildDynamicAttribute(
tagName: string,
name: string,
attribute: AttributeCursor
): DynamicAttribute {
if (requiresSanitization(tagName, name)) {
return new SafeDynamicAttribute(attribute);
} else {
return new SimpleDynamicAttribute(attribute);
}
}
function buildDynamicProperty(
tagName: string,
name: string,
attribute: AttributeCursor
): DynamicAttribute {
if (requiresSanitization(tagName, name)) {
return new SafeDynamicProperty(name, attribute);
}
if (isUserInputValue(tagName, name)) {
return new InputValueDynamicAttribute(name, attribute);
}
if (isOptionSelected(tagName, name)) {
return new OptionSelectedDynamicAttribute(name, attribute);
}
return new DefaultDynamicProperty(name, attribute);
}
export abstract class DynamicAttribute implements AttributeOperation {
constructor(public attribute: AttributeCursor) {}
abstract set(dom: ElementBuilder, value: unknown, env: Environment): void;
abstract update(value: unknown, env: Environment): void;
}
export class SimpleDynamicAttribute extends DynamicAttribute {
set(dom: ElementBuilder, value: unknown, _env: Environment): void {
let normalizedValue = normalizeValue(value);
if (normalizedValue !== null) {
let { name, namespace } = this.attribute;
dom.__setAttribute(name, normalizedValue, namespace);
}
}
update(value: unknown, _env: Environment): void {
let normalizedValue = normalizeValue(value);
let { element, name } = this.attribute;
if (normalizedValue === null) {
element.removeAttribute(name);
} else {
element.setAttribute(name, normalizedValue);
}
}
}
export class DefaultDynamicProperty extends DynamicAttribute {
constructor(private normalizedName: string, attribute: AttributeCursor) {
super(attribute);
}
value: unknown;
set(dom: ElementBuilder, value: unknown, _env: Environment): void {
if (value !== null && value !== undefined) {
this.value = value;
dom.__setProperty(this.normalizedName, value);
}
}
update(value: unknown, _env: Environment): void {
let { element } = this.attribute;
if (this.value !== value) {
(element as any)[this.normalizedName] = this.value = value;
if (value === null || value === undefined) {
this.removeAttribute();
}
}
}
protected removeAttribute() {
// TODO this sucks but to preserve properties first and to meet current
// semantics we must do this.
let { element, namespace } = this.attribute;
if (namespace) {
element.removeAttributeNS(namespace, this.normalizedName);
} else {
element.removeAttribute(this.normalizedName);
}
}
}
export class SafeDynamicProperty extends DefaultDynamicProperty {
set(dom: ElementBuilder, value: unknown, env: Environment): void {
let { element, name } = this.attribute;
let sanitized = sanitizeAttributeValue(element, name, value);
super.set(dom, sanitized, env);
}
update(value: unknown, env: Environment): void {
let { element, name } = this.attribute;
let sanitized = sanitizeAttributeValue(element, name, value);
super.update(sanitized, env);
}
}
export class SafeDynamicAttribute extends SimpleDynamicAttribute {
set(dom: ElementBuilder, value: unknown, env: Environment): void {
let { element, name } = this.attribute;
let sanitized = sanitizeAttributeValue(element, name, value);
super.set(dom, sanitized, env);
}
update(value: unknown, env: Environment): void {
let { element, name } = this.attribute;
let sanitized = sanitizeAttributeValue(element, name, value);
super.update(sanitized, env);
}
}
export class InputValueDynamicAttribute extends DefaultDynamicProperty {
set(dom: ElementBuilder, value: unknown) {
dom.__setProperty('value', normalizeStringValue(value));
}
update(value: unknown) {
let input = castToBrowser(this.attribute.element, ['input', 'textarea']);
let currentValue = input.value;
let normalizedValue = normalizeStringValue(value);
if (currentValue !== normalizedValue) {
input.value = normalizedValue;
}
}
}
export class OptionSelectedDynamicAttribute extends DefaultDynamicProperty {
set(dom: ElementBuilder, value: unknown): void {
if (value !== null && value !== undefined && value !== false) {
dom.__setProperty('selected', true);
}
}
update(value: unknown): void {
let option = castToBrowser(this.attribute.element, 'option');
if (value) {
option.selected = true;
} else {
option.selected = false;
}
}
}
function isOptionSelected(tagName: string, attribute: string) {
return tagName === 'OPTION' && attribute === 'selected';
}
function isUserInputValue(tagName: string, attribute: string) {
return (tagName === 'INPUT' || tagName === 'TEXTAREA') && attribute === 'value';
}
function normalizeValue(value: unknown): Option<string> {
if (
value === false ||
value === undefined ||
value === null ||
typeof (value as Dict).toString === 'undefined'
) {
return null;
}
if (value === true) {
return '';
}
// onclick function etc in SSR
if (typeof value === 'function') {
return null;
}
return String(value);
}
let DebugStyleAttributeManager: {
new (attribute: AttributeCursor): AttributeOperation;
};
if (DEBUG) {
DebugStyleAttributeManager = class extends SimpleDynamicAttribute {
set(dom: ElementBuilder, value: unknown, env: Environment): void {
warnIfStyleNotTrusted(value);
super.set(dom, value, env);
}
update(value: unknown, env: Environment): void {
warnIfStyleNotTrusted(value);
super.update(value, env);
}
};
}