/
class_map.ts
50 lines (40 loc) · 1.04 KB
/
class_map.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
import { parseClassDescriptorStringForIdentifier } from "./class_descriptor"
import { Scope } from "./scope"
export class ClassMap {
readonly scope: Scope
constructor(scope: Scope) {
this.scope = scope
}
has(name: string) {
return name in this.values
}
get(name: string) {
return this.values[name]
}
get values() {
const { identifier } = this
return this.descriptorStrings.reduce((values, descriptorString) => {
const descriptor = parseClassDescriptorStringForIdentifier(descriptorString, identifier)
if (descriptor) {
values[descriptor.name] = descriptor.className
}
return values
}, {} as { [name: string]: string })
}
get descriptorStrings() {
const value = this.element.getAttribute(this.classAttribute) || ""
return value.split(/\s+/)
}
get classAttribute() {
return this.schema.classAttribute
}
get schema() {
return this.scope.schema
}
get element() {
return this.scope.element
}
get identifier() {
return this.scope.identifier
}
}