UNPKG

7.28 kBTypeScriptView Raw
1import '../../globals';
2import * as ReworkCSS from '../../css';
3/**
4 * An interface describing the shape of a type on which the selectors may apply.
5 * Note, the ui/core/view.View implements Node.
6 */
7export interface Node {
8 parent?: Node;
9 _modalParent?: Node;
10 id?: string;
11 nodeName?: string;
12 cssType?: string;
13 cssClasses?: Set<string>;
14 cssPseudoClasses?: Set<string>;
15 getChildIndex?(node: Node): number;
16 getChildAt?(index: number): Node;
17}
18export interface Declaration {
19 property: string;
20 value: string;
21}
22export type ChangeMap<T extends Node> = Map<T, Changes>;
23export interface Changes {
24 attributes?: Set<string>;
25 pseudoClasses?: Set<string>;
26}
27declare const enum Rarity {
28 Invalid = 4,
29 Id = 3,
30 Class = 2,
31 Type = 1,
32 PseudoClass = 0,
33 Attribute = 0,
34 Universal = 0,
35 Inline = 0
36}
37interface LookupSorter {
38 sortById(id: string, sel: SelectorCore): any;
39 sortByClass(cssClass: string, sel: SelectorCore): any;
40 sortByType(cssType: string, sel: SelectorCore): any;
41 sortAsUniversal(sel: SelectorCore): any;
42}
43declare type Combinator = '+' | '>' | '~' | ' ';
44export declare abstract class SelectorCore {
45 pos: number;
46 specificity: number;
47 rarity: Rarity;
48 combinator: Combinator;
49 ruleset: RuleSet;
50 /**
51 * Dynamic selectors depend on attributes and pseudo classes.
52 */
53 dynamic: boolean;
54 abstract match(node: Node): boolean;
55 /**
56 * If the selector is static returns if it matches the node.
57 * If the selector is dynamic returns if it may match the node, and accumulates any changes that may affect its state.
58 */
59 abstract accumulateChanges(node: Node, map: ChangeAccumulator): boolean;
60 lookupSort(sorter: LookupSorter, base?: SelectorCore): void;
61}
62export declare abstract class SimpleSelector extends SelectorCore {
63 accumulateChanges(node: Node, map?: ChangeAccumulator): boolean;
64 mayMatch(node: Node): boolean;
65 trackChanges(node: Node, map: ChangeAccumulator): void;
66}
67export declare class InvalidSelector extends SimpleSelector {
68 e: Error;
69 constructor(e: Error);
70 toString(): string;
71 match(node: Node): boolean;
72 lookupSort(sorter: LookupSorter, base?: SelectorCore): void;
73}
74export declare class UniversalSelector extends SimpleSelector {
75 toString(): string;
76 match(node: Node): boolean;
77}
78export declare class IdSelector extends SimpleSelector {
79 id: string;
80 constructor(id: string);
81 toString(): string;
82 match(node: Node): boolean;
83 lookupSort(sorter: LookupSorter, base?: SelectorCore): void;
84}
85export declare class TypeSelector extends SimpleSelector {
86 cssType: string;
87 constructor(cssType: string);
88 toString(): string;
89 match(node: Node): boolean;
90 lookupSort(sorter: LookupSorter, base?: SelectorCore): void;
91}
92export declare class ClassSelector extends SimpleSelector {
93 cssClass: string;
94 constructor(cssClass: string);
95 toString(): string;
96 match(node: Node): boolean;
97 lookupSort(sorter: LookupSorter, base?: SelectorCore): void;
98}
99declare type AttributeTest = '=' | '^=' | '$=' | '*=' | '=' | '~=' | '|=';
100export declare class AttributeSelector extends SimpleSelector {
101 attribute: string;
102 test?: AttributeTest;
103 value?: string;
104 constructor(attribute: string, test?: AttributeTest, value?: string);
105 toString(): string;
106 match(node: Node): boolean;
107 mayMatch(node: Node): boolean;
108 trackChanges(node: Node, map: ChangeAccumulator): void;
109}
110export declare class PseudoClassSelector extends SimpleSelector {
111 cssPseudoClass: string;
112 constructor(cssPseudoClass: string);
113 toString(): string;
114 match(node: Node): boolean;
115 mayMatch(node: Node): boolean;
116 trackChanges(node: Node, map: ChangeAccumulator): void;
117}
118export declare class SimpleSelectorSequence extends SimpleSelector {
119 selectors: SimpleSelector[];
120 private head;
121 constructor(selectors: SimpleSelector[]);
122 toString(): string;
123 match(node: Node): boolean;
124 mayMatch(node: Node): boolean;
125 trackChanges(node: any, map: any): void;
126 lookupSort(sorter: LookupSorter, base?: SelectorCore): void;
127}
128export declare class Selector extends SelectorCore {
129 selectors: SimpleSelector[];
130 private groups;
131 private last;
132 constructor(selectors: SimpleSelector[]);
133 toString(): string;
134 match(node: Node): boolean;
135 lookupSort(sorter: LookupSorter, base?: SelectorCore): void;
136 accumulateChanges(node: Node, map?: ChangeAccumulator): boolean;
137}
138export declare namespace Selector {
139 class ChildGroup {
140 private selectors;
141 dynamic: boolean;
142 constructor(selectors: SiblingGroup[]);
143 match(node: Node): Node;
144 mayMatch(node: Node): Node;
145 trackChanges(node: Node, map: ChangeAccumulator): void;
146 }
147 class SiblingGroup {
148 private selectors;
149 dynamic: boolean;
150 constructor(selectors: SimpleSelector[]);
151 match(node: Node): Node;
152 mayMatch(node: Node): Node;
153 trackChanges(node: Node, map: ChangeAccumulator): void;
154 }
155 interface Bound {
156 left: Node;
157 right: Node;
158 }
159}
160export declare class RuleSet {
161 selectors: SelectorCore[];
162 declarations: Declaration[];
163 tag: string | number;
164 scopedTag: string;
165 constructor(selectors: SelectorCore[], declarations: Declaration[]);
166 toString(): string;
167 lookupSort(sorter: LookupSorter): void;
168}
169export declare function fromAstNodes(astRules: ReworkCSS.Node[]): RuleSet[];
170export declare function createSelector(sel: string): SimpleSelector | SimpleSelectorSequence | Selector;
171export declare class SelectorsMap<T extends Node> implements LookupSorter {
172 private id;
173 private class;
174 private type;
175 private universal;
176 private position;
177 constructor(rulesets: RuleSet[]);
178 query(node: T): SelectorsMatch<T>;
179 sortById(id: string, sel: SelectorCore): void;
180 sortByClass(cssClass: string, sel: SelectorCore): void;
181 sortByType(cssType: string, sel: SelectorCore): void;
182 sortAsUniversal(sel: SelectorCore): void;
183 private addToMap;
184 private makeDocSelector;
185}
186interface ChangeAccumulator {
187 addAttribute(node: Node, attribute: string): void;
188 addPseudoClass(node: Node, pseudoClass: string): void;
189}
190export declare class SelectorsMatch<T extends Node> implements ChangeAccumulator {
191 changeMap: ChangeMap<T>;
192 selectors: SelectorCore[];
193 addAttribute(node: T, attribute: string): void;
194 addPseudoClass(node: T, pseudoClass: string): void;
195 properties(node: T): Changes;
196}
197export declare const CSSHelper: {
198 createSelector: typeof createSelector;
199 SelectorCore: typeof SelectorCore;
200 SimpleSelector: typeof SimpleSelector;
201 InvalidSelector: typeof InvalidSelector;
202 UniversalSelector: typeof UniversalSelector;
203 TypeSelector: typeof TypeSelector;
204 ClassSelector: typeof ClassSelector;
205 AttributeSelector: typeof AttributeSelector;
206 PseudoClassSelector: typeof PseudoClassSelector;
207 SimpleSelectorSequence: typeof SimpleSelectorSequence;
208 Selector: typeof Selector;
209 RuleSet: typeof RuleSet;
210 SelectorsMap: typeof SelectorsMap;
211 fromAstNodes: typeof fromAstNodes;
212 SelectorsMatch: typeof SelectorsMatch;
213};
214export {};