From 4a877d3484136ae8fa35b1a53bf192fccb5b45f0 Mon Sep 17 00:00:00 2001 From: Leonard Thieu Date: Mon, 26 Jun 2017 11:17:38 -0400 Subject: [PATCH] [sizzle] Add type definitions. --- types/sizzle/index.d.ts | 63 ++++++++++++++++++++++++++++++++++++ types/sizzle/sizzle-tests.ts | 54 +++++++++++++++++++++++++++++++ types/sizzle/tsconfig.json | 23 +++++++++++++ types/sizzle/tslint.json | 6 ++++ 4 files changed, 146 insertions(+) create mode 100644 types/sizzle/index.d.ts create mode 100644 types/sizzle/sizzle-tests.ts create mode 100644 types/sizzle/tsconfig.json create mode 100644 types/sizzle/tslint.json diff --git a/types/sizzle/index.d.ts b/types/sizzle/index.d.ts new file mode 100644 index 0000000000..bfe24791bf --- /dev/null +++ b/types/sizzle/index.d.ts @@ -0,0 +1,63 @@ +// Type definitions for sizzle 2.3 +// Project: https://sizzlejs.com +// Definitions by: Leonard Thieu +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +export as namespace Sizzle; + +declare const Sizzle: SizzleStatic; +export = Sizzle; + +interface SizzleStatic { + selectors: Sizzle.Selectors; + >(selector: string, context: Element | Document | DocumentFragment, results: TArrayLike): TArrayLike; + (selector: string, context?: Element | Document | DocumentFragment): Element[]; + // tslint:disable-next-line:ban-types + compile(selector: string): Function; + matchSelector(element: Element, selector: string): boolean; + matches(selector: string, elements: Element[]): Element[]; +} + +declare namespace Sizzle { + interface Selectors { + cacheLength: number; + match: { [name: string]: RegExp; }; + find: { [name: string]: Selectors.FindFunction; }; + preFilter: { [name: string]: Selectors.PreFilterFunction; }; + filter: { [name: string]: Selectors.FilterFunction; }; + attrHandle: { [name: string]: Selectors.AttrHandleFunction; }; + pseudos: { [name: string]: Selectors.PseudoFunction; }; + setFilters: { [name: string]: Selectors.SetFilterFunction; }; + createPseudo(fn: Selectors.CreatePseudoFunction): Selectors.PseudoFunction; + } + + namespace Selectors { + interface FindFunction { + (match: RegExpMatchArray, context: Element | Document, isXML: boolean): Element[] | void; + } + + interface PreFilterFunction { + (match: RegExpMatchArray): string[]; + } + + interface FilterFunction { + (element: string, ...matches: string[]): boolean; + } + + interface AttrHandleFunction { + (elem: any, casePreservedName: string, isXML: boolean): string; + } + + interface PseudoFunction { + (elem: Element): boolean; + } + + interface CreatePseudoFunction { + (...args: any[]): PseudoFunction; + } + + interface SetFilterFunction { + (elements: Element[], argument: number, not: boolean): Element[]; + } + } +} diff --git a/types/sizzle/sizzle-tests.ts b/types/sizzle/sizzle-tests.ts new file mode 100644 index 0000000000..3c6d17dc64 --- /dev/null +++ b/types/sizzle/sizzle-tests.ts @@ -0,0 +1,54 @@ +/// + +function pseudos() { + const $test = jQuery(document); + Sizzle.selectors.pseudos['fixed'] = (elem) => { + // $test[0] = elem as HTMLElement; + return $test.css('position') === 'fixed'; + }; +} + +function createPseudos_0() { + Sizzle.selectors.pseudos['not'] = + Sizzle.selectors.createPseudo((subSelector) => { + const matcher = Sizzle.compile(subSelector); + return (elem) => { + return !matcher(elem); + }; + }); +} + +function createPseudos_1() { + // An implementation of a case-insensitive contains pseudo + // made for all versions of jQuery + (($) => { + function icontains(elem: HTMLElement, text: string) { + return ( + elem.textContent || + elem.innerText || + $(elem).text() || + '' + ).toLowerCase().indexOf((text || '').toLowerCase()) > -1; + } + + // $.expr.pseudos.icontains = $.expr.createPseudo(function(text) { + // return function(elem) { + // return icontains(elem as HTMLElement, text); + // }; + // }); + })(jQuery); +} + +function setFilters_0() { + Sizzle.selectors.setFilters['first'] = (elements, argument, not) => { + // No argument for first + return not ? elements.slice(1) : [elements[0]]; + }; +} + +function setFilters_1(oldPOS: RegExp) { + Sizzle.selectors.match['POS'] = new RegExp(oldPOS.source.replace('first', 'uno'), 'gi'); + Sizzle.selectors.setFilters['uno'] = Sizzle.selectors.setFilters['first']; + delete Sizzle.selectors.setFilters['first']; + Sizzle('div:uno'); // ==> [
] +} diff --git a/types/sizzle/tsconfig.json b/types/sizzle/tsconfig.json new file mode 100644 index 0000000000..aa10d374d5 --- /dev/null +++ b/types/sizzle/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6", + "dom" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "sizzle-tests.ts" + ] +} diff --git a/types/sizzle/tslint.json b/types/sizzle/tslint.json new file mode 100644 index 0000000000..a4bcc87748 --- /dev/null +++ b/types/sizzle/tslint.json @@ -0,0 +1,6 @@ +{ + "extends": "dtslint/dt.json", + "rules": { + "callable-types": false + } +}