-
Notifications
You must be signed in to change notification settings - Fork 24
/
Attribute.ts
568 lines (519 loc) · 18.7 KB
/
Attribute.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
import { assertNever } from "@opticss/util";
// TODO: make selectables belong to a template. that template can have template-wide config associated to it.
// E.g. namespace mappings.
export interface HasNamespace {
readonly namespaceURL: string | null;
}
// Ok so a quick rundown on the type strategy for tagname and attribute values:
// There are exclusive interfaces that can be passed in and this prevents the
// creation of nonsensical value data like `{unknown: true, value: "asdf"}`. But
// when it comes time to read these values we have to read them off a data type
// where any of the values might be present without introducing a bunch of
// casting So the normalized types are a super set of the types that are
// exclusive when passed as arguments. This keeps us from having to do a bunch
// of casting.
/**
* A value that contains no dynamic component.
*
* Sometimes a dynamic value is one of several known constant values, in those cases
* a ValueChoice should be used.
*/
export interface ValueConstant {
constant: string;
}
/**
* Indicates the value, if it exists, may have any possible value including
* possibly whitespace characters.
*/
export interface ValueUnknown {
unknown: true;
}
/**
* Indicates the value, if it exists, may have any possible value but will
* only be a single identifier.
*/
export interface ValueUnknownIdentifier {
unknownIdentifier: true;
}
/**
* Indicates there is no value.
*/
export interface ValueAbsent {
absent: true;
}
/**
* The only thing that is known about the value is a constant prefix.
*
* This might be used when a value is set to a constant value concatenated with a dynamic expression.
* In some cases this is enough information to decide that a selector doesn't match.
*/
export interface ValueStartsWith {
startsWith: string;
whitespace?: boolean;
}
/**
* The only thing that is known about the value is a constant suffix.
*
* This might be used when a value is set to a constant value concatenated with a dynamic expression.
* In some cases this is enough information to decide that a selector doesn't match.
*/
export interface ValueEndsWith {
endsWith: string;
whitespace?: boolean;
}
/**
* The only thing that is known about the value is a constant prefix and suffix.
*
* This might be used when a value has a dynamic expression embedded within a a constant value.
* In some cases this is enough information to decide that a selector doesn't match.
*/
export type ValueStartsAndEndsWith = ValueStartsWith & ValueEndsWith;
export type ValueStartsAndOrEndsWith = Partial<ValueStartsAndEndsWith>;
export type AttributeValueChoiceOption =
ValueAbsent |
ValueConstant |
ValueStartsWith |
ValueEndsWith |
ValueStartsAndEndsWith |
AttributeValueSet;
/**
* The value may have one of several values.
* Assumed to match if any of the choices matches.
*/
export interface AttributeValueChoice {
oneOf: Array<AttributeValueChoiceOption>;
}
export type AttributeValueSetItem =
ValueUnknownIdentifier |
ValueConstant |
ValueStartsWith |
ValueEndsWith |
ValueStartsAndEndsWith |
AttributeValueChoice;
/**
* An attribute value set represents a space delimited set of values
* like you would expect to find in an html class attribute.
*/
export interface AttributeValueSet {
allOf: Array<AttributeValueSetItem>;
}
export type AttributeValue =
ValueAbsent |
ValueUnknown |
ValueUnknownIdentifier |
ValueConstant |
ValueStartsWith |
ValueEndsWith |
ValueStartsAndEndsWith |
AttributeValueChoice |
AttributeValueSet;
export type FlattenedAttributeValueSetItem =
ValueAbsent |
ValueUnknownIdentifier |
ValueConstant |
ValueStartsWith |
ValueEndsWith |
ValueStartsAndEndsWith;
export interface FlattenedAttributeValueSet {
allOf: Array<FlattenedAttributeValueSetItem>;
}
export type FlattenedAttributeValue =
ValueAbsent |
ValueUnknown |
ValueUnknownIdentifier |
ValueConstant |
ValueStartsWith |
ValueEndsWith |
ValueStartsAndEndsWith |
FlattenedAttributeValueSet;
export interface SerializedAttribute {
namespaceURL?: string | null;
name: string;
value: AttributeValue;
}
/**
* Represents an arbitrary html attribute in a document. Based on the value type it will match against
* the attribute selector syntax as follows:
*
* ValueUnknown:
* * Never excludes a selector that involves the attribute.
*
* ValueAbsent:
* * Will not match selectors that require a value to be present.
*
* ValueChoice:
* * Will exclude a selector if all of the choices exclude it.
*
* ValueConstant:
* * [attr] irregardless of the constant value.
* * [attr=value] if the constant value is value.
* * [attr^=value] if the constant value starts with value.
* * [attr$=value] if the constant value ends with value.
* * [attr|=value] if the constant value is value or is prefixed value followed by a '-'.
* * [attr~=value] if the space-separated constant value includes the word value.
* * [attr*=value] if the constant value includes value.
*
* ValueStartsWith:
* * [attr] irregardless of the startsWith value.
* * [attr=value] if the value starts with the startsWith value.
* * [attr^=value] if the startsWith value starts with value or vice versa.
* * [attr$=value] assumed to match the unspecified trailing value.
* * [attr|=value] if the startsWith value starts with '<value>-'
* * [attr~=value] assumed to match the unspecified trailing value.
* * [attr*=value] assumed to match the unspecified trailing value.
*
* ValueEndsWith:
* * [attr] irregardless of the endsWith value.
* * [attr=value] if the value ends with the endsWith value.
* * [attr^=value] assumed to match the unspecified leading value.
* * [attr$=value] if the endsWith value ends with value or vice versa.
* * [attr|=value] assumed to match against the unspecified leading value.
* * [attr~=value] assumed to match the unspecified leading value.
* * [attr*=value] assumed to match the unspecified leading value.
*
* ValueStartsAndEndsWith:
* * [attr] irregardless of the endsWith value.
* * [attr=value] if the value starts with the startsWith value and ends with the endsWith value.
* * [attr^=value] if the startsWith value starts with value or vice versa.
* * [attr$=value] if the endsWith value ends with value or vice versa.
* * [attr|=value] if the startsWith value starts with '<value>-'
* * [attr~=value] assumed to match the unspecified middle value.
* * [attr*=value] assumed to match the unspecified middle value.
*/
export abstract class AttributeBase implements HasNamespace {
private _namespaceURL: string | null;
private _name: string;
private _value: AttributeValue;
private _constants: Set<string> | undefined;
constructor(namespaceURL: string | null, name: string, value: AttributeValue = { unknown: true }) {
this._namespaceURL = namespaceURL;
this._name = name;
this._value = value;
}
get namespaceURL(): string | null {
return this._namespaceURL;
}
get name(): string {
return this._name;
}
get value(): AttributeValue {
return this._value;
}
isNamed(name: string, ns: string | null = null): boolean {
return this.namespaceURL === ns &&
this.name === name;
}
constants(condition?: AttributeValue): Set<string> {
if (condition === undefined && this._constants !== undefined) {
return this._constants;
}
let result = new Set<string>();
condition = condition || this.value;
if (isConstant(condition)) {
result.add(condition.constant);
} else if (isChoice(condition)) {
for (let c of condition.oneOf) {
for (let constant of this.constants(c)) {
result.add(constant);
}
}
} else if (isSet(condition)) {
for (let c of condition.allOf) {
for (let constant of this.constants(c)) {
result.add(constant);
}
}
}
return result;
}
/**
* Check whether the value is legal according to the attribute's value expression.
*/
isLegal(value: string, condition?: AttributeValue): boolean {
condition = condition || this.value;
if (isUnknown(condition)) {
return true;
} else if (isUnknownIdentifier(condition)) {
return !/\s/.test(value);
} else if (isAbsent(condition)) {
return value.length === 0;
} else if (isConstant(condition)) {
return value === condition.constant;
} else if (isStartsWith(condition)) {
let start = condition.startsWith;
let matches = value.startsWith(start);
if (!matches) return false;
let suffix = value.substring(start.length, value.length);
return (condition.whitespace || !suffix.match(/\s/));
} else if (isEndsWith(condition)) {
let ending = condition.endsWith;
let matches = value.endsWith(ending);
if (!matches) return false;
let prefix = value.substring(0, value.length - ending.length);
return (!!condition.whitespace || !prefix.match(/\s/));
} else if (isStartsAndEndsWith(condition)) {
let start = condition.startsWith;
let end = condition.endsWith;
let matches = value.startsWith(start) && value.endsWith(end);
if (!matches) return false;
let middle = value.substring(start.length, value.length - end.length);
return (condition.whitespace || !middle.match(/\s/));
} else if (isChoice(condition)) {
return condition.oneOf.some(c => this.isLegal(value, c));
} else if (isSet(condition)) {
let values = value.split(/\s+/);
// TODO: this is wrong because it allows some classes to be used more than
// once and others to not be used at all and also because some conditions
// may require multiple values.
//
// The algorithm should be something like:
// If there is a set value (`allOf`) we must remove all choice values
// (`oneOf`) by permuting the values into flat lists where each element
// must be legal against a single value of the split input. each value in
// split list must be tested against each condition. If there is a value
// that didn't test true return false for that permutation. If multiple
// values test true, ensure that there is a value that tests true for each
// condition once and only once. If there is a permutation that passes
// exit early and return true otherwise false. This feels like a case
// where we can apply dynamic programming. Perhaps something like:
// https://stackoverflow.com/questions/11376672/partial-subtree-matching-algorithm
//
// it's not clear if we need this yet, so I'm going to leave it broken for now.
return condition.allOf.every(c => values.some(v => this.isLegal(v, c)));
} else {
return assertNever(condition);
}
}
isStatic(value: string | null, condition = this.value): boolean | undefined {
if (isAbsent(condition)) {
return value === null ? true : undefined;
} else if (isConstant(condition)) {
return condition.constant === value ? true : undefined;
} else if (isChoice(condition)) {
let constants = this.constants(condition);
if (value && constants.has(value)) {
return false;
} else if (!value) {
return condition.oneOf.find((c => isAbsent(c))) ? false : undefined;
} else {
return undefined;
}
} else if (isUnknown(condition) || isUnknownIdentifier(condition)) {
return false;
} else if (isSet(condition)) {
return condition.allOf.reduce<undefined | boolean>(
(prev, a) => {
let r = this.isStatic(value, a);
if (r === undefined) return prev;
if (prev === undefined) return r;
return prev && r;
},
undefined);
} else {
return false;
}
}
isAmbiguous(condition = this.value): condition is ValueUnknown | ValueStartsWith | ValueEndsWith | ValueStartsAndEndsWith | AttributeValueChoice | AttributeValueSet {
if (isUnknown(condition)) {
return true;
} else if (isUnknownIdentifier(condition)) {
return true;
} else if (isAbsent(condition)) {
return false;
} else if (isConstant(condition)) {
return false;
} else if (isStartsWith(condition)) {
return true;
} else if (isEndsWith(condition)) {
return true;
} else if (isStartsAndEndsWith(condition)) {
return true;
} else if (isChoice(condition)) {
return condition.oneOf.some(c => this.isAmbiguous(c));
} else if (isSet(condition)) {
return condition.allOf.some(c => this.isAmbiguous(c));
} else {
return assertNever(condition);
}
}
sameNamespace(namespace: string | undefined) {
if (!(this.namespaceURL || namespace)) {
return true;
} else {
return this.namespaceURL === namespace;
}
}
flattenedValue(value: AttributeValue = this.value): Array<FlattenedAttributeValue> {
if (isSet(value)) {
let newSets = new Array<FlattenedAttributeValueSet>();
newSets.push({
allOf: new Array<FlattenedAttributeValueSetItem>(),
});
value.allOf.forEach(v => {
if (isChoice(v)) {
let res = this.flattenedValue(v);
let origLength = newSets.length;
for (let vi of res) {
for (let j = 0; j < origLength; j++) {
if (isFlattenedSet(vi)) {
newSets.push({
allOf: newSets[j].allOf.concat(vi.allOf),
});
} else if (isAbsent(vi)) {
let newSet = newSets[j].allOf.slice();
newSet.push(vi);
newSets.push({
allOf: newSet,
});
// TODO
} else if (isUnknown(vi)) {
// TODO
} else {
newSets.push({
allOf: newSets[j].allOf.concat(vi),
});
}
}
}
for (let j = 0; j < origLength; j++) {
newSets.shift();
}
} else {
newSets.forEach(newSet => {
newSet.allOf.push(v);
});
}
});
return newSets;
} else if (isChoice(value)) {
let values = new Array<FlattenedAttributeValue>();
value.oneOf.forEach(v => {
if (isSet(v)) {
let res = this.flattenedValue(v);
values = values.concat(res);
} else {
values.push(v);
}
});
return values;
} else {
return [value];
}
}
valueToString(value: AttributeValue): string {
if (isAbsent(value)) {
return "---";
} else if (isUnknown(value)) {
return "???";
} else if (isUnknownIdentifier(value)) {
return "?";
} else if (isConstant(value)) {
return `${value.constant}`;
} else if (isStartsAndEndsWith(value)) {
return value.startsWith + "*" + value.endsWith;
} else if (isStartsWith(value)) {
return value.startsWith + "*";
} else if (isEndsWith(value)) {
return "*" + value.endsWith;
} else if (isChoice(value)) {
return "(" + value.oneOf.reduce(
(prev, v) => {
prev.push(this.valueToString(v));
return prev;
},
new Array<string>(),
).join("|") + ")";
} else if (isSet(value)) {
return value.allOf.map(v => this.valueToString(v)).join(" ");
} else {
return assertNever(value);
}
}
toString() {
let plainAttr;
if (isAbsent(this.value)) {
plainAttr = `${this.name}`;
} else {
plainAttr = `${this.name}="${this.valueToString(this.value)}"`;
}
if (this.namespaceURL) {
return `${this.namespaceURL}:${plainAttr}`;
} else {
return plainAttr;
}
}
toJSON(): SerializedAttribute {
let result: SerializedAttribute = {
name: this.name,
value: this.value,
};
if (this.namespaceURL) {
result.namespaceURL = this.namespaceURL;
}
return result;
}
static fromJSON(json: SerializedAttribute): AttributeNS | Attribute | Identifier | Class {
if (json.namespaceURL) {
return new AttributeNS(json.namespaceURL, json.name, json.value as ValueConstant);
} else {
if (json.name === "id") {
return new Identifier(json.value as ValueConstant);
} else if (json.name === "class") {
return new Class(json.value as ValueConstant);
} else {
return new Attribute(json.name, json.value as ValueConstant);
}
}
}
}
export class AttributeNS extends AttributeBase {
constructor(namespaceURL: string, name: string, value: AttributeValue = {unknown: true}) {
super(namespaceURL, name, value);
}
}
export class Attribute extends AttributeBase {
constructor(name: string, value: AttributeValue = {unknown: true}) {
super(null, name, value);
}
}
export class Identifier extends Attribute {
constructor(value: AttributeValue = { unknown: true }) {
super("id", value);
}
}
export class Class extends Attribute {
constructor(value: AttributeValue = { unknown: true }) {
super("class", value);
}
}
export type Attr = Attribute | AttributeNS | Identifier | Class;
export function isAbsent(value: FlattenedAttributeValue | AttributeValue): value is ValueAbsent {
return (<ValueAbsent>value).absent !== undefined;
}
export function isUnknown(value: object): value is ValueUnknown {
return (<ValueUnknown>value).unknown === true;
}
export function isUnknownIdentifier(value: FlattenedAttributeValue | AttributeValue): value is ValueUnknownIdentifier {
return (<ValueUnknownIdentifier>value).unknownIdentifier === true;
}
export function isConstant(value: object): value is ValueConstant {
return typeof (<ValueConstant>value).constant === "string";
}
export function isStartsWith(value: FlattenedAttributeValue | AttributeValue): value is ValueStartsWith {
return (<ValueStartsWith>value).startsWith !== undefined && (<ValueStartsAndEndsWith>value).endsWith === undefined;
}
export function isEndsWith(value: FlattenedAttributeValue | AttributeValue): value is ValueEndsWith {
return (<ValueEndsWith>value).endsWith !== undefined && (<ValueStartsAndEndsWith>value).startsWith === undefined;
}
export function isStartsAndEndsWith(value: FlattenedAttributeValue | AttributeValue): value is ValueStartsAndEndsWith {
return (<ValueStartsAndEndsWith>value).endsWith !== undefined && (<ValueStartsAndEndsWith>value).startsWith !== undefined;
}
export function isChoice(value: FlattenedAttributeValue | AttributeValue): value is AttributeValueChoice {
return (<AttributeValueChoice>value).oneOf !== undefined;
}
export function isSet(value: AttributeValue): value is AttributeValueSet {
return (<AttributeValueSet>value).allOf !== undefined;
}
export function isFlattenedSet(value: FlattenedAttributeValue): value is FlattenedAttributeValueSet {
return (<FlattenedAttributeValueSet>value).allOf !== undefined;
}