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
|
// Type definitions for nwsapi 2.2
// Project: https://github.com/dperini/nwsapi, https://dperini.github.io/nwsapi/
// Definitions by: ExE Boss <https://github.com/ExE-Boss>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference path="./_internal/umd-global.d.ts"/>
type ResolveElementType<T extends string> = string extends T
? Element
: T extends keyof HTMLElementTagNameMap
? HTMLElementTagNameMap[T]
: T extends keyof SVGElementTagNameMap
? SVGElementTagNameMap[T]
: Element;
export = nwsapi;
declare function nwsapi(global: nwsapi.Global): nwsapi.NWSAPI;
declare namespace nwsapi {
// tslint:disable: no-redundant-jsdoc-2
/**
* @enum
* The `MatcherMode` type should be described using a **TypeScript** enum,\
* but **TypeScript** doesn't treat `true`, `false` or `null`
* as constant expressions in enum initializers.
*/
type MatcherMode = MatcherMode.SELECT | MatcherMode.MATCH | MatcherMode.USE_COLLECTION_ITEM;
// tslint:enable: no-redundant-jsdoc-2
namespace MatcherMode {
/**
* Used to select elements.
*
* | variable | type | relation to `e`
* | :------: | ------------------ | :--------------:
* | `c` | `ArrayLike<Element>` | `c[i] === e`
*/
type SELECT = true;
/**
* Used to select elements.
*
*
* | variable | type | relation to `e`
* | :------: | ---------------------------- | :---------------:
* | `c` | `{ item(i: number): Element }` | `c.item(i) === e`
*/
type USE_COLLECTION_ITEM = null;
/**
* Used to check whether an element matches this selector.
*
* | variable | type | relation to `e`
* | :------: | ------- | :---------------:
* | `c` | `Element` | `c === e`
*/
type MATCH = false;
}
interface Global {
readonly document: Document;
readonly DOMException: new (message?: string, name?: string) => DOMException;
}
interface Config {
readonly [option: string]: boolean;
/**
* Whether `Dom.byId` returns all elements with the matching `[id]`, or just the first.
*
* @default true
*/
readonly IDS_DUPES: boolean;
/**
* Whether tag names should always be compared case-insensitively.
*
* @default true
*/
readonly MIXEDCASE: boolean;
/**
* Whether errors should be logged when `Config.VERBOSITY` is `false`.
*
* @default true
*/
readonly LOGERRORS: boolean;
/**
* Whether errors cause a `DOMException` to be thrown instead of being silently ignored.
*
* @default true
*/
readonly VERBOSITY: boolean;
}
interface NWSAPI {
readonly Version: string;
// exported cache objects
readonly matchLambdas: {
[selector: string]: MatchLambda;
};
readonly selectLambdas: {
[selector: string]: SelectLambda;
};
readonly matchResolvers: {
[selectors: string]: MatchResolver;
};
readonly selectResolvers: {
[selectors: string]: SelectResolver;
};
// exported compiler macros
readonly CFG: CFG;
readonly M_BODY: string;
readonly S_BODY: string;
readonly M_TEST: string;
readonly S_TEST: string;
// exported engine methods
/** context agnostic `getElementsByClassName` */
byTag<T extends string>(tag: T, context: ContextNode): Array<ResolveElementType<T>>;
/** context agnostic `getElementById` */
byId(id: string, context: ContextNode): Element[];
/** context agnostic `getElementsByTagName` */
byClass(className: string, context: ContextNode): Element[];
/**
* Equivalent of w3c `querySelector` method.
*
* @param callback
* The callback that gets invoked for the first element that matches `selector`.
*/
first<S extends string>(
selector: S,
context?: ContextNode | null,
callback?: ((element: ResolveElementType<S>) => void) | null,
): ResolveElementType<S> | null;
/**
* Equivalent of w3c `querySelectorAll` method.
*
* @param callback
* The callback that gets invoked for every encountered element that matches `selector`.
*
* Return `false` to stop searching for more elements.
*/
select<S extends string>(
selector: S,
context?: ContextNode | null,
callback?: ((element: ResolveElementType<S>) => boolean | void) | null,
): Array<ResolveElementType<S>>;
/**
* Equivalent of w3c `matches` method.
*
* @param element The element to check whether it matches the `selector`.
* @param callback The callback that gets invoked if `element` matches the `selector`.
*/
match<S extends string>(
selector: S,
element: Element,
callback?: ((element: ResolveElementType<S>) => void) | null,
): element is ResolveElementType<S>;
/**
* Equivalent of w3c `closest` method.
*/
closest<S extends string>(
selector: S,
element: Element,
callback?: ((element: ResolveElementType<S>) => void) | null,
): ResolveElementType<S> | null;
/**
* Compile groups or single selector strings into
* executable functions for matching or selecting.
*/
compile<E extends Element, M extends MatcherMode>(
selector: string,
mode: M,
callback?: ((element: E) => void) | null,
): M extends MatcherMode.MATCH
? MatchLambda<E>
: M extends MatcherMode.SELECT | MatcherMode.USE_COLLECTION_ITEM
? SelectLambda<E>
: never;
/**
* Returns the configuration object.
*
* @return The configuration object.
*/
configure(): Config;
/**
* Returns the current value of the option.
*
* @param option The configuration option.
* @return The current value of the option.
*/
configure(option: string): boolean;
/**
* Configure the engine to use special handling.
*
* @param options The new options to set.
* @param clear Whether to clear the lambda cache.
*/
configure(options: Partial<Config>, clear?: boolean): true;
/**
* centralized error and exceptions handling
*
* @param constructor The error constructor to use, defaults to `DOMException` with a `name` of `"SyntaxError"`.
*/
emit(message: string, constructor?: (new (message: string) => Error) | null): void;
/** special handling configuration flags */
readonly Config: Config;
/** passed to resolvers as the `s` variable */
readonly Snapshot: Snapshot;
/** overrides QSA methods (only for browsers) */
install(all?: boolean): void;
/** restore QSA methods (only for browsers) */
uninstall(): void;
readonly Operators: {
[operator: string]: AttributeOperator;
readonly "=": AttributeOperator & { p1: "^"; p2: "$"; p3: "true" };
readonly "^=": AttributeOperator & { p1: "^"; p2: ""; p3: "true" };
readonly "$=": AttributeOperator & { p1: ""; p2: "$"; p3: "true" };
readonly "*=": AttributeOperator & { p1: ""; p2: ""; p3: "true" };
readonly "|=": AttributeOperator & { p1: "^"; p2: "(-|$)"; p3: "true" };
readonly "~=": AttributeOperator & { p1: "(^|\\s)"; p2: "(\\s|$)"; p3: "true" };
};
readonly Selectors: {
[pseudoClass: string]: CustomSelector;
};
/** register a new selector combinator symbol and its related function resolver */
registerCombinator(combinator: string, resolver: (match: RegExpMatchArray) => string): void;
/** register a new attribute operator symbol and its related function resolver */
registerOperator(operator: string, resolver: AttributeOperator): void;
/** register a new selector symbol and its related function resolver */
registerSelector<
R extends string | RegExp | { [Symbol.match](string: string): RegExpMatchArray | null },
F extends (
this: {
readonly Expression: R;
readonly Callback: F;
},
...args: Parameters<CustomSelector["Callback"]>
) => CustomSelectorResult
>(
name: string,
regExp: R,
func: F,
): void;
}
interface Snapshot {
doc: Document;
from: ContextNode;
root: Element;
readonly byTag: NWSAPI["byTag"];
readonly first: NWSAPI["first"];
readonly select: NWSAPI["select"];
readonly ancestor: NWSAPI["closest"];
/**
* Fast resolver for the :nth-of-type() and :nth-last-of-type() pseudo-classes
*
* @param element The element from which the sibling is being searched for.
* @param dir The direction to search in.
*
* | value | description
* | ------- | :----------
* | `false` | search forward.
* | `true` | search backward.
* | `2` | clear cache and immediately return `-1`, ignoring `element`.
*
* @return The index where the next element can be found.
*/
nthOfType(element: Element, dir?: boolean): number;
nthOfType(element: unknown, dir: 2): -1;
/**
* Fast resolver for the :nth-child() and :nth-last-child() pseudo-classes
*
* @param element The element from which the sibling is being searched for.
* @param dir The direction to search in.
*
* | value | description
* | ------- | :----------
* | `false` | search forward.
* | `true` | search backward.
* | `2` | clear cache and immediately return `-1`, ignoring `element`.
*
* @return The index where the next element can be found.
*/
nthElement(element: Element, dir?: boolean): number;
nthElement(element: unknown, dir: 2): -1;
/**
* namespace aware `hasAttribute` helper for XML/XHTML documents
*/
hasAttributeNS(e: Element, name: string): boolean;
}
type ContextNode = Document | DocumentFragment | Element;
type SelectLambda<T extends Element = Element> = <E extends T, R extends ArrayLike<Element>>(
element: ArrayLike<E> | NodeListOf<E> | HTMLCollectionOf<E>,
callback: ((element: E) => unknown) | undefined,
context: ContextNode,
results: R,
) => R;
type MatchLambda<T extends Element = Element> = <E extends T, R extends ArrayLike<Element>>(
element: E,
callback: ((element: E) => void) | undefined,
context: ContextNode,
results: R,
) => R;
interface SelectResolver {
callback: ((node: Node) => void) | undefined;
context: ContextNode;
factory: SelectLambda[];
htmlset: Array<() => Element[]>;
nodeset: string[];
results: Element[];
}
interface MatchResolver {
factory: MatchLambda[];
}
interface CustomSelector {
readonly Expression: string | RegExp | { [Symbol.match](string: string): RegExpMatchArray | null };
Callback(
this: this,
match: RegExpMatchArray,
source: string,
mode: MatcherMode,
callback: ((element: Element) => unknown) | undefined,
): CustomSelectorResult;
}
interface CustomSelectorResult {
/** The ECMAScript source string. */
readonly source: string;
/** Whether the selector compilation was successful. */
readonly status: boolean;
/**
* Used to declare new variables.
*
* @example
* ```js
* return {
* modvar: `\
* foo,
* bar = "baz",
* biz = 123
* `,
* };
* ```
*/
readonly modvar?: string | undefined;
/** Used to return a new `match` array. */
readonly match?: RegExpMatchArray | undefined;
}
interface AttributeOperator {
/**
* The RegExp string before the expected attribute value.
*/
readonly p1: string;
/**
* The RegExp string after the expected attribute value.
*/
readonly p2: string;
/**
* The expected result of the invocation of the RegExp `test` method.
*/
readonly p3: "true" | "false";
}
interface CFG {
readonly operators: string;
readonly combinators: string;
}
}
|