UNPKG

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