From 02db5ccb68be79df3f24cfc323bad5a609ff4d5f Mon Sep 17 00:00:00 2001 From: Johan Davidsson Date: Thu, 14 Mar 2019 21:06:51 +0100 Subject: [PATCH] Added typings for domutils (#33866) * Added new types for domelementtype * Adding types for module domutils * Updating PULL_REQUEST_TEMPLATE.md * Revert changes to .github/PULL_REQUEST_TEMPLATE.md * Update test and documentation * Removed namespace and fixed tests --- types/domutils/domutils-tests.ts | 9 +++ types/domutils/index.d.ts | 127 +++++++++++++++++++++++++++++++ types/domutils/tsconfig.json | 23 ++++++ types/domutils/tslint.json | 1 + 4 files changed, 160 insertions(+) create mode 100644 types/domutils/domutils-tests.ts create mode 100644 types/domutils/index.d.ts create mode 100644 types/domutils/tsconfig.json create mode 100644 types/domutils/tslint.json diff --git a/types/domutils/domutils-tests.ts b/types/domutils/domutils-tests.ts new file mode 100644 index 0000000000..10a70dd255 --- /dev/null +++ b/types/domutils/domutils-tests.ts @@ -0,0 +1,9 @@ +import * as DomUtils from 'domutils'; + +const elem = { name: 'elem' }; +const next = { name: 'next' }; +const child = { name: 'child' }; + +DomUtils.append(elem, next); +DomUtils.appendChild(elem, child); +DomUtils.getName(elem); diff --git a/types/domutils/index.d.ts b/types/domutils/index.d.ts new file mode 100644 index 0000000000..14971086cb --- /dev/null +++ b/types/domutils/index.d.ts @@ -0,0 +1,127 @@ +// Type definitions for domutils 1.7 +// Project: https://github.com/FB55/domutils#readme +// Definitions by: Johan Davidsson +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.1 + +import { DomElement } from "domhandler"; +/*** + * Append an element after another + * + * @argument elem The element to append to + * @argument next The element be added + */ +export function append(elem: DomElement, next: DomElement): void; +/*** + * Append a child to an element + * + * @argument elem The element to append to + * @argument child The element to be added as a child + */ +export function appendChild(elem: DomElement, child: DomElement): void; +/*** + * Compare the position of one node against another node in any other document. + * The return value is a bitmask with the following values: + * + * document order: + * > There is an ordering, document order, defined on all the nodes in the + * > document corresponding to the order in which the first character of the + * > XML representation of each node occurs in the XML representation of the + * > document after expansion of general entities. Thus, the document element + * > node will be the first node. Element nodes occur before their children. + * > Thus, document order orders element nodes in order of the occurrence of + * > their start-tag in the XML (after expansion of entities). The attribute + * > nodes of an element occur after the element and before its children. The + * > relative order of attribute nodes is implementation-dependent./ + * + * Source: + * http://www.w3.org/TR/DOM-Level-3-Core/glossary.html#dt-document-order + * @argument nodaA The first node to use in the comparison + * @argument nodeB The second node to use in the comparison + * + * @return A bitmask describing the input nodes' relative position. + * + * See http://dom.spec.whatwg.org/#dom-node-comparedocumentposition for + * a description of these values. + */ +export function compareDocumentPosition(nodeA: DomElement, nodeB: DomElement): number; +export function existsOne(test: any, elems: DomElement[]): boolean; +export function filter(test: any, element: DomElement, recurse: boolean, limit: number): DomElement[]; +export function find(test: any, elems: DomElement[], recurse: boolean, limit: number): DomElement[]; +export function findAll(test: any, rootElems: DomElement[]): DomElement[]; +export function findOne(test: any, elems: DomElement[]): DomElement; +export function findOneChild(test: any, elems: DomElement[]): DomElement; +export function getAttributeValue(elem: DomElement, name: string): string; +export function getChildren(elem: DomElement): DomElement[]; +/*** + * Legacy + */ +export function getElementById(id: any, element: any, recurse?: any): any; +/*** + * Legacy + */ +export function getElements(options: any, element: any, recurse?: any, limit?: any): any; +/*** + * Legacy + */ +export function getElementsByTagName(name: any, element: any, recurse?: any, limit?: any): any; +/*** + * Legacy + */ +export function getElementsByTagType(type: any, element: any, recurse?: any, limit?: any): any; +export function getInnerHTML(elem: DomElement, opts: any): string; +/*** + * Returns the name property of an element + * + * @argument elem The element to get the name for + */ +export function getName(elem: DomElement): string; +/*** + * Returns a string representing the array of DomElements + * + * @argument dom An array of DomElement that should be stringified + * @argument [opts] Optional options object + */ +export function getOuterHTML(dom: DomElement[], opts: { decodeEntities?: boolean, xmlMode?: boolean}): string; +export function getParent(elem: DomElement): DomElement; +export function getSiblings(elem: DomElement): DomElement[]; +export function getText(elem: DomElement): string; +export function hasAttrib(elem: DomElement, name: string): boolean; +export function isTag(elem: DomElement): boolean; +/*** + * Prepend an element before another + * + * @argument elem The element to append to + * @argument prev The element be added + */ +export function prepend(elem: DomElement, prev: DomElement): void; +/*** + * Remove an element from the dom + * + * @argument elem The element to be removed + */ +export function removeElement(elem: DomElement): void; +/*** + * Given an array of nodes, remove any member that is contained by another. + */ +export function removeSubsets(nodes: DomElement[]): DomElement[]; +/*** + * Replace an element in the dom + * + * @argument elem The element to be replaced + * @argument replacement The element to be added + */ +export function replaceElement(elem: DomElement, replacement: DomElement): void; +/*** + * Legacy + */ +export function testElement(options: any, element: any): any; +/*** + * Sort an array of nodes based on their relative position in the document and + * remove any duplicate nodes. If the array contains nodes that do not belong + * to the same document, sort order is unspecified. + * + * @argument nodes Array of DOM nodes + * @returns collection of unique nodes, sorted in document order + */ +export function uniqueSort(nodes: DomElement[]): DomElement[]; diff --git a/types/domutils/tsconfig.json b/types/domutils/tsconfig.json new file mode 100644 index 0000000000..891f9dd70e --- /dev/null +++ b/types/domutils/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "domutils-tests.ts" + ] +} diff --git a/types/domutils/tslint.json b/types/domutils/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/domutils/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }