This repository was archived by the owner on May 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAttributes.ts
231 lines (222 loc) · 7.47 KB
/
Attributes.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
import { Modifier, ModifierLevel } from '../../core/src/Modifier';
import { CssStyle } from './CssStyle';
import { ClassList } from './ClassList';
import { makeVersionable } from '../../core/src/Memory/Versionable';
export class Attributes extends Modifier {
level = ModifierLevel.LOW;
private _record: Record<string, string>;
style = new CssStyle();
// Avoid copiying FontAwesome classes on paragraph break.
// TODO : need to be improved to better take care of color classes, etc.
preserveAfterParagraphBreak = false;
classList = new ClassList();
constructor(attributes?: Attributes | NamedNodeMap | Record<string, string>) {
super();
this.style.on('update', this._triggerUpdate.bind(this));
this.classList.on('update', this._triggerUpdate.bind(this));
if (attributes instanceof Attributes) {
for (const key of attributes.keys()) {
this.set(key, attributes.get(key));
}
} else if (attributes instanceof NamedNodeMap) {
for (const attribute of Array.from(attributes)) {
this.set(attribute.name, attribute.value);
}
} else if (attributes) {
for (const key of Object.keys(attributes)) {
this.set(key, attributes[key]);
}
}
if (attributes && (this.style.length || this.classList.length)) {
console.log('-------\nnew attributes');
if (this.style.length) console.log(this.style.keys(), this.style.values());
if (this.classList.length) console.log('class : ' + this.classList.className);
}
}
//--------------------------------------------------------------------------
// Getters
//--------------------------------------------------------------------------
get length(): number {
return this.keys().length;
}
get name(): string {
const name = [];
for (const attributeName of this.keys()) {
name.push(`${attributeName}: "${this.get(attributeName)}"`);
}
return `{${name.join(', ')}}`;
}
//--------------------------------------------------------------------------
// Lifecycle
//--------------------------------------------------------------------------
/**
* Return a clone of this record.
*/
clone(): this {
const clone = new this.constructor();
if (this._record) {
clone._record = makeVersionable({ ...this._record });
}
if (this.style.length) {
clone.style = this.style.clone();
}
if (this.classList.length) {
clone.classList = this.classList.clone();
}
return clone;
}
/**
* Return a string representing the attributes.
*/
toString(): string {
if (!this.length) return `${this.constructor.name}: {}`;
const valueRepr = [];
for (const key of this.keys()) {
valueRepr.push(`${key}: "${this.get(key)}"`);
}
return `${this.constructor.name}: { ${valueRepr.join(', ')} }`;
}
//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------
/**
* Return true if the record has the given key, false otherwise.
*
* @param key
*/
has(key: string): boolean {
return this.keys().includes(key.toLowerCase());
}
/**
* Return an array containing all the keys in the record.
*/
keys(): string[] {
const keys = this._record
? Object.keys(this._record).filter(key => {
return (
(key !== 'style' || !!this.style.length) &&
(key !== 'class' || !!this.classList.length)
);
})
: [];
if (this.classList.length && !keys.includes('class')) {
// The node was not parsed with a class attribute, add it in place.
// Use `get` for its value but record its position in the record.
keys.push('class');
}
if (this.style.length && !keys.includes('style')) {
// The node was not parsed with a style attribute, keep it always at
// the end of the attributes list.
keys.push('style');
}
return keys;
}
/**
* Return an array containing all the values in the record.
*/
values(): string[] {
return this.keys().map(key => this.get(key));
}
/**
* Return the record matching the given name.
*
* @param name
*/
get(name: string): string {
name = name.toLowerCase();
if (name === 'style') {
return this.style?.cssText;
} else if (name === 'class') {
return this.classList?.className;
} else {
return this._record?.[name];
}
}
/**
* Set the record with the given name to the given value.
*
* @param name
* @param value
*/
set(name: string, value: string): void {
name = name.toLowerCase();
if (!this._record) {
this._record = makeVersionable({});
}
if (name === 'style') {
if (this.style) {
this.style.reset(value);
} else {
this.style = new CssStyle();
this.style.on('update', this._triggerUpdate.bind(this));
}
// Use `get` for its value but record its position in the record.
this._record.style = null;
} else if (name === 'class') {
this.classList.reset(value);
// Use `get` for its value but record its position in the record.
this._record.class = null;
} else {
this._record[name] = value;
}
this.trigger('update');
}
/**
* Remove the records with the given names.
*
* @param names
*/
remove(...names: string[]): void {
for (let name of names) {
name = name.toLowerCase();
if (name === 'style') {
this.style.clear();
} else if (name === 'class') {
this.classList.clear();
} else if (this._record) {
delete this._record[name];
}
}
if (names.length) {
this.trigger('update');
}
}
clear(): void {
delete this._record;
this.style.clear();
this.classList.clear();
this.trigger('update');
}
/**
* Return true if the given attributes are the same as the ones in this
* record.
*
* @param otherAttributes
*/
isSameAs(otherAttributes: Attributes): boolean {
if (otherAttributes) {
return (
this.length === otherAttributes.length &&
this.keys().every(key => {
return this.get(key) === otherAttributes.get(key);
})
);
} else {
return !this.length;
}
}
/**
* @override
*/
off(eventName: string, callback?: Function): void {
super.off(eventName, callback);
this.classList.off(eventName, callback);
this.style.off(eventName, callback);
}
//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------
_triggerUpdate(): void {
this.trigger('update');
}
}