mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-08-12 04:50:18 +00:00
Add types for dom-parser (#41291)
This commit is contained in:
committed by
Armando Aguirre
parent
c4d3560712
commit
e8560d9532
@@ -0,0 +1,29 @@
|
||||
import DomParser = require('dom-parser');
|
||||
|
||||
const parser = new DomParser();
|
||||
|
||||
const exampleHtml =
|
||||
'<html><body><div id="one" class="myclass"></div><div id="two" class="myclass"></div></body></html>';
|
||||
|
||||
const dom = parser.parseFromString(exampleHtml); // $ExpectType Dom
|
||||
|
||||
const element = dom.getElementById('one'); // $ExpectType Node | null
|
||||
dom.getElementsByClassName('myclass'); // $ExpectType Node[] | null
|
||||
dom.getElementsByTagName('div'); // $ExpectType Node[] | null
|
||||
dom.getElementsByName('somenonexistentname'); // $ExpectType Node[] | null
|
||||
dom.getElementsByAttribute('nonexistentattr'); // $ExpectType Node[] | null
|
||||
|
||||
if (element) {
|
||||
element.getAttribute('madeupattr'); // $ExpectType string | null
|
||||
|
||||
element.nodeType; // $ExpectType NodeType
|
||||
element.nodeName; // $ExpectType string
|
||||
element.childNodes; // $ExpectType Node[]
|
||||
element.firstChild; // $ExpectType Node | null
|
||||
element.lastChild; // $ExpectType Node | null
|
||||
element.parentNode; // $ExpectType Node | null
|
||||
element.attributes; // $ExpectType string[]
|
||||
element.innerHTML; // $ExpectType string
|
||||
element.outerHTML; // $ExpectType string
|
||||
element.textContent; // $ExpectType string
|
||||
}
|
||||
Vendored
+55
@@ -0,0 +1,55 @@
|
||||
// Type definitions for dom-parser 0.1
|
||||
// Project: https://github.com/ershov-konst/dom-parser#readme
|
||||
// Definitions by: Guy Bidkar <https://github.com/gbidkar>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
|
||||
declare class DomParser {
|
||||
constructor();
|
||||
|
||||
parseFromString(html: string): DomParser.Dom;
|
||||
}
|
||||
|
||||
declare namespace DomParser {
|
||||
interface Dom extends DOMSearchable {
|
||||
getElementsByClassName(className: string): Node[] | null;
|
||||
getElementsByTagName(tagName: string): Node[] | null;
|
||||
getElementsByName(name: string): Node[] | null;
|
||||
getElementById(id: string): Node | null;
|
||||
|
||||
getElementsByAttribute(attribute: string): Node[] | null;
|
||||
}
|
||||
|
||||
interface Node extends DOMSearchable {
|
||||
nodeType: NodeType;
|
||||
nodeName: string;
|
||||
childNodes: Node[];
|
||||
firstChild: Node | null;
|
||||
lastChild: Node | null;
|
||||
parentNode: Node | null;
|
||||
attributes: string[];
|
||||
innerHTML: string;
|
||||
outerHTML: string;
|
||||
textContent: string;
|
||||
|
||||
getElementsByClassName(className: string): Node[] | null;
|
||||
getElementsByTagName(tagName: string): Node[] | null;
|
||||
getElementsByName(name: string): Node[] | null;
|
||||
getElementById(id: string): Node | null;
|
||||
|
||||
getAttribute(name: string): string | null;
|
||||
}
|
||||
|
||||
interface DOMSearchable {
|
||||
getElementsByClassName(className: string): Node[] | null;
|
||||
getElementsByTagName(tagName: string): Node[] | null;
|
||||
getElementsByName(name: string): Node[] | null;
|
||||
getElementById(id: string): Node | null;
|
||||
}
|
||||
|
||||
enum NodeType {
|
||||
ELEMENT_NODE = 1,
|
||||
TEXT_NODE = 3
|
||||
}
|
||||
}
|
||||
|
||||
export = DomParser;
|
||||
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"lib": [
|
||||
"es6"
|
||||
],
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"strictFunctionTypes": true,
|
||||
"strictNullChecks": true,
|
||||
"baseUrl": "../",
|
||||
"typeRoots": [
|
||||
"../"
|
||||
],
|
||||
"types": [],
|
||||
"noEmit": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"files": [
|
||||
"index.d.ts",
|
||||
"dom-parser-tests.ts"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
Reference in New Issue
Block a user