[sizzle] Add type definitions.

This commit is contained in:
Leonard Thieu
2017-06-28 11:48:11 -04:00
parent c126a9ec44
commit 4a877d3484
4 changed files with 146 additions and 0 deletions
+63
View File
@@ -0,0 +1,63 @@
// Type definitions for sizzle 2.3
// Project: https://sizzlejs.com
// Definitions by: Leonard Thieu <https://github.com/leonard-thieu>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
export as namespace Sizzle;
declare const Sizzle: SizzleStatic;
export = Sizzle;
interface SizzleStatic {
selectors: Sizzle.Selectors;
<TArrayLike extends ArrayLike<Element>>(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[];
}
}
}
+54
View File
@@ -0,0 +1,54 @@
/// <reference types="jquery" />
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'); // ==> [ <div> ]
}
+23
View File
@@ -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"
]
}
+6
View File
@@ -0,0 +1,6 @@
{
"extends": "dtslint/dt.json",
"rules": {
"callable-types": false
}
}